use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class StatusResolverTest method status_incompleteBlockchain.
@Test
public void status_incompleteBlockchain() {
when(blockStore.getMinNumber()).thenReturn(1L);
Keccak256 genesisHash = new Keccak256("ee5c851e70650111887bb6c04e18ef4353391abe37846234c17895a9ca2b33d5");
Keccak256 parentHash = new Keccak256("133e83bb305ef21ea7fc86fcced355db2300887274961a136ca5e8c8763687d9");
when(genesis.getHash()).thenReturn(genesisHash);
when(genesis.getParentHash()).thenReturn(parentHash);
when(genesis.getNumber()).thenReturn(0L);
BlockDifficulty genesisDifficulty = new BlockDifficulty(BigInteger.valueOf(10L));
when(genesis.getCumulativeDifficulty()).thenReturn(genesisDifficulty);
Status status = target.currentStatus();
Assert.assertEquals(0L, status.getBestBlockNumber());
Assert.assertEquals(genesisHash.getBytes(), status.getBestBlockHash());
Assert.assertEquals(parentHash.getBytes(), status.getBestBlockParentHash());
Assert.assertEquals(genesisDifficulty, status.getTotalDifficulty());
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class StatusResolverTest method status.
@Test
public void status() {
Block bestBlock = mock(Block.class);
Keccak256 blockHash = mock(Keccak256.class);
byte[] hashBytes = new byte[] { 0x00 };
long blockNumber = 52;
BlockDifficulty totalDifficulty = mock(BlockDifficulty.class);
when(blockStore.getBestBlock()).thenReturn(bestBlock);
when(bestBlock.getHash()).thenReturn(blockHash);
when(bestBlock.getParentHash()).thenReturn(blockHash);
when(blockHash.getBytes()).thenReturn(hashBytes);
when(bestBlock.getNumber()).thenReturn(blockNumber);
when(blockStore.getTotalDifficultyForHash(eq(hashBytes))).thenReturn(totalDifficulty);
Status status = target.currentStatus();
Assert.assertEquals(blockNumber, status.getBestBlockNumber());
Assert.assertEquals(hashBytes, status.getBestBlockHash());
Assert.assertEquals(hashBytes, status.getBestBlockParentHash());
Assert.assertEquals(totalDifficulty, status.getTotalDifficulty());
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class RemascTestRunner method start.
public void start() {
this.blockchain = this.builder.build();
((BlockChainImpl) this.blockchain).setNoValidation(true);
this.addedSiblings = new ArrayList<>();
List<Block> mainChainBlocks = new ArrayList<>();
this.blockchain.tryToConnect(this.genesis);
BlockFactory blockFactory = new BlockFactory(builder.getConfig().getActivationConfig());
final ProgramInvokeFactoryImpl programInvokeFactory = new ProgramInvokeFactoryImpl();
BlockTxSignatureCache blockTxSignatureCache = new BlockTxSignatureCache(new ReceivedTxSignatureCache());
BridgeSupportFactory bridgeSupportFactory = new BridgeSupportFactory(new RepositoryBtcBlockStoreWithCache.Factory(builder.getConfig().getNetworkConstants().getBridgeConstants().getBtcParams()), builder.getConfig().getNetworkConstants().getBridgeConstants(), builder.getConfig().getActivationConfig());
PrecompiledContracts precompiledContracts = new PrecompiledContracts(builder.getConfig(), bridgeSupportFactory);
BlockExecutor blockExecutor = new BlockExecutor(builder.getConfig().getActivationConfig(), builder.getRepositoryLocator(), new TransactionExecutorFactory(builder.getConfig(), builder.getBlockStore(), null, blockFactory, programInvokeFactory, precompiledContracts, blockTxSignatureCache));
for (int i = 0; i <= this.initialHeight; i++) {
int finalI = i;
List<SiblingElement> siblingsForCurrentHeight = this.siblingElements.stream().filter(siblingElement -> siblingElement.getHeightToBeIncluded() == finalI).collect(Collectors.toList());
List<BlockHeader> blockSiblings = new ArrayList<>();
// Going to add siblings
BlockDifficulty cummDifficulty = BlockDifficulty.ZERO;
if (siblingsForCurrentHeight.size() > 0) {
cummDifficulty = blockchain.getTotalDifficulty();
}
for (SiblingElement sibling : siblingsForCurrentHeight) {
RskAddress siblingCoinbase = TestUtils.randomAddress();
Block mainchainSiblingParent = mainChainBlocks.get(sibling.getHeight() - 1);
Block siblingBlock = createBlock(this.genesis, mainchainSiblingParent, PegTestUtils.createHash3(), siblingCoinbase, Collections.emptyList(), minerFee, this.gasPrice, (long) i, this.txValue, this.txSigningKey, null);
blockSiblings.add(siblingBlock.getHeader());
builder.getBlockStore().saveBlock(siblingBlock, cummDifficulty.add(siblingBlock.getCumulativeDifficulty()), false);
this.addedSiblings.add(siblingBlock);
}
long txNonce = i;
RskAddress coinbase = fixedCoinbase != null ? fixedCoinbase : TestUtils.randomAddress();
Block block = createBlock(this.genesis, this.blockchain.getBestBlock(), PegTestUtils.createHash3(), coinbase, blockSiblings, minerFee, this.gasPrice, txNonce, this.txValue, this.txSigningKey, null);
mainChainBlocks.add(block);
blockExecutor.executeAndFillAll(block, this.blockchain.getBestBlock().getHeader());
block.seal();
ImportResult result = this.blockchain.tryToConnect(block);
System.out.println(result);
}
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class AccountBuilder method build.
public Account build() {
byte[] privateKeyBytes = HashUtil.keccak256(name.getBytes());
ECKey key = ECKey.fromPrivate(privateKeyBytes);
Account account = new Account(key);
if (blockChain != null) {
Block best = blockChain.getStatus().getBestBlock();
BlockDifficulty td = blockChain.getStatus().getTotalDifficulty();
RepositorySnapshot repository = repositoryLocator.snapshotAt(blockChain.getBestBlock().getHeader());
Repository track = repository.startTracking();
track.createAccount(account.getAddress());
if (this.balance != null)
track.addBalance(account.getAddress(), this.balance);
if (this.code != null) {
track.saveCode(account.getAddress(), this.code);
track.getCode(account.getAddress());
}
track.commit();
track.save();
// Check that code is there...
repository.getCode(account.getAddress());
best.setStateRoot(repository.getRoot());
best.flushRLP();
blockStore.saveBlock(best, td, true);
}
return account;
}
use of co.rsk.core.BlockDifficulty in project rskj by rsksmart.
the class IndexedBlockStoreTest method test6.
// leveldb + mapdb, multi branch, total difficulty test
@Test
@Ignore("Ethereum block format")
public void test6() throws IOException {
BigInteger bi = new BigInteger(32, new Random());
String testDir = "test_db_" + bi;
config.setDataBaseDir(testDir);
DB indexDB = createMapDB(testDir);
KeyValueDataSource blocksDB = new LevelDbDataSource("blocks", config.databaseDir());
blocksDB.init();
try {
IndexedBlockStore indexedBlockStore = new IndexedBlockStore(blockFactory, blocksDB, new MapDBBlocksIndex(indexDB));
Block genesis = RskTestFactory.getGenesisInstance(config);
List<Block> bestLine = getRandomChain(blockFactory, 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(blockFactory, 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 = RskTestFactory.getGenesisInstance(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);
}
}
Aggregations