@Before public void setup() { remoteBlockchain = createInMemoryBlockchain(genesisBlock); }
private static MutableBlockchain buildBlockchain(final BlockHeader genesisBlockHeader) { final Block genesisBlock = new Block(genesisBlockHeader, BlockBody.empty()); return createInMemoryBlockchain(genesisBlock); }
@Before public void setup() { headerTestFixture.number(1); Block genesisBlock = createEmptyBlock(0, Hash.ZERO, proposerKeys); // not normally signed but ok blockChain = createInMemoryBlockchain(genesisBlock); final VoteTally voteTally = mock(VoteTally.class); when(voteTally.getValidators()).thenReturn(validators); when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(voteTally); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, null, null); when(protocolContext.getConsensusState()).thenReturn(cliqueContext); when(protocolContext.getBlockchain()).thenReturn(blockChain); when(minerExecutor.startAsyncMining(any(), any())).thenReturn(blockMiner); when(syncState.isInSync()).thenReturn(true); miningTracker = new CliqueMiningTracker(proposerAddress, protocolContext); }
private BlockchainWithData setupBlockchain( final int blocksToAdd, final List<Address> accountsToSetup, final List<UInt256> storageKeys) { checkArgument(blocksToAdd >= 1, "Must add at least one block to the queries"); final WorldStateArchive worldStateArchive = createInMemoryWorldStateArchive(); // Generate some queries data final List<BlockData> blockData = new ArrayList<>(blocksToAdd); final List<Block> blocks = gen.blockSequence(blocksToAdd, worldStateArchive, accountsToSetup, storageKeys); for (int i = 0; i < blocksToAdd; i++) { final Block block = blocks.get(i); final List<TransactionReceipt> receipts = gen.receipts(block); blockData.add(new BlockData(block, receipts)); } // Setup blockchain final MutableBlockchain blockchain = createInMemoryBlockchain(blocks.get(0)); blockData .subList(1, blockData.size()) .forEach(b -> blockchain.appendBlock(b.block, b.receipts)); return new BlockchainWithData(blockchain, blockData, worldStateArchive); }
@Test public void nonValidatorIsNotAllowedToCreateABlock() { genesisBlock = createEmptyBlock(otherNodeKeyPair); blockChain = createInMemoryBlockchain(genesisBlock); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); final VoteProposer voteProposer = new VoteProposer(); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null); cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext); headerBuilder.parentHash(Hash.ZERO).number(3); final BlockHeader parentHeader = headerBuilder.buildHeader(); assertThat( CliqueHelpers.addressIsAllowedToProduceNextBlock( AddressHelpers.ofValue(1), cliqueProtocolContext, parentHeader)) .isFalse(); } }
@Test public void exceptionIsThrownIfOnAnOrphanedChain() { final Address otherAddress = Util.publicKeyToAddress(otherNodeKeyPair.getPublicKey()); validatorList.add(otherAddress); genesisBlock = createEmptyBlock(proposerKeyPair); blockChain = createInMemoryBlockchain(genesisBlock); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); final VoteProposer voteProposer = new VoteProposer(); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null); cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext); headerBuilder.parentHash(Hash.ZERO).number(3); final BlockHeader parentHeader = TestHelpers.createCliqueSignedBlockHeader(headerBuilder, otherNodeKeyPair, validatorList); assertThatThrownBy( () -> CliqueHelpers.addressIsAllowedToProduceNextBlock( localAddress, cliqueProtocolContext, parentHeader)) .isInstanceOf(RuntimeException.class) .hasMessage("The block was on a orphaned chain."); }
private MutableBlockchain createShortChain( final Blockchain blockchain, final long truncateAtBlockNumber) { final BlockHeader genesisHeader = blockchain.getBlockHeader(BlockHeader.GENESIS_BLOCK_NUMBER).get(); final BlockBody genesisBody = blockchain.getBlockBody(genesisHeader.getHash()).get(); final Block genesisBlock = new Block(genesisHeader, genesisBody); final MutableBlockchain shortChain = createInMemoryBlockchain(genesisBlock); long nextBlock = genesisHeader.getNumber() + 1; while (nextBlock <= truncateAtBlockNumber) { final BlockHeader header = blockchain.getBlockHeader(nextBlock).get(); final BlockBody body = blockchain.getBlockBody(header.getHash()).get(); final List<TransactionReceipt> receipts = blockchain.getTxReceipts(header.getHash()).get(); final Block block = new Block(header, body); shortChain.appendBlock(block, receipts); nextBlock++; } return shortChain; } }
private MutableBlockchain createShortChain(final long lastBlockToInclude) { final BlockHeader genesisHeader = blockchain.getBlockHeader(BlockHeader.GENESIS_BLOCK_NUMBER).get(); final BlockBody genesisBody = blockchain.getBlockBody(genesisHeader.getHash()).get(); final Block genesisBlock = new Block(genesisHeader, genesisBody); final MutableBlockchain shortChain = createInMemoryBlockchain(genesisBlock); long nextBlock = genesisHeader.getNumber() + 1; while (nextBlock <= lastBlockToInclude) { final BlockHeader header = blockchain.getBlockHeader(nextBlock).get(); final BlockBody body = blockchain.getBlockBody(header.getHash()).get(); final List<TransactionReceipt> receipts = blockchain.getTxReceipts(header.getHash()).get(); final Block block = new Block(header, body); shortChain.appendBlock(block, receipts); nextBlock++; } return shortChain; }
@Test public void signerIsValidIfInsufficientBlocksExistInHistory() { final Address otherAddress = Util.publicKeyToAddress(otherNodeKeyPair.getPublicKey()); validatorList.add(otherAddress); validatorList.add(AddressHelpers.ofValue(1)); validatorList.add(AddressHelpers.ofValue(2)); validatorList.add(AddressHelpers.ofValue(3)); // Should require 2 blocks between signings. genesisBlock = createEmptyBlock(proposerKeyPair); blockChain = createInMemoryBlockchain(genesisBlock); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); final VoteProposer voteProposer = new VoteProposer(); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null); cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext); headerBuilder.parentHash(genesisBlock.getHash()).number(1); final Block block_1 = createEmptyBlock(otherNodeKeyPair); blockChain.appendBlock(block_1, Lists.newArrayList()); assertThat( CliqueHelpers.addressIsAllowedToProduceNextBlock( localAddress, cliqueProtocolContext, genesisBlock.getHeader())) .isTrue(); assertThat( CliqueHelpers.addressIsAllowedToProduceNextBlock( localAddress, cliqueProtocolContext, block_1.getHeader())) .isTrue(); }
private MutableBlockchain createShortChain(final long truncateAtBlockNumber) { final BlockHeader genesisHeader = blockchain.getBlockHeader(BlockHeader.GENESIS_BLOCK_NUMBER).get(); final BlockBody genesisBody = blockchain.getBlockBody(genesisHeader.getHash()).get(); final Block genesisBlock = new Block(genesisHeader, genesisBody); final MutableBlockchain shortChain = createInMemoryBlockchain(genesisBlock); long nextBlock = genesisHeader.getNumber() + 1; while (nextBlock <= truncateAtBlockNumber) { final BlockHeader header = blockchain.getBlockHeader(nextBlock).get(); final BlockBody body = blockchain.getBlockBody(header.getHash()).get(); final List<TransactionReceipt> receipts = blockchain.getTxReceipts(header.getHash()).get(); final Block block = new Block(header, body); shortChain.appendBlock(block, receipts); nextBlock++; } return shortChain; } }
@Test public void networkWithOneValidatorIsAllowedToCreateConsecutiveBlocks() { final Address localAddress = Util.publicKeyToAddress(proposerKeyPair.getPublicKey()); genesisBlock = createEmptyBlock(proposerKeyPair); blockChain = createInMemoryBlockchain(genesisBlock); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); final VoteProposer voteProposer = new VoteProposer(); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null); cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext); headerBuilder.number(1).parentHash(genesisBlock.getHash()); final Block block_1 = createEmptyBlock(proposerKeyPair); blockChain.appendBlock(block_1, Lists.newArrayList()); assertThat( CliqueHelpers.addressIsAllowedToProduceNextBlock( localAddress, cliqueProtocolContext, block_1.getHeader())) .isTrue(); }
@Before public void setup() { genesisBlock = blockDataGenerator.genesisBlock(); localBlockchain = createInMemoryBlockchain(genesisBlock); final WorldStateArchive worldStateArchive = createInMemoryWorldStateArchive(); ethProtocolManager = EthProtocolManagerTestUtil.create(localBlockchain, worldStateArchive); ethContext = ethProtocolManager.ethContext(); protocolContext = new ProtocolContext<>(localBlockchain, worldStateArchive, null); }
public static EthProtocolManager create(final TimeoutPolicy timeoutPolicy) { final ProtocolSchedule<Void> protocolSchedule = MainnetProtocolSchedule.create(); final GenesisConfigFile config = GenesisConfigFile.mainnet(); final GenesisState genesisState = GenesisState.fromConfig(config, protocolSchedule); final Blockchain blockchain = createInMemoryBlockchain(genesisState.getBlock()); final WorldStateArchive worldStateArchive = createInMemoryWorldStateArchive(); return create(blockchain, worldStateArchive, timeoutPolicy); }
@Test public void networkWithTwoValidatorsIsAllowedToProduceBlockIfNotPreviousBlockProposer() { final Address otherAddress = Util.publicKeyToAddress(otherNodeKeyPair.getPublicKey()); validatorList.add(otherAddress); genesisBlock = createEmptyBlock(otherNodeKeyPair); blockChain = createInMemoryBlockchain(genesisBlock); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); final VoteProposer voteProposer = new VoteProposer(); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null); cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext); headerBuilder.number(1).parentHash(genesisBlock.getHash()); final Block block_1 = createEmptyBlock(proposerKeyPair); blockChain.appendBlock(block_1, Lists.newArrayList()); headerBuilder.number(2).parentHash(block_1.getHash()); final Block block_2 = createEmptyBlock(otherNodeKeyPair); blockChain.appendBlock(block_2, Lists.newArrayList()); assertThat( CliqueHelpers.addressIsAllowedToProduceNextBlock( localAddress, cliqueProtocolContext, block_1.getHeader())) .isFalse(); assertThat( CliqueHelpers.addressIsAllowedToProduceNextBlock( localAddress, cliqueProtocolContext, block_2.getHeader())) .isTrue(); }
@Test public void networkWithTwoValidatorsIsNotAllowedToProduceBlockIfIsPreviousBlockProposer() { final Address otherAddress = Util.publicKeyToAddress(otherNodeKeyPair.getPublicKey()); validatorList.add(otherAddress); genesisBlock = createEmptyBlock(proposerKeyPair); blockChain = createInMemoryBlockchain(genesisBlock); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); final VoteProposer voteProposer = new VoteProposer(); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null); cliqueProtocolContext = new ProtocolContext<>(blockChain, null, cliqueContext); headerBuilder.parentHash(genesisBlock.getHash()).number(1); final Block block_1 = createEmptyBlock(proposerKeyPair); blockChain.appendBlock(block_1, Lists.newArrayList()); headerBuilder.parentHash(block_1.getHeader().getHash()).number(2); final BlockHeader block_2 = TestHelpers.createCliqueSignedBlockHeader(headerBuilder, proposerKeyPair, validatorList); final SignerRateLimitValidationRule validationRule = new SignerRateLimitValidationRule(); assertThat(validationRule.validate(block_2, block_1.getHeader(), cliqueProtocolContext)) .isFalse(); }
@BeforeClass public static void setupClass() { genesisBlock = blockDataGenerator.genesisBlock(); localBlockchain = createInMemoryBlockchain(genesisBlock); // Setup local chain for (int i = 1; i <= chainHeight; i++) { final BlockDataGenerator.BlockOptions options = new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); final Block block = blockDataGenerator.block(options); final List<TransactionReceipt> receipts = blockDataGenerator.receipts(block); localBlockchain.appendBlock(block, receipts); } }
blockChain = createInMemoryBlockchain(genesisBlock);
@BeforeClass public static void setupClass() { genesisBlock = blockDataGenerator.genesisBlock(); localBlockchain = createInMemoryBlockchain(genesisBlock); // Setup local chain. for (int i = 1; i <= chainHeight; i++) { final BlockDataGenerator.BlockOptions options = new BlockDataGenerator.BlockOptions() .setBlockNumber(i) .setParentHash(localBlockchain.getBlockHashByNumber(i - 1).get()); final Block block = blockDataGenerator.block(options); final List<TransactionReceipt> receipts = blockDataGenerator.receipts(block); localBlockchain.appendBlock(block, receipts); } }
@Before public void setup() { protocolSchedule = CliqueProtocolSchedule.create( GenesisConfigFile.DEFAULT.getConfigOptions(), proposerKeyPair); final Address otherAddress = Util.publicKeyToAddress(otherKeyPair.getPublicKey()); validatorList.add(otherAddress); final VoteTallyCache voteTallyCache = mock(VoteTallyCache.class); when(voteTallyCache.getVoteTallyAfterBlock(any())).thenReturn(new VoteTally(validatorList)); voteProposer = new VoteProposer(); final CliqueContext cliqueContext = new CliqueContext(voteTallyCache, voteProposer, null); final Block genesis = GenesisState.fromConfig(GenesisConfigFile.mainnet(), protocolSchedule).getBlock(); blockchain = createInMemoryBlockchain(genesis); protocolContext = new ProtocolContext<>(blockchain, stateArchive, cliqueContext); // Add a block above the genesis final BlockHeaderTestFixture headerTestFixture = new BlockHeaderTestFixture(); headerTestFixture.number(1).parentHash(genesis.getHeader().getHash()); final Block emptyBlock = new Block( TestHelpers.createCliqueSignedBlockHeader( headerTestFixture, otherKeyPair, validatorList), new BlockBody(Lists.newArrayList(), Lists.newArrayList())); blockchain.appendBlock(emptyBlock, Lists.newArrayList()); }
@Before public void constructThreeBlockChain() { for (int i = 0; i < 3; i++) { validators.add(AddressHelpers.ofValue(i)); } headerBuilder.extraData( new CliqueExtraData( BytesValue.wrap(new byte[32]), Signature.create(BigInteger.TEN, BigInteger.TEN, (byte) 1), validators) .encode()); genesisBlock = createEmptyBlock(0, Hash.ZERO); blockChain = createInMemoryBlockchain(genesisBlock); block_1 = createEmptyBlock(1, genesisBlock.getHeader().getHash()); block_2 = createEmptyBlock(1, block_1.getHeader().getHash()); blockChain.appendBlock(block_1, Lists.emptyList()); blockChain.appendBlock(block_2, Lists.emptyList()); }