Skip to content

Commit 6370b96

Browse files
committed
fix(evm): Pass block timestamp to MakeSigner and refactor MsgEthereumTx field usage
- Update all calls to `gethcore.MakeSigner` to include the block time as a Unix timestamp (seconds), using `evm.ParseBlockTimeUnixU64(ctx)`. - Add `ParseBlockTimeUnixU64` utility to extract block time from `sdk.Context` in a safe, reusable way. - Refactor usage of MsgEthereumTx field getters to use direct struct fields (`From`, `To`, `Value`, `GasFeeCap`, etc.), improving efficiency and clarity. - Enhance `ParseWeiAsMultipleOfMicronibi` to return `uint256.Int` and handle nil, zero, negative, and overflow edge cases with clear error messages.
1 parent 1002634 commit 6370b96

File tree

5 files changed

+66
-27
lines changed

5 files changed

+66
-27
lines changed

app/evmante/evmante_can_transfer.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ func (ctd CanTransferDecorator) AnteHandle(
2525
ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler,
2626
) (sdk.Context, error) {
2727
ethCfg := evm.EthereumConfig(ctd.EVMKeeper.EthChainID(ctx))
28-
signer := gethcore.MakeSigner(ethCfg, big.NewInt(ctx.BlockHeight()))
28+
signer := gethcore.MakeSigner(
29+
ethCfg,
30+
big.NewInt(ctx.BlockHeight()),
31+
evm.ParseBlockTimeUnixU64(ctx),
32+
)
2933

3034
for _, msg := range tx.GetMsgs() {
3135
msgEthTx, ok := msg.(*evm.MsgEthereumTx)
@@ -57,25 +61,25 @@ func (ctd CanTransferDecorator) AnteHandle(
5761
return ctx, errors.Wrapf(
5862
sdkerrors.ErrInsufficientFee,
5963
"gas fee cap (wei) less than block base fee (wei); (%s < %s)",
60-
evmMsg.GasFeeCap(), baseFeeWeiPerGas,
64+
evmMsg.GasFeeCap, baseFeeWeiPerGas,
6165
)
6266
}
6367

6468
// check that caller has enough balance to cover asset transfer for **topmost** call
6569
// NOTE: here the gas consumed is from the context with the infinite gas meter
6670

67-
if evmMsg.Value().Sign() > 0 {
68-
nibiruAddr := eth.EthAddrToNibiruAddr(evmMsg.From())
71+
if evmMsg.Value.Sign() > 0 {
72+
nibiruAddr := eth.EthAddrToNibiruAddr(evmMsg.From)
6973
balanceNative := ctd.Bank.GetBalance(ctx, nibiruAddr, evm.EVMBankDenom).Amount.BigInt()
7074
balanceWei := evm.NativeToWei(balanceNative)
7175

72-
if balanceWei.Cmp(evmMsg.Value()) < 0 {
76+
if balanceWei.Cmp(evmMsg.Value) < 0 {
7377
return ctx, errors.Wrapf(
7478
sdkerrors.ErrInsufficientFunds,
7579
"failed to transfer %s wei ( balance=%s )from address %s using the EVM block context transfer function",
76-
evmMsg.Value(),
80+
evmMsg.Value,
7781
balanceWei,
78-
evmMsg.From(),
82+
evmMsg.From,
7983
)
8084
}
8185
}

app/evmante/evmante_sigverify.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ func (esvd EthSigVerificationDecorator) AnteHandle(
3636
chainID := esvd.evmKeeper.EthChainID(ctx)
3737
ethCfg := evm.EthereumConfig(chainID)
3838
blockNum := big.NewInt(ctx.BlockHeight())
39-
signer := gethcore.MakeSigner(ethCfg, blockNum)
39+
signer := gethcore.MakeSigner(
40+
ethCfg,
41+
blockNum,
42+
evm.ParseBlockTimeUnixU64(ctx),
43+
)
4044

4145
for _, msg := range tx.GetMsgs() {
4246
msgEthTx, ok := msg.(*evm.MsgEthereumTx)

x/evm/const.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,13 @@ func ParseWeiAsMultipleOfMicronibi(weiInt *big.Int) (newWeiInt *big.Int, err err
153153
newWeiInt = NativeToWei(WeiToNative(weiInt))
154154
return newWeiInt, nil
155155
}
156+
157+
// Parses the block time as a Unix timestamp in seconds for use with
158+
// "gethcore.MakeSigner".
159+
func ParseBlockTimeUnixU64(ctx sdk.Context) uint64 {
160+
blockTimeI64 := ctx.BlockTime().Unix()
161+
if blockTimeI64 <= 0 {
162+
return 0
163+
}
164+
return uint64(blockTimeI64)
165+
}

x/evm/keeper/grpc_query.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,11 @@ func (k Keeper) TraceTx(
495495
evmCfg.BaseFeeWei = baseFeeWeiPerGas
496496
}
497497

498-
signer := gethcore.MakeSigner(evmCfg.ChainConfig, big.NewInt(ctx.BlockHeight()))
498+
signer := gethcore.MakeSigner(
499+
evmCfg.ChainConfig,
500+
big.NewInt(ctx.BlockHeight()),
501+
evm.ParseBlockTimeUnixU64(ctx),
502+
)
499503
txConfig := statedb.NewEmptyTxConfig(gethcommon.BytesToHash(ctx.HeaderHash().Bytes()))
500504

501505
// gas used at this point corresponds to GetProposerAddress & CalculateBaseFee
@@ -679,7 +683,11 @@ func (k Keeper) TraceBlock(
679683
tracerConfig, _ = json.Marshal(req.TraceConfig.TracerConfig)
680684
}
681685

682-
signer := gethcore.MakeSigner(evmCfg.ChainConfig, big.NewInt(ctx.BlockHeight()))
686+
signer := gethcore.MakeSigner(
687+
evmCfg.ChainConfig,
688+
big.NewInt(ctx.BlockHeight()),
689+
evm.ParseBlockTimeUnixU64(ctx),
690+
)
683691
txsLength := len(req.Txs)
684692
results := make([]*evm.TxTraceResult, 0, txsLength)
685693

x/evm/keeper/msg_server.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/ethereum/go-ethereum/core/vm"
1919
"github.com/ethereum/go-ethereum/crypto"
2020
"github.com/ethereum/go-ethereum/params"
21+
"github.com/holiman/uint256"
2122

2223
"github.com/NibiruChain/nibiru/v2/eth"
2324
"github.com/NibiruChain/nibiru/v2/x/evm"
@@ -301,42 +302,42 @@ func (k *Keeper) ApplyEvmMsg(
301302
// needed when `ApplyMessage` is called under contexts where ante handlers
302303
// are not run, for example `eth_call` and `eth_estimateGas`.
303304
evmObj.StateDB.PrepareAccessList(
304-
msg.From(),
305-
msg.To(),
305+
msg.From,
306+
msg.To,
306307
evmObj.ActivePrecompiles(params.Rules{}),
307308
msg.AccessList(),
308309
)
309310

310-
msgWei, err := ParseWeiAsMultipleOfMicronibi(msg.Value())
311+
msgWei, err := ParseWeiAsMultipleOfMicronibi(msg.Value)
311312
if err != nil {
312-
return nil, errors.Wrapf(err, "ApplyEvmMsg: invalid wei amount %s", msg.Value())
313+
return nil, errors.Wrapf(err, "ApplyEvmMsg: invalid wei amount %s", msg.Value)
313314
}
314315

315316
// take over the nonce management from evm:
316317
// - reset sender's nonce to msg.Nonce() before calling evm.
317318
// - increase sender's nonce by one no matter the result.
318-
evmObj.StateDB.SetNonce(msg.From(), msg.Nonce())
319+
evmObj.StateDB.SetNonce(msg.From, msg.Nonce)
319320

320321
var returnBz []byte
321322
var vmErr error
322323
if contractCreation {
323324
returnBz, _, gasRemaining, vmErr = evmObj.Create(
324-
vm.AccountRef(msg.From()),
325-
msg.Data(),
325+
vm.AccountRef(msg.From),
326+
msg.Data,
326327
gasRemaining,
327328
msgWei,
328329
)
329330
} else {
330331
returnBz, gasRemaining, vmErr = evmObj.Call(
331-
vm.AccountRef(msg.From()),
332-
*msg.To(),
333-
msg.Data(),
332+
vm.AccountRef(msg.From),
333+
*msg.To,
334+
msg.Data,
334335
gasRemaining,
335336
msgWei,
336337
)
337338
}
338339
// Increment nonce after processing the message
339-
evmObj.StateDB.SetNonce(msg.From(), msg.Nonce()+1)
340+
evmObj.StateDB.SetNonce(msg.From, msg.Nonce+1)
340341

341342
// EVM execution error needs to be available for the JSON-RPC client
342343
var vmError string
@@ -374,21 +375,33 @@ func (k *Keeper) ApplyEvmMsg(
374375
return evmResp, nil
375376
}
376377

377-
func ParseWeiAsMultipleOfMicronibi(weiInt *big.Int) (newWeiInt *big.Int, err error) {
378+
func ParseWeiAsMultipleOfMicronibi(weiInt *big.Int) (
379+
newWeiInt *uint256.Int, err error,
380+
) {
378381
// if "weiValue" is nil, 0, or negative, early return
379-
if weiInt == nil || !(weiInt.Cmp(big.NewInt(0)) > 0) {
380-
return weiInt, nil
382+
cmpSign := weiInt.Cmp(big.NewInt(0))
383+
if weiInt == nil {
384+
return (*uint256.Int)(nil), nil
385+
} else if cmpSign == 0 {
386+
return uint256.NewInt(0), nil
387+
} else if cmpSign < 0 {
388+
return newWeiInt, fmt.Errorf("Wei parsing error: negative wei value cannot be a uint256 (%s)", weiInt)
381389
}
382390

383391
// err if weiInt is too small
384392
tenPow12 := new(big.Int).Exp(big.NewInt(10), big.NewInt(12), nil)
385393
if weiInt.Cmp(tenPow12) < 0 {
386-
return weiInt, fmt.Errorf(
387-
"wei amount is too small (%s), cannot transfer less than 1 micronibi. 1 NIBI == 10^6 micronibi == 10^18 wei", weiInt)
394+
return newWeiInt, fmt.Errorf(
395+
"Wei parsing error: wei amount is too small (%s), cannot transfer less than 1 micronibi. 1 NIBI == 10^6 micronibi == 10^18 wei", weiInt)
388396
}
389397

390398
// truncate to highest micronibi amount
391-
newWeiInt = evm.NativeToWei(evm.WeiToNative(weiInt))
399+
newWeiInt, overflowed := uint256.FromBig(
400+
evm.NativeToWei(evm.WeiToNative(weiInt)),
401+
)
402+
if overflowed {
403+
return newWeiInt, fmt.Errorf("Wei parsing error: overflow occured in conversion from big.Int to uint256.Int for wei value %s", weiInt)
404+
}
392405
return newWeiInt, nil
393406
}
394407

0 commit comments

Comments
 (0)