use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class NodeBlockProcessorTest method processGetBlockMessageUsingBlockInBlockchain.
@Test
public void processGetBlockMessageUsingBlockInBlockchain() throws UnknownHostException {
final Blockchain blockchain = BlockChainBuilder.ofSize(10);
final Block block = blockchain.getBlockByNumber(5);
final Keccak256 blockHash = block.getHash();
final BlockStore store = new BlockStore();
BlockNodeInformation nodeInformation = new BlockNodeInformation();
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
RskSystemProperties config = new RskSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
final NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
final SimpleMessageChannel sender = new SimpleMessageChannel();
Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).isEmpty());
processor.processGetBlock(sender, block.getHash().getBytes());
Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).contains(blockHash));
Assert.assertFalse(sender.getMessages().isEmpty());
Assert.assertEquals(1, sender.getMessages().size());
final Message message = sender.getMessages().get(0);
Assert.assertEquals(MessageType.BLOCK_MESSAGE, message.getMessageType());
final BlockMessage bMessage = (BlockMessage) message;
Assert.assertEquals(block.getHash(), bMessage.getBlock().getHash());
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class NodeBlockProcessorTest method processStatusHavingBestBlockInBlockchainStore.
@Test
@Ignore("Ignored when Process status deleted on block processor")
public void processStatusHavingBestBlockInBlockchainStore() throws UnknownHostException {
final BlockStore store = new BlockStore();
final Blockchain blockchain = BlockChainBuilder.ofSize(2);
BlockNodeInformation nodeInformation = new BlockNodeInformation();
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
RskSystemProperties config = new RskSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
final NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
final SimpleMessageChannel sender = new SimpleMessageChannel();
final Block block = blockchain.getBlockByNumber(1);
final Keccak256 blockHash = block.getHash();
store.saveBlock(block);
// final Status status = new Status(block.getNumber(), block.getHash());
Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).isEmpty());
// processor.processStatus(sender, status);
Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).contains(blockHash));
Assert.assertEquals(0, sender.getGetBlockMessages().size());
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class NodeBlockProcessorTest method processGetBlockMessageUsingBlockInStore.
@Test
public void processGetBlockMessageUsingBlockInStore() throws UnknownHostException {
final Block block = new BlockGenerator().getBlock(3);
final Keccak256 blockHash = block.getHash();
final BlockStore store = new BlockStore();
store.saveBlock(block);
final Blockchain blockchain = BlockChainBuilder.ofSize(0);
BlockNodeInformation nodeInformation = new BlockNodeInformation();
SyncConfiguration syncConfiguration = SyncConfiguration.IMMEDIATE_FOR_TESTING;
RskSystemProperties config = new RskSystemProperties();
BlockSyncService blockSyncService = new BlockSyncService(config, store, blockchain, nodeInformation, syncConfiguration);
final NodeBlockProcessor processor = new NodeBlockProcessor(store, blockchain, nodeInformation, blockSyncService, syncConfiguration);
final SimpleMessageChannel sender = new SimpleMessageChannel();
Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).isEmpty());
processor.processGetBlock(sender, block.getHash().getBytes());
Assert.assertTrue(nodeInformation.getBlocksByNode(sender.getPeerNodeID()).contains(blockHash));
Assert.assertFalse(sender.getMessages().isEmpty());
Assert.assertEquals(1, sender.getMessages().size());
final Message message = sender.getMessages().get(0);
Assert.assertEquals(MessageType.BLOCK_MESSAGE, message.getMessageType());
final BlockMessage bMessage = (BlockMessage) message;
Assert.assertEquals(block.getHash(), bMessage.getBlock().getHash());
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class IndexedBlockStoreTest method test6.
// leveldb + mapdb, multi branch, total difficulty test
@Test
public void test6() throws IOException {
BigInteger bi = new BigInteger(32, new Random());
String testDir = "test_db_" + bi;
config.setDataBaseDir(testDir);
DB indexDB = createMapDB(testDir);
Map<Long, List<IndexedBlockStore.BlockInfo>> indexMap = createIndexMap(indexDB);
KeyValueDataSource blocksDB = new LevelDbDataSource(config, "blocks");
blocksDB.init();
try {
IndexedBlockStore indexedBlockStore = new IndexedBlockStore(indexMap, blocksDB, indexDB);
Block genesis = Genesis.getInstance(config);
List<Block> bestLine = getRandomChain(genesis.getHash().getBytes(), 1, 100);
indexedBlockStore.saveBlock(genesis, genesis.getCumulativeDifficulty(), true);
BlockDifficulty td = genesis.getCumulativeDifficulty();
for (int i = 0; i < bestLine.size(); ++i) {
Block newBlock = bestLine.get(i);
td = td.add(newBlock.getCumulativeDifficulty());
indexedBlockStore.saveBlock(newBlock, td, true);
}
byte[] forkParentHash = bestLine.get(60).getHash().getBytes();
long forkParentNumber = bestLine.get(60).getNumber();
List<Block> forkLine = getRandomChain(forkParentHash, forkParentNumber + 1, 50);
for (int i = 0; i < forkLine.size(); ++i) {
Block newBlock = forkLine.get(i);
Block parentBlock = indexedBlockStore.getBlockByHash(newBlock.getParentHash().getBytes());
td = indexedBlockStore.getTotalDifficultyForHash(parentBlock.getHash().getBytes());
td = td.add(newBlock.getCumulativeDifficulty());
indexedBlockStore.saveBlock(newBlock, td, false);
}
// calc all TDs
Map<Keccak256, BlockDifficulty> tDiffs = new HashMap<>();
td = Genesis.getInstance(config).getCumulativeDifficulty();
for (Block block : bestLine) {
td = td.add(block.getCumulativeDifficulty());
tDiffs.put(block.getHash(), td);
}
Map<Keccak256, BlockDifficulty> tForkDiffs = new HashMap<>();
Block block = forkLine.get(0);
td = tDiffs.get(block.getParentHash());
for (Block currBlock : forkLine) {
td = td.add(currBlock.getCumulativeDifficulty());
tForkDiffs.put(currBlock.getHash(), td);
}
// Assert tds on bestLine
for (Keccak256 hash : tDiffs.keySet()) {
BlockDifficulty currTD = tDiffs.get(hash);
BlockDifficulty checkTd = indexedBlockStore.getTotalDifficultyForHash(hash.getBytes());
assertEquals(checkTd, currTD);
}
// Assert tds on forkLine
for (Keccak256 hash : tForkDiffs.keySet()) {
BlockDifficulty currTD = tForkDiffs.get(hash);
BlockDifficulty checkTd = indexedBlockStore.getTotalDifficultyForHash(hash.getBytes());
assertEquals(checkTd, currTD);
}
indexedBlockStore.flush();
// Assert tds on bestLine
for (Keccak256 hash : tDiffs.keySet()) {
BlockDifficulty currTD = tDiffs.get(hash);
BlockDifficulty checkTd = indexedBlockStore.getTotalDifficultyForHash(hash.getBytes());
assertEquals(checkTd, currTD);
}
// check total difficulty
Block bestBlock = bestLine.get(bestLine.size() - 1);
BlockDifficulty totalDifficulty = indexedBlockStore.getTotalDifficultyForHash(bestBlock.getHash().getBytes());
BlockDifficulty totalDifficulty_ = tDiffs.get(bestBlock.getHash());
assertEquals(totalDifficulty_, totalDifficulty);
// Assert tds on forkLine
for (Keccak256 hash : tForkDiffs.keySet()) {
BlockDifficulty currTD = tForkDiffs.get(hash);
BlockDifficulty checkTd = indexedBlockStore.getTotalDifficultyForHash(hash.getBytes());
assertEquals(checkTd, currTD);
}
} finally {
blocksDB.close();
indexDB.close();
FileUtil.recursiveDelete(testDir);
}
}
use of co.rsk.crypto.Keccak256 in project rskj by rsksmart.
the class PrevMinGasPriceValidatorTest method noParentBlock.
@Test
public void noParentBlock() {
Block block = Mockito.mock(Block.class);
Mockito.when(block.getParentHash()).thenReturn(new Keccak256(PARENT_HASH));
Mockito.when(block.getMinimumGasPrice()).thenReturn(BLOCK_MGP);
PrevMinGasPriceRule pmgpv = new PrevMinGasPriceRule();
Assert.assertFalse(pmgpv.isValid(block, null));
}
Aggregations