public static Hash generateTestBlockHash(final long number) { final byte[] bytes = Long.toString(number).getBytes(UTF_8); return Hash.hash(BytesValue.wrap(bytes)); }
private Hash hash(final UInt256 key) { return Hash.hash(key.getBytes()); } }
@Test public void shouldReturnBlockHashUsingLookupFromFrameWhenItIsWithinTheAllowedRange() { final Hash blockHash = Hash.hash(BytesValue.fromHexString("0x1293487297")); when(blockHashLookup.getBlockHash(100)).thenReturn(blockHash); assertBlockHash(100, blockHash, 200); verify(blockHashLookup).getBlockHash(100); }
@Test public void shouldGetHashOfAccountCodeWhenCodeIsPresent() { final BytesValue code = BytesValue.fromHexString("0xabcdef"); worldStateUpdater.getOrCreate(REQUESTED_ADDRESS).setCode(code); assertThat(executeOperation(REQUESTED_ADDRESS)).isEqualTo(Hash.hash(code)); }
@Test public void roundTripWithHash() { for (final boolean reverse : Arrays.asList(true, false)) { final Hash hash = Hash.hash(BytesValue.wrap(new byte[10])); final int skip = 10; final int maxHeaders = 128; final GetBlockHeadersMessage initialMessage = GetBlockHeadersMessage.create(hash, maxHeaders, skip, reverse); final MessageData raw = new RawMessage(EthPV62.GET_BLOCK_HEADERS, initialMessage.getData()); final GetBlockHeadersMessage message = GetBlockHeadersMessage.readFrom(raw); Assertions.assertThat(message.blockNumber()).isEmpty(); Assertions.assertThat(message.hash().get()).isEqualTo(hash); Assertions.assertThat(message.reverse()).isEqualTo(reverse); Assertions.assertThat(message.skip()).isEqualTo(skip); Assertions.assertThat(message.maxHeaders()).isEqualTo(maxHeaders); } }
@Test public void getNodeData_saveAndGetRegularValue() { BytesValue bytes = BytesValue.fromHexString("0x123456"); KeyValueStorageWorldStateStorage storage = emptyStorage(); storage.updater().putAccountStorageTrieNode(Hash.hash(bytes), bytes).commit(); assertThat(storage.getNodeData(Hash.hash(bytes))).contains(bytes); }
@Test public void getAccountStateTrieNode_saveAndGetRegularValue() { BytesValue bytes = BytesValue.fromHexString("0x123456"); KeyValueStorageWorldStateStorage storage = emptyStorage(); storage.updater().putAccountStateTrieNode(Hash.hash(bytes), bytes).commit(); assertThat(storage.getAccountStateTrieNode(Hash.hash(bytes))).contains(bytes); }
@Test public void getAccountStorageTrieNode_saveAndGetRegularValue() { BytesValue bytes = BytesValue.fromHexString("0x123456"); KeyValueStorageWorldStateStorage storage = emptyStorage(); storage.updater().putAccountStorageTrieNode(Hash.hash(bytes), bytes).commit(); assertThat(storage.getAccountStateTrieNode(Hash.hash(bytes))).contains(bytes); }
@Test public void getAccountStorageTrieNode_saveAndGetSpecialValues() { KeyValueStorageWorldStateStorage storage = emptyStorage(); storage .updater() .putAccountStorageTrieNode( Hash.hash(MerklePatriciaTrie.EMPTY_TRIE_NODE), MerklePatriciaTrie.EMPTY_TRIE_NODE) .putAccountStorageTrieNode(Hash.hash(BytesValue.EMPTY), BytesValue.EMPTY) .commit(); assertThat(storage.getAccountStorageTrieNode(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) .contains(MerklePatriciaTrie.EMPTY_TRIE_NODE); assertThat(storage.getAccountStorageTrieNode(Hash.EMPTY)).contains(BytesValue.EMPTY); }
@Test public void getAccountStateTrieNode_saveAndGetSpecialValues() { KeyValueStorageWorldStateStorage storage = emptyStorage(); storage .updater() .putAccountStateTrieNode( Hash.hash(MerklePatriciaTrie.EMPTY_TRIE_NODE), MerklePatriciaTrie.EMPTY_TRIE_NODE) .putAccountStateTrieNode(Hash.hash(BytesValue.EMPTY), BytesValue.EMPTY) .commit(); assertThat(storage.getAccountStateTrieNode(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) .contains(MerklePatriciaTrie.EMPTY_TRIE_NODE); assertThat(storage.getAccountStateTrieNode(Hash.EMPTY)).contains(BytesValue.EMPTY); }
@Test public void getNodeData_saveAndGetSpecialValues() { KeyValueStorageWorldStateStorage storage = emptyStorage(); storage .updater() .putAccountStorageTrieNode( Hash.hash(MerklePatriciaTrie.EMPTY_TRIE_NODE), MerklePatriciaTrie.EMPTY_TRIE_NODE) .putAccountStorageTrieNode(Hash.hash(BytesValue.EMPTY), BytesValue.EMPTY) .commit(); assertThat(storage.getNodeData(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH)) .contains(MerklePatriciaTrie.EMPTY_TRIE_NODE); assertThat(storage.getNodeData(Hash.EMPTY)).contains(BytesValue.EMPTY); }
public String getAddress() { return Address.extract(Hash.hash(keyPair.getPublicKey().getEncodedBytes())).toString(); }
@Test public void insufficientCommitSealsFailsValidation() { final KeyPair committerKeyPair = KeyPair.generate(); final Address committerAddress = Address.extract(Hash.hash(committerKeyPair.getPublicKey().getEncodedBytes())); final List<Address> validators = singletonList(committerAddress); final VoteTally voteTally = new VoteTally(validators); final ProtocolContext<IbftContext> context = new ProtocolContext<>(null, null, new IbftContext(voteTally, null)); final BlockHeader header = createProposedBlockHeader(validators, emptyList(), false); // Note that no committer seals are in the header's IBFT extra data. final IbftExtraData headerExtraData = IbftExtraData.decode(header.getExtraData()); assertThat(headerExtraData.getSeals().size()).isEqualTo(0); assertThat(commitSealsValidationRule.validate(header, null, context)).isFalse(); }
@Test public void proposerInValidatorListPassesValidation() { final KeyPair proposerKeyPair = KeyPair.generate(); final Address proposerAddress = Address.extract(Hash.hash(proposerKeyPair.getPublicKey().getEncodedBytes())); final List<Address> validators = Lists.newArrayList(proposerAddress); final List<KeyPair> committers = Lists.newArrayList(proposerKeyPair); final VoteTally voteTally = new VoteTally(validators); final ProtocolContext<IbftContext> context = new ProtocolContext<>(null, null, new IbftContext(voteTally, null)); final IbftCoinbaseValidationRule coinbaseValidationRule = new IbftCoinbaseValidationRule(); BlockHeader header = createProposedBlockHeader(proposerKeyPair, validators, committers); assertThat(coinbaseValidationRule.validate(header, null, context)).isTrue(); }
@Test public void getCode_saveAndGetRegularValue() { BytesValue bytes = BytesValue.fromHexString("0x123456"); KeyValueStorageWorldStateStorage storage = emptyStorage(); storage.updater().putCode(bytes).commit(); assertThat(storage.getCode(Hash.hash(bytes))).contains(bytes); }
@Test public void roundTripRlp() { final Hash digest = Hash.hash(BytesValue.of(1)); final ConsensusRoundIdentifier expectedRoundIdentifier = new ConsensusRoundIdentifier(1, 1); final PreparePayload preparePayload = new PreparePayload(expectedRoundIdentifier, digest); final BytesValueRLPOutput rlpOutput = new BytesValueRLPOutput(); preparePayload.writeTo(rlpOutput); final RLPInput rlpInput = RLP.input(rlpOutput.encoded()); final PreparePayload actualPreparePayload = PreparePayload.readFrom(rlpInput); final ConsensusRoundIdentifier actualConsensusRoundIdentifier = actualPreparePayload.getRoundIdentifier(); final Hash actualDigest = actualPreparePayload.getDigest(); assertThat(actualConsensusRoundIdentifier) .isEqualToComparingFieldByField(expectedRoundIdentifier); assertThat(actualDigest).isEqualTo(digest); assertThat(actualPreparePayload.getMessageType()).isEqualTo(IbftV2.PREPARE); } }
private static Hash expectedHeaderHash() { BlockHeaderBuilder builder = setHeaderFieldsExceptForExtraData(); builder.extraData( new IbftExtraData(VANITY_DATA, emptyList(), VOTE, 0, VALIDATORS) .encodeWithoutCommitSealsAndRoundNumber()); BytesValueRLPOutput rlpOutput = new BytesValueRLPOutput(); builder.buildBlockHeader().writeTo(rlpOutput); return Hash.hash(rlpOutput.encoded()); } }
@Test public void ibftValidateHeaderPasses() { final KeyPair proposerKeyPair = KeyPair.generate(); final Address proposerAddress = Address.extract(Hash.hash(proposerKeyPair.getPublicKey().getEncodedBytes())); final List<Address> validators = singletonList(proposerAddress); final BlockHeader parentHeader = buildBlockHeader(1, proposerKeyPair, validators, null); final BlockHeader blockHeader = buildBlockHeader(2, proposerKeyPair, validators, parentHeader); final BlockHeaderValidator<IbftContext> validator = IbftBlockHeaderValidationRulesetFactory.ibftBlockHeaderValidator(5); assertThat( validator.validateHeader( blockHeader, parentHeader, IbftProtocolContextFixture.protocolContext(validators), HeaderValidationMode.FULL)) .isTrue(); }
@Test public void proposerNotInValidatorListFailsValidation() { final KeyPair proposerKeyPair = KeyPair.generate(); final KeyPair otherValidatorKeyPair = KeyPair.generate(); final Address otherValidatorNodeAddress = Address.extract(Hash.hash(otherValidatorKeyPair.getPublicKey().getEncodedBytes())); final List<Address> validators = Lists.newArrayList(otherValidatorNodeAddress); final List<KeyPair> committers = Lists.newArrayList(otherValidatorKeyPair); final VoteTally voteTally = new VoteTally(validators); final ProtocolContext<IbftContext> context = new ProtocolContext<>(null, null, new IbftContext(voteTally, null)); final IbftCoinbaseValidationRule coinbaseValidationRule = new IbftCoinbaseValidationRule(); BlockHeader header = createProposedBlockHeader(proposerKeyPair, validators, committers); assertThat(coinbaseValidationRule.validate(header, null, context)).isFalse(); } }
@Test public void shouldZeroOutLeftMostBitsToGetAddress() { // If EXTCODEHASH of A is X, then EXTCODEHASH of A + 2**160 is X. final BytesValue code = BytesValue.fromHexString("0xabcdef"); worldStateUpdater.getOrCreate(REQUESTED_ADDRESS).setCode(code); final Bytes32 value = Words.fromAddress(REQUESTED_ADDRESS) .asUInt256() .plus(UInt256.of(2).pow(UInt256.of(160))) .getBytes(); final MessageFrame frame = createMessageFrame(value); operation.execute(frame); assertThat(frame.getStackItem(0)).isEqualTo(Hash.hash(code)); }