Skip to content

Commit

Permalink
getbalance: even better impl (ethereum#214)
Browse files Browse the repository at this point in the history
* getbalance: better impl

* getbalance: fix tests
  • Loading branch information
tynes authored Feb 23, 2021
1 parent 30264cf commit 346abeb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
13 changes: 13 additions & 0 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
"golang.org/x/crypto/sha3"
)

type revision struct {
Expand Down Expand Up @@ -264,6 +265,18 @@ func (s *StateDB) GetBalance(addr common.Address) *big.Int {
return common.Big0
}

func (s *StateDB) GetOVMBalance(addr common.Address) *big.Int {
position := big.NewInt(3)
eth := common.HexToAddress("0x4200000000000000000000000000000000000006")
hasher := sha3.NewLegacyKeccak256()
hasher.Write(common.LeftPadBytes(addr.Bytes(), 32))
hasher.Write(common.LeftPadBytes(position.Bytes(), 32))
digest := hasher.Sum(nil)
key := common.BytesToHash(digest)
slot := s.GetState(eth, key)
return slot.Big()
}

func (s *StateDB) GetNonce(addr common.Address) uint64 {
stateObject := s.getStateObject(addr)
if stateObject != nil {
Expand Down
10 changes: 8 additions & 2 deletions core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -572,8 +572,14 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
}
// Transactor should have enough funds to cover the costs
// cost == V + GP * GL
if pool.currentState.GetBalance(from).Cmp(tx.Cost()) < 0 {
return ErrInsufficientFunds
if vm.UsingOVM {
if pool.currentState.GetOVMBalance(from).Cmp(tx.Cost()) < 0 {
return ErrInsufficientFunds
}
} else {
if pool.currentState.GetBalance(from).Cmp(tx.Cost()) < 0 {
return ErrInsufficientFunds
}
}
// Ensure the transaction has more gas than the basic tx fee.
intrGas, err := IntrinsicGas(tx.Data(), tx.To() == nil, true, pool.istanbul)
Expand Down
16 changes: 2 additions & 14 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import (
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
"github.com/tyler-smith/go-bip39"
"golang.org/x/crypto/sha3"
)

const (
Expand Down Expand Up @@ -537,19 +536,8 @@ func (s *PublicBlockChainAPI) GetBalance(ctx context.Context, address common.Add
if state == nil || err != nil {
return nil, err
}

// Use the OVM_ETH predeploy for balances since there is no
// native ETH. This should be moved into statedb.GetBalance
position := big.NewInt(3)
eth := common.HexToAddress("0x4200000000000000000000000000000000000006")
hasher := sha3.NewLegacyKeccak256()
hasher.Write(common.LeftPadBytes(address.Bytes(), 32))
hasher.Write(common.LeftPadBytes(position.Bytes(), 32))

digest := hasher.Sum(nil)
key := common.BytesToHash(digest)
slot := state.GetState(eth, key)
return (*hexutil.Big)(slot.Big()), state.Error()
balance := state.GetOVMBalance(address)
return (*hexutil.Big)(balance), state.Error()
}

// Result structs for GetProof
Expand Down

0 comments on commit 346abeb

Please sign in to comment.