Skip to content

Commit 3288dab

Browse files
committed
Tests and fix for set-peer-stake
1 parent c9e256b commit 3288dab

File tree

7 files changed

+80
-14
lines changed

7 files changed

+80
-14
lines changed

convex-core/src/main/java/convex/core/cpos/CPoSConstants.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,21 @@ public class CPoSConstants {
2626
* Milliseconds before peer stake influence starts to decay (3 mins default)
2727
*/
2828
public static final double PEER_DECAY_DELAY = 3*60*1000;
29+
2930
/**
30-
* Time for peer stake to decay by factor 1/e (5 mins default)
31+
* Time for peer stake to decay by factor 1/e (30 mins default)
3132
*/
3233
public static final double PEER_DECAY_TIME = 5*60*1000;
34+
35+
/**
36+
* Minimum proportion of stake that a peer can decay to
37+
*/
38+
public static final double PEER_DECAY_MINIMUM = 0.001;
39+
3340
/**
3441
* Maximum time a block can be resurrected from the past (1 min)
3542
*/
3643
public static final long MAX_BLOCK_BACKDATE = 60*1000;
3744

45+
3846
}

convex-core/src/main/java/convex/core/cvm/State.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ private BlockResult applyTransactions(Block block) throws InvalidBlockException
487487
State state = this;
488488
int blockLength = block.length();
489489
Result[] results = new Result[blockLength];
490+
long fees=0L;
490491

491492
AVector<SignedData<ATransaction>> transactions = block.getTransactions();
492493
for (int i = 0; i < blockLength; i++) {
@@ -501,6 +502,10 @@ private BlockResult applyTransactions(Block block) throws InvalidBlockException
501502
// record results from result context
502503
results[i] = Result.fromContext(CVMLong.create(i),rc);
503504

505+
// Get fees from ResultContext
506+
// NOTE: Juice fees includes transaction overhead fees
507+
fees+=rc.getJuiceFees();
508+
504509
// state update
505510
state = rc.context.getState();
506511
//} catch (Exception e) {
@@ -509,8 +514,15 @@ private BlockResult applyTransactions(Block block) throws InvalidBlockException
509514
// log.error(msg,e);
510515
//}
511516
}
517+
518+
// maybe add used juice to peer fees
519+
if (fees>0L) {
520+
long oldFees=state.getGlobalFees().longValue();
521+
long newFees=oldFees+fees;
522+
state=state.withGlobalFees(CVMLong.create(newFees));
523+
}
512524

