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 getOriginalStorageValue() { final MutableWorldState worldState = createEmpty(); final WorldUpdater setupUpdater = worldState.updater(); final MutableAccount setupAccount = setupUpdater.createAccount(ADDRESS); setupAccount.setStorageValue(UInt256.ONE, UInt256.of(2)); setupUpdater.commit(); final WorldUpdater updater = worldState.updater(); final MutableAccount account = updater.getOrCreate(ADDRESS); assertThat(account.getOriginalStorageValue(UInt256.ONE)).isEqualTo(UInt256.of(2)); account.setStorageValue(UInt256.ONE, UInt256.of(3)); assertThat(account.getOriginalStorageValue(UInt256.ONE)).isEqualTo(UInt256.of(2)); }
@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); }
MutableAccount account = updater.createAccount(ADDRESS); account.setBalance(Wei.of(100000)); account.setStorageValue(storageKey, originalStorageValue); assertThat(account.getStorageValue(storageKey)).isEqualTo(originalStorageValue); updater.commit(); assertThat(updater.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(originalStorageValue); account.clearStorage(); account.setStorageValue(storageKey, newStorageValue); assertThat(account.getStorageValue(storageKey)).isEqualTo(newStorageValue); assertThat(worldState.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(originalStorageValue);
@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 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 originalStorageValueIsAlwaysZeroIfStorageWasCleared() { final MutableWorldState worldState = createEmpty(); final WorldUpdater setupUpdater = worldState.updater(); final MutableAccount setupAccount = setupUpdater.createAccount(ADDRESS); setupAccount.setStorageValue(UInt256.ONE, UInt256.of(2)); setupUpdater.commit(); final WorldUpdater updater = worldState.updater(); final MutableAccount account = updater.getOrCreate(ADDRESS); account.clearStorage(); assertThat(account.getOriginalStorageValue(UInt256.ONE)).isEqualTo(UInt256.ZERO); }
@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()); }
MutableAccount account = updater.createAccount(ADDRESS); account.setBalance(Wei.of(100000)); account.setStorageValue(storageKey, storageValue); updater.commit(); worldState.persist();
@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 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 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()); }