Skip to content

Commit ef422ee

Browse files
committed
light: implemented odr-capable trie and state structures
1 parent e640861 commit ef422ee

10 files changed

Lines changed: 1091 additions & 15 deletions

File tree

‎light/odr.go‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2015 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
// Package light implements on-demand retrieval capable state and chain objects
18+
// for the Ethereum Light Client.
19+
package light
20+
21+
import (
22+
"github.com/ethereum/go-ethereum/common"
23+
"github.com/ethereum/go-ethereum/crypto"
24+
"github.com/ethereum/go-ethereum/ethdb"
25+
"github.com/ethereum/go-ethereum/rlp"
26+
"golang.org/x/net/context"
27+
)
28+
29+
// OdrBackend is an interface to a backend service that handles odr retrievals
30+
type OdrBackend interface {
31+
Database() ethdb.Database
32+
Retrieve(ctx context.Context, req OdrRequest) error
33+
}
34+
35+
// OdrRequest is an interface for retrieval requests
36+
type OdrRequest interface {
37+
StoreResult(db ethdb.Database)
38+
}
39+
40+
// TrieRequest is the ODR request type for state/storage trie entries
41+
type TrieRequest struct {
42+
OdrRequest
43+
root common.Hash
44+
key []byte
45+
proof []rlp.RawValue
46+
}
47+
48+
// StoreResult stores the retrieved data in local database
49+
func (req *TrieRequest) StoreResult(db ethdb.Database) {
50+
storeProof(db, req.proof)
51+
}
52+
53+
// storeProof stores the new trie nodes obtained from a merkle proof in the database
54+
func storeProof(db ethdb.Database, proof []rlp.RawValue) {
55+
for _, buf := range proof {
56+
hash := crypto.Sha3(buf)
57+
val, _ := db.Get(hash)
58+
if val == nil {
59+
db.Put(hash, buf)
60+
}
61+
}
62+
}
63+
64+
// NodeDataRequest is the ODR request type for node data (used for retrieving contract code)
65+
type NodeDataRequest struct {
66+
OdrRequest
67+
hash common.Hash
68+
data []byte
69+
}
70+
71+
// GetData returns the retrieved node data after a successful request
72+
func (req *NodeDataRequest) GetData() []byte {
73+
return req.data
74+
}
75+
76+
// StoreResult stores the retrieved data in local database
77+
func (req *NodeDataRequest) StoreResult(db ethdb.Database) {
78+
db.Put(req.hash[:], req.GetData())
79+
}
80+
81+
var sha3_nil = crypto.Sha3Hash(nil)
82+
83+
// retrieveNodeData tries to retrieve node data with the given hash from the network
84+
func retrieveNodeData(ctx context.Context, odr OdrBackend, hash common.Hash) ([]byte, error) {
85+
if hash == sha3_nil {
86+
return nil, nil
87+
}
88+
res, _ := odr.Database().Get(hash[:])
89+
if res != nil {
90+
return res, nil
91+
}
92+
r := &NodeDataRequest{hash: hash}
93+
if err := odr.Retrieve(ctx, r); err != nil {
94+
return nil, err
95+
} else {
96+
return r.GetData(), nil
97+
}
98+
}

