use of co.rsk.test.World in project rskj by rsksmart.
the class BlocksBloomProcessorTest method processBlocksToFillRangeAndStartTheNextOne.
@Test
public void processBlocksToFillRangeAndStartTheNextOne() {
World world = new World();
Blockchain blockchain = world.getBlockChain();
BlockChainBuilder.extend(blockchain, 8, false, false);
KeyValueDataSource dataSource = new HashMapDB();
BlocksBloomStore blocksBloomStore = new BlocksBloomStore(4, 2, dataSource);
BlocksBloomProcessor blocksBloomProcessor = new BlocksBloomProcessor(blocksBloomStore, world.getBlockStore());
blocksBloomProcessor.processNewBlockNumber(4);
blocksBloomProcessor.processNewBlockNumber(6);
BlocksBloom result = blocksBloomProcessor.getBlocksBloomInProcess();
Assert.assertNotNull(result);
Assert.assertEquals(4, result.fromBlock());
Assert.assertEquals(4, result.toBlock());
Assert.assertFalse(dataSource.keys().isEmpty());
Assert.assertEquals(1, dataSource.keys().size());
}
use of co.rsk.test.World in project rskj by rsksmart.
the class BlocksBloomProcessorTest method processBlocksToFillRange.
@Test
public void processBlocksToFillRange() {
World world = new World();
Blockchain blockchain = world.getBlockChain();
BlockChainBuilder.extend(blockchain, 8, false, false);
KeyValueDataSource dataSource = new HashMapDB();
BlocksBloomStore blocksBloomStore = new BlocksBloomStore(4, 2, dataSource);
BlocksBloomProcessor blocksBloomProcessor = new BlocksBloomProcessor(blocksBloomStore, world.getBlockStore());
blocksBloomProcessor.processNewBlockNumber(4);
blocksBloomProcessor.processNewBlockNumber(5);
BlocksBloom result = blocksBloomProcessor.getBlocksBloomInProcess();
Assert.assertNull(result);
Assert.assertFalse(dataSource.keys().isEmpty());
Assert.assertEquals(1, dataSource.keys().size());
}
use of co.rsk.test.World in project rskj by rsksmart.
the class Web3ImplTest method getUncleByBlockHashAndIndexBlockWithUncles.
@Test
public void getUncleByBlockHashAndIndexBlockWithUncles() {
/* Structure:
* Genesis
* | | |
* A B C
* | \ / ____/
* D E
* | /
* F
*
* A-D-F mainchain
* B and C uncles of E
* E uncle of F
* */
World world = new World();
Web3Impl web3 = createWeb3(world);
Block genesis = world.getBlockChain().getBestBlock();
Block blockA = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(genesis).build();
blockA.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(blockA));
Block blockB = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(genesis).build();
blockB.setBitcoinMergedMiningHeader(new byte[] { 0x02 });
assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(blockB));
Block blockC = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(genesis).build();
blockC.setBitcoinMergedMiningHeader(new byte[] { 0x03 });
assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(blockC));
// block D must have a higher difficulty than block E and its uncles so it doesn't fall behind due to a reorg
Block blockD = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(100).parent(blockA).build();
blockD.setBitcoinMergedMiningHeader(new byte[] { 0x04 });
assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(blockD));
List<BlockHeader> blockEUncles = Arrays.asList(blockB.getHeader(), blockC.getHeader());
Block blockE = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(blockA).uncles(blockEUncles).build();
blockE.setBitcoinMergedMiningHeader(new byte[] { 0x05 });
assertEquals(ImportResult.IMPORTED_NOT_BEST, world.getBlockChain().tryToConnect(blockE));
List<BlockHeader> blockFUncles = Arrays.asList(blockE.getHeader());
Block blockF = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).difficulty(10).parent(blockD).uncles(blockFUncles).build();
blockF.setBitcoinMergedMiningHeader(new byte[] { 0x06 });
assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(blockF));
String blockFhash = "0x" + blockF.getHash();
String blockEhash = "0x" + blockE.getHash();
String blockBhash = "0x" + blockB.getHash();
String blockChash = "0x" + blockC.getHash();
BlockResultDTO result = web3.eth_getUncleByBlockHashAndIndex(blockFhash, "0x00");
assertEquals(blockEhash, result.getHash());
assertEquals(2, result.getUncles().size());
assertTrue(result.getUncles().contains(blockBhash));
assertTrue(result.getUncles().contains(blockChash));
assertEquals(TypeConverter.toQuantityJsonHex(30), result.getCumulativeDifficulty());
}
use of co.rsk.test.World in project rskj by rsksmart.
the class Web3ImplTest method getTransactionByHash.
@Test
public void getTransactionByHash() {
ReceiptStore receiptStore = new ReceiptStoreImpl(new HashMapDB());
World world = new World(receiptStore);
Web3Impl web3 = createWeb3(world, receiptStore);
Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(2000000)).build();
Account acc2 = new AccountBuilder().name("acc2").build();
Transaction tx = new TransactionBuilder().sender(acc1).receiver(acc2).value(BigInteger.valueOf(1000000)).build();
List<Transaction> txs = new ArrayList<>();
txs.add(tx);
Block block1 = createCanonicalBlock(world, txs);
String hashString = tx.getHash().toHexString();
TransactionResultDTO tr = web3.eth_getTransactionByHash(hashString);
assertNotNull(tr);
assertEquals("0x" + hashString, tr.getHash());
String blockHashString = "0x" + block1.getHash();
assertEquals(blockHashString, tr.getBlockHash());
assertEquals("0x", tr.getInput());
assertEquals("0x" + ByteUtil.toHexString(tx.getReceiveAddress().getBytes()), tr.getTo());
// Check the v value used to encode the transaction
// NOT the v value used in signature
// the encoded value includes chain id
Assert.assertArrayEquals(new byte[] { tx.getEncodedV() }, TypeConverter.stringHexToByteArray(tr.getV()));
Assert.assertThat(TypeConverter.stringHexToBigInteger(tr.getS()), is(tx.getSignature().getS()));
Assert.assertThat(TypeConverter.stringHexToBigInteger(tr.getR()), is(tx.getSignature().getR()));
}
use of co.rsk.test.World in project rskj by rsksmart.
the class Web3ImplTest method getBlockByHashWithTransactionsHashAsResult.
@Test
public void getBlockByHashWithTransactionsHashAsResult() {
World world = new World();
Web3Impl web3 = createWeb3(world);
Account acc1 = new AccountBuilder(world).name("acc1").balance(Coin.valueOf(220000)).build();
Account acc2 = new AccountBuilder().name("acc2").build();
Transaction tx = new TransactionBuilder().sender(acc1).receiver(acc2).value(BigInteger.valueOf(0)).build();
List<Transaction> txs = new ArrayList<>();
txs.add(tx);
Block genesis = world.getBlockChain().getBestBlock();
Block block1 = new BlockBuilder(world.getBlockChain(), world.getBridgeSupportFactory(), world.getBlockStore()).trieStore(world.getTrieStore()).parent(genesis).transactions(txs).build();
block1.setBitcoinMergedMiningHeader(new byte[] { 0x01 });
assertEquals(ImportResult.IMPORTED_BEST, world.getBlockChain().tryToConnect(block1));
String block1HashString = block1.getHashJsonString();
BlockResultDTO bresult = web3.eth_getBlockByHash(block1HashString, false);
assertNotNull(bresult);
assertEquals(block1HashString, bresult.getHash());
assertEquals(1, bresult.getTransactions().size());
assertEquals(tx.getHash().toJsonString(), bresult.getTransactions().get(0));
assertEquals(0, bresult.getUncles().size());
}
Aggregations