Skip to content

Commit

Permalink
core, core/state, core/vm: remove exported account getters (#3618)
Browse files Browse the repository at this point in the history
Removed exported statedb object accessors, reducing the chance for nasty
bugs to creep in. It's also ugly and unnecessary to have these methods.
  • Loading branch information
obscuren authored and fjl committed Feb 22, 2017
1 parent 46ec435 commit 024d41d
Show file tree
Hide file tree
Showing 23 changed files with 245 additions and 238 deletions.
18 changes: 11 additions & 7 deletions cmd/evm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/ethereum/go-ethereum/core/vm/runtime"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/logger/glog"
"gopkg.in/urfave/cli.v1"
Expand Down Expand Up @@ -115,9 +114,13 @@ func run(ctx *cli.Context) error {
glog.SetToStderr(true)
glog.SetV(ctx.GlobalInt(VerbosityFlag.Name))

db, _ := ethdb.NewMemDatabase()
statedb, _ := state.New(common.Hash{}, db)
sender := statedb.CreateAccount(common.StringToAddress("sender"))
var (
db, _ = ethdb.NewMemDatabase()
statedb, _ = state.New(common.Hash{}, db)
address = common.StringToAddress("sender")
sender = vm.AccountRef(address)
)
statedb.CreateAccount(common.StringToAddress("sender"))

logger := vm.NewStructLogger(nil)

Expand Down Expand Up @@ -166,10 +169,11 @@ func run(ctx *cli.Context) error {
},
})
} else {
receiver := statedb.CreateAccount(common.StringToAddress("receiver"))
receiver.SetCode(crypto.Keccak256Hash(code), code)
receiverAddress := common.StringToAddress("receiver")
statedb.CreateAccount(receiverAddress)
statedb.SetCode(receiverAddress, code)

ret, err = runtime.Call(receiver.Address(), common.Hex2Bytes(ctx.GlobalString(InputFlag.Name)), &runtime.Config{
ret, err = runtime.Call(receiverAddress, common.Hex2Bytes(ctx.GlobalString(InputFlag.Name)), &runtime.Config{
Origin: sender.Address(),
State: statedb,
GasLimit: common.Big(ctx.GlobalString(GasFlag.Name)).Uint64(),
Expand Down
1 change: 0 additions & 1 deletion core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ func (self *BlockChain) loadLastState() error {
return err
}
self.stateCache = statedb
self.stateCache.GetAccount(common.Address{})

// Issue a status log for the user
headerTd := self.GetTd(currentHeader.Hash(), currentHeader.Number.Uint64())
Expand Down
10 changes: 5 additions & 5 deletions core/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ func ValidateDAOHeaderExtraData(config *params.ChainConfig, header *types.Header
// contract.
func ApplyDAOHardFork(statedb *state.StateDB) {
// Retrieve the contract to refund balances into
refund := statedb.GetOrNewStateObject(params.DAORefundContract)
if !statedb.Exist(params.DAORefundContract) {
statedb.CreateAccount(params.DAORefundContract)
}

// Move every DAO account and extra-balance account funds into the refund contract
for _, addr := range params.DAODrainList {
if account := statedb.GetStateObject(addr); account != nil {
refund.AddBalance(account.Balance())
account.SetBalance(new(big.Int))
}
statedb.AddBalance(params.DAORefundContract, statedb.GetBalance(addr))
statedb.SetBalance(addr, new(big.Int))
}
}
12 changes: 6 additions & 6 deletions core/state/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type (
account *common.Address
}
resetObjectChange struct {
prev *StateObject
prev *stateObject
}
suicideChange struct {
account *common.Address
Expand Down Expand Up @@ -86,7 +86,7 @@ func (ch resetObjectChange) undo(s *StateDB) {
}

func (ch suicideChange) undo(s *StateDB) {
obj := s.GetStateObject(*ch.account)
obj := s.getStateObject(*ch.account)
if obj != nil {
obj.suicided = ch.prev
obj.setBalance(ch.prevbalance)
Expand All @@ -103,19 +103,19 @@ func (ch touchChange) undo(s *StateDB) {
}

func (ch balanceChange) undo(s *StateDB) {
s.GetStateObject(*ch.account).setBalance(ch.prev)
s.getStateObject(*ch.account).setBalance(ch.prev)
}

func (ch nonceChange) undo(s *StateDB) {
s.GetStateObject(*ch.account).setNonce(ch.prev)
s.getStateObject(*ch.account).setNonce(ch.prev)
}

func (ch codeChange) undo(s *StateDB) {
s.GetStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
s.getStateObject(*ch.account).setCode(common.BytesToHash(ch.prevhash), ch.prevcode)
}

func (ch storageChange) undo(s *StateDB) {
s.GetStateObject(*ch.account).setState(ch.key, ch.prevalue)
s.getStateObject(*ch.account).setState(ch.key, ch.prevalue)
}

func (ch refundChange) undo(s *StateDB) {
Expand Down
6 changes: 3 additions & 3 deletions core/state/managed_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

type account struct {
stateObject *StateObject
stateObject *stateObject
nstart uint64
nonces []bool
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func (ms *ManagedState) getAccount(addr common.Address) *account {
} else {
// Always make sure the state account nonce isn't actually higher
// than the tracked one.
so := ms.StateDB.GetStateObject(addr)
so := ms.StateDB.getStateObject(addr)
if so != nil && uint64(len(account.nonces))+account.nstart < so.Nonce() {
ms.accounts[addr] = newAccount(so)
}
Expand All @@ -138,6 +138,6 @@ func (ms *ManagedState) getAccount(addr common.Address) *account {
return ms.accounts[addr]
}

func newAccount(so *StateObject) *account {
func newAccount(so *stateObject) *account {
return &account{so, so.Nonce(), nil}
}
2 changes: 1 addition & 1 deletion core/state/managed_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func create() (*ManagedState, *account) {
statedb, _ := New(common.Hash{}, db)
ms := ManageState(statedb)
ms.StateDB.SetNonce(addr, 100)
ms.accounts[addr] = newAccount(ms.StateDB.GetStateObject(addr))
ms.accounts[addr] = newAccount(ms.StateDB.getStateObject(addr))
return ms, ms.accounts[addr]
}

Expand Down
Loading

0 comments on commit 024d41d

Please sign in to comment.