‎light/state.go‎

Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
// Copyright 2015 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package light
18+
19+
import (
20+
"math/big"
21+
22+
"github.com/ethereum/go-ethereum/common"
23+
"github.com/ethereum/go-ethereum/logger"
24+
"github.com/ethereum/go-ethereum/logger/glog"
25+
"golang.org/x/net/context"
26+
)
27+
28+
// StartingNonce determines the default nonce when new accounts are being created.
29+
var StartingNonce uint64
30+
31+
// LightState is a memory representation of a state.
32+
// This version is ODR capable, caching only the already accessed part of the
33+
// state, retrieving unknown parts on-demand from the ODR backend. Changes are
34+
// never stored in the local database, only in the memory objects.
35+
type LightState struct {
36+
odr OdrBackend
37+
trie *LightTrie
38+
39+
stateObjects map[string]*StateObject
40+
}
41+
42+
// NewLightState creates a new LightState with the specified root.
43+
// Note that the creation of a light state is always successful, even if the
44+
// root is non-existent. In that case, ODR retrieval will always be unsuccessful
45+
// and every operation will return with an error or wait for the context to be
46+
// cancelled.
47+
func NewLightState(root common.Hash, odr OdrBackend) *LightState {
48+
tr := NewLightTrie(root, odr, true)
49+
return &LightState{
50+
odr: odr,
51+
trie: tr,
52+
stateObjects: make(map[string]*StateObject),
53+
}
54+
}
55+
56+
// HasAccount returns true if an account exists at the given address
57+
func (self *LightState) HasAccount(ctx context.Context, addr common.Address) (bool, error) {
58+
so, err := self.GetStateObject(ctx, addr)
59+
return so != nil, err
60+
}
61+
62+
// GetBalance retrieves the balance from the given address or 0 if the account does
63+
// not exist
64+
func (self *LightState) GetBalance(ctx context.Context, addr common.Address) (*big.Int, error) {
65+
stateObject, err := self.GetStateObject(ctx, addr)
66+
if err != nil {
67+
return common.Big0, err
68+
}
69+
if stateObject != nil {
70+
return stateObject.balance, nil
71+
}
72+
73+
return common.Big0, nil
74+
}
75+
76+
// GetNonce returns the nonce at the given address or 0 if the account does
77+
// not exist
78+
func (self *LightState) GetNonce(ctx context.Context, addr common.Address) (uint64, error) {
79+
stateObject, err := self.GetStateObject(ctx, addr)
80+
if err != nil {
81+
return 0, err
82+
}
83+
if stateObject != nil {
84+
return stateObject.nonce, nil
85+
}
86+
return 0, nil
87+
}
88+
89+
// GetCode returns the contract code at the given address or nil if the account
90+
// does not exist
91+
func (self *LightState) GetCode(ctx context.Context, addr common.Address) ([]byte, error) {
92+
stateObject, err := self.GetStateObject(ctx, addr)
93+
if err != nil {
94+
return nil, err
95+
}
96+
if stateObject != nil {
97+
return stateObject.code, nil
98+
}
99+
return nil, nil
100+
}
101+
102+
// GetState returns the contract storage value at storage address b from the
103+
// contract address a or common.Hash{} if the account does not exist
104+
func (self *LightState) GetState(ctx context.Context, a common.Address, b common.Hash) (common.Hash, error) {
105+
stateObject, err := self.GetStateObject(ctx, a)
106+
if err == nil && stateObject != nil {
107+
return stateObject.GetState(ctx, b)
108+
}
109+
return common.Hash{}, err
110+
}
111+
112+
// IsDeleted returns true if the given account has been marked for deletion
113+
// or false if the account does not exist
114+
func (self *LightState) IsDeleted(ctx context.Context, addr common.Address) (bool, error) {
115+
stateObject, err := self.GetStateObject(ctx, addr)
116+
if err == nil && stateObject != nil {
117+
return stateObject.remove, nil
118+
}
119+
return false, err
120+
}
121+
122+
/*
123+
* SETTERS
124+
*/
125+
126+
// AddBalance adds the given amount to the balance of the specified account
127+
func (self *LightState) AddBalance(ctx context.Context, addr common.Address, amount *big.Int) error {
128+
stateObject, err := self.GetOrNewStateObject(ctx, addr)
129+
if err == nil && stateObject != nil {
130+
stateObject.AddBalance(amount)
131+
}
132+
return err
133+
}
134+
135+
// SetNonce sets the nonce of the specified account
136+
func (self *LightState) SetNonce(ctx context.Context, addr common.Address, nonce uint64) error {
137+
stateObject, err := self.GetOrNewStateObject(ctx, addr)
138+
if err == nil && stateObject != nil {
139+
stateObject.SetNonce(nonce)
140+
}
141+
return err
142+
}
143+
144+
// SetCode sets the contract code at the specified account
145+
func (self *LightState) SetCode(ctx context.Context, addr common.Address, code []byte) error {
146+
stateObject, err := self.GetOrNewStateObject(ctx, addr)
147+
if err == nil && stateObject != nil {
148+
stateObject.SetCode(code)
149+
}
150+
return err
151+
}
152+
153+
// SetState sets the storage value at storage address key of the account addr
154+
func (self *LightState) SetState(ctx context.Context, addr common.Address, key common.Hash, value common.Hash) error {
155+
stateObject, err := self.GetOrNewStateObject(ctx, addr)
156+
if err == nil && stateObject != nil {
157+
stateObject.SetState(key, value)
158+
}
159+
return err
160+
}
161+
162+
// Delete marks an account to be removed and clears its balance
163+
func (self *LightState) Delete(ctx context.Context, addr common.Address) (bool, error) {
164+
stateObject, err := self.GetOrNewStateObject(ctx, addr)
165+
if err == nil && stateObject != nil {
166+
stateObject.MarkForDeletion()
167+
stateObject.balance = new(big.Int)
168+
169+
return true, nil
170+
}
171+
172+
return false, err
173+
}
174+
175+
//
176+
// Get, set, new state object methods
177+
//
178+
179+
// GetStateObject returns the state object of the given account or nil if the
180+
// account does not exist
181+
func (self *LightState) GetStateObject(ctx context.Context, addr common.Address) (stateObject *StateObject, err error) {
182+
stateObject = self.stateObjects[addr.Str()]
183+
if stateObject != nil {
184+
if stateObject.deleted {
185+
stateObject = nil
186+
}
187+
return stateObject, nil
188+
}
189+
data, err := self.trie.Get(ctx, addr[:])
190+
if err != nil {
191+
return nil, err
192+
}
193+
if len(data) == 0 {
194+
return nil, nil
195+
}
196+
197+
stateObject, err = DecodeObject(ctx, addr, self.odr, []byte(data))
198+
if err != nil {
199+
return nil, err
200+
}
201+
202+
self.SetStateObject(stateObject)
203+
204+
return stateObject, nil
205+
}
206+
207+
// SetStateObject sets the state object of the given account
208+
func (self *LightState) SetStateObject(object *StateObject) {
209+
self.stateObjects[object.Address().Str()] = object
210+
}
211+
212+
// GetOrNewStateObject returns the state object of the given account or creates a
213+
// new one if the account does not exist
214+
func (self *LightState) GetOrNewStateObject(ctx context.Context, addr common.Address) (*StateObject, error) {
215+
stateObject, err := self.GetStateObject(ctx, addr)
216+
if err == nil && (stateObject == nil || stateObject.deleted) {
217+
stateObject, err = self.CreateStateObject(ctx, addr)
218+
}
219+
return stateObject, err
220+
}
221+
222+
// newStateObject creates a state object whether it exists in the state or not
223+
func (self *LightState) newStateObject(addr common.Address) *StateObject {
224+
if glog.V(logger.Core) {
225+
glog.Infof("(+) %x\n", addr)
226+
}
227+
228+
stateObject := NewStateObject(addr, self.odr)
229+
stateObject.SetNonce(StartingNonce)
230+
self.stateObjects[addr.Str()] = stateObject
231+
232+
return stateObject
233+
}
234+
235+
// CreateStateObject creates creates a new state object and takes ownership.
236+
// This is different from "NewStateObject"
237+
func (self *LightState) CreateStateObject(ctx context.Context, addr common.Address) (*StateObject, error) {
238+
// Get previous (if any)
239+
so, err := self.GetStateObject(ctx, addr)
240+
if err != nil {
241+
return nil, err
242+
}
243+
// Create a new one
244+
newSo := self.newStateObject(addr)
245+
246+
// If it existed set the balance to the new account
247+
if so != nil {
248+
newSo.balance = so.balance
249+
}
250+
251+
return newSo, nil
252+
}
253+
254+
//
255+
// Setting, copying of the state methods
256+
//
257+
258+
// Copy creates a copy of the state
259+
func (self *LightState) Copy() *LightState {
260+
// ignore error - we assume state-to-be-copied always exists
261+
state := NewLightState(common.Hash{}, self.odr)
262+
state.trie = self.trie
263+
for k, stateObject := range self.stateObjects {
264+
state.stateObjects[k] = stateObject.Copy()
265+
}
266+
267+
return state
268+
}
269+
270+
// Set copies the contents of the given state onto this state, overwriting
271+
// its contents
272+
func (self *LightState) Set(state *LightState) {
273+
self.trie = state.trie
274+
self.stateObjects = state.stateObjects
275+
}

0 commit comments

Comments
 (0)