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: address consensus issue: tx goes OOG but refunds get applied anyways #1603

Merged
merged 9 commits into from
Dec 11, 2021
17 changes: 11 additions & 6 deletions packages/vm/src/evm/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ export default class EVM {
;(<any>this._state).addWarmedAddress((await this._generateAddress(message)).buf)
}

const oldRefund = this._refund.clone()

await this._state.checkpoint()
if (this._vm.DEBUG) {
debug('-'.repeat(100))
Expand Down Expand Up @@ -187,12 +189,18 @@ export default class EVM {
} ]`
)
}
const err = result.execResult.exceptionError

// TODO: Move `gasRefund` to a tx-level result object
// instead of `ExecResult`.
// This clause captures any error which happened during execution
// If that is the case, then set the _refund tracker to the old refund value
if (err) {
// TODO: Move `gasRefund` to a tx-level result object
// instead of `ExecResult`.
this._refund = oldRefund
result.execResult.selfdestruct = {}
}
result.execResult.gasRefund = this._refund.clone()

const err = result.execResult.exceptionError
if (err) {
if (this._vm._common.gteHardfork('homestead') || err.error != ERROR.CODESTORE_OUT_OF_GAS) {
result.execResult.logs = []
Expand Down Expand Up @@ -477,7 +485,6 @@ export default class EVM {
eei._result.selfdestruct = message.selfdestruct
}

const oldRefund = this._refund.clone()
const interpreter = new Interpreter(this._vm, eei)
const interpreterRes = await interpreter.run(message.code as Buffer, opts)

Expand All @@ -494,8 +501,6 @@ export default class EVM {
logs: [],
selfdestruct: {},
}
// Revert gas refund if message failed
this._refund = oldRefund
}

return {
Expand Down
41 changes: 41 additions & 0 deletions packages/vm/tests/api/runTx.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,44 @@ tape('runTx() -> API return values', async (t) => {
t.end()
})
})

tape('runTx() -> consensus bugs', async (t) => {
t.test('validate out-of-gas does not give any refunds', async (t) => {
// There was a consensus bug in the following mainnet tx:
// 0xe3b0fb0a45bc905d1f98baabaadd194901267d02de74cdad187b7feb8920d7b3
// This tx does not access any other accounts and creates a new contract,
// so we are safe to setup the account which creates this contract
// and just re-use the tx.
const txData = {
blockHash: '0x78046e1c8f7956f2b654033c02f348b1c1d111701ec83cd9b5bf690e92efdedd',
blockNumber: 2772981,
gasLimit: 489999,
gasPrice: 41000000000,
data: '0x6060604052604060405190810160405280600981526020017f546f6b656e20302e31000000000000000000000000000000000000000000000081526020015060009080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106200008b57805160ff1916838001178555620000bc565b82800160010185558215620000bc579182015b82811115620000bb5782518255916020019190600101906200009e565b5b509050620000e491905b80821115620000e0576000816000905550600101620000c6565b5090565b5050604060405190810160405280600d81526020017f7472616c616c6120746f6b656e0000000000000000000000000000000000000081526020015060019080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106200016d57805160ff19168380011785556200019e565b828001600101855582156200019e579182015b828111156200019d57825182559160200191906001019062000180565b5b509050620001c691905b80821115620001c2576000816000905550600101620001a8565b5090565b5050604060405190810160405280600481526020017f476f6c640000000000000000000000000000000000000000000000000000000081526020015060029080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106200024f57805160ff191683800117855562000280565b8280016001018555821562000280579182015b828111156200027f57825182559160200191906001019062000262565b5b509050620002a891905b80821115620002a45760008160009055506001016200028a565b5090565b50506002600360006101000a81548160ff02191690837f0100000000000000000000000000000000000000000000000000000000000000908102040217905550621e84806004553462000000576040516200107638038062001076833981016040528080519060200190919080518201919060200180519060200190919080518201919060200150505b83600560003373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550836004819055508260019080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10620003b557805160ff1916838001178555620003e6565b82800160010185558215620003e6579182015b82811115620003e5578251825591602001919060010190620003c8565b5b5090506200040e91905b808211156200040a576000816000905550600101620003f0565b5090565b50508060029080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106200045e57805160ff19168380011785556200048f565b828001600101855582156200048f579182015b828111156200048e57825182559160200191906001019062000471565b5b509050620004b791905b80821115620004b357600081600090555060010162000499565b5090565b505081600360006101000a81548160ff02191690837f01000000000000000000000000000000000000000000000000000000000000009081020402179055505b505050505b610b6b806200050b6000396000f3606060405236156100a7576000357c01000000000000000000000000000000000000000000000000000000009004806306fdde03146100b9578063095ea7b31461013457806318160ddd1461017057806323b872dd14610193578063313ce567146101d85780635a3b7e42146101fe57806370a082311461027957806395d89b41146102aa578063a9059cbb14610325578063cae9ca511461034b578063dd62ed3e146103ca575b34610000576100b75b610000565b565b005b34610000576100c6610404565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f1680156101265780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b346100005761015860048080359060200190919080359060200190919050506104a2565b60405180821515815260200191505060405180910390f35b346100005761017d610504565b6040518082815260200191505060405180910390f35b34610000576101c0600480803590602001909190803590602001909190803590602001909190505061050a565b60405180821515815260200191505060405180910390f35b34610000576101e561073d565b604051808260ff16815260200191505060405180910390f35b346100005761020b610750565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f16801561026b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b346100005761029460048080359060200190919050506107ee565b6040518082815260200191505060405180910390f35b34610000576102b7610806565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f1680156103175780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b346100005761034960048080359060200190919080359060200190919050506108a4565b005b34610000576103b2600480803590602001909190803590602001909190803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091905050610a13565b60405180821515815260200191505060405180910390f35b34610000576103ee6004808035906020019091908035906020019091905050610b46565b6040518082815260200191505060405180910390f35b60018054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561049a5780601f1061046f5761010080835404028352916020019161049a565b820191906000526020600020905b81548152906001019060200180831161047d57829003601f168201915b505050505081565b600081600660003373ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550600190505b92915050565b60045481565b600081600560008673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561054257610000565b600560008473ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482600560008673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020540110156105a357610000565b600660008573ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482111561060057610000565b81600560008673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555081600560008573ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555081600660008673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a3600190505b9392505050565b600360009054906101000a900460ff1681565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156107e65780601f106107bb576101008083540402835291602001916107e6565b820191906000526020600020905b8154815290600101906020018083116107c957829003601f168201915b505050505081565b60056020528060005260406000206000915090505481565b60028054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561089c5780601f106108715761010080835404028352916020019161089c565b820191906000526020600020905b81548152906001019060200180831161087f57829003601f168201915b505050505081565b80600560003373ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410156108da57610000565b600560008373ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205481600560008573ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205401101561093b57610000565b80600560003373ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600560008473ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a35b5050565b60006000849050610a2485856104a2565b15610b3d578073ffffffffffffffffffffffffffffffffffffffff16638f4ffcb133863087604051857c0100000000000000000000000000000000000000000000000000000000028152600401808573ffffffffffffffffffffffffffffffffffffffff1681526020018481526020018373ffffffffffffffffffffffffffffffffffffffff168152602001806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f168015610b0c5780820380516001836020036101000a031916815260200191505b5095505050505050600060405180830381600087803b156100005760325a03f1156100005750505060019150610b3e565b5b509392505050565b600660205281600052604060002060205280600052604060002060009150915050548156',
nonce: 2,
r: '0x79d4e717d1249512572b90ca87fe545dde24815e33eb74064bdd7336463f6d8',
s: '0x4a16b7d119cdc34e454fa2cc0a152904f7deb23e2a5f2966f70981361c853874',
v: '0x26',
value: '0x0',
}
const beforeBalance = new BN('149123788000000000')
const afterBalance = new BN('129033829000000000')

const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.SpuriousDragon })
common.setHardforkByBlockNumber(2772981)
const vm = new VM({ common })

const addr = Address.fromString('0xd3563d8f19a85c95beab50901fd59ca4de69174c')
const acc = await vm.stateManager.getAccount(addr)
acc.balance = beforeBalance
acc.nonce = new BN(2)
await vm.stateManager.putAccount(addr, acc)

const tx = Transaction.fromTxData(txData, { common })
await vm.runTx({ tx })

const newBalance = (await vm.stateManager.getAccount(addr)).balance
t.ok(newBalance.eq(afterBalance))
t.end()
})
})