Skip to content

Commit

Permalink
v1.13.11
Browse files Browse the repository at this point in the history
  • Loading branch information
mmsqe committed Sep 24, 2024
1 parent 9a27dd8 commit 7ae9ab6
Show file tree
Hide file tree
Showing 12 changed files with 97 additions and 84 deletions.
8 changes: 5 additions & 3 deletions app/ante/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params"
"github.com/holiman/uint256"
)

type AccountGetter func(sdk.AccAddress) sdk.AccountI
Expand Down Expand Up @@ -244,10 +245,11 @@ func CheckEthCanTransfer(
if value == nil || value.Sign() == -1 {
return fmt.Errorf("value (%s) must be positive", value)
}
v, _ := uint256.FromBig(value)
from := common.BytesToAddress(msgEthTx.From)
// check that caller has enough balance to cover asset transfer for **topmost** call
// NOTE: here the gas consumed is from the context with the infinite gas meter
if value.Sign() > 0 && !canTransfer(ctx, evmKeeper, evmParams.EvmDenom, from, value) {
if value.Sign() > 0 && !canTransfer(ctx, evmKeeper, evmParams.EvmDenom, from, v) {
return errorsmod.Wrapf(
errortypes.ErrInsufficientFunds,
"failed to transfer %s from address %s using the EVM block context transfer function",
Expand All @@ -261,9 +263,9 @@ func CheckEthCanTransfer(
}

// canTransfer adapted the core.CanTransfer from go-ethereum
func canTransfer(ctx sdk.Context, evmKeeper EVMKeeper, denom string, from common.Address, amount *big.Int) bool {
func canTransfer(ctx sdk.Context, evmKeeper EVMKeeper, denom string, from common.Address, amount *uint256.Int) bool {
balance := evmKeeper.GetBalance(ctx, sdk.AccAddress(from.Bytes()), denom)
return balance.Cmp(amount) >= 0
return balance.Cmp(amount.ToBig()) >= 0
}

// CheckAndSetEthSenderNonce handles incrementing the sequence of the signer (i.e sender). If the transaction is a
Expand Down
20 changes: 11 additions & 9 deletions app/ante/eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
ethermint "github.com/evmos/ethermint/types"
"github.com/evmos/ethermint/x/evm/statedb"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/holiman/uint256"
)

func (suite *AnteTestSuite) TestNewEthAccountVerificationDecorator() {
Expand Down Expand Up @@ -67,7 +68,7 @@ func (suite *AnteTestSuite) TestNewEthAccountVerificationDecorator() {
"success new account",
tx,
func() {
vmdb.AddBalance(addr, big.NewInt(1000000))
vmdb.AddBalance(addr, uint256.NewInt(1000000))
},
true,
true,
Expand All @@ -79,7 +80,7 @@ func (suite *AnteTestSuite) TestNewEthAccountVerificationDecorator() {
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr.Bytes())
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

vmdb.AddBalance(addr, big.NewInt(1000000))
vmdb.AddBalance(addr, uint256.NewInt(1000000))
},
true,
true,
Expand Down Expand Up @@ -263,7 +264,7 @@ func (suite *AnteTestSuite) TestEthGasConsumeDecorator() {
tx2,
0,
func() {
vmdb.AddBalance(addr, big.NewInt(1000000))
vmdb.AddBalance(addr, uint256.NewInt(1000000))
},
false, true,
0,
Expand All @@ -274,7 +275,7 @@ func (suite *AnteTestSuite) TestEthGasConsumeDecorator() {
tx2,
0,
func() {
vmdb.AddBalance(addr, big.NewInt(1000000))
vmdb.AddBalance(addr, uint256.NewInt(1000000))
suite.ctx = suite.ctx.WithBlockGasMeter(storetypes.NewGasMeter(1))
},
false, true,
Expand All @@ -290,7 +291,8 @@ func (suite *AnteTestSuite) TestEthGasConsumeDecorator() {
func() {
limit := new(big.Int).SetUint64(math.MaxUint64)
balance := new(big.Int).Mul(limit, gasPrice)
vmdb.AddBalance(addr, balance)
b, _ := uint256.FromBig(balance)
vmdb.AddBalance(addr, b)
},
false, false,
0,
Expand All @@ -301,7 +303,7 @@ func (suite *AnteTestSuite) TestEthGasConsumeDecorator() {
tx2,
tx2GasLimit, // it's capped
func() {
vmdb.AddBalance(addr, big.NewInt(1001000000000000))
vmdb.AddBalance(addr, uint256.NewInt(1001000000000000))
suite.ctx = suite.ctx.WithBlockGasMeter(storetypes.NewGasMeter(10000000000000000000))
},
true, false,
Expand All @@ -313,7 +315,7 @@ func (suite *AnteTestSuite) TestEthGasConsumeDecorator() {
dynamicFeeTx,
tx2GasLimit, // it's capped
func() {
vmdb.AddBalance(addr, big.NewInt(1001000000000000))
vmdb.AddBalance(addr, uint256.NewInt(1001000000000000))
suite.ctx = suite.ctx.WithBlockGasMeter(storetypes.NewGasMeter(10000000000000000000))
},
true, false,
Expand All @@ -325,7 +327,7 @@ func (suite *AnteTestSuite) TestEthGasConsumeDecorator() {
dynamicFeeTx,
tx2GasLimit, // it's capped
func() {
vmdb.AddBalance(addr, big.NewInt(1001000000000000))
vmdb.AddBalance(addr, uint256.NewInt(1001000000000000))
suite.ctx = suite.ctx.WithIsReCheckTx(true)
},
true, false,
Expand Down Expand Up @@ -448,7 +450,7 @@ func (suite *AnteTestSuite) TestCanTransferDecorator() {
acc := suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr.Bytes())
suite.app.AccountKeeper.SetAccount(suite.ctx, acc)

vmdb.AddBalance(addr, big.NewInt(1000000))
vmdb.AddBalance(addr, uint256.NewInt(1000000))
},
true,
},
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ require (
github.com/gorilla/websocket v1.5.1
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/hashicorp/go-metrics v0.5.3
github.com/holiman/uint256 v1.2.4
github.com/improbable-eng/grpc-web v0.15.0
github.com/onsi/ginkgo/v2 v2.7.0
github.com/onsi/gomega v1.26.0
Expand Down Expand Up @@ -165,7 +166,6 @@ require (
github.com/hashicorp/yamux v0.1.1 // indirect
github.com/hdevalence/ed25519consensus v0.2.0 // indirect
github.com/holiman/bloomfilter/v2 v2.0.3 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/huandu/skiplist v1.2.0 // indirect
github.com/huin/goupnp v1.3.0 // indirect
github.com/iancoleman/strcase v0.3.0 // indirect
Expand Down Expand Up @@ -268,8 +268,8 @@ replace (
github.com/tidwall/btree => github.com/crypto-org-chain/btree v0.0.0-20240406140148-2687063b042c
)

// v1.13.10
// v1.13.15
replace (
github.com/cockroachdb/pebble => github.com/cockroachdb/pebble v0.0.0-20230928194634-aa077af62593
github.com/ethereum/go-ethereum => github.com/mmsqe/go-ethereum v1.10.24-0.20240924081659-242ce2c70b45
github.com/ethereum/go-ethereum => github.com/mmsqe/go-ethereum v1.10.24-0.20240924082006-22e7cf66a86c
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -908,8 +908,8 @@ github.com/mitchellh/pointerstructure v1.2.0/go.mod h1:BRAsLI5zgXmw97Lf6s25bs8oh
github.com/mmcloughlin/addchain v0.4.0 h1:SobOdjm2xLj1KkXN5/n0xTIWyZA2+s99UCY1iPfkHRY=
github.com/mmcloughlin/addchain v0.4.0/go.mod h1:A86O+tHqZLMNO4w6ZZ4FlVQEadcoqkyU72HC5wJ4RlU=
github.com/mmcloughlin/profile v0.1.1/go.mod h1:IhHD7q1ooxgwTgjxQYkACGA77oFTDdFVejUS1/tS/qU=
github.com/mmsqe/go-ethereum v1.10.24-0.20240924081659-242ce2c70b45 h1:b8Vf/4/Ze9pgqvPmccXfsqczIROcUfPT1+4NDd+QiAo=
github.com/mmsqe/go-ethereum v1.10.24-0.20240924081659-242ce2c70b45/go.mod h1:9uIchBJxZTJLXXdZQiVUmmHqckFa6V3T8lJJcTgcdJE=
github.com/mmsqe/go-ethereum v1.10.24-0.20240924082006-22e7cf66a86c h1:z+eYgqh5ZsOx8kgbAEnPqTnJR5DEXiij/YA8c9oXh9c=
github.com/mmsqe/go-ethereum v1.10.24-0.20240924082006-22e7cf66a86c/go.mod h1:8ivw+R75VH4GtpKg+qDrx6Djs6octb9olzaibicEXic=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
Expand Down
4 changes: 2 additions & 2 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ schema = 3
version = "v0.4.0"
hash = "sha256-Ol5bznli6Dh5uXi8GUGsjZo8DzCdF0hqsUMwDNUpxP0="
[mod."github.com/ethereum/go-ethereum"]
version = "v1.10.24-0.20240924081659-242ce2c70b45"
hash = "sha256-X0RAB6r1w+ArrJmk9PvwXyPN4AQNtvaPaM8mS77FOxg="
version = "v1.10.24-0.20240924082006-22e7cf66a86c"
hash = "sha256-EnKPDk/f1xQdqttVtYQb4Q9KO7sXX1k2mBHDbat621o="
replaced = "github.com/mmsqe/go-ethereum"
[mod."github.com/fatih/color"]
version = "v1.16.0"
Expand Down
4 changes: 2 additions & 2 deletions x/evm/genesis_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package evm_test

import (
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -13,6 +12,7 @@ import (
"github.com/evmos/ethermint/x/evm"
"github.com/evmos/ethermint/x/evm/statedb"
"github.com/evmos/ethermint/x/evm/types"
"github.com/holiman/uint256"
"github.com/stretchr/testify/suite"
)

Expand Down Expand Up @@ -47,7 +47,7 @@ func (suite *GenesisTestSuite) TestInitGenesis() {
{
"valid account",
func() {
vmdb.AddBalance(address, big.NewInt(1))
vmdb.AddBalance(address, uint256.NewInt(1))
},
&types.GenesisState{
Params: types.DefaultParams(),
Expand Down
7 changes: 4 additions & 3 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/params"
"github.com/holiman/uint256"
)

// NewEVM generates a go-ethereum VM from the provided Message fields and the chain parameters
Expand Down Expand Up @@ -383,16 +384,16 @@ func (k *Keeper) ApplyMessageWithConfig(
// - prepare accessList(post-berlin)
// - reset transient storage(eip 1153)
stateDB.Prepare(rules, msg.From, cfg.CoinBase, msg.To, vm.DefaultActivePrecompiles(rules), msg.AccessList)

v, _ := uint256.FromBig(msg.Value)
if contractCreation {
// take over the nonce management from evm:
// - reset sender's nonce to msg.Nonce() before calling evm.
// - increase sender's nonce by one no matter the result.
stateDB.SetNonce(sender.Address(), msg.Nonce)
ret, _, leftoverGas, vmErr = evm.Create(sender, msg.Data, leftoverGas, msg.Value)
ret, _, leftoverGas, vmErr = evm.Create(sender, msg.Data, leftoverGas, v)
stateDB.SetNonce(sender.Address(), msg.Nonce+1)
} else {
ret, leftoverGas, vmErr = evm.Call(sender, *msg.To, msg.Data, leftoverGas, msg.Value)
ret, leftoverGas, vmErr = evm.Call(sender, *msg.To, msg.Data, leftoverGas, v)
}

refundQuotient := params.RefundQuotient
Expand Down
6 changes: 3 additions & 3 deletions x/evm/keeper/statedb_benchmark_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package keeper_test

import (
"math/big"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -12,6 +11,7 @@ import (

"github.com/evmos/ethermint/tests"
"github.com/evmos/ethermint/testutil"
"github.com/holiman/uint256"
)

type StatedbBenchmarkTestSuite struct {
Expand Down Expand Up @@ -52,7 +52,7 @@ func BenchmarkAddBalance(b *testing.B) {
suite.SetupTest(b)
vmdb := suite.StateDB()

amt := big.NewInt(10)
amt := uint256.NewInt(10)

b.ResetTimer()
b.ReportAllocs()
Expand Down Expand Up @@ -144,7 +144,7 @@ func BenchmarkSubBalance(b *testing.B) {
suite.SetupTest(b)
vmdb := suite.StateDB()

amt := big.NewInt(10)
amt := uint256.NewInt(10)

b.ResetTimer()
b.ReportAllocs()
Expand Down
33 changes: 17 additions & 16 deletions x/evm/keeper/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/evmos/ethermint/testutil"
"github.com/evmos/ethermint/x/evm/statedb"
"github.com/evmos/ethermint/x/evm/types"
"github.com/holiman/uint256"
)

type StateDBTestSuite struct {
Expand Down Expand Up @@ -56,11 +57,11 @@ func (suite *StateDBTestSuite) TestCreateAccount() {
"reset account (keep balance)",
suite.Address,
func(vmdb vm.StateDB, addr common.Address) {
vmdb.AddBalance(addr, big.NewInt(100))
suite.Require().NotZero(vmdb.GetBalance(addr).Int64())
vmdb.AddBalance(addr, uint256.NewInt(100))
suite.Require().NotZero(vmdb.GetBalance(addr).ToBig().Int64())
},
func(vmdb vm.StateDB, addr common.Address) {
suite.Require().Equal(vmdb.GetBalance(addr).Int64(), int64(100))
suite.Require().Equal(vmdb.GetBalance(addr).ToBig().Int64(), int64(100))
},
},
{
Expand Down Expand Up @@ -88,17 +89,17 @@ func (suite *StateDBTestSuite) TestCreateAccount() {
func (suite *StateDBTestSuite) TestAddBalance() {
testCases := []struct {
name string
amount *big.Int
amount *uint256.Int
isNoOp bool
}{
{
"positive amount",
big.NewInt(100),
uint256.NewInt(100),
false,
},
{
"zero amount",
big.NewInt(0),
uint256.NewInt(0),
true,
},
}
Expand All @@ -111,9 +112,9 @@ func (suite *StateDBTestSuite) TestAddBalance() {
post := vmdb.GetBalance(suite.Address)

if tc.isNoOp {
suite.Require().Equal(prev.Int64(), post.Int64())
suite.Require().Equal(prev.ToBig().Int64(), post.ToBig().Int64())
} else {
suite.Require().Equal(new(big.Int).Add(prev, tc.amount).Int64(), post.Int64())
suite.Require().Equal(new(big.Int).Add(prev.ToBig(), tc.amount.ToBig()).Int64(), post.ToBig().Int64())
}
})
}
Expand All @@ -122,27 +123,27 @@ func (suite *StateDBTestSuite) TestAddBalance() {
func (suite *StateDBTestSuite) TestSubBalance() {
testCases := []struct {
name string
amount *big.Int
amount *uint256.Int
malleate func(vm.StateDB)
isNoOp bool
}{
{
"positive amount, below zero",
big.NewInt(100),
uint256.NewInt(100),
func(vm.StateDB) {},
true,
},
{
"positive amount, above zero",
big.NewInt(50),
uint256.NewInt(50),
func(vmdb vm.StateDB) {
vmdb.AddBalance(suite.Address, big.NewInt(100))
vmdb.AddBalance(suite.Address, uint256.NewInt(100))
},
false,
},
{
"zero amount",
big.NewInt(0),
uint256.NewInt(0),
func(vm.StateDB) {},
true,
},
Expand All @@ -158,9 +159,9 @@ func (suite *StateDBTestSuite) TestSubBalance() {
post := vmdb.GetBalance(suite.Address)

if tc.isNoOp {
suite.Require().Equal(prev.Int64(), post.Int64())
suite.Require().Equal(prev.ToBig().Int64(), post.ToBig().Int64())
} else {
suite.Require().Equal(new(big.Int).Sub(prev, tc.amount).Int64(), post.Int64())
suite.Require().Equal(new(big.Int).Sub(prev.ToBig(), tc.amount.ToBig()).Int64(), post.ToBig().Int64())
}
})
}
Expand Down Expand Up @@ -545,7 +546,7 @@ func (suite *StateDBTestSuite) TestEmpty() {
{
"not empty, positive balance",
suite.Address,
func(vmdb vm.StateDB) { vmdb.AddBalance(suite.Address, big.NewInt(100)) },
func(vmdb vm.StateDB) { vmdb.AddBalance(suite.Address, uint256.NewInt(100)) },
false,
},
{"empty, account doesn't exist", tests.GenerateAddress(), func(vm.StateDB) {}, true},
Expand Down
Loading

0 comments on commit 7ae9ab6

Please sign in to comment.