Skip to content

Commit

Permalink
Add specialised get-peer-stake function
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Oct 10, 2024
1 parent 0ef9357 commit 5df209a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
5 changes: 5 additions & 0 deletions convex-core/src/main/cvx/convex/core/metadata.cvx
Original file line number Diff line number Diff line change
Expand Up @@ -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."}
Expand Down
18 changes: 18 additions & 0 deletions convex-core/src/main/java/convex/core/lang/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,24 @@ public Context invoke(Context context, ACell[] args) {
return context.withResult(Juice.LOOKUP,stake);
}
});

public static final CoreFn<CVMLong> 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<CVMLong> CREATE_PEER = reg(new CoreFn<>(Symbols.CREATE_PEER,65) {

Expand Down
4 changes: 3 additions & 1 deletion convex-core/src/main/java/convex/core/lang/Symbols.java
Original file line number Diff line number Diff line change
Expand Up @@ -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*");
Expand Down Expand Up @@ -349,6 +350,7 @@ public class Symbols {





public static Symbol intern(String s) {
AString name=Strings.create(s);
Expand Down
23 changes: 22 additions & 1 deletion convex-core/src/test/java/convex/core/lang/CoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<evalL(ctx,"(get-peer-stake my-peer)"));

// null for non-existing peer
assertNull(eval(ctx,"(get-peer-stake 0x1234567812345678123456781234567812345678123456781234567812345678)"));

assertCastError(step(ctx,"(get-peer-stake :foo)"));

assertArityError(step(ctx,"(get-peer-stake)"));
assertArityError(step(ctx,"(get-peer-stake my-peer *address*)"));
}

@Test
public void testSetPeerStake() {
Expand All @@ -3413,13 +3430,17 @@ public void testSetPeerStake() {
long STK=1000000;
Context ctx=context();


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

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

assertCVMEquals(STK,eval(ctx,"(get-peer-stake "+KEY+")")); // own stake just set
assertCVMEquals(0,eval(ctx,"(get-stake "+KEY+" *address*)")); // no delegated stake on this peer


// Check stake has been established
PeerStatus ps=ctx.getState().getPeer(KEY);
assertEquals(ps, eval(ctx,"(get-in *state* [:peers "+KEY+"])"));
Expand Down

0 comments on commit 5df209a

Please sign in to comment.