Skip to content

feat: post tx hooks #54

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

Merged
merged 10 commits into from
May 6, 2025
Merged

feat: post tx hooks #54

merged 10 commits into from
May 6, 2025

Conversation

vladjdk
Copy link
Member

@vladjdk vladjdk commented Apr 4, 2025

Reopening the PR #2 from @Reecepbcups

Closes #18

@vladjdk vladjdk requested a review from a team as a code owner April 4, 2025 16:12
@vladjdk vladjdk changed the title Reece/post tx hooks feat: post tx hooks Apr 4, 2025
@vladjdk vladjdk requested review from Copilot and removed request for a team April 11, 2025 22:33
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (2)

x/vm/keeper/state_transition.go:212

  • The variable 'txConfig' is referenced but not defined in the provided context. Ensure that 'txConfig' is properly declared or passed in.
TxHash:            txConfig.TxHash,

x/vm/keeper/state_transition.go:231

  • The variable 'commit' is used without being defined in the diff. Please ensure that 'commit' is correctly declared or initialized.
else if commit != nil {

Copy link
Contributor

@Eric-Warehime Eric-Warehime left a comment

Choose a reason for hiding this comment

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

Looks good, but I think we should add tests around some of the success and failure conditions here, especially since this is modifying the state transition function.

Comment on lines +200 to +203
cumulativeGasUsed += ctx.BlockGasMeter().GasConsumed()
if cumulativeGasUsed > limit {
cumulativeGasUsed = limit
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Not too familiar with this code path at this point, but is this the expected behavior? Possibly we should be stopping execution if we've gone over the gas limit and returning a out of gas error?

Copy link
Member Author

@vladjdk vladjdk May 5, 2025

Choose a reason for hiding this comment

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

I viewed some historical Ethermint updates, and it seems like this value from the receipt is only used with external tooling. I still haven't found a concrete example of when something like this could happen, but if you look at

// calculate a minimum amount of gas to be charged to sender if GasLimit
// is considerably higher than GasUsed to stay more aligned with Tendermint gas mechanics
// for more info https://github.com/evmos/ethermint/issues/1085
gasLimit := math.LegacyNewDec(int64(msg.Gas())) //#nosec G115 -- int overflow is not a concern here -- msg gas is not exceeding int64 max value
minGasMultiplier := k.GetMinGasMultiplier(ctx)
minimumGasUsed := gasLimit.Mul(minGasMultiplier)
if !minimumGasUsed.TruncateInt().IsUint64() {
return nil, errorsmod.Wrapf(types.ErrGasOverflow, "minimumGasUsed(%s) is not a uint64", minimumGasUsed.TruncateInt().String())
}
if msg.Gas() < leftoverGas {
return nil, errorsmod.Wrapf(types.ErrGasOverflow, "message gas limit < leftover gas (%d < %d)", msg.Gas(), leftoverGas)
}
gasUsed := math.LegacyMaxDec(minimumGasUsed, math.LegacyNewDec(int64(temporaryGasUsed))).TruncateInt().Uint64() //#nosec G115 -- int overflow is not a concern here
// reset leftoverGas, to be used by the tracer
leftoverGas = msg.Gas() - gasUsed
return &types.MsgEthereumTxResponse{
GasUsed: gasUsed,
VmError: vmError,
, the minimumGasUsed could be higher than the actual gas used, which may cause the cumulativeGasUsed + BlockGasMeter to exceed the limit.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a test where this case would be hit. This could probably happen in the real world where someone's transaction is at the end of the FIFO list and the gas limit exceeds the rest of the Cosmos block gas limit, but the consumed gas still remains under it. In this case, the EVM would refund the gas.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe a separate question, but since we're refunding any gas not consumed via res.GasUsed, does that mean the post tx hooks are essentially free to execute?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, added a comment above them to clarify this


if err = k.PostTxProcessing(tmpCtx, signerAddr, msg, receipt); err != nil {
// If hooks returns an error, revert the whole tx.
res.VmError = fmt.Sprintf("failed to execute post transaction processing: %s", err)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: can we use errorsmod.Wrap here?

Comment on lines +200 to +203
cumulativeGasUsed += ctx.BlockGasMeter().GasConsumed()
if cumulativeGasUsed > limit {
cumulativeGasUsed = limit
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe a separate question, but since we're refunding any gas not consumed via res.GasUsed, does that mean the post tx hooks are essentially free to execute?

Copy link
Contributor

@Eric-Warehime Eric-Warehime left a comment

Choose a reason for hiding this comment

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

LGTM. Let's add some comment around gas usage not being accounted in the post hooks and use errorsmod if we can.

@vladjdk
Copy link
Member Author

vladjdk commented May 6, 2025

Thanks. Added a comment that the hooks won't charge for any gas and can be thought of like EndBlockers for individual EVM transactions in that sense. Fixed the error as well.

@vladjdk vladjdk merged commit 0bc9f45 into main May 6, 2025
17 checks passed
@vladjdk vladjdk deleted the reece/post-tx-hooks branch May 6, 2025 19:39
GNaD13 pushed a commit to oraichain/evm that referenced this pull request Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature]: add hooks after an EVM Tx is processed but before it is commited
3 participants