Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VM] cleanup some changes made in the TangerineWhistle PR #814

Merged
merged 13 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/vm/lib/evm/eei.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,14 @@ export default class EEI {
return this._state.accountIsEmpty(address)
}

/**
* Returns true if account exists in the state trie (it can be empty). Returns false if the account is `null`.
* @param address - Address of account
*/
async accountExists(address: Buffer): Promise<boolean> {
return this._state.accountExists(address)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test for this (StateManager accountExists is actually being tested; the accountExists of EEI is actually a proxy function but we should still test that)

}

private _getReturnCode(results: EVMResult) {
// This preserves the previous logic, but seems to contradict the EEI spec
// https://github.com/ewasm/design/blob/38eeded28765f3e193e12881ea72a6ab807a3371/eth_interface.md
Expand Down
11 changes: 4 additions & 7 deletions packages/vm/lib/evm/opFns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -676,16 +676,13 @@ export const handlers: { [k: string]: OpHandler } = {
if (runState._common.gteHardfork('spuriousDragon')) {
// We are at or after Spurious Dragon
// Call new account gas: account is DEAD and we transfer nonzero value
if ((await runState.stateManager.accountIsEmpty(toAddressBuf)) && !value.isZero()) {
if ((await runState.eei.isAccountEmpty(toAddressBuf)) && !value.isZero()) {
runState.eei.useGas(new BN(runState._common.param('gasPrices', 'callNewAccount')))
}
} else if (!(await runState.stateManager.accountExists(toAddressBuf))) {
// We are before Spurious Dragon
} else if (!(await runState.eei.accountExists(toAddressBuf))) {
// We are before Spurious Dragon and the account does not exist.
// Call new account gas: account does not exist (it is not in the state trie, not even as an "empty" account)
const accountDoesNotExist = !(await runState.stateManager.accountExists(toAddressBuf))
if (accountDoesNotExist) {
runState.eei.useGas(new BN(runState._common.param('gasPrices', 'callNewAccount')))
}
runState.eei.useGas(new BN(runState._common.param('gasPrices', 'callNewAccount')))
}

if (!value.isZero()) {
Expand Down
29 changes: 19 additions & 10 deletions packages/vm/lib/state/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,30 +51,39 @@ export default class Cache {
}
}

/**
* Returns true if the key was deleted and thus existed in the cache earlier
* @param key - trie key to lookup
*/
keyIsDeleted(key: Buffer): boolean {
const keyStr = key.toString('hex')
const it = this._cache.find(keyStr)
if (it.node) {
return it.value.deleted
}
return false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test for this

}

/**
* Looks up address in underlying trie.
* @param address - Address of account
* @param create - Create emtpy account if non-existent
*/
async _lookupAccount(address: Buffer, create: boolean = true): Promise<Account | undefined> {
async _lookupAccount(address: Buffer): Promise<Account> {
const raw = await this._trie.get(address)
if (raw || create) {
const account = new Account(raw)
return account
}
const account = new Account(raw)
return account
}

/**
* Looks up address in cache, if not found, looks it up
* in the underlying trie.
* @param key - Address of account
* @param create - Create emtpy account if non-existent
*/
async getOrLoad(key: Buffer, create: boolean = true): Promise<Account | undefined> {
evertonfraga marked this conversation as resolved.
Show resolved Hide resolved
async getOrLoad(key: Buffer): Promise<Account> {
let account = this.lookup(key)

if (!account) {
account = await this._lookupAccount(key, create)
account = await this._lookupAccount(key)
if (account) {
this._update(key, account as Account, false, false)
}
Expand Down Expand Up @@ -114,7 +123,7 @@ export default class Cache {
it.next()
} else if (it.value && it.value.deleted) {
it.value.modified = false
it.value.deleted = false
it.value.deleted = true
it.value.val = new Account().serialize()
await this._trie.del(Buffer.from(it.key, 'hex'))
next = it.hasNext
Expand Down
2 changes: 1 addition & 1 deletion packages/vm/lib/state/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface StorageDump {
export interface StateManager {
copy(): StateManager
getAccount(address: Buffer): Promise<Account>
putAccount(address: Buffer, account: Account | null): Promise<void>
putAccount(address: Buffer, account: Account): Promise<void>
deleteAccount(address: Buffer): Promise<void>
touchAccount(address: Buffer): void
putContractCode(address: Buffer, value: Buffer): Promise<void>
Expand Down
9 changes: 7 additions & 2 deletions packages/vm/lib/state/stateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ export default class DefaultStateManager implements StateManager {
this.touchAccount(address)
}

/**
* Deletes an [`@ethereumjs/account`](https://github.com/ethereumjs/ethereumjs-vm/tree/master/packages/account)
* from state under the provided `address`. The account will also be removed from the state trie.
* @param address - Address of the account which should be deleted
*/
async deleteAccount(address: Buffer) {
this._cache.del(address)
this.touchAccount(address)
Expand All @@ -115,7 +120,7 @@ export default class DefaultStateManager implements StateManager {
const codeHash = keccak256(value)

if (codeHash.equals(KECCAK256_NULL)) {
//return
return
}

const account = await this.getAccount(address)
Expand Down Expand Up @@ -489,7 +494,7 @@ export default class DefaultStateManager implements StateManager {
*/
async accountExists(address: Buffer): Promise<boolean> {
const account = await this._cache.lookup(address)
if (account) {
if (account && !this._cache.keyIsDeleted(address)) {
return true
}
if (await this._cache._trie.get(address)) {
Expand Down