use of org.ethereum.db.MutableRepository 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 org.ethereum.db.MutableRepository in project rskj by rsksmart.
the class DslFilesTest method getBalance.
private static Coin getBalance(World world, String address) {
Block bestBlock = world.getBlockChain().getBestBlock();
Repository repo = new MutableRepository(world.getTrieStore(), world.getTrieStore().retrieve(bestBlock.getStateRoot()).get());
RskAddress rskAddress = new RskAddress(address);
return repo.getBalance(rskAddress);
}
use of org.ethereum.db.MutableRepository in project rskj by rsksmart.
the class TransactionExecutorTest method firstTxIsRemovedWhenTheCacheLimitSizeIsExceeded.
@Test
public void firstTxIsRemovedWhenTheCacheLimitSizeIsExceeded() {
ReceivedTxSignatureCache receivedTxSignatureCache = mock(ReceivedTxSignatureCache.class);
BlockTxSignatureCache blockTxSignatureCache = new BlockTxSignatureCache(receivedTxSignatureCache);
MutableRepository cacheTrack = mock(MutableRepository.class);
when(repository.startTracking()).thenReturn(cacheTrack);
RskAddress sender = new RskAddress("0000000000000000000000000000000000000001");
RskAddress receiver = new RskAddress("0000000000000000000000000000000000000002");
byte[] gasLimit = BigInteger.valueOf(4000000).toByteArray();
byte[] txNonce = BigInteger.valueOf(1L).toByteArray();
Coin gasPrice = Coin.valueOf(1);
Coin value = new Coin(BigInteger.valueOf(2));
when(repository.getNonce(sender)).thenReturn(BigInteger.valueOf(1L));
when(repository.getBalance(sender)).thenReturn(new Coin(BigInteger.valueOf(68000L)));
Transaction transaction = getTransaction(sender, receiver, gasLimit, txNonce, gasPrice, value);
assertTrue(executeValidTransaction(transaction, blockTxSignatureCache));
for (int i = 0; i < MAX_CACHE_SIZE; i++) {
if (i == MAX_CACHE_SIZE - 1) {
assertTrue(blockTxSignatureCache.containsTx(transaction));
}
sender = new RskAddress(TestUtils.randomAddress().getBytes());
when(repository.getNonce(sender)).thenReturn(BigInteger.valueOf(1L));
when(repository.getBalance(sender)).thenReturn(new Coin(BigInteger.valueOf(68000L)));
Transaction transactionAux = getTransaction(sender, receiver, gasLimit, txNonce, gasPrice, value);
assertTrue(executeValidTransaction(transactionAux, blockTxSignatureCache));
}
assertFalse(blockTxSignatureCache.containsTx(transaction));
}
use of org.ethereum.db.MutableRepository 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 org.ethereum.db.MutableRepository in project rskj by rsksmart.
the class TransactionExecutorTest method InvalidTxsIsInBlockAndShouldntBeInCache.
@Test
public void InvalidTxsIsInBlockAndShouldntBeInCache() {
ReceivedTxSignatureCache receivedTxSignatureCache = mock(ReceivedTxSignatureCache.class);
BlockTxSignatureCache blockTxSignatureCache = new BlockTxSignatureCache(receivedTxSignatureCache);
MutableRepository cacheTrack = mock(MutableRepository.class);
when(repository.startTracking()).thenReturn(cacheTrack);
RskAddress sender = new RskAddress("0000000000000000000000000000000000000001");
RskAddress receiver = new RskAddress("0000000000000000000000000000000000000002");
byte[] gasLimit = BigInteger.valueOf(4000000).toByteArray();
byte[] txNonce = BigInteger.valueOf(1L).toByteArray();
Coin gasPrice = Coin.valueOf(1);
Coin value = new Coin(BigInteger.valueOf(2));
Transaction transaction = getTransaction(sender, receiver, gasLimit, txNonce, gasPrice, value);
when(executionBlock.getGasLimit()).thenReturn(BigInteger.valueOf(6800000).toByteArray());
when(repository.getNonce(sender)).thenReturn(BigInteger.valueOf(1L));
when(repository.getBalance(sender)).thenReturn(new Coin(BigInteger.valueOf(0L)));
TransactionExecutor txExecutor = new TransactionExecutor(constants, activationConfig, transaction, txIndex, rskAddress, repository, blockStore, receiptStore, blockFactory, programInvokeFactory, executionBlock, gasUsedInTheBlock, vmConfig, true, precompiledContracts, deletedAccounts, blockTxSignatureCache);
assertEquals(0, transaction.transactionCost(constants, activationConfig.forBlock(executionBlock.getNumber())));
assertFalse(txExecutor.executeTransaction());
assertFalse(blockTxSignatureCache.containsTx(transaction));
}
Aggregations