From a6803e74c853510da8b0ca6a7f6c9c3161bba63f Mon Sep 17 00:00:00 2001 From: nicksavers Date: Sun, 14 Sep 2014 20:11:46 +0200 Subject: [PATCH 1/2] Call Repository interface instead of implementation --- .../org/ethereum/core/BlockchainImpl.java | 10 +- .../java/org/ethereum/db/RepositoryImpl.java | 7 +- .../org/ethereum/facade/EthereumImpl.java | 3 +- .../java/org/ethereum/facade/Repository.java | 185 ++++++++++++++++++ .../java/org/ethereum/json/JSONHelper.java | 4 +- .../ethereum/jsontestsuite/TestRunner.java | 5 +- .../org/ethereum/manager/WorldManager.java | 5 +- .../java/org/ethereum/trie/TrackTrie.java | 11 ++ .../java/org/ethereum/trie/TrieFacade.java | 4 + .../main/java/org/ethereum/vm/Program.java | 26 +-- .../java/org/ethereum/vm/ProgramInvoke.java | 4 +- .../org/ethereum/vm/ProgramInvokeFactory.java | 6 +- .../org/ethereum/vm/ProgramInvokeImpl.java | 12 +- .../ethereum/vm/ProgramInvokeMockImpl.java | 7 +- .../java/org/ethereum/vm/ProgramResult.java | 9 +- .../java/org/ethereum/core/WalletTest.java | 6 +- .../java/org/ethereum/db/RepositoryTest.java | 35 ++-- .../java/org/ethereum/vm/VMComplexTest.java | 10 +- .../src/test/java/org/ethereum/vm/VMTest.java | 4 +- .../org/ethereum/gui/ProgramPlayDialog.java | 2 +- 20 files changed, 279 insertions(+), 76 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java b/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java index 85a00c51..71e342c4 100644 --- a/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/core/BlockchainImpl.java @@ -1,7 +1,7 @@ package org.ethereum.core; -import org.ethereum.db.RepositoryImpl; import org.ethereum.facade.Blockchain; +import org.ethereum.facade.Repository; import org.ethereum.listener.EthereumListener; import org.ethereum.manager.WorldManager; import org.ethereum.net.BlockQueue; @@ -59,7 +59,7 @@ public class BlockchainImpl implements Blockchain { // to avoid using minGasPrice=0 from Genesis for the wallet private static final long INITIAL_MIN_GAS_PRICE = 10 * SZABO.longValue(); - private RepositoryImpl repository; + private Repository repository; private Block lastBlock; // keep the index of the chain for @@ -68,7 +68,7 @@ public class BlockchainImpl implements Blockchain { private final BlockQueue blockQueue = new BlockQueue(); - public BlockchainImpl(RepositoryImpl repository) { + public BlockchainImpl(Repository repository) { this.repository = repository; } @@ -325,7 +325,7 @@ public class BlockchainImpl implements Blockchain { if (isContractCreation || code != null) { // START TRACKING FOR REVERT CHANGES OPTION - RepositoryImpl trackRepository = repository.getTrack(); + Repository trackRepository = repository.getTrack(); trackRepository.startTracking(); try { @@ -386,7 +386,7 @@ public class BlockchainImpl implements Blockchain { * @param contractAddress */ private void applyProgramResult(ProgramResult result, BigInteger gasDebit, - RepositoryImpl repository, byte[] senderAddress, + Repository repository, byte[] senderAddress, byte[] contractAddress, byte[] coinbase, boolean initResults) { if (result.getException() != null diff --git a/ethereumj-core/src/main/java/org/ethereum/db/RepositoryImpl.java b/ethereumj-core/src/main/java/org/ethereum/db/RepositoryImpl.java index 3123f39b..f7eead54 100644 --- a/ethereumj-core/src/main/java/org/ethereum/db/RepositoryImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/db/RepositoryImpl.java @@ -13,6 +13,7 @@ import org.ethereum.listener.EthereumListener; import org.ethereum.manager.WorldManager; import org.ethereum.trie.TrackTrie; import org.ethereum.trie.Trie; +import org.ethereum.trie.TrieFacade; import org.ethereum.util.ByteUtil; import org.ethereum.vm.DataWord; import org.iq80.leveldb.DBIterator; @@ -92,10 +93,10 @@ public class RepositoryImpl implements Repository { this.contractDetailsDB = contractDetailsDB; } - public RepositoryImpl getTrack() { + public Repository getTrack() { TrackTrie trackState = new TrackTrie(accountStateDB); TrackDatabase trackDetails = new TrackDatabase(contractDetailsDB); - return new RepositoryImpl (trackState, trackDetails); + return new RepositoryImpl(trackState, trackDetails); } public void startTracking() { @@ -210,7 +211,7 @@ public class RepositoryImpl implements Repository { return state; } - public Trie getWorldState() { + public TrieFacade getWorldState() { return worldState; } diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java b/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java index 90882464..ba4b853d 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/EthereumImpl.java @@ -7,7 +7,6 @@ import java.util.concurrent.Future; import org.ethereum.core.Transaction; import org.ethereum.core.Wallet; -import org.ethereum.db.RepositoryImpl; import org.ethereum.listener.EthereumListener; import org.ethereum.manager.WorldManager; import org.ethereum.net.client.ClientPeer; @@ -195,7 +194,7 @@ public class EthereumImpl implements Ethereum { @Override - public RepositoryImpl getRepository(){ + public Repository getRepository(){ return WorldManager.getInstance().getRepository(); } diff --git a/ethereumj-core/src/main/java/org/ethereum/facade/Repository.java b/ethereumj-core/src/main/java/org/ethereum/facade/Repository.java index 12fa9bbd..a749464f 100644 --- a/ethereumj-core/src/main/java/org/ethereum/facade/Repository.java +++ b/ethereumj-core/src/main/java/org/ethereum/facade/Repository.java @@ -1,7 +1,14 @@ package org.ethereum.facade; +import java.math.BigInteger; + import org.ethereum.core.AccountState; +import org.ethereum.core.Block; +import org.ethereum.core.BlockchainImpl; import org.ethereum.db.ContractDetails; +import org.ethereum.trie.TrieFacade; +import org.ethereum.vm.DataWord; +import org.iq80.leveldb.DBIterator; /** * www.ethereumJ.com @@ -12,6 +19,184 @@ import org.ethereum.db.ContractDetails; public interface Repository { + /** + * Create a new account in the database + * + * @param address of the contract + * @return newly created account state + */ + public AccountState createAccount(byte[] addr); + + /** + * Retrieve an account + * + * @param address of the account + * @return account state as stored in the database + */ public AccountState getAccountState(byte[] addr); + + /** + * Deletes the account + * + * @param address of the account + */ + public void delete(byte[] addr); + + /** + * Increase the account nonce of the given account by one + * + * @param addr of the account + * @return new value of the nonce + */ + public BigInteger increaseNonce(byte[] addr); + + /** + * Get current nonce of a given account + * + * @param addres of the account + * @return value of the nonce + */ + public BigInteger getNonce(byte[] addr); + + /** + * Retrieve contract details for a given account from the database + * + * @param address of the account + * @return new contract details + */ public ContractDetails getContractDetails(byte[] addr); + + /** + * Store code associated with an account + * + * @param address for the account + * @param code that will be associated with this account + */ + public void saveCode(byte[] addr, byte[] code); + + /** + * Retrieve the code associated with an account + * + * @param address of the account + * @return code in byte-array format + */ + public byte[] getCode(byte[] addr); + + /** + * Put a value in storage of an account at a given key + * + * @param address of the account + * @param key of the data to store + * @param value is the data to store + */ + public void addStorageRow(byte[] addr, DataWord key, DataWord value); + + /** + * Retrieve storage value from an account for a given key + * + * @param address of the account + * @param key associated with this value + * @return data in the form of a DataWord + */ + public DataWord getStorageValue(byte[] addr, DataWord key); + + /** + * Save block and post state in the database + * + * @param block the Block to store + */ + public void saveBlock(Block block); + + /** + * Retrieve block from the blockchain + * + * @param blockNr number of block in the blockchain + * @return Block containing header, uncles and transactions + */ + public Block getBlock(long blockNr); + + /** + * Retrieve balance of an account + * + * @param address of the account + * @return balance of the account as a BigInteger value + */ + public BigInteger getBalance(byte[] addr); + + /** + * Add value to the balance of an account + * + * @param address of the account + * @param value to be added + * @return new balance of the account + */ + public BigInteger addBalance(byte[] addr, BigInteger value); + + /** + * Returns an iterator over the accounts in this database in proper sequence + * + * @return an iterator over the accounts in this database in proper sequence + */ + public DBIterator getAccountsIterator(); + + /** + * Return the current state as the Trie data structure + * + * @return the Trie representing the entire current state + */ + public TrieFacade getWorldState(); + + /** + * Load the blockchain into cache memory + * + * @return the Blockchain object + */ + public BlockchainImpl loadBlockchain(); + + /** + * Dump the full state of the current repository into a file with JSON format + * It contains all the contracts/account, their attributes and + * + * @param block of the current state + * @param gasUsed the amount of gas used in the block until that point + * @param txNumber is the number of the transaction for which the dump has to be made + * @param txHash is the hash of the given transaction. + * If null, the block state post coinbase reward is dumped. + */ + public void dumpState(Block block, long gasUsed, int txNumber, byte[] txHash); + + /** + * Start tracking the database changes + */ + public void startTracking(); + + /** + * Return a repository snapshot of the current state + * + * @return the repository in its current state + */ + public Repository getTrack(); + + /** + * Store all the temporary changes made + * to the repository in the actual database + */ + public void commit(); + + /** + * Undo all the changes made so far + * to a snapshot of the repository + */ + public void rollback(); + + /** + * Check to see if the current repository has an open connection to the database + * @return true if connection to database is open + */ + public boolean isClosed(); + + /** + * Close the database + */ + public void close(); } diff --git a/ethereumj-core/src/main/java/org/ethereum/json/JSONHelper.java b/ethereumj-core/src/main/java/org/ethereum/json/JSONHelper.java index 113e994c..252590df 100644 --- a/ethereumj-core/src/main/java/org/ethereum/json/JSONHelper.java +++ b/ethereumj-core/src/main/java/org/ethereum/json/JSONHelper.java @@ -4,7 +4,7 @@ import org.ethereum.core.AccountState; import org.ethereum.core.Block; import org.ethereum.db.ByteArrayWrapper; import org.ethereum.db.ContractDetails; -import org.ethereum.db.RepositoryImpl; +import org.ethereum.facade.Repository; import org.ethereum.util.ByteUtil; import org.ethereum.vm.DataWord; import org.spongycastle.util.encoders.Hex; @@ -67,7 +67,7 @@ public class JSONHelper { public static void dumpBlock(ObjectNode blockNode, Block block, long gasUsed, byte[] state, List keys, - RepositoryImpl repository) { + Repository repository) { blockNode.put("coinbase", Hex.toHexString(block.getCoinbase())); blockNode.put("difficulty", new BigInteger(1, block.calcDifficulty()).toString()); diff --git a/ethereumj-core/src/main/java/org/ethereum/jsontestsuite/TestRunner.java b/ethereumj-core/src/main/java/org/ethereum/jsontestsuite/TestRunner.java index d67c3318..a083b2b2 100644 --- a/ethereumj-core/src/main/java/org/ethereum/jsontestsuite/TestRunner.java +++ b/ethereumj-core/src/main/java/org/ethereum/jsontestsuite/TestRunner.java @@ -3,6 +3,7 @@ package org.ethereum.jsontestsuite; import org.ethereum.db.ByteArrayWrapper; import org.ethereum.db.ContractDetails; import org.ethereum.db.RepositoryImpl; +import org.ethereum.facade.Repository; import org.ethereum.util.ByteUtil; import org.ethereum.vm.*; import org.slf4j.Logger; @@ -46,7 +47,7 @@ public class TestRunner { List results = new ArrayList<>(); - RepositoryImpl repository = new RepositoryImpl(); + Repository repository = new RepositoryImpl(); /* 1. Store pre-exist accounts - Pre */ for (ByteArrayWrapper key : testCase.getPre().keySet()){ @@ -141,7 +142,7 @@ public class TestRunner { results.add(output); } - if (!Arrays.equals(expectedCode, actualCode)){ + if (!Arrays.equals(expectedCode, actualCode)) { String output = String.format("The code result is different. account: [ %s ], expectedCode: [ %s ] is actualCode: [ %s ] ", diff --git a/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java b/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java index 3e45b86b..62ace861 100644 --- a/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java +++ b/ethereumj-core/src/main/java/org/ethereum/manager/WorldManager.java @@ -10,6 +10,7 @@ import org.ethereum.core.BlockchainImpl; import org.ethereum.core.Wallet; import org.ethereum.crypto.HashUtil; import org.ethereum.db.RepositoryImpl; +import org.ethereum.facade.Repository; import org.ethereum.listener.EthereumListener; import org.ethereum.net.client.ClientPeer; import org.ethereum.net.client.PeerData; @@ -26,7 +27,7 @@ import org.ethereum.net.peerdiscovery.PeerDiscovery; public class WorldManager { private BlockchainImpl blockchain; - private RepositoryImpl repository; + private Repository repository; private Wallet wallet; private PeerDiscovery peerDiscovery; @@ -143,7 +144,7 @@ public class WorldManager { this.wallet = wallet; } - public RepositoryImpl getRepository() { + public Repository getRepository() { return repository; } diff --git a/ethereumj-core/src/main/java/org/ethereum/trie/TrackTrie.java b/ethereumj-core/src/main/java/org/ethereum/trie/TrackTrie.java index e558e763..dda1f7ff 100644 --- a/ethereumj-core/src/main/java/org/ethereum/trie/TrackTrie.java +++ b/ethereumj-core/src/main/java/org/ethereum/trie/TrackTrie.java @@ -81,4 +81,15 @@ public class TrackTrie implements TrieFacade { trie.delete(key); } } + + @Override + public byte[] getRootHash() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getTrieDump() { + return trie.getTrieDump(); + } } diff --git a/ethereumj-core/src/main/java/org/ethereum/trie/TrieFacade.java b/ethereumj-core/src/main/java/org/ethereum/trie/TrieFacade.java index ef6b51eb..476bf812 100644 --- a/ethereumj-core/src/main/java/org/ethereum/trie/TrieFacade.java +++ b/ethereumj-core/src/main/java/org/ethereum/trie/TrieFacade.java @@ -11,4 +11,8 @@ public interface TrieFacade { public void update(byte[] key, byte[] value); public byte[] get(byte[] key); public void delete(byte[] key); + + public byte[] getRootHash(); + + public String getTrieDump(); } diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/Program.java b/ethereumj-core/src/main/java/org/ethereum/vm/Program.java index 9685e471..b7d130b9 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/Program.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/Program.java @@ -2,7 +2,7 @@ package org.ethereum.vm; import org.ethereum.crypto.HashUtil; import org.ethereum.db.ContractDetails; -import org.ethereum.db.RepositoryImpl; +import org.ethereum.facade.Repository; import org.ethereum.util.ByteUtil; import org.ethereum.util.Utils; import org.slf4j.Logger; @@ -291,13 +291,13 @@ public class Program { result.getRepository().addBalance(senderAddress, endowment.negate()); result.getRepository().addBalance(newAddress, endowment); - RepositoryImpl trackRepositoryImpl = result.getRepository().getTrack(); - trackRepositoryImpl.startTracking(); + Repository trackRepository = result.getRepository().getTrack(); + trackRepository.startTracking(); // [5] COOK THE INVOKE AND EXECUTE ProgramInvoke programInvoke = ProgramInvokeFactory.createProgramInvoke(this, new DataWord(newAddress), DataWord.ZERO, - new DataWord(gas), BigInteger.ZERO, null, trackRepositoryImpl, this.invokeData.getCallDeep() + 1); + new DataWord(gas), BigInteger.ZERO, null, trackRepository, this.invokeData.getCallDeep() + 1); VM vm = new VM(); Program program = new Program(programCode.array(), programInvoke); @@ -309,18 +309,18 @@ public class Program { result.getException() instanceof Program.OutOfGasException) { logger.info("contract run halted by OutOfGas: new contract init ={}" , Hex.toHexString(newAddress)); - trackRepositoryImpl.rollback(); + trackRepository.rollback(); stackPushZero(); return; } // 4. CREATE THE CONTRACT OUT OF RETURN byte[] code = result.getHReturn().array(); - trackRepositoryImpl.saveCode(newAddress, code); + trackRepository.saveCode(newAddress, code); // IN SUCCESS PUSH THE ADDRESS INTO THE STACK stackPush(new DataWord(newAddress)); - trackRepositoryImpl.commit(); + trackRepository.commit(); // 5. REFUND THE REMAIN GAS long refundGas = gas - result.getGasUsed(); @@ -397,15 +397,15 @@ public class Program { // actual gas subtract this.spendGas(gas.intValue(), "internal call"); - RepositoryImpl trackRepositoryImpl = result.getRepository().getTrack(); - trackRepositoryImpl.startTracking(); - trackRepositoryImpl.addBalance(toAddress, endowmentValue.value()); + Repository trackRepository = result.getRepository().getTrack(); + trackRepository.startTracking(); + trackRepository.addBalance(toAddress, endowmentValue.value()); ProgramInvoke programInvoke = ProgramInvokeFactory.createProgramInvoke(this, toAddressDW, endowmentValue, gas, result.getRepository().getBalance(toAddress), data.array(), - trackRepositoryImpl, this.invokeData.getCallDeep() + 1); + trackRepository, this.invokeData.getCallDeep() + 1); ProgramResult result = null; @@ -422,7 +422,7 @@ public class Program { result.getException() instanceof Program.OutOfGasException) { logger.info("contract run halted by OutOfGas: contract={}" , Hex.toHexString(toAddress)); - trackRepositoryImpl.rollback(); + trackRepository.rollback(); stackPushZero(); return; } @@ -443,7 +443,7 @@ public class Program { } // 4. THE FLAG OF SUCCESS IS ONE PUSHED INTO THE STACK - trackRepositoryImpl.commit(); + trackRepository.commit(); stackPushOne(); // 5. REFUND THE REMAIN GAS diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/ProgramInvoke.java b/ethereumj-core/src/main/java/org/ethereum/vm/ProgramInvoke.java index 50befc3a..f6133a1a 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/ProgramInvoke.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/ProgramInvoke.java @@ -1,6 +1,6 @@ package org.ethereum.vm; -import org.ethereum.db.RepositoryImpl; +import org.ethereum.facade.Repository; /** * www.ethereumJ.com @@ -27,7 +27,7 @@ public interface ProgramInvoke { public DataWord getDifficulty(); public DataWord getGaslimit(); - public RepositoryImpl getRepository(); + public Repository getRepository(); public boolean byTransaction(); boolean byTestingSuite(); diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/ProgramInvokeFactory.java b/ethereumj-core/src/main/java/org/ethereum/vm/ProgramInvokeFactory.java index e6b15388..002794cd 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/ProgramInvokeFactory.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/ProgramInvokeFactory.java @@ -2,7 +2,7 @@ package org.ethereum.vm; import org.ethereum.core.Block; import org.ethereum.core.Transaction; -import org.ethereum.db.RepositoryImpl; +import org.ethereum.facade.Repository; import org.ethereum.manager.WorldManager; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -21,7 +21,7 @@ public class ProgramInvokeFactory { private static Logger logger = LoggerFactory.getLogger("VM"); // Invocation by the wire tx - public static ProgramInvoke createProgramInvoke(Transaction tx, Block block, RepositoryImpl repository) { + public static ProgramInvoke createProgramInvoke(Transaction tx, Block block, Repository repository) { // https://ethereum.etherpad.mozilla.org/26 Block lastBlock = WorldManager.getInstance().getBlockchain().getLastBlock(); @@ -122,7 +122,7 @@ public class ProgramInvokeFactory { public static ProgramInvoke createProgramInvoke(Program program, DataWord toAddress, DataWord inValue, DataWord inGas, BigInteger balanceInt, byte[] dataIn, - RepositoryImpl repository, int callDeep) { + Repository repository, int callDeep) { DataWord address = toAddress; DataWord origin = program.getOriginAddress(); diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/ProgramInvokeImpl.java b/ethereumj-core/src/main/java/org/ethereum/vm/ProgramInvokeImpl.java index 826db823..ec6de27a 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/ProgramInvokeImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/ProgramInvokeImpl.java @@ -1,6 +1,6 @@ package org.ethereum.vm; -import org.ethereum.db.RepositoryImpl; +import org.ethereum.facade.Repository; import java.math.BigInteger; import java.util.Arrays; @@ -26,7 +26,7 @@ public class ProgramInvokeImpl implements ProgramInvoke { Map storage; - private RepositoryImpl repository; + private Repository repository; private boolean byTransaction = true; private boolean byTestingSuite = false; private int callDeep = 0; @@ -34,7 +34,7 @@ public class ProgramInvokeImpl implements ProgramInvoke { public ProgramInvokeImpl(DataWord address, DataWord origin, DataWord caller, DataWord balance, DataWord gasPrice, DataWord gas, DataWord callValue, byte[] msgData, DataWord lastHash, DataWord coinbase, DataWord timestamp, DataWord number, DataWord difficulty, - DataWord gaslimit, RepositoryImpl repository, int callDeep) { + DataWord gaslimit, Repository repository, int callDeep) { // Transaction env this.address = address; @@ -63,7 +63,7 @@ public class ProgramInvokeImpl implements ProgramInvoke { byte[] gasPrice, byte[] gas, byte[] callValue, byte[] msgData, byte[] lastHash, byte[] coinbase, long timestamp, long number, byte[] difficulty, long gaslimit, - RepositoryImpl repository, boolean byTestingSuite) { + Repository repository, boolean byTestingSuite) { this(address, origin, caller, balance, gasPrice, gas, callValue, msgData, lastHash, coinbase, timestamp, number, difficulty, gaslimit, repository); this.byTestingSuite = byTestingSuite; @@ -74,7 +74,7 @@ public class ProgramInvokeImpl implements ProgramInvoke { byte[] gasPrice, byte[] gas, byte[] callValue, byte[] msgData, byte[] lastHash, byte[] coinbase, long timestamp, long number, byte[] difficulty, long gaslimit, - RepositoryImpl repository) { + Repository repository) { // Transaction env this.address = new DataWord(address); @@ -215,7 +215,7 @@ public class ProgramInvokeImpl implements ProgramInvoke { /* Storage */ public Map getStorage() { return storage; } - public RepositoryImpl getRepository() { + public Repository getRepository() { return repository; } diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/ProgramInvokeMockImpl.java b/ethereumj-core/src/main/java/org/ethereum/vm/ProgramInvokeMockImpl.java index 8f6b0749..5afd67f1 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/ProgramInvokeMockImpl.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/ProgramInvokeMockImpl.java @@ -3,6 +3,7 @@ package org.ethereum.vm; import org.ethereum.crypto.ECKey; import org.ethereum.crypto.HashUtil; import org.ethereum.db.RepositoryImpl; +import org.ethereum.facade.Repository; import org.spongycastle.util.encoders.Hex; /** @@ -15,7 +16,7 @@ public class ProgramInvokeMockImpl implements ProgramInvoke { private byte[] msgData; - private RepositoryImpl repository = null; + private Repository repository = null; private String ownerAddress = "cd2a3d9f938e13cd947ec05abc7fe734df8dd826"; public ProgramInvokeMockImpl(byte[] msgDataRaw) { @@ -179,11 +180,11 @@ public class ProgramInvokeMockImpl implements ProgramInvoke { } @Override - public RepositoryImpl getRepository() { + public Repository getRepository() { return this.repository; } - public void setRepository(RepositoryImpl repository) { + public void setRepository(Repository repository) { this.repository = repository; } diff --git a/ethereumj-core/src/main/java/org/ethereum/vm/ProgramResult.java b/ethereumj-core/src/main/java/org/ethereum/vm/ProgramResult.java index 35584621..becc0d75 100644 --- a/ethereumj-core/src/main/java/org/ethereum/vm/ProgramResult.java +++ b/ethereumj-core/src/main/java/org/ethereum/vm/ProgramResult.java @@ -1,6 +1,6 @@ package org.ethereum.vm; -import org.ethereum.db.RepositoryImpl; +import org.ethereum.facade.Repository; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -18,7 +18,7 @@ public class ProgramResult { private RuntimeException exception; private List deleteAccounts; - private RepositoryImpl repository = null; + private Repository repository = null; /* * for testing runs , @@ -55,11 +55,11 @@ public class ProgramResult { this.exception = exception; } - public RepositoryImpl getRepository() { + public Repository getRepository() { return repository; } - public void setRepository(RepositoryImpl repository) { + public void setRepository(Repository repository) { this.repository = repository; } @@ -83,7 +83,6 @@ public class ProgramResult { deleteAccounts.addAll(accounts); } - public List getDeleteAccounts() { return deleteAccounts; } diff --git a/ethereumj-core/src/test/java/org/ethereum/core/WalletTest.java b/ethereumj-core/src/test/java/org/ethereum/core/WalletTest.java index fcf53e9f..e1bf1ffd 100644 --- a/ethereumj-core/src/test/java/org/ethereum/core/WalletTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/core/WalletTest.java @@ -2,7 +2,7 @@ package org.ethereum.core; import org.ethereum.crypto.ECKey; import org.ethereum.crypto.HashUtil; -import org.ethereum.db.RepositoryImpl; +import org.ethereum.facade.Repository; import org.ethereum.manager.WorldManager; import org.junit.*; import org.junit.runners.MethodSorters; @@ -37,7 +37,7 @@ public class WalletTest { @Test // Testing account for simple balance set public void accountTest_1(){ - RepositoryImpl repository = WorldManager.getInstance().getRepository(); + Repository repository = WorldManager.getInstance().getRepository(); ECKey cowKey = ECKey.fromPrivate(HashUtil.sha3("cow".getBytes())); repository.createAccount(cowKey.getAddress()); @@ -55,7 +55,7 @@ public class WalletTest { @Test // test account balance with pending "unblocked" transaction public void accountTest_2(){ - RepositoryImpl repository = WorldManager.getInstance().getRepository(); + Repository repository = WorldManager.getInstance().getRepository(); ECKey cowKey = ECKey.fromPrivate(HashUtil.sha3("cow".getBytes())); repository.createAccount(cowKey.getAddress()); diff --git a/ethereumj-core/src/test/java/org/ethereum/db/RepositoryTest.java b/ethereumj-core/src/test/java/org/ethereum/db/RepositoryTest.java index 8cedc266..40f3c12e 100644 --- a/ethereumj-core/src/test/java/org/ethereum/db/RepositoryTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/db/RepositoryTest.java @@ -1,6 +1,7 @@ package org.ethereum.db; import org.ethereum.core.AccountState; +import org.ethereum.facade.Repository; import org.ethereum.vm.DataWord; import org.junit.*; import org.junit.runners.MethodSorters; @@ -23,7 +24,7 @@ public class RepositoryTest { public void test1() { String addr = "cd2a3d9f938e13cd947ec05abc7fe734df8dd826"; - RepositoryImpl repository = new RepositoryImpl(); + Repository repository = new RepositoryImpl(); try { AccountState createdState = repository.createAccount(Hex.decode(addr)); @@ -38,7 +39,7 @@ public class RepositoryTest { public void test2() { String addr = "cd2a3d9f938e13cd947ec05abc7fe734df8dd826"; - RepositoryImpl repository = new RepositoryImpl(); + Repository repository = new RepositoryImpl(); try { BigInteger nonce0 = repository.getNonce(Hex.decode(addr)); @@ -61,7 +62,7 @@ public class RepositoryTest { public void test3() { String addr = "cd2a3d9f938e13cd947ec05abc7fe734df8dd826"; - RepositoryImpl repository = new RepositoryImpl(); + Repository repository = new RepositoryImpl(); try { BigInteger nonce0 = repository.getNonce(Hex.decode(addr)); @@ -85,7 +86,7 @@ public class RepositoryTest { public void test4() { String addr = "cd2a3d9f938e13cd947ec05abc7fe734df8dd826"; - RepositoryImpl repository = new RepositoryImpl(); + Repository repository = new RepositoryImpl(); try { BigInteger balance0 = repository.getBalance(Hex.decode(addr)); @@ -107,7 +108,7 @@ public class RepositoryTest { public void test5() { String addr = "cd2a3d9f938e13cd947ec05abc7fe734df8dd826"; - RepositoryImpl repository = new RepositoryImpl(); + Repository repository = new RepositoryImpl(); try { BigInteger balance0 = repository.getBalance(Hex.decode(addr)); @@ -133,7 +134,7 @@ public class RepositoryTest { public void test6() { String addr = "cd2a3d9f938e13cd947ec05abc7fe734df8dd826"; - RepositoryImpl repository = new RepositoryImpl(); + Repository repository = new RepositoryImpl(); byte[] code; try { @@ -151,7 +152,7 @@ public class RepositoryTest { String codeString = "7f60c860005461012c602054000000000000000000000000000000000000000000600060206000f200"; String codeHash = "8f0d7fc8cc6fdd688fa58ae9256310069f5659ed2a8a3af994d80350fbf1e798"; - RepositoryImpl repository = new RepositoryImpl(); + Repository repository = new RepositoryImpl(); try { byte[] code0 = repository.getCode(Hex.decode(addr)); @@ -174,7 +175,7 @@ public class RepositoryTest { String addr = "cd2a3d9f938e13cd947ec05abc7fe734df8dd826"; String codeHash = "c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"; - RepositoryImpl repository = new RepositoryImpl(); + Repository repository = new RepositoryImpl(); try { byte[] code0 = repository.getCode(Hex.decode(addr)); @@ -197,7 +198,7 @@ public class RepositoryTest { byte[] keyBytes = Hex.decode("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"); DataWord key = new DataWord(keyBytes); - RepositoryImpl repository = new RepositoryImpl(); + Repository repository = new RepositoryImpl(); try { DataWord value = repository.getStorageValue(Hex.decode(addr), key); @@ -211,7 +212,7 @@ public class RepositoryTest { public void test10() { String addr = "cd2a3d9f938e13cd947ec05abc7fe734df8dd826"; - RepositoryImpl repository = new RepositoryImpl(); + Repository repository = new RepositoryImpl(); try { repository.createAccount(Hex.decode(addr)); @@ -233,7 +234,7 @@ public class RepositoryTest { String addr = "cd2a3d9f938e13cd947ec05abc7fe734df8dd826"; String expectedStorageHash = "a737c40a4aa895fb9eb464536c376ee7c2c08eb733c8fd2353fcc62dc734f075"; - RepositoryImpl repository = new RepositoryImpl(); + Repository repository = new RepositoryImpl(); try { repository.createAccount(Hex.decode(addr)); @@ -283,10 +284,10 @@ public class RepositoryTest { String expectedStorageHash = "365ed874ad42c2b4af335212465291e03dcd1f0c5b600f40f048ed238ad61fd3"; long expectedBalance = 333; - RepositoryImpl origRepository = new RepositoryImpl(); + Repository origRepository = new RepositoryImpl(); try { - RepositoryImpl repository = origRepository.getTrack(); + Repository repository = origRepository.getTrack(); repository.startTracking(); repository.createAccount(Hex.decode(addr)); @@ -310,8 +311,8 @@ public class RepositoryTest { long expectedBalance_1 = 55500; long expectedBalance_2 = 0; - RepositoryImpl origRepository = new RepositoryImpl(); - RepositoryImpl repository = origRepository.getTrack(); + Repository origRepository = new RepositoryImpl(); + Repository repository = origRepository.getTrack(); repository.startTracking(); repository.createAccount(Hex.decode(addr)); @@ -336,8 +337,8 @@ public class RepositoryTest { long expectedBalance = 55500; - RepositoryImpl origRepository = new RepositoryImpl(); - RepositoryImpl repository = origRepository.getTrack(); + Repository origRepository = new RepositoryImpl(); + Repository repository = origRepository.getTrack(); try { repository.createAccount(Hex.decode(addr_1)); diff --git a/ethereumj-core/src/test/java/org/ethereum/vm/VMComplexTest.java b/ethereumj-core/src/test/java/org/ethereum/vm/VMComplexTest.java index 22b84192..fbc97473 100644 --- a/ethereumj-core/src/test/java/org/ethereum/vm/VMComplexTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/vm/VMComplexTest.java @@ -2,7 +2,7 @@ package org.ethereum.vm; import org.ethereum.core.AccountState; import org.ethereum.crypto.HashUtil; -import org.ethereum.db.RepositoryImpl; +import org.ethereum.facade.Repository; import org.junit.FixMethodOrder; import org.junit.Ignore; import org.junit.Test; @@ -59,7 +59,7 @@ public class VMComplexTest { ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl(); pi.setOwnerAddress("77045e71a7a2c50903d88e564cd72fab11e82051"); - RepositoryImpl repository = pi.getRepository(); + Repository repository = pi.getRepository(); repository.createAccount(callerAddrB); repository.addBalance(callerAddrB, new BigInteger("100000000000000000000")); @@ -134,7 +134,7 @@ public class VMComplexTest { ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl(); pi.setOwnerAddress(contractB_addr); - RepositoryImpl repository = pi.getRepository(); + Repository repository = pi.getRepository(); byte[] contractB_addr_bytes = Hex.decode(contractB_addr); byte[] codeB = Hex.decode(code_b); @@ -234,7 +234,7 @@ public class VMComplexTest { ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl(); pi.setOwnerAddress(contractB_addr); - RepositoryImpl repository = pi.getRepository(); + Repository repository = pi.getRepository(); repository.createAccount(contractA_addr_bytes); repository.saveCode(contractA_addr_bytes, codeA); @@ -315,7 +315,7 @@ public class VMComplexTest { ProgramInvokeMockImpl pi = new ProgramInvokeMockImpl(); pi.setOwnerAddress(contractA_addr); - RepositoryImpl repository = pi.getRepository(); + Repository repository = pi.getRepository(); byte[] caller_addr_bytes = Hex.decode(callerAddr); diff --git a/ethereumj-core/src/test/java/org/ethereum/vm/VMTest.java b/ethereumj-core/src/test/java/org/ethereum/vm/VMTest.java index e543120c..5b75a78b 100644 --- a/ethereumj-core/src/test/java/org/ethereum/vm/VMTest.java +++ b/ethereumj-core/src/test/java/org/ethereum/vm/VMTest.java @@ -1,6 +1,6 @@ package org.ethereum.vm; -import org.ethereum.db.RepositoryImpl; +import org.ethereum.facade.Repository; import org.ethereum.vm.Program.OutOfGasException; import org.junit.FixMethodOrder; import org.junit.Test; @@ -1540,7 +1540,7 @@ public class VMTest { vm.step(program); vm.step(program); - RepositoryImpl repository = program.result.getRepository(); + Repository repository = program.result.getRepository(); DataWord key = new DataWord(Hex.decode(s_expected_key)); DataWord val = repository.getStorageValue(invoke.getOwnerAddress().getNoLeadZeroesData(), key); diff --git a/ethereumj-studio/src/main/java/org/ethereum/gui/ProgramPlayDialog.java b/ethereumj-studio/src/main/java/org/ethereum/gui/ProgramPlayDialog.java index 6b024eed..882f03cd 100755 --- a/ethereumj-studio/src/main/java/org/ethereum/gui/ProgramPlayDialog.java +++ b/ethereumj-studio/src/main/java/org/ethereum/gui/ProgramPlayDialog.java @@ -45,7 +45,7 @@ public class ProgramPlayDialog extends JPanel implements ActionListener, WorldManager.getInstance().getRepository()); } - public ProgramPlayDialog(byte[] code, ProgramInvoke programInvoke, RepositoryImpl tractRepository) { + public ProgramPlayDialog(byte[] code, ProgramInvoke programInvoke, Repository tractRepository) { pi = programInvoke; outputList = new ArrayList(); From 055318b7d4b8ec00610780f69ef9bcb132e1e50f Mon Sep 17 00:00:00 2001 From: nicksavers Date: Mon, 15 Sep 2014 08:22:21 +0200 Subject: [PATCH 2/2] Fix getRootHash() for TrackTrie --- ethereumj-core/src/main/java/org/ethereum/trie/TrackTrie.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ethereumj-core/src/main/java/org/ethereum/trie/TrackTrie.java b/ethereumj-core/src/main/java/org/ethereum/trie/TrackTrie.java index dda1f7ff..424a2fa4 100644 --- a/ethereumj-core/src/main/java/org/ethereum/trie/TrackTrie.java +++ b/ethereumj-core/src/main/java/org/ethereum/trie/TrackTrie.java @@ -84,8 +84,7 @@ public class TrackTrie implements TrieFacade { @Override public byte[] getRootHash() { - // TODO Auto-generated method stub - return null; + return trie.getRootHash(); } @Override