Skip to content

Commit 5df209a

Browse files
committed
Add specialised get-peer-stake function
1 parent 0ef9357 commit 5df209a

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

convex-core/src/main/cvx/convex/core/metadata.cvx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,11 @@
627627
:signature [{:params [coll keys]}
628628
{:params [coll keys not-found]}]}}
629629

630+
get-peer-stake
631+
{:doc {:description "Gets the peer stake for a specific peer."
632+
:examples [{:code "(get-stake my-peer-key)"}]
633+
:signature [{:params [peer-key]}]}}
634+
630635
get-stake
631636
{:doc {:description "Gets the delgated stake for an account on a specific peer."
632637
:errors {:CAST "If the account is not an address."}

convex-core/src/main/java/convex/core/lang/Core.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,24 @@ public Context invoke(Context context, ACell[] args) {
991991
return context.withResult(Juice.LOOKUP,stake);
992992
}
993993
});
994+
995+
public static final CoreFn<CVMLong> GET_PEER_STAKE = reg(new CoreFn<>(Symbols.GET_PEER_STAKE,70) {
996+
997+
@Override
998+
public Context invoke(Context context, ACell[] args) {
999+
if (args.length != 1) return context.withArityError(exactArityMessage(1, args.length));
1000+
1001+
ABlob b=RT.ensureBlob(args[0]);
1002+
if (b == null) return context.withCastError(0,args, Types.BLOB);
1003+
AccountKey accountKey = AccountKey.create(b);
1004+
if (accountKey==null) return context.withArgumentError("Peer Key must be 32 bytes");
1005+
1006+
PeerStatus ps=context.getState().getPeer(accountKey);
1007+
CVMLong stake=(ps==null)?null:CVMLong.create(ps.getPeerStake());
1008+
1009+
return context.withResult(Juice.LOOKUP,stake);
1010+
}
1011+
});
9941012

9951013
public static final CoreFn<CVMLong> CREATE_PEER = reg(new CoreFn<>(Symbols.CREATE_PEER,65) {
9961014

convex-core/src/main/java/convex/core/lang/Symbols.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public class Symbols {
109109
public static final Symbol SET_PEER_STAKE = intern("set-peer-stake");
110110
public static final Symbol EVICT_PEER = intern("evict-peer");
111111

112-
public static final Symbol GET_STAKE = intern("get-stake");;
112+
public static final Symbol GET_STAKE = intern("get-stake");
113+
public static final Symbol GET_PEER_STAKE = intern("get-peer-stake");
113114

114115
public static final Symbol CALL = intern("call");
115116
public static final Symbol CALL_STAR = intern("call*");
@@ -349,6 +350,7 @@ public class Symbols {
349350

350351

351352

353+
352354

353355
public static Symbol intern(String s) {
354356
AString name=Strings.create(s);

convex-core/src/test/java/convex/core/lang/CoreTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3400,11 +3400,28 @@ public void testGetStake() {
34003400
// null for non-existing peer
34013401
assertNull(eval(ctx,"(get-stake 0x1234567812345678123456781234567812345678123456781234567812345678 *address*)"));
34023402

3403+
assertCastError(step(ctx,"(get-stake :foo *address*)"));
34033404
assertCastError(step(ctx,"(get-stake my-peer :foo)"));
34043405

34053406
assertArityError(step(ctx,"(get-stake my-peer)"));
34063407
assertArityError(step(ctx,"(get-stake my-peer *address* :foo)"));
34073408
}
3409+
3410+
@Test
3411+
public void testGetPeerStake() {
3412+
Context ctx=step(context(),"(def my-peer 0x"+InitTest.FIRST_PEER_KEY.toHexString()+")");
3413+
3414+
// existing peer has positive stake
3415+
assertTrue(0L<evalL(ctx,"(get-peer-stake my-peer)"));
3416+
3417+
// null for non-existing peer
3418+
assertNull(eval(ctx,"(get-peer-stake 0x1234567812345678123456781234567812345678123456781234567812345678)"));
3419+
3420+
assertCastError(step(ctx,"(get-peer-stake :foo)"));
3421+
3422+
assertArityError(step(ctx,"(get-peer-stake)"));
3423+
assertArityError(step(ctx,"(get-peer-stake my-peer *address*)"));
3424+
}
34083425

34093426
@Test
34103427
public void testSetPeerStake() {
@@ -3413,13 +3430,17 @@ public void testSetPeerStake() {
34133430
long STK=1000000;
34143431
Context ctx=context();
34153432

3416-
34173433
assertNull(ctx.getState().getPeer(KEY));
34183434
assertStateError(step(ctx,"(set-peer-stake "+KEY+" "+STK+")"));
3435+
assertNull(eval(ctx,"(get-peer-stake "+KEY+")")); // no peer exists yet
34193436

34203437
// create peer with initial stake
34213438
ctx=exec(ctx,"(create-peer "+KEY+" "+STK+")");
34223439

3440+
assertCVMEquals(STK,eval(ctx,"(get-peer-stake "+KEY+")")); // own stake just set
3441+
assertCVMEquals(0,eval(ctx,"(get-stake "+KEY+" *address*)")); // no delegated stake on this peer
3442+
3443+
34233444
// Check stake has been established
34243445
PeerStatus ps=ctx.getState().getPeer(KEY);
34253446
assertEquals(ps, eval(ctx,"(get-in *state* [:peers "+KEY+"])"));

0 commit comments

Comments
 (0)