@JsonCreator public PostSection( @JsonProperty("hash") final String hash, @JsonProperty("logs") final String logs, @JsonProperty("indexes") final Indexes indexes) { this.rootHash = Hash.fromHexString(hash); this.logsHash = Hash.fromHexString(logs); this.indexes = indexes; } }
@Test public void shouldGetExpectedValueForEmptyHash() { assertThat(Hash.EMPTY) .isEqualTo( Hash.fromHexString("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")); } }
Expectation( @JsonProperty("hash") final String hash, @JsonProperty("sender") final String sender) { this.succeeds = hash != null && sender != null; if (succeeds) { this.hash = Hash.fromHexString(hash); this.sender = Address.fromHexString(sender); } else { this.hash = null; this.sender = null; } }
@JsonCreator public BlockchainReferenceTestCaseSpec( @JsonProperty("network") final String network, @JsonProperty("blocks") final CandidateBlock[] candidateBlocks, @JsonProperty("genesisBlockHeader") final BlockHeaderMock genesisBlockHeader, @JsonProperty("genesisRLP") final String genesisRLP, @JsonProperty("pre") final Map<String, WorldStateMock.AccountMock> accounts, @JsonProperty("lastblockhash") final String lastBlockHash, @JsonProperty("sealEngine") final String sealEngine) { this.network = network; this.candidateBlocks = candidateBlocks; this.genesisBlockHeader = genesisBlockHeader; this.genesisRLP = genesisRLP == null ? null : BytesValue.fromHexString(genesisRLP); this.lastBlockHash = Hash.fromHexString(lastBlockHash); this.worldStateArchive = buildWorldStateArchive(accounts); this.blockchain = buildBlockchain(genesisBlockHeader); this.sealEngine = sealEngine; this.protocolContext = new ProtocolContext<>(this.blockchain, this.worldStateArchive, null); }
@Test @SuppressWarnings("unchecked") public void returnsValidatorsForBlockHash() { final JsonRpcRequest request = new JsonRpcRequest("2.0", "clique_getSignersAtHash", new Object[] {BLOCK_HASH}); when(blockchainQueries.blockByHash(Hash.fromHexString(BLOCK_HASH))) .thenReturn(Optional.of(blockWithMetadata)); when(blockWithMetadata.getHeader()).thenReturn(blockHeader); when(voteTallyCache.getVoteTallyAfterBlock(blockHeader)).thenReturn(voteTally); when(voteTally.getValidators()).thenReturn(validators); final JsonRpcSuccessResponse response = (JsonRpcSuccessResponse) method.response(request); assertEquals(validatorsAsStrings, response.getResult()); }
@Test public void failsOnInvalidBlockHash() { final JsonRpcRequest request = new JsonRpcRequest("2.0", "clique_getSigners", new Object[] {BLOCK_HASH}); when(blockchainQueries.blockByHash(Hash.fromHexString(BLOCK_HASH))) .thenReturn(Optional.empty()); final JsonRpcErrorResponse response = (JsonRpcErrorResponse) method.response(request); assertThat(response.getError().name()).isEqualTo(JsonRpcError.INTERNAL_ERROR.name()); } }
@Test public void getBlockByHashForUnknownBlock() throws Exception { final String id = "123"; final String blockHashString = "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d15273321"; final Hash blockHash = Hash.fromHexString(blockHashString); final RequestBody body = RequestBody.create( JSON, "{\"jsonrpc\":\"2.0\",\"id\":" + Json.encode(id) + ",\"method\":\"eth_getBlockByHash\", \"params\": [\"" + blockHashString + "\",true]}"); // Setup mocks when(blockchainQueries.blockByHash(eq(blockHash))).thenReturn(Optional.empty()); try (final Response resp = client.newCall(buildPostRequest(body)).execute()) { assertThat(resp.code()).isEqualTo(200); // Check general format of result final JsonObject json = new JsonObject(resp.body().string()); testHelper.assertValidJsonRpcResult(json, id); // Check result final Object result = json.getValue("result"); // For now, no block will be returned so we should get null assertThat(result).isNull(); } }
@Override public Hash execute(final JsonRequestFactories node) { final String signedTransactionData = signedTransactionData(); try { return Hash.fromHexString( node.eth().ethSendRawTransaction(signedTransactionData).send().getTransactionHash()); } catch (final IOException e) { throw new RuntimeException(e); } }
public Condition validatorsAtBlockHashFromBlockNumberEqual( final Node node, final long blockNumber, final PantheonNode... validators) { final DefaultBlockParameter blockParameter = DefaultBlockParameter.valueOf(BigInteger.valueOf(blockNumber)); final String blockHash = node.execute(eth.block(blockParameter)).getHash(); return new ExpectValidatorsAtBlockHash( clique, fromHexString(blockHash), validatorAddresses(validators)); }
@Test public void createFromJsonWithAllocs() throws Exception { final GenesisState genesisState = GenesisState.fromJson( Resources.toString(GenesisStateTest.class.getResource("genesis1.json"), Charsets.UTF_8), MainnetProtocolSchedule.create()); final BlockHeader header = genesisState.getBlock().getHeader(); assertThat(header.getStateRoot()) .isEqualTo( Hash.fromHexString( "0x92683e6af0f8a932e5fe08c870f2ae9d287e39d4518ec544b0be451f1035fd39")); assertThat(header.getTransactionsRoot()).isEqualTo(Hash.EMPTY_TRIE_HASH); assertThat(header.getReceiptsRoot()).isEqualTo(Hash.EMPTY_TRIE_HASH); assertThat(header.getOmmersHash()).isEqualTo(Hash.EMPTY_LIST_HASH); assertThat(header.getExtraData()).isEqualTo(BytesValue.EMPTY); assertThat(header.getParentHash()).isEqualTo(Hash.ZERO); final DefaultMutableWorldState worldState = new DefaultMutableWorldState( new KeyValueStorageWorldStateStorage(new InMemoryKeyValueStorage())); genesisState.writeStateTo(worldState); final Account first = worldState.get(Address.fromHexString("0x0000000000000000000000000000000000000001")); final Account second = worldState.get(Address.fromHexString("0x0000000000000000000000000000000000000002")); assertThat(first).isNotNull(); assertThat(first.getBalance().toLong()).isEqualTo(111111111L); assertThat(second).isNotNull(); assertThat(second.getBalance().toLong()).isEqualTo(222222222L); }
@Test public void containsAccount_AccountExists() { final MutableWorldState worldState = createEmpty(); final WorldUpdater updater = worldState.updater(); updater.createAccount(ADDRESS).setBalance(Wei.of(100000)); updater.commit(); assertNotNull(worldState.get(ADDRESS)); assertEquals( Hash.fromHexString("0xa3e1c133a5a51b03399ed9ad0380f3182e9e18322f232b816dd4b9094f871e1b"), worldState.rootHash()); }
@Test public void getAccountNonce_AccountExists() { final MutableWorldState worldState = createEmpty(); final WorldUpdater updater = worldState.updater(); updater.createAccount(ADDRESS).setNonce(1L); updater.commit(); assertEquals(1L, worldState.get(ADDRESS).getNonce()); assertEquals( Hash.fromHexString("0x9648b05cc2eef5513ae2edfe16bfcedb3d1c60ffb5dff3fc501bd3e4ae39f536"), worldState.rootHash()); }
@Test public void replaceAccountNonce() { final MutableWorldState worldState = createEmpty(); final WorldUpdater updater = worldState.updater(); final MutableAccount account = updater.createAccount(ADDRESS); account.setNonce(1L); account.setNonce(2L); updater.commit(); assertEquals(2L, worldState.get(ADDRESS).getNonce()); assertEquals( Hash.fromHexString("0x7f64d13e61301a5154a5f06483a38572629e977b316cbe5a28b5f0522010a4bf"), worldState.rootHash()); }
@Test public void replaceStorageValue_ZeroValue() { final MutableWorldState worldState = createEmpty(); final WorldUpdater updater = worldState.updater(); final MutableAccount account = updater.createAccount(ADDRESS); account.setBalance(Wei.of(100000)); account.setStorageValue(UInt256.ONE, UInt256.of(2)); account.setStorageValue(UInt256.ONE, UInt256.ZERO); updater.commit(); assertEquals( Hash.fromHexString("0xa3e1c133a5a51b03399ed9ad0380f3182e9e18322f232b816dd4b9094f871e1b"), worldState.rootHash()); }
@Test public void setStorageValue_ZeroValue() { final MutableWorldState worldState = createEmpty(); final WorldUpdater updater = worldState.updater(); final MutableAccount account = updater.createAccount(ADDRESS); account.setBalance(Wei.of(100000)); account.setStorageValue(UInt256.ZERO, UInt256.ZERO); updater.commit(); assertEquals(UInt256.ZERO, worldState.get(ADDRESS).getStorageValue(UInt256.ZERO)); assertEquals( Hash.fromHexString("0xa3e1c133a5a51b03399ed9ad0380f3182e9e18322f232b816dd4b9094f871e1b"), worldState.rootHash()); }
@Test public void replaceAccountBalance() { final MutableWorldState worldState = createEmpty(); final WorldUpdater updater = worldState.updater(); final MutableAccount account = updater.createAccount(ADDRESS); account.setBalance(Wei.of(100000)); account.setBalance(Wei.of(200000)); updater.commit(); assertEquals(Wei.of(200000), worldState.get(ADDRESS).getBalance()); assertEquals( Hash.fromHexString("0xbfa4e0598cc2b810a8ccc4a2d9a4c575574d05c9c4a7f915e6b8545953a5051e"), worldState.rootHash()); }
@Test public void setStorageValue_NonzeroValue() { final MutableWorldState worldState = createEmpty(); final WorldUpdater updater = worldState.updater(); final MutableAccount account = updater.createAccount(ADDRESS); account.setBalance(Wei.of(100000)); account.setStorageValue(UInt256.ONE, UInt256.of(2)); updater.commit(); assertEquals(UInt256.of(2), worldState.get(ADDRESS).getStorageValue(UInt256.ONE)); assertEquals( Hash.fromHexString("0xd31ce0bf3bf8790083a8ebde418244fda3b1cca952d7119ed244f86d03044656"), worldState.rootHash()); }
@Test public void replaceStorageValue_NonzeroValue() { final MutableWorldState worldState = createEmpty(); final WorldUpdater updater = worldState.updater(); final MutableAccount account = updater.createAccount(ADDRESS); account.setBalance(Wei.of(100000)); account.setStorageValue(UInt256.ONE, UInt256.of(2)); account.setStorageValue(UInt256.ONE, UInt256.of(3)); updater.commit(); assertEquals(UInt256.of(3), worldState.get(ADDRESS).getStorageValue(UInt256.ONE)); assertEquals( Hash.fromHexString("0x1d0ddb5079fe5b8689124b68c9e5bb3f4d8e13c2f7489d24f088c78fd45e058d"), worldState.rootHash()); }
@Test public void replaceAccountCode() { final MutableWorldState worldState = createEmpty(); final WorldUpdater updater = worldState.updater(); final MutableAccount account = updater.createAccount(ADDRESS); account.setBalance(Wei.of(100000)); account.setCode(BytesValue.of(1, 2, 3)); account.setCode(BytesValue.of(3, 2, 1)); updater.commit(); assertEquals(BytesValue.of(3, 2, 1), worldState.get(ADDRESS).getCode()); assertEquals( Hash.fromHexString("0xc14f5e30581de9155ea092affa665fad83bcd9f98e45c4a42885b9b36d939702"), worldState.rootHash()); }
@Test public void revert() { final MutableWorldState worldState = createEmpty(); final WorldUpdater updater1 = worldState.updater(); final MutableAccount account1 = updater1.createAccount(ADDRESS); account1.setBalance(Wei.of(200000)); updater1.commit(); final WorldUpdater updater2 = worldState.updater(); final MutableAccount account2 = updater2.getMutable(ADDRESS); account2.setBalance(Wei.of(300000)); assertEquals(Wei.of(300000), updater2.get(ADDRESS).getBalance()); updater2.revert(); assertEquals(Wei.of(200000), updater2.get(ADDRESS).getBalance()); updater2.commit(); assertEquals(Wei.of(200000), worldState.get(ADDRESS).getBalance()); assertEquals( Hash.fromHexString("0xbfa4e0598cc2b810a8ccc4a2d9a4c575574d05c9c4a7f915e6b8545953a5051e"), worldState.rootHash()); }