use of co.rsk.peg.BridgeSupportFactory in project rskj by rsksmart.
the class EthModuleTest method getCode.
@Test
public void getCode() {
byte[] expectedCode = new byte[] { 1, 2, 3 };
TransactionPool mockTransactionPool = mock(TransactionPool.class);
PendingState mockPendingState = mock(PendingState.class);
doReturn(expectedCode).when(mockPendingState).getCode(any(RskAddress.class));
doReturn(mockPendingState).when(mockTransactionPool).getPendingState();
EthModule eth = new EthModule(null, (byte) 0, null, mockTransactionPool, null, null, null, null, null, new BridgeSupportFactory(null, null, null), config.getGasEstimationCap());
String addr = eth.getCode(TestUtils.randomAddress().toHexString(), "pending");
Assert.assertThat(Hex.decode(addr.substring("0x".length())), is(expectedCode));
}
use of co.rsk.peg.BridgeSupportFactory in project rskj by rsksmart.
the class EthModuleTest method callSmokeTest.
@Test
public void callSmokeTest() {
CallArguments args = new CallArguments();
BlockResult blockResult = mock(BlockResult.class);
Block block = mock(Block.class);
ExecutionBlockRetriever retriever = mock(ExecutionBlockRetriever.class);
when(retriever.retrieveExecutionBlock("latest")).thenReturn(blockResult);
when(blockResult.getBlock()).thenReturn(block);
byte[] hReturn = TypeConverter.stringToByteArray("hello");
ProgramResult executorResult = mock(ProgramResult.class);
when(executorResult.getHReturn()).thenReturn(hReturn);
ReversibleTransactionExecutor executor = mock(ReversibleTransactionExecutor.class);
when(executor.executeTransaction(eq(blockResult.getBlock()), any(), any(), any(), any(), any(), any(), any())).thenReturn(executorResult);
EthModule eth = new EthModule(null, anyByte(), null, null, executor, retriever, null, null, null, new BridgeSupportFactory(null, null, null), config.getGasEstimationCap());
String expectedResult = TypeConverter.toUnformattedJsonHex(hReturn);
String actualResult = eth.call(args, "latest");
assertEquals(expectedResult, actualResult);
}
use of co.rsk.peg.BridgeSupportFactory in project rskj by rsksmart.
the class EthModuleTest method test_revertedTransaction.
@Test
public void test_revertedTransaction() {
CallArguments args = new CallArguments();
BlockResult blockResult = mock(BlockResult.class);
Block block = mock(Block.class);
ExecutionBlockRetriever retriever = mock(ExecutionBlockRetriever.class);
when(retriever.retrieveExecutionBlock("latest")).thenReturn(blockResult);
when(blockResult.getBlock()).thenReturn(block);
byte[] hreturn = Hex.decode("08c379a000000000000000000000000000000000000000000000000000000000" + "0000002000000000000000000000000000000000000000000000000000000000" + "0000000f6465706f73697420746f6f2062696700000000000000000000000000" + "00000000");
ProgramResult executorResult = mock(ProgramResult.class);
when(executorResult.isRevert()).thenReturn(true);
when(executorResult.getHReturn()).thenReturn(hreturn);
ReversibleTransactionExecutor executor = mock(ReversibleTransactionExecutor.class);
when(executor.executeTransaction(eq(blockResult.getBlock()), any(), any(), any(), any(), any(), any(), any())).thenReturn(executorResult);
EthModule eth = new EthModule(null, anyByte(), null, null, executor, retriever, null, null, null, new BridgeSupportFactory(null, null, null), config.getGasEstimationCap());
try {
eth.call(args, "latest");
} catch (RskJsonRpcRequestException e) {
assertThat(e.getMessage(), Matchers.containsString("deposit too big"));
}
}
use of co.rsk.peg.BridgeSupportFactory in project rskj by rsksmart.
the class TransactionTest method constantCallConflictTest.
@Test
public void constantCallConflictTest() throws Exception {
/*
0x095e7baea6a6c7c4c2dfeb977efac326af552d87 contract is the following Solidity code:
contract Test {
uint a = 256;
function set(uint s) {
a = s;
}
function get() returns (uint) {
return a;
}
}
*/
String json = "{ " + " 'test1' : { " + " 'env' : { " + " 'currentCoinbase' : '2adc25665018aa1fe0e6bc666dac8fc2697ff9ba', " + " 'currentDifficulty' : '0x0100', " + " 'currentGasLimit' : '0x0f4240', " + " 'currentNumber' : '0x00', " + " 'currentTimestamp' : '0x01', " + " 'previousHash' : '5e20a0453cecd065ea59c37ac63e079ee08998b6045136a8ce6635c7912ec0b6' " + " }, " + " 'logs' : [ " + " ], " + " 'out' : '0x', " + " 'post' : { " + " '095e7baea6a6c7c4c2dfeb977efac326af552d87' : { " + " 'balance' : '0x0de0b6b3a76586a0', " + " 'code' : '0x606060405260e060020a600035046360fe47b1811460245780636d4ce63c14602e575b005b6004356000556022565b6000546060908152602090f3', " + " 'nonce' : '0x00', " + " 'storage' : { " + " '0x00' : '0x0400' " + " } " + " }, " + " '0000000000000000000000000000000001000008' : { " + " 'balance' : '0x67EB', " + " 'code' : '0x', " + " 'nonce' : '0x00', " + " 'storage' : { " + " } " + " }, " + " 'a94f5374fce5edbc8e2a8697c15331677e6ebf0b' : { " + " 'balance' : '0x0DE0B6B3A7621175', " + " 'code' : '0x', " + " 'nonce' : '0x01', " + " 'storage' : { " + " } " + " } " + " }, " + " 'postStateRoot' : '17454a767e5f04461256f3812ffca930443c04a47d05ce3f38940c4a14b8c479', " + " 'pre' : { " + " '095e7baea6a6c7c4c2dfeb977efac326af552d87' : { " + " 'balance' : '0x0de0b6b3a7640000', " + " 'code' : '0x606060405260e060020a600035046360fe47b1811460245780636d4ce63c14602e575b005b6004356000556022565b6000546060908152602090f3', " + " 'nonce' : '0x00', " + " 'storage' : { " + " '0x00' : '0x02' " + " } " + " }, " + " 'a94f5374fce5edbc8e2a8697c15331677e6ebf0b' : { " + " 'balance' : '0x0de0b6b3a7640000', " + " 'code' : '0x', " + " 'nonce' : '0x00', " + " 'storage' : { " + " } " + " } " + " }, " + " 'transaction' : { " + " 'data' : '0x60fe47b10000000000000000000000000000000000000000000000000000000000000400', " + " 'gasLimit' : '0x061a80', " + " 'gasPrice' : '0x01', " + " 'nonce' : '0x00', " + " 'secretKey' : '45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8', " + " 'to' : '095e7baea6a6c7c4c2dfeb977efac326af552d87', " + " 'value' : '0x0186a0' " + " } " + " } " + "}";
StateTestSuite stateTestSuite = new StateTestSuite(json.replaceAll("'", "\""));
List<String> res = new StateTestRunner(stateTestSuite.getTestCases().get("test1")) {
@Override
protected ProgramResult executeTransaction(Transaction tx) {
// first emulating the constant call (Ethereum.callConstantFunction)
// to ensure it doesn't affect the final state
{
Repository track = repository.startTracking();
Transaction txConst = CallTransaction.createCallTransaction(0, 0, 100000000000000L, new RskAddress("095e7baea6a6c7c4c2dfeb977efac326af552d87"), 0, CallTransaction.Function.fromSignature("get"), config.getNetworkConstants().getChainId());
txConst.sign(new byte[] {});
Block bestBlock = block;
BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(config.getNetworkConstants().getBridgeConstants().getBtcParams()), config.getNetworkConstants().getBridgeConstants(), config.getActivationConfig());
TransactionExecutorFactory transactionExecutorFactory = new TransactionExecutorFactory(config, new BlockStoreDummy(), null, blockFactory, invokeFactory, new PrecompiledContracts(config, bridgeSupportFactory), new BlockTxSignatureCache(new ReceivedTxSignatureCache()));
TransactionExecutor executor = transactionExecutorFactory.newInstance(txConst, 0, bestBlock.getCoinbase(), track, bestBlock, 0).setLocalCall(true);
executor.executeTransaction();
track.rollback();
System.out.println("Return value: " + new CallTransaction.IntType("uint").decode(executor.getResult().getHReturn()));
}
// now executing the JSON test transaction
return super.executeTransaction(tx);
}
}.setstateTestUSeREMASC(true).runImpl();
if (!res.isEmpty())
throw new RuntimeException("Test failed: " + res);
}
use of co.rsk.peg.BridgeSupportFactory in project rskj by rsksmart.
the class RemascStorageProviderTest method paysOnlyBlocksWithEnoughBalanceAccumulatedAfterRFS.
@Test
public void paysOnlyBlocksWithEnoughBalanceAccumulatedAfterRFS() throws IOException {
Constants constants = spy(Constants.testnet(null));
// we need to pass chain id check, and make believe that testnet config has same chain id as cow account
when(constants.getChainId()).thenReturn(Constants.REGTEST_CHAIN_ID);
when(constants.getMinimumPayableGas()).thenReturn(BigInteger.valueOf(21000L));
RskSystemProperties config = spy(new TestSystemProperties());
when(config.getNetworkConstants()).thenReturn(constants);
long txValue = 10000;
long gasLimit = 100000L;
long gasPrice = 10L;
long lowGasPrice = 1L;
long minerFee = 21000;
RskAddress coinbase = randomAddress();
BlockChainBuilder builder = new BlockChainBuilder().setTesting(true).setGenesis(genesisBlock).setConfig(config);
RemascTestRunner testRunner = new RemascTestRunner(builder, this.genesisBlock).txValue(txValue).minerFee(minerFee).initialHeight(13).siblingElements(new ArrayList<>()).txSigningKey(this.cowKey).gasPrice(gasPrice);
testRunner.setFixedCoinbase(coinbase);
testRunner.start();
Blockchain blockchain = testRunner.getBlockChain();
RepositoryLocator repositoryLocator = builder.getRepositoryLocator();
List<Block> blocks = new ArrayList<>();
blocks.add(RemascTestRunner.createBlock(genesisBlock, blockchain.getBestBlock(), PegTestUtils.createHash3(), coinbase, Collections.emptyList(), gasLimit, gasPrice, 14, txValue, cowKey, null));
blocks.add(RemascTestRunner.createBlock(genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbase, Collections.emptyList(), gasLimit, lowGasPrice, 15, txValue, cowKey, null));
blocks.add(RemascTestRunner.createBlock(genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbase, Collections.emptyList(), gasLimit, gasPrice, 16, txValue, cowKey, null));
blocks.add(RemascTestRunner.createBlock(genesisBlock, blocks.get(blocks.size() - 1), PegTestUtils.createHash3(), coinbase, Collections.emptyList(), gasLimit, lowGasPrice, 17, txValue, cowKey, null));
blocks.addAll(createSimpleBlocks(blocks.get(blocks.size() - 1), 10, coinbase));
BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(config.getNetworkConstants().getBridgeConstants().getBtcParams()), config.getNetworkConstants().getBridgeConstants(), config.getActivationConfig());
BlockExecutor blockExecutor = new BlockExecutor(config.getActivationConfig(), repositoryLocator, new TransactionExecutorFactory(config, builder.getBlockStore(), null, new BlockFactory(config.getActivationConfig()), new ProgramInvokeFactoryImpl(), new PrecompiledContracts(config, bridgeSupportFactory), new BlockTxSignatureCache(new ReceivedTxSignatureCache())));
for (Block b : blocks) {
blockExecutor.executeAndFillAll(b, blockchain.getBestBlock().getHeader());
Assert.assertEquals(ImportResult.IMPORTED_BEST, blockchain.tryToConnect(b));
RepositorySnapshot repository = repositoryLocator.snapshotAt(blockchain.getBestBlock().getHeader());
long blockNumber = blockchain.getBestBlock().getNumber();
if (blockNumber == 24) {
// before first special block
assertEquals(Coin.valueOf(1663200L), RemascTestRunner.getAccountBalance(repository, coinbase));
} else if (blockNumber == 25 || blockNumber == 26) {
// after first and second special block
assertEquals(Coin.valueOf(1829520L), RemascTestRunner.getAccountBalance(repository, coinbase));
} else if (blockNumber == 27 || blockNumber == 28) {
// after third and fourth special block
assertEquals(Coin.valueOf(1999167L), RemascTestRunner.getAccountBalance(repository, coinbase));
}
}
}
Aggregations