Skip to content

Commit

Permalink
Updates to improve juice fees
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Sep 22, 2023
1 parent 6419188 commit 051aafb
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 22 deletions.
5 changes: 0 additions & 5 deletions convex-core/src/main/java/convex/core/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ public class Constants {
*/
public static final long INITIAL_TIMESTAMP = Instant.parse("2020-02-02T00:20:20.0202Z").toEpochMilli();

/**
* Base juice cost per top level transaction
*/
public static final long BASE_TRANSACTION_JUICE = 500L;

/**
* Juice price in the initial Genesis State
*/
Expand Down
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ private Context prepareTransaction(ATransaction t, long juicePrice) {
long balance=account.getBalance();
long juiceLimit=Juice.calcAvailable(balance, juicePrice);
juiceLimit=Math.min(Constants.MAX_TRANSACTION_JUICE,juiceLimit);
long initialJuice=Constants.BASE_TRANSACTION_JUICE;
long initialJuice=0;
if (juiceLimit<=initialJuice) {
return Context.createFake(this,origin).withJuiceError();
}
Expand Down
7 changes: 3 additions & 4 deletions convex-core/src/main/java/convex/core/lang/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public Context completeTransaction(State initialState, long juicePrice) {
long executionJuice=this.juice;

// TODO: Extra juice for transaction size??
long trxJuice=Juice.BASE_TRANSACTION_JUICE;
long trxJuice=Juice.TRANSACTION;

long totalJuice=executionJuice+trxJuice;
long juiceFees=Juice.addMul(0,totalJuice,juicePrice);
Expand Down Expand Up @@ -379,10 +379,9 @@ public Context completeTransaction(State initialState, long juicePrice) {
state=state.putAccount(address,account);

// maybe add used juice to miner fees
if (executionJuice>0L) {
long transactionFees = executionJuice*juicePrice;
if (juiceFees>0L) {
long oldFees=state.getGlobalFees().longValue();
long newFees=oldFees+transactionFees;
long newFees=oldFees+juiceFees;
state=state.withGlobalFees(CVMLong.create(newFees));
}

Expand Down
17 changes: 12 additions & 5 deletions convex-core/src/main/java/convex/core/lang/Juice.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
*
*/
public class Juice {

/**
* Base Juice cost for any top level transaction, added to consumed juice
*/
public static final long TRANSACTION = 500L;

/**
* Base Juice cost for any sub transaction executed, added to consumed juice
*/
public static final long SUB_TRANSACTION = 50L;

/**
* Juice required to resolve a constant value
*
Expand Down Expand Up @@ -486,14 +497,10 @@ private static long costNumeric(ACell a) {
* @return Available juice
*/
public static long calcAvailable(long balance, long juicePrice) {
long limit = (balance/juicePrice)-Juice.BASE_TRANSACTION_JUICE;
long limit = (balance/juicePrice)-Juice.TRANSACTION;
return Math.max(0, limit);
}

/**
* Base Juice cost for any transaction, added to consumed juice
*/
public static final long BASE_TRANSACTION_JUICE = 0;



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import convex.core.transactions.ATransaction;
import convex.core.transactions.Invoke;
import convex.core.transactions.Transfer;
import convex.core.util.Utils;

/**
* Tests for State transition scenarios
Expand Down Expand Up @@ -77,7 +76,7 @@ public void testAccountTransfers() throws BadSignatureException {
assertNull(s.getBalance(ADDRESS_C));

long JPRICE=s.getJuicePrice().longValue(); // Juice price
long TCOST = (Constants.BASE_TRANSACTION_JUICE+ Juice.TRANSFER) * JPRICE;
long TCOST = (Juice.TRANSACTION + Juice.TRANSFER) * JPRICE;
long AMT=50; // Amount for small transfers

{ // transfer from existing to existing account A->B
Expand Down
8 changes: 6 additions & 2 deletions convex-core/src/test/java/convex/core/TransactionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,16 @@ protected State apply(ATransaction t) {
@Test
public void testTransfer() {
long AMT=999;
long IBAL=state().getAccount(HERO).getBalance();
Transfer t1=Transfer.create(HERO, 1, VILLAIN, AMT);
State s=apply(t1);
long expectedFees=(Constants.BASE_TRANSACTION_JUICE+Juice.TRANSFER)*JP;
assertEquals(AMT+expectedFees,state().getAccount(HERO).getBalance()-s.getAccount(HERO).getBalance());
long expectedFees=(Juice.TRANSACTION+Juice.TRANSFER)*JP;
assertEquals(expectedFees,s.getGlobalFees().longValue());

long NBAL=s.getAccount(HERO).getBalance();
long balanceDrop=IBAL-NBAL;
assertEquals(AMT+expectedFees,balanceDrop);

// We expect a Transfer to be completely encoded
assertTrue(t1.isCompletelyEncoded());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public void testSingleBlockConsensus() throws Exception {
long INITIAL_BALANCE_PROPOSER = INITIAL_STATE.getBalance(PADDRESS);
long INITIAL_BALANCE_RECEIVER = INITIAL_STATE.getBalance(RADDRESS);
long TRANSFER_AMOUNT = 100;
long TJUICE=Constants.BASE_TRANSACTION_JUICE+Juice.TRANSFER;
long TJUICE=Juice.TRANSACTION+Juice.TRANSFER;

ATransaction trans = Transfer.create(PADDRESS, 1, RADDRESS, TRANSFER_AMOUNT); // note 1 = first sequence number required
Peer[] bs3 = proposeTransactions(bs2, PROPOSER, trans);
Expand Down Expand Up @@ -309,7 +309,7 @@ public void testMultiBlockConsensus() throws Exception {
AccountKey RKEY = KEYS[RECEIVER];
Long INITIAL_BALANCE_PROPOSER = INITIAL_STATE.getBalance(PADDRESS);
Long INITIAL_BALANCE_RECEIVER = INITIAL_STATE.getBalance(RADDRESS);
long TJUICE=Constants.BASE_TRANSACTION_JUICE+Juice.TRANSFER;
long TJUICE=Juice.TRANSACTION+Juice.TRANSFER;

Peer[] bs3 = bs2;
for (int i = 0; i < NUM_PEERS; i++) {
Expand Down Expand Up @@ -412,7 +412,7 @@ public void testGossipConsensus() throws Exception {
AccountKey RKEY = KEYS[RECEIVER];
long INITIAL_BALANCE_PROPOSER = INITIAL_STATE.getBalance(PADDRESS);
long INITIAL_BALANCE_RECEIVER = INITIAL_STATE.getBalance(RADDRESS);
long expectedJuice=(Constants.BASE_TRANSACTION_JUICE+Juice.TRANSFER)*(NUM_INITIAL_TRANS+TX_ROUNDS);
long expectedJuice=(Juice.TRANSACTION+Juice.TRANSFER)*(NUM_INITIAL_TRANS+TX_ROUNDS);


Peer[] bs3 = bs2;
Expand Down

0 comments on commit 051aafb

Please sign in to comment.