Skip to content

Commit 3f6baa4

Browse files
committed
Documented methods & removed old manifest
1 parent 558c67d commit 3f6baa4

5 files changed

Lines changed: 36 additions & 131 deletions

File tree

‎cmd/mist/assets/examples/coin.js‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎core/block_processor.go‎

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ func (self *BlockProcessor) ApplyTransactions(coinbase *state.StateObject, state
143143
return receipts, handled, unhandled, erroneous, err
144144
}
145145

146+
// Process block will attempt to process the given block's transactions and applies them
147+
// on top of the block's parent state (given it exists) and will return wether it was
148+
// successful or not.
146149
func (sm *BlockProcessor) Process(block *types.Block) (td *big.Int, err error) {
147150
// Processing a blocks may never happen simultaneously
148151
sm.mutex.Lock()
@@ -158,14 +161,14 @@ func (sm *BlockProcessor) Process(block *types.Block) (td *big.Int, err error) {
158161
}
159162
parent := sm.bc.GetBlock(header.ParentHash)
160163

161-
return sm.ProcessWithParent(block, parent)
164+
return sm.processWithParent(block, parent)
162165
}
163166

164-
func (sm *BlockProcessor) ProcessWithParent(block, parent *types.Block) (td *big.Int, err error) {
167+
func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (td *big.Int, err error) {
165168
sm.lastAttemptedBlock = block
166169

170+
// Create a new state based on the parent's root (e.g., create copy)
167171
state := state.New(parent.Root(), sm.db)
168-
//state := state.New(parent.Trie().Copy())
169172

170173
// Block validation
171174
if err = sm.ValidateBlock(block, parent); err != nil {
@@ -179,31 +182,38 @@ func (sm *BlockProcessor) ProcessWithParent(block, parent *types.Block) (td *big
179182

180183
header := block.Header()
181184

185+
// Validate the received block's bloom with the one derived from the generated receipts.
186+
// For valid blocks this should always validate to true.
182187
rbloom := types.CreateBloom(receipts)
183188
if bytes.Compare(rbloom, header.Bloom) != 0 {
184189
err = fmt.Errorf("unable to replicate block's bloom=%x", rbloom)
185190
return
186191
}
187192

193+
// The transactions Trie's root (R = (Tr [[H1, T1], [H2, T2], ... [Hn, Tn]]))
194+
// can be used by light clients to make sure they've received the correct Txs
188195
txSha := types.DeriveSha(block.Transactions())
189196
if bytes.Compare(txSha, header.TxHash) != 0 {
190197
err = fmt.Errorf("validating transaction root. received=%x got=%x", header.TxHash, txSha)
191198
return
192199
}
193200

201+
// Tre receipt Trie's root (R = (Tr [[H1, R1], ... [Hn, R1]]))
194202
receiptSha := types.DeriveSha(receipts)
195203
if bytes.Compare(receiptSha, header.ReceiptHash) != 0 {
196204
fmt.Println("receipts", receipts)
197205
err = fmt.Errorf("validating receipt root. received=%x got=%x", header.ReceiptHash, receiptSha)
198206
return
199207
}
200208

209+
// Accumulate static rewards; block reward, uncle's and uncle inclusion.
201210
if err = sm.AccumulateRewards(state, block, parent); err != nil {
202211
return
203212
}
204213

214+
// Commit state objects/accounts to a temporary trie (does not save)
215+
// used to calculate the state root.
205216
state.Update(ethutil.Big0)
206-
207217
if !bytes.Equal(header.Root, state.Root()) {
208218
err = fmt.Errorf("invalid merkle root. received=%x got=%x", header.Root, state.Root())
209219
return
@@ -213,10 +223,6 @@ func (sm *BlockProcessor) ProcessWithParent(block, parent *types.Block) (td *big
213223
td = CalculateTD(block, parent)
214224
// Sync the current block's state to the database
215225
state.Sync()
216-
// Set the block hashes for the current messages
217-
state.Manifest().SetHash(block.Hash())
218-
// Reset the manifest XXX We need this?
219-
state.Manifest().Reset()
220226
// Remove transactions from the pool
221227
sm.txpool.RemoveSet(block.Transactions())
222228

@@ -296,27 +302,6 @@ func (sm *BlockProcessor) AccumulateRewards(statedb *state.StateDB, block, paren
296302
return nil
297303
}
298304

299-
func (sm *BlockProcessor) GetMessages(block *types.Block) (messages []*state.Message, err error) {
300-
if !sm.bc.HasBlock(block.Header().ParentHash) {
301-
return nil, ParentError(block.Header().ParentHash)
302-
}
303-
304-
sm.lastAttemptedBlock = block
305-
306-
var (
307-
parent = sm.bc.GetBlock(block.Header().ParentHash)
308-
//state = state.New(parent.Trie().Copy())
309-
state = state.New(parent.Root(), sm.db)
310-
)
311-
312-
defer state.Reset()
313-
314-
sm.TransitionState(state, parent, block)
315-
sm.AccumulateRewards(state, block, parent)
316-
317-
return state.Manifest().Messages, nil
318-
}
319-
320305
func (sm *BlockProcessor) GetLogs(block *types.Block) (logs state.Logs, err error) {
321306
if !sm.bc.HasBlock(block.Header().ParentHash) {
322307
return nil, ParentError(block.Header().ParentHash)

‎state/manifest.go‎

Lines changed: 0 additions & 61 deletions
This file was deleted.

‎state/statedb.go‎

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ type StateDB struct {
2222

2323
stateObjects map[string]*StateObject
2424

25-
manifest *Manifest
26-
2725
refund map[string]*big.Int
2826

2927
logs Logs
@@ -32,7 +30,7 @@ type StateDB struct {
3230
// Create a new state from a given trie
3331
func New(root []byte, db ethutil.Database) *StateDB {
3432
trie := trie.New(ethutil.CopyBytes(root), db)
35-
return &StateDB{db: db, trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest(), refund: make(map[string]*big.Int)}
33+
return &StateDB{db: db, trie: trie, stateObjects: make(map[string]*StateObject), refund: make(map[string]*big.Int)}
3634
}
3735

3836
func (self *StateDB) EmptyLogs() {
@@ -47,6 +45,13 @@ func (self *StateDB) Logs() Logs {
4745
return self.logs
4846
}
4947

48+
func (self *StateDB) Refund(addr []byte, gas *big.Int) {
49+
if self.refund[string(addr)] == nil {
50+
self.refund[string(addr)] = new(big.Int)
51+
}
52+
self.refund[string(addr)].Add(self.refund[string(addr)], gas)
53+
}
54+
5055
// Retrieve the balance from the given address or 0 if object not found
5156
func (self *StateDB) GetBalance(addr []byte) *big.Int {
5257
stateObject := self.GetStateObject(addr)
@@ -57,13 +62,6 @@ func (self *StateDB) GetBalance(addr []byte) *big.Int {
5762
return ethutil.Big0
5863
}
5964

60-
func (self *StateDB) Refund(addr []byte, gas *big.Int) {
61-
if self.refund[string(addr)] == nil {
62-
self.refund[string(addr)] = new(big.Int)
63-
}
64-
self.refund[string(addr)].Add(self.refund[string(addr)], gas)
65-
}
66-
6765
func (self *StateDB) AddBalance(addr []byte, amount *big.Int) {
6866
stateObject := self.GetStateObject(addr)
6967
if stateObject != nil {
@@ -103,6 +101,7 @@ func (self *StateDB) SetCode(addr, code []byte) {
103101
}
104102
}
105103

104+
// TODO vars
106105
func (self *StateDB) GetState(a, b []byte) []byte {
107106
stateObject := self.GetStateObject(a)
108107
if stateObject != nil {
@@ -212,25 +211,21 @@ func (s *StateDB) Cmp(other *StateDB) bool {
212211
}
213212

214213
func (self *StateDB) Copy() *StateDB {
215-
if self.trie != nil {
216-
state := New(nil, self.db)
217-
state.trie = self.trie.Copy()
218-
for k, stateObject := range self.stateObjects {
219-
state.stateObjects[k] = stateObject.Copy()
220-
}
221-
222-
for addr, refund := range self.refund {
223-
state.refund[addr] = new(big.Int).Set(refund)
224-
}
225-
226-
logs := make(Logs, len(self.logs))
227-
copy(logs, self.logs)
228-
state.logs = logs
214+
state := New(nil, self.db)
215+
state.trie = self.trie.Copy()
216+
for k, stateObject := range self.stateObjects {
217+
state.stateObjects[k] = stateObject.Copy()
218+
}
229219

230-
return state
220+
for addr, refund := range self.refund {
221+
state.refund[addr] = new(big.Int).Set(refund)
231222
}
232223

233-
return nil
224+
logs := make(Logs, len(self.logs))
225+
copy(logs, self.logs)
226+
state.logs = logs
227+
228+
return state
234229
}
235230

236231
func (self *StateDB) Set(state *StateDB) {
@@ -301,10 +296,6 @@ func (self *StateDB) Update(gasUsed *big.Int) {
301296
}
302297
}
303298

304-
func (self *StateDB) Manifest() *Manifest {
305-
return self.manifest
306-
}
307-
308299
// Debug stuff
309300
func (self *StateDB) CreateOutputForDiff() {
310301
for _, stateObject := range self.stateObjects {

‎vm/vm.go‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ func New(env Environment) *Vm {
3838
func (self *Vm) Run(me, caller ContextRef, code []byte, value, gas, price *big.Int, callData []byte) (ret []byte, err error) {
3939
self.env.SetDepth(self.env.Depth() + 1)
4040

41-
msg := self.env.State().Manifest().AddMessage(&state.Message{
42-
To: me.Address(), From: caller.Address(),
43-
Input: callData,
44-
Origin: self.env.Origin(),
45-
Timestamp: self.env.Time(), Coinbase: self.env.Coinbase(), Number: self.env.BlockNumber(),
46-
Value: value,
47-
})
4841
context := NewContext(caller, me, code, gas, price)
4942

5043
vmlogger.Debugf("(%d) (%x) %x (code=%d) gas: %v (d) %x\n", self.env.Depth(), caller.Address()[:4], context.Address(), len(code), context.Gas, callData)
@@ -618,8 +611,6 @@ func (self *Vm) Run(me, caller ContextRef, code []byte, value, gas, price *big.I
618611
val, loc := stack.Popn()
619612
statedb.SetState(context.Address(), loc.Bytes(), val)
620613

621-
msg.AddStorageChange(loc.Bytes())
622-
623614
self.Printf(" {0x%x : 0x%x}", loc.Bytes(), val.Bytes())
624615
case JUMP:
625616
jump(pc, stack.Pop())
@@ -670,7 +661,6 @@ func (self *Vm) Run(me, caller ContextRef, code []byte, value, gas, price *big.I
670661
dataGas.Mul(dataGas, GasCreateByte)
671662
if context.UseGas(dataGas) {
672663
ref.SetCode(ret)
673-
msg.Output = ret
674664
}
675665
addr = ref.Address()
676666

@@ -713,7 +703,6 @@ func (self *Vm) Run(me, caller ContextRef, code []byte, value, gas, price *big.I
713703
vmlogger.Debugln(err)
714704
} else {
715705
stack.Push(ethutil.BigTrue)
716-
msg.Output = ret
717706

718707
mem.Set(retOffset.Uint64(), retSize.Uint64(), ret)
719708
}

0 commit comments

Comments
 (0)