Skip to content

Commit

Permalink
vm -> debugging: logger improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerd77 committed Feb 10, 2021
1 parent 95546cf commit 26274d8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 22 deletions.
6 changes: 4 additions & 2 deletions packages/vm/lib/evm/evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,9 @@ export default class EVM {
async _reduceSenderBalance(account: Account, message: Message): Promise<void> {
account.balance.isub(message.value)
const result = this._state.putAccount(message.caller, account)
debug(`Reduce sender (${message.caller.toString()}) balance (-> ${account.balance.toString()})`)
debug(
`Reduced sender (${message.caller.toString()}) balance (-> ${account.balance.toString()})`
)
return result
}

Expand All @@ -506,7 +508,7 @@ export default class EVM {
toAccount.balance = newBalance
// putAccount as the nonce may have changed for contract creation
const result = this._state.putAccount(message.to, toAccount)
debug(`Add toAccount (${message.to.toString()}) balance (-> ${toAccount.balance.toString()})`)
debug(`Added toAccount (${message.to.toString()}) balance (-> ${toAccount.balance.toString()})`)
return result
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vm/lib/runBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ async function applyTransactions(this: VM, block: Block, opts: RunBlockOpts) {
let txReceipt
let receiptLog = `Generate tx receipt gasUsed=${gasUsed} bitvector=${short(
abstractTxReceipt.bitvector
)} (${abstractTxReceipt.bitvector.length} bytes) logs=${abstractTxReceipt.logs.length} `
)} (${abstractTxReceipt.bitvector.length} bytes) logs=${abstractTxReceipt.logs.length}`
if (this._common.gteHardfork('byzantium')) {
txReceipt = {
status: txRes.execResult.exceptionError ? 0 : 1, // Receipts have a 0 as status on error
Expand Down
32 changes: 14 additions & 18 deletions packages/vm/lib/runTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async function _runTx(this: VM, opts: RunTxOpts): Promise<RunTxResult> {
throw new Error('base fee exceeds gas limit')
}
gasLimit.isub(basefee)
debugGas(`Subtracting base fee (${basefee.toString()}) from gasLimit (-> ${gasLimit.toString()})`)
debugGas(`Subtracting base fee (${basefee}) from gasLimit (-> ${gasLimit})`)

// Check from account's balance and nonce
let fromAccount = await state.getAccount(caller)
Expand All @@ -138,13 +138,13 @@ async function _runTx(this: VM, opts: RunTxOpts): Promise<RunTxResult> {
const cost = tx.getUpfrontCost()
if (balance.lt(cost)) {
throw new Error(
`sender doesn't have enough funds to send tx. The upfront cost is: ${cost.toString()} and the sender's account only has: ${balance.toString()}`
`sender doesn't have enough funds to send tx. The upfront cost is: ${cost} and the sender's account only has: ${balance}`
)
}
} else if (!opts.skipNonce) {
if (!nonce.eq(tx.nonce)) {
throw new Error(
`the tx doesn't have the correct nonce. account has nonce of: ${nonce.toString()} tx has nonce of: ${tx.nonce.toString()}`
`the tx doesn't have the correct nonce. account has nonce of: ${nonce} tx has nonce of: ${tx.nonce}`
)
}
}
Expand All @@ -155,7 +155,7 @@ async function _runTx(this: VM, opts: RunTxOpts): Promise<RunTxResult> {
fromAccount.balance.isub(txCost)
await state.putAccount(caller, fromAccount)
debug(
`Update fromAccount (caller) nonce (-> ${fromAccount.nonce.toString()}) and balance(-> ${fromAccount.balance.toString()})`
`Update fromAccount (caller) nonce (-> ${fromAccount.nonce}) and balance(-> ${fromAccount.balance})`
)

/*
Expand All @@ -174,9 +174,9 @@ async function _runTx(this: VM, opts: RunTxOpts): Promise<RunTxResult> {
debug(
`Running tx=0x${tx
.hash()
.toString('hex')} with caller=${caller.toString()} gasLimit=${gasLimit.toString()} to=${
.toString('hex')} with caller=${caller.toString()} gasLimit=${gasLimit} to=${
to ? to.toString() : ''
} value=${value.toString()} data=0x${data.toString('hex')}`
} value=${value} data=0x${data.toString('hex')}`
)
const results = (await evm.executeMessage(message)) as RunTxResult
debug('-'.repeat(100))
Expand All @@ -185,20 +185,20 @@ async function _runTx(this: VM, opts: RunTxOpts): Promise<RunTxResult> {
results.gasUsed
} exceptionError=${
results.execResult.exceptionError ? results.execResult.exceptionError.error : ''
} returnValue=${short(
results.execResult.returnValue
)} gasRefund=${results.execResult.gasRefund?.toString()} ]`
} returnValue=${short(results.execResult.returnValue)} gasRefund=${
results.execResult.gasRefund
} ]`
)

/*
* Parse results
*/
// Generate the bloom for the tx
results.bloom = txLogsBloom(results.execResult.logs)
debug(`Generate tx bloom`)
debug(`Generated tx bloom with logs=${results.execResult.logs?.length}`)
// Caculate the total gas used
results.gasUsed.iadd(basefee)
debugGas(`tx add baseFee ${basefee.toString()} to gasUsed (-> ${results.gasUsed.toString()})`)
debugGas(`tx add baseFee ${basefee} to gasUsed (-> ${results.gasUsed})`)
// Process any gas refund
// TODO: determine why the gasRefund from execResult is not used here directly
let gasRefund = evm._refund
Expand All @@ -207,9 +207,7 @@ async function _runTx(this: VM, opts: RunTxOpts): Promise<RunTxResult> {
gasRefund = results.gasUsed.divn(2)
}
results.gasUsed.isub(gasRefund)
debug(
`Subtract tx gasRefund (${gasRefund.toString()}) from gasUsed (-> ${results.gasUsed.toString()})`
)
debug(`Subtract tx gasRefund (${gasRefund}) from gasUsed (-> ${results.gasUsed})`)
} else {
debug(`No tx gasRefund`)
}
Expand All @@ -222,7 +220,7 @@ async function _runTx(this: VM, opts: RunTxOpts): Promise<RunTxResult> {
fromAccount.balance.iadd(txCostDiff)
await state.putAccount(caller, fromAccount)
debug(
`Refund txCostDiff (${txCostDiff.toString()}) to fromAccount (caller) balance (-> ${fromAccount.balance.toString()})`
`Refunded txCostDiff (${txCostDiff}) to fromAccount (caller) balance (-> ${fromAccount.balance})`
)

// Update miner's balance
Expand All @@ -235,9 +233,7 @@ async function _runTx(this: VM, opts: RunTxOpts): Promise<RunTxResult> {
// the state.putAccount function puts this into the "touched" accounts. This will thus be removed when
// we clean the touched accounts below in case we are in a fork >= SpuriousDragon
await state.putAccount(miner, minerAccount)
debug(
`tx update miner account (${miner.toString()}) balance (-> ${minerAccount.balance.toString()})`
)
debug(`tx update miner account (${miner.toString()}) balance (-> ${minerAccount.balance})`)

/*
* Cleanup accounts
Expand Down
6 changes: 5 additions & 1 deletion packages/vm/lib/state/stateManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ export default class DefaultStateManager implements StateManager {
* @param account - The account to store
*/
async putAccount(address: Address, account: Account): Promise<void> {
debug(`Save account ${address}`)
debug(
`Save account address=${address} nonce=${account.nonce} balance=${account.balance} contract=${
account.isContract() ? 'yes' : 'no'
} empty=${account.isEmpty() ? 'yes' : 'no'}`
)
this._cache.put(address, account)
this.touchAccount(address)
}
Expand Down

0 comments on commit 26274d8

Please sign in to comment.