@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); }
@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); }
private Map<UInt256, UInt256> setupStorageForCapture(final MessageFrame frame) { final MutableAccount account = mock(MutableAccount.class); when(worldUpdater.getMutable(frame.getRecipientAddress())).thenReturn(account); final Map<UInt256, UInt256> updatedStorage = new TreeMap<>(); updatedStorage.put(UInt256.ZERO, UInt256.of(233)); updatedStorage.put(UInt256.ONE, UInt256.of(2424)); when(account.getUpdatedStorage()).thenReturn(updatedStorage); final Bytes32 word1 = Bytes32.fromHexString("0x01"); final Bytes32 word2 = Bytes32.fromHexString("0x02"); final Bytes32 word3 = Bytes32.fromHexString("0x03"); frame.writeMemory(UInt256.ZERO, UInt256.of(32), word1); frame.writeMemory(UInt256.of(32), UInt256.of(32), word2); frame.writeMemory(UInt256.of(64), UInt256.of(32), word3); return updatedStorage; } }
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 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 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 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); }
@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 shouldGetHashOfAccountCodeWhenCodeIsPresent() { final BytesValue code = BytesValue.fromHexString("0xabcdef"); worldStateUpdater.getOrCreate(REQUESTED_ADDRESS).setCode(code); assertThat(executeOperation(REQUESTED_ADDRESS)).isEqualTo(Hash.hash(code)); }
@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_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 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 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)); }
@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 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 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 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()); }
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); updater.commit(); worldState.persist(); 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); assertThat(worldState.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(originalStorageValue); assertThat(updater.getMutable(ADDRESS).getStorageValue(storageKey)).isEqualTo(newStorageValue); assertThat(updater.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(newStorageValue); assertThat(worldState.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(newStorageValue); assertThat(updater.getMutable(ADDRESS).getStorageValue(storageKey)).isEqualTo(newStorageValue); assertThat(updater.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(newStorageValue); assertThat(worldState.get(ADDRESS).getStorageValue(storageKey)).isEqualTo(newStorageValue);
@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()); }