From 024d41d0c2660d8f1dfbeb14921c7109e30493a2 Mon Sep 17 00:00:00 2001 From: Jeffrey Wilcke Date: Wed, 22 Feb 2017 23:29:59 +0100 Subject: core, core/state, core/vm: remove exported account getters (#3618) Removed exported statedb object accessors, reducing the chance for nasty bugs to creep in. It's also ugly and unnecessary to have these methods. --- light/state.go | 20 ++++++++++++++++++++ light/vm_env.go | 31 +++++++++++-------------------- 2 files changed, 31 insertions(+), 20 deletions(-) (limited to 'light') diff --git a/light/state.go b/light/state.go index b6cefc9b9..f19748e89 100644 --- a/light/state.go +++ b/light/state.go @@ -268,6 +268,26 @@ func (self *LightState) CreateStateObject(ctx context.Context, addr common.Addre return newSo, nil } +// ForEachStorage calls a callback function for every key/value pair found +// in the local storage cache. Note that unlike core/state.StateObject, +// light.StateObject only returns cached values and doesn't download the +// entire storage tree. +func (self *LightState) ForEachStorage(ctx context.Context, addr common.Address, cb func(key, value common.Hash) bool) error { + so, err := self.GetStateObject(ctx, addr) + if err != nil { + return err + } + + if so == nil { + return nil + } + + for h, v := range so.storage { + cb(h, v) + } + return nil +} + // // Setting, copying of the state methods // diff --git a/light/vm_env.go b/light/vm_env.go index d2cc7e960..ebd229de8 100644 --- a/light/vm_env.go +++ b/light/vm_env.go @@ -21,7 +21,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/core/vm" "github.com/ethereum/go-ethereum/crypto" "golang.org/x/net/context" ) @@ -64,27 +63,10 @@ func (self *VMState) RevertToSnapshot(idx int) { self.snapshots = self.snapshots[:idx] } -// GetAccount returns the account object of the given account or nil if the -// account does not exist -func (s *VMState) GetAccount(addr common.Address) vm.Account { - so, err := s.state.GetStateObject(s.ctx, addr) - s.errHandler(err) - if err != nil { - // return a dummy state object to avoid panics - so = s.state.newStateObject(addr) - } - return so -} - // CreateAccount creates creates a new account object and takes ownership. -func (s *VMState) CreateAccount(addr common.Address) vm.Account { - so, err := s.state.CreateStateObject(s.ctx, addr) +func (s *VMState) CreateAccount(addr common.Address) { + _, err := s.state.CreateStateObject(s.ctx, addr) s.errHandler(err) - if err != nil { - // return a dummy state object to avoid panics - so = s.state.newStateObject(addr) - } - return so } // AddBalance adds the given amount to the balance of the specified account @@ -99,6 +81,15 @@ func (s *VMState) SubBalance(addr common.Address, amount *big.Int) { s.errHandler(err) } +// ForEachStorage calls a callback function for every key/value pair found +// in the local storage cache. Note that unlike core/state.StateObject, +// light.StateObject only returns cached values and doesn't download the +// entire storage tree. +func (s *VMState) ForEachStorage(addr common.Address, cb func(key, value common.Hash) bool) { + err := s.state.ForEachStorage(s.ctx, addr, cb) + s.errHandler(err) +} + // GetBalance retrieves the balance from the given address or 0 if the account does // not exist func (s *VMState) GetBalance(addr common.Address) *big.Int { -- cgit v1.2.3