Skip to content
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

Problem: evm transfer is not efficient #448

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 3 additions & 3 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 4 additions & 0 deletions x/evm/keeper/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@
}
}

func (k *Keeper) Transfer(ctx sdk.Context, sender, recipient sdk.AccAddress, coins sdk.Coins) error {
return k.bankKeeper.SendCoins(ctx, sender, recipient, coins)

Check warning on line 76 in x/evm/keeper/statedb.go

View check run for this annotation

Codecov / codecov/patch

x/evm/keeper/statedb.go#L75-L76

Added lines #L75 - L76 were not covered by tests
}

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
Expand Down
11 changes: 0 additions & 11 deletions x/evm/keeper/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ func (suite *StateDBTestSuite) TestAddBalance() {
big.NewInt(0),
true,
},
{
"negative amount",
big.NewInt(-1),
true,
},
}

for _, tc := range testCases {
Expand Down Expand Up @@ -151,12 +146,6 @@ func (suite *StateDBTestSuite) TestSubBalance() {
func(vm.StateDB) {},
true,
},
{
"negative amount",
big.NewInt(-1),
func(vm.StateDB) {},
true,
},
}

for _, tc := range testCases {
Expand Down
1 change: 1 addition & 0 deletions x/evm/statedb/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 32 additions & 2 deletions x/evm/statedb/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
journalIndex int
}

func Transfer(db vm.StateDB, sender, recipient common.Address, amount *big.Int) {
db.(*StateDB).Transfer(sender, recipient, amount)

Check warning on line 47 in x/evm/statedb/statedb.go

View check run for this annotation

Codecov / codecov/patch

x/evm/statedb/statedb.go#L46-L47

Added lines #L46 - L47 were not covered by tests
}

var _ vm.StateDB = &StateDB{}

// StateDB structs within the ethereum protocol are used to store anything
Expand Down Expand Up @@ -382,11 +386,33 @@
* SETTERS
*/

// Transfer from one account to another
func (s *StateDB) Transfer(sender, recipient common.Address, amount *big.Int) {
if amount.Sign() == 0 {
yihuang marked this conversation as resolved.
Show resolved Hide resolved
return
}

Check warning on line 393 in x/evm/statedb/statedb.go

View check run for this annotation

Codecov / codecov/patch

x/evm/statedb/statedb.go#L392-L393

Added lines #L392 - L393 were not covered by tests
if amount.Sign() < 0 {
panic("negative amount")

Check warning on line 395 in x/evm/statedb/statedb.go

View check run for this annotation

Codecov / codecov/patch

x/evm/statedb/statedb.go#L395

Added line #L395 was not covered by tests
}

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
}

Check warning on line 405 in x/evm/statedb/statedb.go

View check run for this annotation

Codecov / codecov/patch

x/evm/statedb/statedb.go#L404-L405

Added lines #L404 - L405 were not covered by tests
}

// 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")

Check warning on line 414 in x/evm/statedb/statedb.go

View check run for this annotation

Codecov / codecov/patch

x/evm/statedb/statedb.go#L414

Added line #L414 was not covered by tests
}
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)
Expand All @@ -397,9 +423,12 @@

// 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")

Check warning on line 430 in x/evm/statedb/statedb.go

View check run for this annotation

Codecov / codecov/patch

x/evm/statedb/statedb.go#L430

Added line #L430 was not covered by tests
}
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)
Expand All @@ -408,6 +437,7 @@
}
}

// 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)
Expand Down
5 changes: 5 additions & 0 deletions x/evm/statedb/statedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Loading