Skip to content

Commit d69a8c3

Browse files
committed
refactor(evm): add more compatibility the new geth StateDB inteface, updating AddBalance and SubBalance to use uint256
1 parent 5937bbe commit d69a8c3

File tree

7 files changed

+38
-24
lines changed

7 files changed

+38
-24
lines changed

app/evmante/evmante_gas_consume_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (s *TestSuite) TestAnteDecEthGasConsume() {
2626
beforeTxSetup: func(deps *evmtest.TestDeps, sdb *statedb.StateDB) {
2727
gasLimit := happyGasLimit()
2828
balance := evm.NativeToWei(new(big.Int).Add(gasLimit, big.NewInt(100)))
29-
sdb.AddBalance(deps.Sender.EthAddr, balance)
29+
sdb.AddBalanceSigned(deps.Sender.EthAddr, balance)
3030
},
3131
txSetup: evmtest.HappyCreateContractTx,
3232
wantErr: "",
@@ -47,7 +47,7 @@ func (s *TestSuite) TestAnteDecEthGasConsume() {
4747
beforeTxSetup: func(deps *evmtest.TestDeps, sdb *statedb.StateDB) {
4848
gasLimit := happyGasLimit()
4949
balance := evm.NativeToWei(new(big.Int).Add(gasLimit, big.NewInt(100)))
50-
sdb.AddBalance(deps.Sender.EthAddr, balance)
50+
sdb.AddBalanceSigned(deps.Sender.EthAddr, balance)
5151
},
5252
txSetup: evmtest.HappyCreateContractTx,
5353
wantErr: "exceeds block gas limit (0)",

app/evmante/evmante_handler_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (s *TestSuite) TestAnteHandlerEVM() {
2929
name: "happy: signed tx, sufficient funds",
3030
beforeTxSetup: func(deps *evmtest.TestDeps, sdb *statedb.StateDB) {
3131
balanceMicronibi := new(big.Int).Add(evmtest.GasLimitCreateContract(), big.NewInt(100))
32-
sdb.AddBalance(
32+
sdb.AddBalanceSigned(
3333
deps.Sender.EthAddr,
3434
evm.NativeToWei(balanceMicronibi),
3535
)

app/evmante/evmante_increment_sender_seq_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func (s *TestSuite) TestAnteDecEthIncrementSenderSequence() {
2222
name: "happy: single message",
2323
beforeTxSetup: func(deps *evmtest.TestDeps, sdb *statedb.StateDB) {
2424
balance := big.NewInt(100)
25-
sdb.AddBalance(deps.Sender.EthAddr, balance)
25+
sdb.AddBalanceSigned(deps.Sender.EthAddr, balance)
2626
},
2727
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
2828
return evmtest.HappyTransferTx(deps, 0)
@@ -34,7 +34,7 @@ func (s *TestSuite) TestAnteDecEthIncrementSenderSequence() {
3434
name: "happy: two messages",
3535
beforeTxSetup: func(deps *evmtest.TestDeps, sdb *statedb.StateDB) {
3636
balance := big.NewInt(100)
37-
sdb.AddBalance(deps.Sender.EthAddr, balance)
37+
sdb.AddBalanceSigned(deps.Sender.EthAddr, balance)
3838
},
3939
txSetup: func(deps *evmtest.TestDeps) sdk.Tx {
4040
txMsgOne := evmtest.HappyTransferTx(deps, 0)

app/evmante/evmante_verify_eth_acc_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (s *TestSuite) TestAnteDecoratorVerifyEthAcc_CheckTx() {
2121
{
2222
name: "happy: sender with funds",
2323
beforeTxSetup: func(deps *evmtest.TestDeps, sdb *statedb.StateDB) {
24-
sdb.AddBalance(deps.Sender.EthAddr, evm.NativeToWei(happyGasLimit()))
24+
sdb.AddBalanceSigned(deps.Sender.EthAddr, evm.NativeToWei(happyGasLimit()))
2525
},
2626
txSetup: evmtest.HappyCreateContractTx,
2727
wantErr: "",

x/evm/statedb/journal_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,11 @@ func (s *Suite) TestCommitRemovesDirties_OnlyStateDB() {
5858
randomAcc := evmtest.NewEthPrivAcc().EthAddr
5959
balDelta := evm.NativeToWei(big.NewInt(4))
6060
// 2 dirties from [createObjectChange, balanceChange]
61-
stateDB.AddBalance(randomAcc, balDelta)
61+
stateDB.AddBalanceSigned(randomAcc, balDelta)
6262
// 1 dirties from [balanceChange]
63-
stateDB.AddBalance(randomAcc, balDelta)
63+
stateDB.AddBalanceSigned(randomAcc, balDelta)
6464
// 1 dirties from [balanceChange]
65-
stateDB.SubBalance(randomAcc, balDelta)
65+
stateDB.AddBalanceSigned(randomAcc, balDelta)
6666
if stateDB.DebugDirtiesCount() != 4 {
6767
debugDirtiesCountMismatch(stateDB, s.T())
6868
s.FailNow("expected 4 dirty journal changes")

x/evm/statedb/statedb.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
gethcore "github.com/ethereum/go-ethereum/core/types"
2929
"github.com/ethereum/go-ethereum/core/vm"
3030
"github.com/ethereum/go-ethereum/crypto"
31+
"github.com/holiman/uint256"
3132
)
3233

3334
var _ vm.StateDB = &StateDB{}
@@ -169,12 +170,12 @@ func (s *StateDB) Empty(addr common.Address) bool {
169170
}
170171

171172
// GetBalance retrieves the balance from the given address or 0 if object not found
172-
func (s *StateDB) GetBalance(addr common.Address) *big.Int {
173+
func (s *StateDB) GetBalance(addr common.Address) *uint256.Int {
173174
stateObject := s.getStateObject(addr)
174175
if stateObject != nil {
175-
return stateObject.Balance()
176+
return uint256.MustFromBig(stateObject.Balance())
176177
}
177-
return common.Big0
178+
return uint256.NewInt(0)
178179
}
179180

180181
// GetNonce returns the nonce of account, 0 if not exists.
@@ -351,18 +352,31 @@ func (s *StateDB) setStateObject(object *stateObject) {
351352
*/
352353

353354
// AddBalance adds amount to the account associated with addr.
354-
func (s *StateDB) AddBalance(addr common.Address, wei *big.Int) {
355+
func (s *StateDB) AddBalance(addr common.Address, wei *uint256.Int) {
355356
stateObject := s.getOrNewStateObject(addr)
356357
if stateObject != nil {
357-
stateObject.AddBalance(wei)
358+
stateObject.AddBalance(wei.ToBig())
359+
}
360+
}
361+
362+
func (s *StateDB) AddBalanceSigned(addr common.Address, wei *big.Int) {
363+
weiAbs, isOverflow := uint256.FromBig(wei)
364+
if isOverflow {
365+
// TODO: Is there a better strategy than panicking here?
366+
panic(fmt.Errorf(
367+
"uint256 overflow occured for big.Int value %s", wei))
368+
} else if wei.Cmp(big.NewInt(0)) >= 0 {
369+
s.AddBalance(addr, weiAbs)
370+
} else {
371+
s.SubBalance(addr, weiAbs)
358372
}
359373
}
360374

361375
// SubBalance subtracts amount from the account associated with addr.
362-
func (s *StateDB) SubBalance(addr common.Address, wei *big.Int) {
376+
func (s *StateDB) SubBalance(addr common.Address, wei *uint256.Int) {
363377
stateObject := s.getOrNewStateObject(addr)
364378
if stateObject != nil {
365-
stateObject.SubBalance(wei)
379+
stateObject.SubBalance(wei.ToBig())
366380
}
367381
}
368382

x/evm/statedb/statedb_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func (s *Suite) TestAccount() {
9898
// create a contract account
9999
db.CreateAccount(address)
100100
db.SetCode(address, []byte("hello world"))
101-
db.AddBalance(address, big.NewInt(100))
101+
db.AddBalanceSigned(address, big.NewInt(100))
102102
db.SetState(address, key1, value1)
103103
db.SetState(address, key2, value2)
104104
s.Require().NoError(db.Commit())
@@ -140,7 +140,7 @@ func (s *Suite) TestAccountOverride() {
140140
amount := big.NewInt(1)
141141

142142
// init an EOA account, account overridden only happens on EOA account.
143-
db.AddBalance(address, amount)
143+
db.AddBalanceSigned(address, amount)
144144
db.SetNonce(address, 1)
145145

146146
// override
@@ -182,19 +182,19 @@ func (s *Suite) TestBalance() {
182182
expBalance *big.Int
183183
}{
184184
{"add balance", func(db *statedb.StateDB) {
185-
db.AddBalance(address, big.NewInt(10))
185+
db.AddBalanceSigned(address, big.NewInt(10))
186186
}, big.NewInt(10)},
187187
{"sub balance", func(db *statedb.StateDB) {
188-
db.AddBalance(address, big.NewInt(10))
188+
db.AddBalanceSigned(address, big.NewInt(10))
189189
// get dirty balance
190190
s.Require().Equal(big.NewInt(10), db.GetBalance(address))
191-
db.SubBalance(address, big.NewInt(2))
191+
db.AddBalanceSigned(address, big.NewInt(2))
192192
}, big.NewInt(8)},
193193
{"add zero balance", func(db *statedb.StateDB) {
194-
db.AddBalance(address, big.NewInt(0))
194+
db.AddBalanceSigned(address, big.NewInt(0))
195195
}, big.NewInt(0)},
196196
{"sub zero balance", func(db *statedb.StateDB) {
197-
db.SubBalance(address, big.NewInt(0))
197+
db.AddBalanceSigned(address, big.NewInt(0))
198198
}, big.NewInt(0)},
199199
}
200200

@@ -366,7 +366,7 @@ func (s *Suite) TestRevertSnapshot() {
366366
// do some arbitrary changes to the storage
367367
db := deps.NewStateDB()
368368
db.SetNonce(address, 1)
369-
db.AddBalance(address, big.NewInt(100))
369+
db.AddBalanceSigned(address, big.NewInt(100))
370370
db.SetCode(address, []byte("hello world"))
371371
db.SetState(address, v1, v2)
372372
db.SetNonce(address2, 1)

0 commit comments

Comments
 (0)