513-
// TODO: changes for complete block?
525+
// TODO: other things for complete block?
514526
return BlockResult.create(state, results);
515527
}
516528

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -411,13 +411,6 @@ public Context completeTransaction(State initialState, ResultContext rc) {
411411
// update Account
412412
state=state.putAccount(address,account);
413413

414-
// maybe add used juice to peer fees
415-
if (juiceFees>0L) {
416-
long oldFees=state.getGlobalFees().longValue();
417-
long newFees=oldFees+juiceFees;
418-
state=state.withGlobalFees(CVMLong.create(newFees));
419-
}
420-
421414
// final state update and result reporting
422415
Context rctx=this.withState(state);
423416
if (juiceFailure) {
@@ -2007,7 +2000,7 @@ public Context setDelegatedStake(AccountKey peerKey, Address staker, long newSta
20072000
* @return Updated Context
20082001
*/
20092002
public Context setPeerStake(AccountKey peerKey, long newStake) {
2010-
return setPeerStake(peerKey,newStake);
2003+
return setPeerStake(peerKey,getAddress(),newStake);
20112004
}
20122005

20132006
/**

convex-core/src/main/java/convex/core/util/Economics.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ public static long swapPrice(long delta,long a, long b) {
5050
}
5151

5252
public static double stakeDecay(long time, long peerTime) {
53+
if (peerTime<0) return CPoSConstants.PEER_DECAY_MINIMUM;
5354
if (peerTime>=time) return 1.0;
5455
double delay=time-peerTime;
5556
delay-=CPoSConstants.PEER_DECAY_DELAY;
5657
if (delay<0) return 1.0;
5758

58-
return Math.max(0.001, Math.exp(-delay/CPoSConstants.PEER_DECAY_TIME));
59+
return Math.max(CPoSConstants.PEER_DECAY_MINIMUM, Math.exp(-delay/CPoSConstants.PEER_DECAY_TIME));
5960
}
6061
}

convex-core/src/test/java/convex/core/TokenomicsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public void testTransferFail() {
179179

180180
protected void checkFinalState(ResultContext rc, boolean memUsed) {
181181
// Nothing should have gone wrong with total coin supply
182-
assertEquals(Coin.MAX_SUPPLY,rc.getState().computeTotalBalance());
182+
assertEquals(Coin.MAX_SUPPLY-rc.getJuiceFees(),rc.getState().computeTotalBalance());
183183

184184
if (memUsed) {
185185
// we expect total memory to have fallen because of memory used

convex-core/src/test/java/convex/core/TransactionTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,10 @@ public void testTransfer() {
7272

7373
long memSize=Cells.storageSize(t1);
7474

75-
State s=apply(t1);
75+
ResultContext rc=state().applyTransaction(t1);
76+
State s=rc.context.getState();
7677
long expectedFees=(Juice.TRANSACTION+Juice.TRANSFER+Juice.TRANSACTION_PER_BYTE*memSize)*JP;
77-
assertEquals(expectedFees,s.getGlobalFees().longValue());
78+
assertEquals(expectedFees,rc.getJuiceFees());
7879

7980
long NBAL=s.getAccount(HERO).getBalance();
8081
long balanceDrop=IBAL-NBAL;

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static org.junit.jupiter.api.Assertions.assertTrue;
2929

3030
import java.io.IOException;
31+
import java.util.HashMap;
3132

3233
import org.junit.jupiter.api.Test;
3334

@@ -36,6 +37,7 @@
3637
import convex.core.ErrorCodes;
3738
import convex.core.cpos.Block;
3839
import convex.core.cpos.BlockResult;
40+
import convex.core.cpos.CPoSConstants;
3941
import convex.core.crypto.AKeyPair;
4042
import convex.core.cvm.State;
4143
import convex.core.data.ABlob;
@@ -57,6 +59,7 @@
5759
import convex.core.data.Lists;
5860
import convex.core.data.MapEntry;
5961
import convex.core.data.Maps;
62+
import convex.core.data.PeerStatus;
6063
import convex.core.data.Sets;
6164
import convex.core.data.SignedData;
6265
import convex.core.data.Strings;
@@ -3380,6 +3383,54 @@ public void testStake() {
33803383
assertArityError(step(ctx,"(stake my-peer 1000 :foo)"));
33813384
}
33823385

3386+
@Test
3387+
public void testSetPeerStake() {
3388+
// Not a real peer key, but we don't care because not actually running one....
3389+
AccountKey KEY=AccountKey.fromHex("1234567812345678123456781234567812345678123456781234567812345678");
3390+
long STK=1000000;
3391+
Context ctx=context();
3392+
3393+
3394+
assertNull(ctx.getState().getPeer(KEY));
3395+
assertStateError(step(ctx,"(set-peer-stake "+KEY+" "+STK+")"));
3396+
3397+
// create peer with initial stake
3398+
ctx=exec(ctx,"(create-peer "+KEY+" "+STK+")");
3399+
3400+
// Check stake has been established
3401+
PeerStatus ps=ctx.getState().getPeer(KEY);
3402+
assertEquals(ps, eval(ctx,"(get-in *state* [:peers "+KEY+"])"));
3403+
assertEquals(STK,ps.getPeerStake());
3404+
assertEquals(STK,ps.getTotalStake());
3405+
assertEquals(STK,ps.getBalance());
3406+
3407+
// Effective stake should be decayed to minimum, since no blocks for this peer yet
3408+
HashMap<AccountKey,Double> stks=ctx.getState().computeStakes();
3409+
assertEquals(STK*CPoSConstants.PEER_DECAY_MINIMUM,stks.get(KEY));
3410+
3411+
// Increase stake
3412+
ctx=exec(ctx,"(set-peer-stake "+KEY+" "+STK*3+")");
3413+
ps=ctx.getState().getPeer(KEY);
3414+
assertEquals(STK*3,ps.getPeerStake());
3415+
assertEquals(STK*3,ps.getTotalStake());
3416+
assertEquals(STK*3,ps.getBalance());
3417+
3418+
3419+
assertFundsError(step(ctx,"(set-peer-stake "+KEY+" 999999999999999999)"));
3420+
assertEquals(Coin.MAX_SUPPLY,ctx.getState().computeTotalBalance());
3421+
3422+
// Finally remove all stake
3423+
ctx=exec(ctx,"(set-peer-stake "+KEY+" 0)");
3424+
ps=ctx.getState().getPeer(KEY);
3425+
assertEquals(0,ps.getPeerStake());
3426+
assertEquals(0,ps.getTotalStake());
3427+
assertEquals(0,ps.getBalance());
3428+
3429+
assertArityError(step(ctx,"(set-peer-stake)"));
3430+
assertArityError(step(ctx,"(set-peer-stake "+KEY+")"));
3431+
assertArityError(step(ctx,"(set-peer-stake "+KEY+" :foo :bar)"));
3432+
}
3433+
33833434
@Test
33843435
public void testSetPeerData() {
33853436
String newHostname = "new_hostname:1234";

0 commit comments

Comments
 (0)