use of org.ethereum.db.BlockStore in project rskj by rsksmart.
the class SnapshotManager method revertToSnapshot.
public boolean revertToSnapshot(Blockchain blockchain, int snapshotId) {
if (snapshotId <= 0 || snapshotId > this.snapshots.size()) {
return false;
}
long newBestBlockNumber = this.snapshots.get(snapshotId - 1).longValue();
List<Long> newSnapshots = this.snapshots.stream().limit(snapshotId).collect(Collectors.toList());
this.snapshots = newSnapshots;
TransactionPool transactionPool = blockchain.getTransactionPool();
long currentBestBlockNumber = blockchain.getBestBlock().getNumber();
if (newBestBlockNumber >= currentBestBlockNumber) {
return true;
}
BlockStore store = blockchain.getBlockStore();
Block block = store.getChainBlockByNumber(newBestBlockNumber);
BlockDifficulty difficulty = blockchain.getBlockStore().getTotalDifficultyForHash(block.getHash().getBytes());
blockchain.setStatus(block, difficulty);
// To clean pending state, first process the fork
blockchain.getTransactionPool().processBest(block);
// then, clear any reverted transaction
transactionPool.removeTransactions(transactionPool.getPendingTransactions());
transactionPool.removeTransactions(transactionPool.getQueuedTransactions());
// Remove removed blocks from store
for (long nb = blockchain.getBestBlock().getNumber() + 1; nb <= currentBestBlockNumber; nb++) {
blockchain.removeBlocksByNumber(nb);
}
return true;
}
use of org.ethereum.db.BlockStore in project rskj by rsksmart.
the class BlockValidatorTest method processValidMGPBlock.
@Test
public void processValidMGPBlock() {
BlockStore blockStore = Mockito.mock(org.ethereum.db.BlockStore.class);
Repository repository = Mockito.mock(Repository.class);
Mockito.when(repository.getSnapshotTo(Mockito.any())).thenReturn(repository);
Mockito.when(repository.getNonce(Mockito.any())).thenReturn(BigInteger.ZERO);
Block parent = new BlockBuilder().minGasPrice(BigInteger.TEN).parent(new BlockGenerator().getGenesisBlock()).build();
List<Transaction> txs = new ArrayList<>();
Transaction tx = Transaction.create(config, "0000000000000000000000000000000000000006", BigInteger.ZERO, BigInteger.ZERO, BigInteger.valueOf(12L), BigInteger.TEN);
tx.sign(new byte[] { 22, 11, 00 });
txs.add(tx);
Block block = new BlockBuilder().transactions(txs).minGasPrice(BigInteger.valueOf(11L)).parent(parent).build();
Mockito.when(blockStore.getBlockByHash(block.getParentHash().getBytes())).thenReturn(parent);
BlockValidatorImpl validator = new BlockValidatorBuilder().addPrevMinGasPriceRule().addTxsMinGasPriceRule().blockStore(blockStore).build();
Assert.assertTrue(validator.isValid(block));
}
use of org.ethereum.db.BlockStore in project rskj by rsksmart.
the class FamilyUtilsTest method getUnclesHeaders.
@Test
public void getUnclesHeaders() {
BlockStore store = createBlockStore();
BlockGenerator blockGenerator = new BlockGenerator();
Block genesis = blockGenerator.getGenesisBlock();
Block block1 = blockGenerator.createChildBlock(genesis);
Block uncle11 = blockGenerator.createChildBlock(genesis);
Block uncle111 = blockGenerator.createChildBlock(uncle11);
Block uncle12 = blockGenerator.createChildBlock(genesis);
Block uncle121 = blockGenerator.createChildBlock(uncle12);
Block block2 = blockGenerator.createChildBlock(block1);
Block uncle21 = blockGenerator.createChildBlock(block1);
Block uncle22 = blockGenerator.createChildBlock(block1);
Block block3 = blockGenerator.createChildBlock(block2);
Block uncle31 = blockGenerator.createChildBlock(block2);
Block uncle32 = blockGenerator.createChildBlock(block2);
store.saveBlock(genesis, TEST_DIFFICULTY, true);
store.saveBlock(block1, TEST_DIFFICULTY, true);
store.saveBlock(uncle11, TEST_DIFFICULTY, false);
store.saveBlock(uncle12, TEST_DIFFICULTY, false);
store.saveBlock(uncle111, TEST_DIFFICULTY, false);
store.saveBlock(uncle121, TEST_DIFFICULTY, false);
store.saveBlock(block2, TEST_DIFFICULTY, true);
store.saveBlock(uncle21, TEST_DIFFICULTY, false);
store.saveBlock(uncle22, TEST_DIFFICULTY, false);
store.saveBlock(block3, TEST_DIFFICULTY, true);
store.saveBlock(uncle31, TEST_DIFFICULTY, false);
store.saveBlock(uncle32, TEST_DIFFICULTY, false);
List<BlockHeader> list = FamilyUtils.getUnclesHeaders(store, block3, 3);
Assert.assertNotNull(list);
Assert.assertFalse(list.isEmpty());
Assert.assertEquals(4, list.size());
Assert.assertTrue(containsHash(uncle11.getHash(), list));
Assert.assertTrue(containsHash(uncle12.getHash(), list));
Assert.assertTrue(containsHash(uncle21.getHash(), list));
Assert.assertTrue(containsHash(uncle22.getHash(), list));
}
use of org.ethereum.db.BlockStore in project rskj by rsksmart.
the class FamilyUtilsTest method getFamilyGetAncestorsUpToLevel.
@Test
public void getFamilyGetAncestorsUpToLevel() {
BlockStore store = createBlockStore();
BlockGenerator blockGenerator = new BlockGenerator();
Block genesis = blockGenerator.getGenesisBlock();
Block block1 = blockGenerator.createChildBlock(genesis);
Block block2 = blockGenerator.createChildBlock(block1);
Block block3 = blockGenerator.createChildBlock(block2);
store.saveBlock(genesis, TEST_DIFFICULTY, true);
store.saveBlock(block1, TEST_DIFFICULTY, true);
store.saveBlock(block2, TEST_DIFFICULTY, true);
store.saveBlock(block3, TEST_DIFFICULTY, true);
Set<Keccak256> family = FamilyUtils.getFamily(store, block3, 2);
Assert.assertNotNull(family);
Assert.assertFalse(family.isEmpty());
Assert.assertEquals(2, family.size());
Assert.assertFalse(family.contains(genesis.getHash()));
Assert.assertFalse(family.contains(block3.getHash()));
Assert.assertTrue(family.contains(block1.getHash()));
Assert.assertTrue(family.contains(block2.getHash()));
}
use of org.ethereum.db.BlockStore in project rskj by rsksmart.
the class FamilyUtilsTest method getEmptyFamilyForGenesis.
@Test
public void getEmptyFamilyForGenesis() {
BlockStore store = createBlockStore();
Block genesis = new BlockGenerator().getGenesisBlock();
store.saveBlock(genesis, TEST_DIFFICULTY, true);
Set<Keccak256> family = FamilyUtils.getFamily(store, genesis, 6);
Assert.assertNotNull(family);
Assert.assertTrue(family.isEmpty());
}
Aggregations