From 76175bca905b1ed5e2e7efa5d9eb241d01603fb0 Mon Sep 17 00:00:00 2001 From: nicksavers Date: Sat, 7 Jun 2014 16:13:05 +0200 Subject: [PATCH] Replace GasLedger with Gas enum class --- .../main/java/org/ethereum/core/Block.java | 4 +- .../src/main/java/org/ethereum/vm/Gas.java | 33 +++++++++++++++ .../main/java/org/ethereum/vm/GasLedger.java | 41 ------------------- .../src/main/java/org/ethereum/vm/VM.java | 21 +++++----- .../java/org/ethereum/core/BlockTest.java | 7 +++- 5 files changed, 53 insertions(+), 53 deletions(-) create mode 100644 ethereumj-core/src/main/java/org/ethereum/vm/Gas.java delete mode 100644 ethereumj-core/src/main/java/org/ethereum/vm/GasLedger.java diff --git a/ethereumj-core/src/main/java/org/ethereum/core/Block.java b/ethereumj-core/src/main/java/org/ethereum/core/Block.java index d1fd49f7..f6b452a0 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/Block.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/Block.java @@ -9,6 +9,8 @@ import org.ethereum.util.RLPElement; import org.ethereum.util.RLPList; import org.spongycastle.util.BigIntegers; +import edu.emory.mathcs.backport.java.util.Arrays; + import java.math.BigInteger; import java.util.ArrayList; import java.util.List; @@ -320,7 +322,7 @@ public class Block { } public byte[] calcDifficulty() { - if (this.header.getParentHash() == null) + if (Arrays.equals(this.header.getParentHash(), Genesis.PARENT_HASH)) return Genesis.DIFFICULTY; else { Block parent = this.getParent(); diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/Gas.java b/ethereumj-core/src/main/java/org/ethereum/vm/Gas.java new file mode 100644 index 00000000..99bd5724 --- /dev/null +++ b/ethereumj-core/src/main/java/org/ethereum/vm/Gas.java @@ -0,0 +1,33 @@ +package org.ethereum.vm; + +/** + * The fundamental network cost unit. Paid for exclusively by Ether, which is converted + * freely to and from Gas as required. Gas does not exist outside of the internal Ethereum + * computation engine; its price is set by the Transaction and miners are free to + * ignore Transactions whose Gas price is too low. + */ +public enum Gas { + + G_STEP(1), + G_STOP(0), + G_SUICIDE(0), + G_SLOAD(20), + G_SHA3(20), + G_SSTORE(100), + G_BALANCE(20), + G_CREATE(100), + G_CALL(20), + G_MEMORY(1), + G_TXDATA(5), + G_TRANSACTION(500); + + private int cost; + + private Gas(int value) { + this.cost = value; + } + + public int cost() { + return cost; + } +} \ No newline at end of file diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/GasLedger.java b/ethereumj-core/src/main/java/org/ethereum/vm/GasLedger.java deleted file mode 100644 index 314cdcbf..00000000 --- a/ethereumj-core/src/main/java/org/ethereum/vm/GasLedger.java +++ /dev/null @@ -1,41 +0,0 @@ -package org.ethereum.vm; - -import java.util.HashMap; - -/** - * www.ethereumJ.com - * User: Roman Mandeleil - * Created on: 04/06/2014 23:58 - */ - -public class GasLedger { - -/* YP Appendix B. - Gstep 1 Default amount of gas to pay for execution cycle. - Gstop 0 Nothing paid for the STOP operation. - Gsuicide 0 Nothing paid for the SUICIDE operation. - Gsha3 20 Paid for a SHA3 operation. - Gsload 20 Paid for a SLOAD operation. - Gsstore 100 Paid for a normal SSTORE operation (doubled or waived sometimes). - Gbalance 20 Paid for a BALANCE operation. - Gcreate 100 Paid for a CREATE operation. - Gcall 20 Paid for a CALL operation. - Gmemory 1 Paid for every additional word when expanding memory. - Gtxdata 5 Paid for every byte of data or code for a transaction. - Gtransaction 500 Paid for every transaction. - */ - - - public static int G_STEP = 1; - public static int G_STOP = 0; - public static int G_SUICIDE = 0; - public static int G_SLOAD = 20; - public static int G_SHA3 = 20; - public static int G_SSTORE = 100; - public static int G_BALANCE = 20; - public static int G_CREATE = 100; - public static int G_CALL = 20; - public static int G_MEMORY = 1; - public static int G_TXDATA = 5; - public static int G_TRANSACTION = 500; -} diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/VM.java b/ethereumj-core/src/main/java/org/ethereum/vm/VM.java index 0c575b7a..712c5321 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/VM.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/VM.java @@ -8,6 +8,7 @@ import java.math.BigInteger; import java.nio.ByteBuffer; import static org.ethereum.vm.OpCode.PUSH1; +import static org.ethereum.vm.Gas.*; /** * www.ethereumJ.com @@ -34,23 +35,23 @@ public class VM { switch (OpCode.code(op)) { case SHA3: - program.spendGas( GasLedger.G_SHA3 ); + program.spendGas(G_SHA3.cost()); break; case SLOAD: - program.spendGas( GasLedger.G_SLOAD ); + program.spendGas(G_SLOAD.cost()); break; case SSTORE: // todo: calc gas in the execution // todo: according to the size break; case BALANCE: - program.spendGas( GasLedger.G_BALANCE ); + program.spendGas(G_BALANCE.cost()); break; case CREATE: - program.spendGas( GasLedger.G_CREATE ); + program.spendGas(G_CREATE.cost()); break; case CALL: - program.spendGas( GasLedger.G_CALL ); + program.spendGas(G_CALL.cost()); break; case MSTORE8: case MSTORE: @@ -58,7 +59,7 @@ public class VM { // todo: according to the size break; default: - program.spendGas( GasLedger.G_STEP ); + program.spendGas( G_STEP.cost() ); break; } @@ -472,11 +473,11 @@ public class VM { DataWord oldValue = program.storageLoad(addr); program.storageSave(addr, value); if (oldValue == null && !value.isZero()){ - program.spendGas(GasLedger.G_SSTORE * 2); + program.spendGas(G_SSTORE.cost() * 2); } else if (oldValue != null && value.isZero()){ - program.spendGas(GasLedger.G_SSTORE * 0); + program.spendGas(G_SSTORE.cost() * 0); } else - program.spendGas(GasLedger.G_SSTORE); + program.spendGas(G_SSTORE.cost()); program.step(); } @@ -554,7 +555,7 @@ public class VM { // memory gas calc int newMemSize = program.getMemSize(); - program.spendGas(GasLedger.G_MEMORY * (newMemSize - oldMemSize) /32); + program.spendGas(G_MEMORY.cost() * (newMemSize - oldMemSize) /32); } program.fullTrace(); } catch (RuntimeException e) { diff --git a/ethereumj-core/src/test/java/org/ethereum/core/BlockTest.java b/ethereumj-core/src/test/java/org/ethereum/core/BlockTest.java index 773af8dc..f0914fd7 100644 --- a/ethereumj-core/src/test/java/org/ethereum/core/BlockTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/core/BlockTest.java @@ -1,5 +1,7 @@ package org.ethereum.core; +import java.math.BigInteger; + import org.ethereum.net.message.BlocksMessage; import org.ethereum.net.message.StaticMessages; import org.ethereum.util.RLPList; @@ -89,7 +91,10 @@ public class BlockTest { @Test public void testCalcDifficulty() { - // Block.calcDifficulty() + Block genesis = Genesis.getInstance(); + byte[] diffBytes = genesis.calcDifficulty(); + BigInteger difficulty = new BigInteger(1, diffBytes); + System.out.println(difficulty.toString()); fail("Yet to be implemented."); }