@Test public void shouldReturnEmptyCodeHashWhenPrecompileHasBalance() { // Sending money to a precompile causes it to exist in the world state archive. worldStateUpdater.getOrCreate(Address.ECREC).setBalance(Wei.of(10)); assertThat(executeOperation(Address.ECREC)).isEqualTo(Hash.EMPTY); }
public static void insertAccount( final WorldUpdater updater, final Address address, final AccountMock toCopy) { final MutableAccount account = updater.getOrCreate(address); account.setNonce(toCopy.nonce()); account.setBalance(toCopy.balance()); account.setCode(toCopy.code()); for (final Map.Entry<UInt256, UInt256> entry : toCopy.storage().entrySet()) { account.setStorageValue(entry.getKey(), entry.getValue()); } }
@Test public void clearStorage() { final UInt256 storageKey = UInt256.of(1L); final UInt256 storageValue = UInt256.of(2L); // Create a world state with one account final MutableWorldState worldState = createEmpty(); final WorldUpdater updater = worldState.updater(); MutableAccount account = updater.createAccount(ADDRESS); account.setBalance(Wei.of(100000)); account.setStorageValue(storageKey, storageValue); assertThat(account.getStorageValue(storageKey)).isEqualTo(storageValue); // Clear storage account = updater.getMutable(ADDRESS); assertThat(account).isNotNull(); assertThat(account.getStorageValue(storageKey)).isEqualTo(storageValue); account.clearStorage(); assertThat(account.getStorageValue(storageKey)).isEqualTo(UInt256.ZERO); assertThat(updater.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(UInt256.ZERO); // Check storage is cleared after committing updater.commit(); assertThat(updater.getMutable(ADDRESS).getStorageValue(storageKey)).isEqualTo(UInt256.ZERO); assertThat(updater.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(UInt256.ZERO); assertThat(worldState.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(UInt256.ZERO); // And after persisting assertThat(updater.getMutable(ADDRESS).getStorageValue(storageKey)).isEqualTo(UInt256.ZERO); assertThat(updater.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(UInt256.ZERO); assertThat(worldState.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(UInt256.ZERO); }
@Test public void clearStorageThenEdit() { final UInt256 storageKey = UInt256.of(1L); final UInt256 originalStorageValue = UInt256.of(2L); final UInt256 newStorageValue = UInt256.of(3L); // Create a world state with one account final MutableWorldState worldState = createEmpty(); final WorldUpdater updater = worldState.updater(); MutableAccount account = updater.createAccount(ADDRESS); account.setBalance(Wei.of(100000)); account.setStorageValue(storageKey, originalStorageValue); assertThat(account.getStorageValue(storageKey)).isEqualTo(originalStorageValue); // Clear storage then edit account = updater.getMutable(ADDRESS); assertThat(account).isNotNull(); assertThat(account.getStorageValue(storageKey)).isEqualTo(originalStorageValue); assertThat(updater.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(originalStorageValue); account.clearStorage(); account.setStorageValue(storageKey, newStorageValue); assertThat(account.getStorageValue(storageKey)).isEqualTo(newStorageValue); // Check storage is cleared after committing updater.commit(); assertThat(updater.getMutable(ADDRESS).getStorageValue(storageKey)).isEqualTo(newStorageValue); assertThat(updater.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(newStorageValue); assertThat(worldState.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(newStorageValue); // And after persisting assertThat(updater.getMutable(ADDRESS).getStorageValue(storageKey)).isEqualTo(newStorageValue); assertThat(updater.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(newStorageValue); assertThat(worldState.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(newStorageValue); }
final WorldUpdater updater = worldState.updater(); MutableAccount account = updater.createAccount(ADDRESS); account.setBalance(Wei.of(100000)); account.setStorageValue(storageKey, originalStorageValue); assertThat(account.getStorageValue(storageKey)).isEqualTo(originalStorageValue);
final WorldUpdater updater = worldState.updater(); MutableAccount account = updater.createAccount(ADDRESS); account.setBalance(Wei.of(100000)); account.setStorageValue(storageKey, storageValue); updater.commit();
@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 getAccountBalance_AccountExists() { final MutableWorldState worldState = createEmpty(); final WorldUpdater updater = worldState.updater(); updater.createAccount(ADDRESS).setBalance(Wei.of(100000)); updater.commit(); assertEquals(Wei.of(100000), worldState.get(ADDRESS).getBalance()); }
@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 shouldCombineUnchangedAndChangedValuesWhenRetrievingStorageEntries() { final MutableWorldState worldState = createEmpty(); WorldUpdater updater = worldState.updater(); MutableAccount account = updater.createAccount(ADDRESS); account.setBalance(Wei.of(100000)); account.setStorageValue(UInt256.ONE, UInt256.of(2)); account.setStorageValue(UInt256.of(2), UInt256.of(5)); updater.commit(); updater = worldState.updater(); account = updater.getMutable(ADDRESS); account.setStorageValue(UInt256.ONE, UInt256.of(3)); account.setStorageValue(UInt256.of(3), UInt256.of(6)); final NavigableMap<Bytes32, UInt256> storage = account.storageEntriesFrom(Hash.ZERO, 10); final NavigableMap<Bytes32, UInt256> expected = new TreeMap<>(); expected.put(hash(UInt256.ONE), UInt256.of(3)); expected.put(hash(UInt256.of(2)), UInt256.of(5)); expected.put(hash(UInt256.of(3)), UInt256.of(6)); assertThat(storage).isEqualTo(expected); }
@Test public void removeAccount_UpdatedAccount() { final MutableWorldState worldState = createEmpty(); final WorldUpdater updater = worldState.updater(); updater.createAccount(ADDRESS).setBalance(Wei.of(100000)); updater.deleteAccount(ADDRESS); updater.commit(); assertEquals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH, worldState.rootHash()); worldState.persist(); assertEquals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH, 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 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()); }
@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 removeAccount_AccountExists() { // Create a world state with one account final MutableWorldState worldState = createEmpty(); WorldUpdater updater = worldState.updater(); updater.createAccount(ADDRESS).setBalance(Wei.of(100000)); updater.commit(); assertNotNull(worldState.get(ADDRESS)); assertNotEquals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH, worldState.rootHash()); // Delete account updater = worldState.updater(); updater.deleteAccount(ADDRESS); assertNull(updater.get(ADDRESS)); assertNull(updater.getMutable(ADDRESS)); updater.commit(); assertNull(updater.get(ADDRESS)); assertEquals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH, worldState.rootHash()); }
@Test public void removeAccount_AccountExistsAndIsPersisted() { // Create a world state with one account final MutableWorldState worldState = createEmpty(); WorldUpdater updater = worldState.updater(); updater.createAccount(ADDRESS).setBalance(Wei.of(100000)); updater.commit(); worldState.persist(); assertNotNull(worldState.get(ADDRESS)); assertNotEquals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH, worldState.rootHash()); // Delete account updater = worldState.updater(); updater.deleteAccount(ADDRESS); assertNull(updater.get(ADDRESS)); assertNull(updater.getMutable(ADDRESS)); // Check account is gone after committing updater.commit(); assertNull(updater.get(ADDRESS)); // And after persisting worldState.persist(); assertNull(updater.get(ADDRESS)); assertEquals(MerklePatriciaTrie.EMPTY_TRIE_NODE_HASH, worldState.rootHash()); }
updater.createAccount(ADDRESS).setBalance(newBalance); assertNotNull(updater.get(ADDRESS)); assertEquals(newBalance, updater.get(ADDRESS).getBalance());