diff --git a/CHANGELOG.md b/CHANGELOG.md index e4ed8e6b09..b22203fc59 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,8 +43,12 @@ Ref: https://keepachangelog.com/en/1.0.0/ - (feemarket) [#433](https://github.com/crypto-org-chain/ethermint/pull/433) Fix sdk int conversion panic with baseFee. * (rpc) [#434](https://github.com/crypto-org-chain/ethermint/pull/434) No need gasPrice when patch gasUsed for `eth_getTransactionReceipt`. * (rpc) [#439](https://github.com/crypto-org-chain/ethermint/pull/439), [#441](https://github.com/crypto-org-chain/ethermint/pull/441) Align trace response for failed tx with go-ethereum. + +### Improvements + * (statedb) [#446](https://github.com/crypto-org-chain/ethermint/pull/446) Re-use the cache store implementation with sdk. * (evm) [#447](https://github.com/crypto-org-chain/ethermint/pull/447) Deduct fee through virtual bank transfer. +* (evm) [#448](https://github.com/crypto-org-chain/ethermint/pull/448) Refactor the evm transfer to be more efficient. ### State Machine Breaking diff --git a/go.mod b/go.mod index ced6559479..cd99173099 100644 --- a/go.mod +++ b/go.mod @@ -242,12 +242,12 @@ require ( replace ( // release/v0.50.x - cosmossdk.io/store => github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240402015425-ec314e8e2d07 + cosmossdk.io/store => github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240402064432-829c54275894 // use cosmos keyring github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0 github.com/cockroachdb/pebble => github.com/cockroachdb/pebble v0.0.0-20230209160836-829675f94811 // release/v0.50.x - github.com/cosmos/cosmos-sdk => github.com/crypto-org-chain/cosmos-sdk v0.46.0-beta2.0.20240402015425-ec314e8e2d07 + github.com/cosmos/cosmos-sdk => github.com/crypto-org-chain/cosmos-sdk v0.46.0-beta2.0.20240402064432-829c54275894 github.com/ethereum/go-ethereum => github.com/crypto-org-chain/go-ethereum v1.10.20-0.20231207063621-43cf32d91c3e // Fix upstream GHSA-h395-qcrw-5vmq vulnerability. // TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409 diff --git a/go.sum b/go.sum index 7291af0693..4c5b9a1f4e 100644 --- a/go.sum +++ b/go.sum @@ -413,10 +413,10 @@ github.com/creachadair/tomledit v0.0.24 h1:5Xjr25R2esu1rKCbQEmjZYlrhFkDspoAbAKb6 github.com/creachadair/tomledit v0.0.24/go.mod h1:9qHbShRWQzSCcn617cMzg4eab1vbLCOjOshAWSzWr8U= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/crypto-org-chain/cosmos-sdk v0.46.0-beta2.0.20240402015425-ec314e8e2d07 h1:XVb9zfI9joZzroBIBiiopd1Un4hWf0Wq2/iLfWTpSY8= -github.com/crypto-org-chain/cosmos-sdk v0.46.0-beta2.0.20240402015425-ec314e8e2d07/go.mod h1:nRk8EA8/fEG4zSme2i/Rq5z3k7TrlsHkOYhrY79hhD8= -github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240402015425-ec314e8e2d07 h1:LlaT9o3Fly1MGX4MRGa/UcwAQzl7tewjryyJ2xTV9jg= -github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240402015425-ec314e8e2d07/go.mod h1:lfuLI1f4o+0SGtlHQS4x5qsjRcZZfYqG8bp3k8hM0M8= +github.com/crypto-org-chain/cosmos-sdk v0.46.0-beta2.0.20240402064432-829c54275894 h1:kzSBRO3fTg3Rys51eB0kxspOCECJheUrbDSPTn48lTI= +github.com/crypto-org-chain/cosmos-sdk v0.46.0-beta2.0.20240402064432-829c54275894/go.mod h1:nRk8EA8/fEG4zSme2i/Rq5z3k7TrlsHkOYhrY79hhD8= +github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240402064432-829c54275894 h1:bMirI/jNhEVw5xJrg1+qnc74bR/J9EuJSxfdgGkjhuQ= +github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240402064432-829c54275894/go.mod h1:lfuLI1f4o+0SGtlHQS4x5qsjRcZZfYqG8bp3k8hM0M8= github.com/crypto-org-chain/go-ethereum v1.10.20-0.20231207063621-43cf32d91c3e h1:vnyepPQ/m25+19xcTuBUdRxmltZ/EjVWNqEjhg7Ummk= github.com/crypto-org-chain/go-ethereum v1.10.20-0.20231207063621-43cf32d91c3e/go.mod h1:+a8pUj1tOyJ2RinsNQD4326YS+leSoKGiG/uVVb0x6Y= github.com/danieljoos/wincred v1.2.0 h1:ozqKHaLK0W/ii4KVbbvluM91W2H3Sh0BncbUNPS7jLE= diff --git a/gomod2nix.toml b/gomod2nix.toml index 12956d9fa9..1cdaeb660c 100644 --- a/gomod2nix.toml +++ b/gomod2nix.toml @@ -41,7 +41,7 @@ schema = 3 version = "v1.3.0" hash = "sha256-EEFK43Cr0g0ndhQhkIKher0FqV3mvkmE9z0sP7uVSHg=" [mod."cosmossdk.io/store"] - version = "v0.0.0-20240402015425-ec314e8e2d07" + version = "v0.0.0-20240402064432-829c54275894" hash = "sha256-0yj6ToF5tiOsJ7B3sBS9lLLEDVrKJxi1tNlqRcWauDw=" replaced = "github.com/crypto-org-chain/cosmos-sdk/store" [mod."cosmossdk.io/tools/confix"] @@ -161,8 +161,8 @@ schema = 3 version = "v1.0.0-beta.4" hash = "sha256-5Kn82nsZfiEtuwhhLZqmMxdAY1tX/Fi3HJ0/MEaRohw=" [mod."github.com/cosmos/cosmos-sdk"] - version = "v0.46.0-beta2.0.20240402015425-ec314e8e2d07" - hash = "sha256-kp8xXSle0X8PW4uIHXmfkGHjj3y6KTpcbvW+oJHsLzI=" + version = "v0.46.0-beta2.0.20240402064432-829c54275894" + hash = "sha256-6Kq/hlyQ4D3v6odGK40wliSfzlvz1J7CbbGs+Uma1js=" replaced = "github.com/crypto-org-chain/cosmos-sdk" [mod."github.com/cosmos/go-bip39"] version = "v1.0.0" diff --git a/x/evm/keeper/state_transition.go b/x/evm/keeper/state_transition.go index 6d1d7f0f2e..cba30668c1 100644 --- a/x/evm/keeper/state_transition.go +++ b/x/evm/keeper/state_transition.go @@ -55,7 +55,7 @@ func (k *Keeper) NewEVM( zero := common.BigToHash(big.NewInt(0)) blockCtx := vm.BlockContext{ CanTransfer: core.CanTransfer, - Transfer: core.Transfer, + Transfer: statedb.Transfer, GetHash: k.GetHashFn(ctx), Coinbase: cfg.CoinBase, GasLimit: ethermint.BlockGasLimit(ctx), diff --git a/x/evm/keeper/statedb.go b/x/evm/keeper/statedb.go index ec58cfcb3c..e718de0148 100644 --- a/x/evm/keeper/statedb.go +++ b/x/evm/keeper/statedb.go @@ -72,6 +72,10 @@ func (k *Keeper) ForEachStorage(ctx sdk.Context, addr common.Address, cb func(ke } } +func (k *Keeper) Transfer(ctx sdk.Context, sender, recipient sdk.AccAddress, coins sdk.Coins) error { + return k.bankKeeper.SendCoins(ctx, sender, recipient, coins) +} + func (k *Keeper) AddBalance(ctx sdk.Context, addr sdk.AccAddress, coins sdk.Coins) error { if err := k.bankKeeper.MintCoins(ctx, types.ModuleName, coins); err != nil { return err diff --git a/x/evm/statedb/interfaces.go b/x/evm/statedb/interfaces.go index c483b0bbdc..ff64d083a0 100644 --- a/x/evm/statedb/interfaces.go +++ b/x/evm/statedb/interfaces.go @@ -27,6 +27,7 @@ import ( type Keeper interface { GetParams(sdk.Context) evmtypes.Params + Transfer(ctx sdk.Context, sender, recipient sdk.AccAddress, coins sdk.Coins) error AddBalance(ctx sdk.Context, addr sdk.AccAddress, coins sdk.Coins) error SubBalance(ctx sdk.Context, addr sdk.AccAddress, coins sdk.Coins) error SetBalance(ctx sdk.Context, addr common.Address, amount *big.Int, denom string) error diff --git a/x/evm/statedb/statedb.go b/x/evm/statedb/statedb.go index 9422e7d5b6..b5f3caf027 100644 --- a/x/evm/statedb/statedb.go +++ b/x/evm/statedb/statedb.go @@ -43,6 +43,10 @@ type revision struct { journalIndex int } +func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) { + db.(*StateDB).Transfer(sender, recipient, amount) +} + var _ vm.StateDB = &StateDB{} // StateDB structs within the ethereum protocol are used to store anything @@ -382,11 +386,33 @@ func (s *StateDB) Context() sdk.Context { * SETTERS */ +// Transfer from one account to another +func (s *StateDB) Transfer(sender, recipient common.Address, amount *big.Int) { + if amount.Sign() == 0 { + return + } + if amount.Sign() < 0 { + panic("negative amount") + } + + coins := sdk.NewCoins(sdk.NewCoin(s.evmDenom, sdkmath.NewIntFromBigIntMut(amount))) + senderAddr := sdk.AccAddress(sender.Bytes()) + recipientAddr := sdk.AccAddress(recipient.Bytes()) + if err := s.ExecuteNativeAction(common.Address{}, nil, func(ctx sdk.Context) error { + return s.keeper.Transfer(ctx, senderAddr, recipientAddr, coins) + }); err != nil { + s.err = err + } +} + // AddBalance adds amount to the account associated with addr. func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) { - if amount.Sign() <= 0 { + if amount.Sign() == 0 { return } + if amount.Sign() < 0 { + panic("negative amount") + } coins := sdk.Coins{sdk.NewCoin(s.evmDenom, sdkmath.NewIntFromBigInt(amount))} if err := s.ExecuteNativeAction(common.Address{}, nil, func(ctx sdk.Context) error { return s.keeper.AddBalance(ctx, sdk.AccAddress(addr.Bytes()), coins) @@ -397,9 +423,12 @@ func (s *StateDB) AddBalance(addr common.Address, amount *big.Int) { // SubBalance subtracts amount from the account associated with addr. func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) { - if amount.Sign() <= 0 { + if amount.Sign() == 0 { return } + if amount.Sign() < 0 { + panic("negative amount") + } coins := sdk.Coins{sdk.NewCoin(s.evmDenom, sdkmath.NewIntFromBigInt(amount))} if err := s.ExecuteNativeAction(common.Address{}, nil, func(ctx sdk.Context) error { return s.keeper.SubBalance(ctx, sdk.AccAddress(addr.Bytes()), coins) @@ -408,6 +437,7 @@ func (s *StateDB) SubBalance(addr common.Address, amount *big.Int) { } } +// SetBalance is called by state override func (s *StateDB) SetBalance(addr common.Address, amount *big.Int) { if err := s.ExecuteNativeAction(common.Address{}, nil, func(ctx sdk.Context) error { return s.keeper.SetBalance(ctx, addr, amount, s.evmDenom) diff --git a/x/evm/statedb/statedb_test.go b/x/evm/statedb/statedb_test.go index 3bebbc4195..de2b1d8042 100644 --- a/x/evm/statedb/statedb_test.go +++ b/x/evm/statedb/statedb_test.go @@ -193,6 +193,11 @@ func (suite *StateDBTestSuite) TestBalance() { {"sub zero balance", func(db *statedb.StateDB) { db.SubBalance(address, big.NewInt(0)) }, big.NewInt(0)}, + {"transfer", func(db *statedb.StateDB) { + db.AddBalance(address, big.NewInt(10)) + db.Transfer(address, address2, big.NewInt(10)) + suite.Require().Equal(big.NewInt(10), db.GetBalance(address2)) + }, big.NewInt(0)}, } for _, tc := range testCases {