diff --git a/convex-core/src/main/cvx/convex/core/metadata.cvx b/convex-core/src/main/cvx/convex/core/metadata.cvx index 3fb089f11..e0bf9150e 100644 --- a/convex-core/src/main/cvx/convex/core/metadata.cvx +++ b/convex-core/src/main/cvx/convex/core/metadata.cvx @@ -627,6 +627,11 @@ :signature [{:params [coll keys]} {:params [coll keys not-found]}]}} + get-peer-stake + {:doc {:description "Gets the peer stake for a specific peer." + :examples [{:code "(get-stake my-peer-key)"}] + :signature [{:params [peer-key]}]}} + get-stake {:doc {:description "Gets the delgated stake for an account on a specific peer." :errors {:CAST "If the account is not an address."} diff --git a/convex-core/src/main/java/convex/core/lang/Core.java b/convex-core/src/main/java/convex/core/lang/Core.java index 57fc699a2..010ed98d5 100644 --- a/convex-core/src/main/java/convex/core/lang/Core.java +++ b/convex-core/src/main/java/convex/core/lang/Core.java @@ -991,6 +991,24 @@ public Context invoke(Context context, ACell[] args) { return context.withResult(Juice.LOOKUP,stake); } }); + + public static final CoreFn GET_PEER_STAKE = reg(new CoreFn<>(Symbols.GET_PEER_STAKE,70) { + + @Override + public Context invoke(Context context, ACell[] args) { + if (args.length != 1) return context.withArityError(exactArityMessage(1, args.length)); + + ABlob b=RT.ensureBlob(args[0]); + if (b == null) return context.withCastError(0,args, Types.BLOB); + AccountKey accountKey = AccountKey.create(b); + if (accountKey==null) return context.withArgumentError("Peer Key must be 32 bytes"); + + PeerStatus ps=context.getState().getPeer(accountKey); + CVMLong stake=(ps==null)?null:CVMLong.create(ps.getPeerStake()); + + return context.withResult(Juice.LOOKUP,stake); + } + }); public static final CoreFn CREATE_PEER = reg(new CoreFn<>(Symbols.CREATE_PEER,65) { diff --git a/convex-core/src/main/java/convex/core/lang/Symbols.java b/convex-core/src/main/java/convex/core/lang/Symbols.java index d5aa54007..8e1b435be 100644 --- a/convex-core/src/main/java/convex/core/lang/Symbols.java +++ b/convex-core/src/main/java/convex/core/lang/Symbols.java @@ -109,7 +109,8 @@ public class Symbols { public static final Symbol SET_PEER_STAKE = intern("set-peer-stake"); public static final Symbol EVICT_PEER = intern("evict-peer"); - public static final Symbol GET_STAKE = intern("get-stake");; + public static final Symbol GET_STAKE = intern("get-stake"); + public static final Symbol GET_PEER_STAKE = intern("get-peer-stake"); public static final Symbol CALL = intern("call"); public static final Symbol CALL_STAR = intern("call*"); @@ -349,6 +350,7 @@ public class Symbols { + public static Symbol intern(String s) { AString name=Strings.create(s); diff --git a/convex-core/src/test/java/convex/core/lang/CoreTest.java b/convex-core/src/test/java/convex/core/lang/CoreTest.java index 51b130467..d4e2b63ba 100644 --- a/convex-core/src/test/java/convex/core/lang/CoreTest.java +++ b/convex-core/src/test/java/convex/core/lang/CoreTest.java @@ -3400,11 +3400,28 @@ public void testGetStake() { // null for non-existing peer assertNull(eval(ctx,"(get-stake 0x1234567812345678123456781234567812345678123456781234567812345678 *address*)")); + assertCastError(step(ctx,"(get-stake :foo *address*)")); assertCastError(step(ctx,"(get-stake my-peer :foo)")); assertArityError(step(ctx,"(get-stake my-peer)")); assertArityError(step(ctx,"(get-stake my-peer *address* :foo)")); } + + @Test + public void testGetPeerStake() { + Context ctx=step(context(),"(def my-peer 0x"+InitTest.FIRST_PEER_KEY.toHexString()+")"); + + // existing peer has positive stake + assertTrue(0L