public BlockWithMetadata<TransactionWithMetadata, Hash> blockWithMetadata( final BlockHeader header) { final KeyPair keyPair = KeyPair.generate(); final List<TransactionWithMetadata> transactions = new ArrayList<>(); for (int i = 0; i < 3; i++) { final Transaction transaction = transactionTestFixture.createTransaction(keyPair); transactions.add( new TransactionWithMetadata(transaction, header.getNumber(), header.getHash(), 0)); } final List<Hash> ommers = new ArrayList<>(); ommers.add(Hash.ZERO); return new BlockWithMetadata<>(header, transactions, ommers, header.getDifficulty(), 0); } }
private void assertBlockResultMatchesBlock(final JsonObject result, final Block block) { final BlockHeader header = block.getHeader(); assertThat(Hash.fromHexString(result.getString("parentHash"))) .isEqualTo(header.getParentHash()); assertThat(Hash.fromHexString(result.getString("sha3Uncles"))) .isEqualTo(header.getOmmersHash()); assertThat(Hash.fromHexString(result.getString("transactionsRoot"))) .isEqualTo(header.getTransactionsRoot()); assertThat(Hash.fromHexString(result.getString("stateRoot"))).isEqualTo(header.getStateRoot()); assertThat(Hash.fromHexString(result.getString("receiptsRoot"))) .isEqualTo(header.getReceiptsRoot()); assertThat(Address.fromHexString(result.getString("miner"))).isEqualTo(header.getCoinbase()); assertThat(UInt256.fromHexString(result.getString("difficulty"))) .isEqualTo(header.getDifficulty()); assertThat(BytesValue.fromHexString(result.getString("extraData"))) .isEqualTo(header.getExtraData()); assertThat(hexStringToInt(result.getString("size"))).isEqualTo(block.calculateSize()); assertThat(Long.decode(result.getString("gasLimit"))).isEqualTo(header.getGasLimit()); assertThat(Long.decode(result.getString("gasUsed"))).isEqualTo(header.getGasUsed()); assertThat(Long.decode(result.getString("timestamp"))).isEqualTo(header.getTimestamp()); assertThat(Long.decode(result.getString("number"))).isEqualTo(header.getNumber()); // Nonce is a data field and should represent 8 bytes exactly final String nonceResult = result.getString("nonce").toLowerCase(); assertThat(nonceResult.length() == 18 && nonceResult.startsWith("0x")).isTrue(); assertThat(Long.parseUnsignedLong(nonceResult.substring(2), 16)).isEqualTo(header.getNonce()); assertThat(Hash.fromHexString(result.getString("hash"))).isEqualTo(header.getHash()); assertThat(LogsBloomFilter.fromHexString(result.getString("logsBloom"))) .isEqualTo(header.getLogsBloom()); }
public BlockWithMetadata<Hash, Hash> blockWithMetadataAndTxHashes(final Block block) { final UInt256 td = block.getHeader().getDifficulty().plus(10L); final int size = block.calculateSize(); final List<Hash> txs = block .getBody() .getTransactions() .stream() .map(Transaction::hash) .collect(Collectors.toList()); final List<Hash> ommers = block.getBody().getOmmers().stream().map(BlockHeader::getHash).collect(Collectors.toList()); return new BlockWithMetadata<>(block.getHeader(), txs, ommers, td, size); }
public BlockWithMetadata<TransactionWithMetadata, Hash> blockWithMetadata( final BlockHeader header) { final KeyPair keyPair = KeyPair.generate(); final List<TransactionWithMetadata> transactions = new ArrayList<>(); for (int i = 0; i < 3; i++) { final Transaction transaction = transactionTestFixture.createTransaction(keyPair); transactions.add( new TransactionWithMetadata(transaction, header.getNumber(), header.getHash(), 0)); } final List<Hash> ommers = new ArrayList<>(); ommers.add(Hash.ZERO); return new BlockWithMetadata<>(header, transactions, ommers, header.getDifficulty(), 0); } }
@Test public void shouldReturnCompleteTxWhenParameterTrue() { final NewBlockHeadersSubscription subscription = createSubscription(true); final List<TransactionWithMetadata> txHashList = transactionsWithMetadata(); final BlockWithMetadata<TransactionWithMetadata, Hash> testBlockWithMetadata = new BlockWithMetadata<>( blockHeader, txHashList, Collections.emptyList(), blockHeader.getDifficulty(), 0); final BlockResult expectedNewBlock = blockResultFactory.transactionComplete(testBlockWithMetadata); when(blockchainQueries.blockByHash(testBlockWithMetadata.getHeader().getHash())) .thenReturn(Optional.of(testBlockWithMetadata)); simulateAddingBlock(); verify(subscriptionManager) .sendMessage(subscriptionIdCaptor.capture(), responseCaptor.capture()); final Long actualSubscriptionId = subscriptionIdCaptor.getValue(); final Object actualBlock = responseCaptor.getValue(); assertThat(actualSubscriptionId).isEqualTo(subscription.getId()); assertThat(actualBlock).isInstanceOf(BlockResult.class); final BlockResult actualBlockResult = (BlockResult) actualBlock; assertThat(actualBlockResult.getTransactions()).hasSize(txHashList.size()); assertThat(actualBlock).isEqualToComparingFieldByFieldRecursively(expectedNewBlock); verify(subscriptionManager, times(1)).sendMessage(any(), any()); verify(blockchainQueries, times(0)).blockByHashWithTxHashes(any()); verify(blockchainQueries, times(1)).blockByHash(any()); }
public BlockWithMetadata<TransactionWithMetadata, Hash> blockWithMetadata(final Block block) { final UInt256 td = block.getHeader().getDifficulty().plus(10L); final int size = block.calculateSize(); final List<Transaction> txs = block.getBody().getTransactions(); final List<TransactionWithMetadata> formattedTxs = new ArrayList<>(txs.size()); for (int i = 0; i < txs.size(); i++) { formattedTxs.add( new TransactionWithMetadata( txs.get(i), block.getHeader().getNumber(), block.getHash(), i)); } final List<Hash> ommers = block.getBody().getOmmers().stream().map(BlockHeader::getHash).collect(Collectors.toList()); return new BlockWithMetadata<>(block.getHeader(), formattedTxs, ommers, td, size); }
private void assertTotalDifficultiesAreConsistent(final Blockchain blockchain, final Block head) { // Check that total difficulties are summed correctly long num = BlockHeader.GENESIS_BLOCK_NUMBER; UInt256 td = UInt256.of(0); while (num <= head.getHeader().getNumber()) { final Hash curHash = blockchain.getBlockHashByNumber(num).get(); final BlockHeader curHead = blockchain.getBlockHeader(curHash).get(); td = td.plus(curHead.getDifficulty()); assertEquals(td, blockchain.getTotalDifficultyByHash(curHash).get()); num += 1; } // Check reported chainhead td assertEquals(td, blockchain.getChainHead().getTotalDifficulty()); }
.setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkBlock) .setDifficulty(chain.get(forkBlock).getHeader().getDifficulty().plus(10L)); final Block fork = gen.block(options); final List<TransactionReceipt> forkReceipts = gen.receipts(fork);
.setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkStart) .setDifficulty(chain.get(forkStart).getHeader().getDifficulty().minus(5L)); forkBlocks.add(gen.block(options));
.setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkBlock) .setDifficulty(chain.get(forkBlock).getHeader().getDifficulty().plus(10L)) .addTransaction(overlappingTx) .addTransaction(gen.transaction());
.setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkStart) .setDifficulty(chain.get(forkStart).getHeader().getDifficulty().minus(5L)); forkBlocks.add(gen.block(options)); .get(forkStart + 1) .getHeader() .getDifficulty() .plus(chain.get(forkStart + 2).getHeader().getDifficulty()); options = new BlockOptions()
.setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkStart) .setDifficulty(chain.get(forkStart).getHeader().getDifficulty().minus(5L)); forkBlocks.add(gen.block(options)); .setParentHash(forkBlocks.get(0).getHash()) .setBlockNumber(forkStart + 1) .setDifficulty(chain.get(forkStart + 1).getHeader().getDifficulty().minus(5L)); forkBlocks.add(gen.block(options)); .setParentHash(chain.get(commonAncestor).getHash()) .setBlockNumber(forkStart) .setDifficulty(chain.get(forkStart).getHeader().getDifficulty().minus(5L)); final Block secondFork = gen.block(options); blockchain.appendBlock(secondFork, gen.receipts(secondFork));
@Test public void logsShouldBeFlaggedAsRemovedWhenBlockIsNotInCanonicalChain() { // create initial blockchain final BlockchainWithData data = setupBlockchain(3); final Block targetBlock = data.blockData.get(data.blockData.size() - 1).block; // check that logs have removed = false List<LogWithMetadata> logs = data.blockchainQueries.matchingLogs(targetBlock.getHash(), new LogsQuery.Builder().build()); assertThat(logs).isNotEmpty(); assertThat(logs).allMatch(l -> !l.isRemoved()); // Create parallel fork of length 1 final int forkBlock = 2; final int commonAncestor = 1; final BlockOptions options = new BlockOptions() .setParentHash(data.blockchain.getBlockHashByNumber(commonAncestor).get()) .setBlockNumber(forkBlock) .setDifficulty( data.blockchain.getBlockHeader(forkBlock).get().getDifficulty().plus(10L)); final Block fork = gen.block(options); final List<TransactionReceipt> forkReceipts = gen.receipts(fork); // Add fork data.blockchain.appendBlock(fork, forkReceipts); // check that logs have removed = true logs = data.blockchainQueries.matchingLogs(targetBlock.getHash(), new LogsQuery.Builder().build()); assertThat(logs).isNotEmpty(); assertThat(logs).allMatch(LogWithMetadata::isRemoved); }