use of co.rsk.db.MutableTrieImpl in project rskj by rsksmart.
the class RepositoryBuilder method build.
public static Repository build(TrieStore trieStore, Map<String, AccountTck> accounts) {
Repository repositoryDummy = new MutableRepository(new MutableTrieImpl(trieStore, new Trie(trieStore)));
Repository track = repositoryDummy.startTracking();
for (String address : accounts.keySet()) {
RskAddress addr = new RskAddress(address);
AccountTck accountTCK = accounts.get(address);
AccountState state = new AccountState(unifiedNumericToBigInteger(accountTCK.getNonce()), new Coin(unifiedNumericToBigInteger(accountTCK.getBalance())));
track.updateAccountState(addr, state);
byte[] code = parseData(accountTCK.getCode());
if (accountTCK.isForcedContract() || code.length > 0 || !accountTCK.getStorage().isEmpty()) {
track.setupContract(addr);
track.saveCode(addr, code);
saveStorageValues(track, addr, accountTCK.getStorage());
}
}
track.commit();
return repositoryDummy;
}
use of co.rsk.db.MutableTrieImpl in project rskj by rsksmart.
the class BridgeUtilsTest method isFreeBridgeTx.
private void isFreeBridgeTx(boolean expected, RskAddress destinationAddress, byte[] privKeyBytes) {
BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(constants.getBridgeConstants().getBtcParams()), constants.getBridgeConstants(), activationConfig);
Bridge bridge = new Bridge(PrecompiledContracts.BRIDGE_ADDR, constants, activationConfig, bridgeSupportFactory);
org.ethereum.core.Transaction rskTx = CallTransaction.createCallTransaction(0, 1, 1, destinationAddress, 0, Bridge.UPDATE_COLLECTIONS, constants.getChainId());
rskTx.sign(privKeyBytes);
TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
Repository repository = new MutableRepository(new MutableTrieCache(new MutableTrieImpl(trieStore, new Trie())));
Block rskExecutionBlock = new BlockGenerator().createChildBlock(getGenesisInstance(trieStore));
bridge.init(rskTx, rskExecutionBlock, repository.startTracking(), null, null, null);
Assert.assertEquals(expected, BridgeUtils.isFreeBridgeTx(rskTx, constants, activationConfig.forBlock(rskExecutionBlock.getNumber())));
}
use of co.rsk.db.MutableTrieImpl in project rskj by rsksmart.
the class TransactionTest method multiSuicideTest.
@Test
public void multiSuicideTest() throws IOException, InterruptedException {
/*
Original contract
pragma solidity ^0.4.3;
contract PsychoKiller {
function () payable {}
function homicide() {
suicide(msg.sender);
}
function multipleHomicide() {
PsychoKiller k = this;
k.homicide();
k.homicide();
k.homicide();
k.homicide();
}
}
*/
BigInteger nonce = config.getNetworkConstants().getInitialNonce();
TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
MutableRepository repository = new MutableRepository(new MutableTrieImpl(trieStore, new Trie(trieStore)));
IndexedBlockStore blockStore = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
BlockTxSignatureCache blockTxSignatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache());
Blockchain blockchain = ImportLightTest.createBlockchain(new TestGenesisLoader(trieStore, getClass().getResourceAsStream("/genesis/genesis-light.json"), nonce, false, true, true).load(), config, repository, blockStore, trieStore);
ECKey sender = ECKey.fromPrivate(Hex.decode("3ec771c31cac8c0dba77a69e503765701d3c2bb62435888d4ffa38fed60c445c"));
System.out.println("address: " + ByteUtil.toHexString(sender.getAddress()));
String code = "6060604052341561000c57fe5b5b6102938061001c6000396000f3006060604052361561004a576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806309e587a514610053578063de990da914610065575b6100515b5b565b005b341561005b57fe5b610063610077565b005b341561006d57fe5b610075610092565b005b3373ffffffffffffffffffffffffffffffffffffffff16ff5b565b60003090508073ffffffffffffffffffffffffffffffffffffffff166309e587a56040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050600060405180830381600087803b15156100fa57fe5b60325a03f1151561010757fe5b5050508073ffffffffffffffffffffffffffffffffffffffff166309e587a56040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050600060405180830381600087803b151561016d57fe5b60325a03f1151561017a57fe5b5050508073ffffffffffffffffffffffffffffffffffffffff166309e587a56040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050600060405180830381600087803b15156101e057fe5b60325a03f115156101ed57fe5b5050508073ffffffffffffffffffffffffffffffffffffffff166309e587a56040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401809050600060405180830381600087803b151561025357fe5b60325a03f1151561026057fe5b5050505b505600a165627a7a72305820084e74021c556522723b6725354378df2fb4b6732f82dd33f5daa29e2820b37c0029";
String abi = "[{\"constant\":false,\"inputs\":[],\"name\":\"homicide\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"multipleHomicide\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"payable\":true,\"type\":\"fallback\"}]";
Transaction tx = createTx(sender, new byte[0], Hex.decode(code), repository);
executeTransaction(blockchain, blockStore, tx, repository, blockTxSignatureCache);
byte[] contractAddress = tx.getContractAddress().getBytes();
CallTransaction.Contract contract1 = new CallTransaction.Contract(abi);
byte[] callData = contract1.getByName("multipleHomicide").encode();
Assert.assertNull(contract1.getConstructor());
Assert.assertNotNull(contract1.parseInvocation(callData));
Assert.assertNotNull(contract1.parseInvocation(callData).toString());
try {
contract1.parseInvocation(new byte[32]);
Assert.fail();
} catch (RuntimeException ex) {
}
try {
contract1.parseInvocation(new byte[2]);
Assert.fail();
} catch (RuntimeException ex) {
}
Transaction tx1 = createTx(sender, contractAddress, callData, repository);
ProgramResult programResult = executeTransaction(blockchain, blockStore, tx1, repository, blockTxSignatureCache).getResult();
// suicide of a single account should be counted only once
Assert.assertEquals(24000, programResult.getFutureRefund());
}
use of co.rsk.db.MutableTrieImpl in project rskj by rsksmart.
the class NetworkStateExporterTest method setup.
@Before
public void setup() {
TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
MutableTrieImpl mutableTrie = new MutableTrieImpl(trieStore, new Trie(trieStore));
repository = new MutableRepository(mutableTrie);
blockchain = mock(Blockchain.class);
block = mock(Block.class);
when(blockchain.getBestBlock()).thenReturn(block);
BlockHeader blockHeader = mock(BlockHeader.class);
when(block.getHeader()).thenReturn(blockHeader);
RepositoryLocator repositoryLocator = mock(RepositoryLocator.class);
when(repositoryLocator.snapshotAt(block.getHeader())).thenReturn(new MutableRepository(mutableTrie));
this.nse = new NetworkStateExporter(repositoryLocator, blockchain);
}
use of co.rsk.db.MutableTrieImpl in project rskj by rsksmart.
the class TransactionTest method dontLogWhenReverting.
@Test
public void dontLogWhenReverting() throws IOException, InterruptedException {
/*
Original contracts
pragma solidity ^0.4.0;
contract TestEventInvoked {
event internalEvent();
function doIt() {
internalEvent();
throw;
}
}
contract TestEventInvoker {
event externalEvent();
function doIt(address invokedAddress) {
externalEvent();
invokedAddress.call.gas(50000)(0xb29f0835);
}
}
*/
BigInteger nonce = config.getNetworkConstants().getInitialNonce();
TrieStore trieStore = new TrieStoreImpl(new HashMapDB());
MutableRepository repository = new MutableRepository(new MutableTrieImpl(trieStore, new Trie(trieStore)));
IndexedBlockStore blockStore = new IndexedBlockStore(blockFactory, new HashMapDB(), new HashMapBlocksIndex());
BlockTxSignatureCache blockTxSignatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache());
Blockchain blockchain = ImportLightTest.createBlockchain(new TestGenesisLoader(trieStore, getClass().getResourceAsStream("/genesis/genesis-light.json"), nonce, false, true, true).load(), config, repository, blockStore, trieStore);
ECKey sender = ECKey.fromPrivate(Hex.decode("3ec771c31cac8c0dba77a69e503765701d3c2bb62435888d4ffa38fed60c445c"));
System.out.println("address: " + ByteUtil.toHexString(sender.getAddress()));
// First contract code TestEventInvoked
String code1 = "6060604052341561000f57600080fd5b5b60ae8061001e6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063b29f083514603d575b600080fd5b3415604757600080fd5b604d604f565b005b7f95481a538d62f8458d3cecac82408d5ff2630d8335962b1cdbac16f1a9b910e760405160405180910390a1600080fd5b5600a165627a7a723058207d93861daff7f4a0479d7f3eb0ca7ef5cef7e2bbf2c4637ab4f021ecc5afa7ad0029";
// Second contract code TestEventInvoker
String code2 = "6060604052341561000f57600080fd5b5b6101358061001f6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063e25fd8a71461003e575b600080fd5b341561004957600080fd5b610075600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610077565b005b7f4cd6f2e769273405c20f3a0c098c9045749deec145502c4838b54206ec5c542860405160405180910390a18073ffffffffffffffffffffffffffffffffffffffff1661c35063b29f0835906040518263ffffffff167c010000000000000000000000000000000000000000000000000000000002815260040160006040518083038160008887f19350505050505b505600a165627a7a7230582019096fd773ebc5581ba378acd64cb1acb450b4eb4866d710f3e3f4e33d635a4b0029";
// Second contract ABI
String abi2 = "[{\"constant\":false,\"inputs\":[{\"name\":\"invokedAddress\",\"type\":\"address\"}],\"name\":\"doIt\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"anonymous\":false,\"inputs\":[],\"name\":\"externalEvent\",\"type\":\"event\"}]";
Transaction tx1 = createTx(sender, new byte[0], Hex.decode(code1), repository);
executeTransaction(blockchain, blockStore, tx1, repository, blockTxSignatureCache);
Transaction tx2 = createTx(sender, new byte[0], Hex.decode(code2), repository);
executeTransaction(blockchain, blockStore, tx2, repository, blockTxSignatureCache);
CallTransaction.Contract contract2 = new CallTransaction.Contract(abi2);
byte[] data = contract2.getByName("doIt").encode(ByteUtil.toHexString(tx1.getContractAddress().getBytes()));
Transaction tx3 = createTx(sender, tx2.getContractAddress().getBytes(), data, repository);
TransactionExecutor executor = executeTransaction(blockchain, blockStore, tx3, repository, blockTxSignatureCache);
Assert.assertEquals(1, executor.getResult().getLogInfoList().size());
Assert.assertFalse(executor.getResult().getLogInfoList().get(0).isRejected());
Assert.assertEquals(1, executor.getVMLogs().size());
}
Aggregations