Skip to content

Commit df5603d

Browse files
committed
Moved logging to state, proper structured block
* Moved logs to state so it's subject to snapshotting * Split up block header * Removed logs from transactions and made them receipts only
1 parent fa890c8 commit df5603d

13 files changed

Lines changed: 59 additions & 53 deletions

File tree

‎ethchain/block.go‎

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,7 @@ func (block *Block) Hash() ethutil.Bytes {
149149
}
150150

151151
func (block *Block) HashNoNonce() []byte {
152-
return ethcrypto.Sha3(ethutil.Encode([]interface{}{block.PrevHash,
153-
block.UncleSha, block.Coinbase, block.state.Trie.Root,
154-
block.ReceiptSha, block.Difficulty, block.Number, block.MinGasPrice,
155-
block.GasLimit, block.GasUsed, block.Time, block.Extra}))
152+
return ethcrypto.Sha3(ethutil.Encode(block.miningHeader()))
156153
}
157154

158155
func (block *Block) State() *ethstate.State {
@@ -235,31 +232,18 @@ func (block *Block) rlpUncles() interface{} {
235232

236233
func (block *Block) SetUncles(uncles []*Block) {
237234
block.Uncles = uncles
238-
239235
block.UncleSha = ethcrypto.Sha3(ethutil.Encode(block.rlpUncles()))
240236
}
241237

242238
func (self *Block) SetReceipts(receipts Receipts) {
243239
self.receipts = receipts
244-
self.SetReceiptHash(receipts)
240+
self.ReceiptSha = DeriveSha(receipts)
241+
self.LogsBloom = CreateBloom(self)
245242
}
246243

247244
func (self *Block) SetTransactions(txs Transactions) {
248-
self.setTransactions(txs)
249-
self.SetTransactionHash(txs)
250-
}
251-
252-
func (block *Block) setTransactions(txs Transactions) {
253-
block.transactions = txs
254-
block.LogsBloom = CreateBloom(block)
255-
}
256-
257-
func (self *Block) SetTransactionHash(transactions Transactions) {
258-
self.TxSha = DeriveSha(transactions)
259-
}
260-
261-
func (self *Block) SetReceiptHash(receipts Receipts) {
262-
self.ReceiptSha = DeriveSha(receipts)
245+
self.transactions = txs
246+
self.TxSha = DeriveSha(txs)
263247
}
264248

265249
func (block *Block) Value() *ethutil.Value {
@@ -285,10 +269,10 @@ func (block *Block) RlpValueDecode(decoder *ethutil.Value) {
285269
if decoder.Get(1).IsNil() == false { // Yes explicitness
286270
//receipts := decoder.Get(1)
287271
//block.receipts = make([]*Receipt, receipts.Len())
288-
it := decoder.Get(1).NewIterator()
289-
block.transactions = make(Transactions, it.Len())
290-
for it.Next() {
291-
block.transactions[it.Idx()] = NewTransactionFromValue(it.Value())
272+
txs := decoder.Get(1)
273+
block.transactions = make(Transactions, txs.Len())
274+
for i := 0; i < txs.Len(); i++ {
275+
block.transactions[i] = NewTransactionFromValue(txs.Get(i))
292276
//receipt := NewRecieptFromValue(receipts.Get(i))
293277
//block.transactions[i] = receipt.Tx
294278
//block.receipts[i] = receipt
@@ -347,7 +331,7 @@ func (self *Block) Receipts() []*Receipt {
347331
return self.receipts
348332
}
349333

350-
func (block *Block) header() []interface{} {
334+
func (block *Block) miningHeader() []interface{} {
351335
return []interface{}{
352336
// Sha of the previous block
353337
block.PrevHash,
@@ -377,11 +361,13 @@ func (block *Block) header() []interface{} {
377361
block.Time,
378362
// Extra data
379363
block.Extra,
380-
// Block's Nonce for validation
381-
block.Nonce,
382364
}
383365
}
384366

367+
func (block *Block) header() []interface{} {
368+
return append(block.miningHeader(), block.Nonce)
369+
}
370+
385371
func (block *Block) String() string {
386372
return fmt.Sprintf(`
387373
BLOCK(%x): Size: %v

‎ethchain/bloom9.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ package ethchain
33
import (
44
"math/big"
55

6+
"github.com/ethereum/go-ethereum/ethstate"
67
"github.com/ethereum/go-ethereum/ethutil"
7-
"github.com/ethereum/go-ethereum/vm"
88
)
99

1010
func CreateBloom(block *Block) []byte {
1111
bin := new(big.Int)
1212
bin.Or(bin, bloom9(block.Coinbase))
13-
for _, tx := range block.Transactions() {
14-
bin.Or(bin, LogsBloom(tx.logs))
13+
for _, receipt := range block.Receipts() {
14+
bin.Or(bin, LogsBloom(receipt.logs))
1515
}
1616

1717
return bin.Bytes()
1818
}
1919

20-
func LogsBloom(logs []vm.Log) *big.Int {
20+
func LogsBloom(logs ethstate.Logs) *big.Int {
2121
bin := new(big.Int)
2222
for _, log := range logs {
2323
data := [][]byte{log.Address}

‎ethchain/state_manager.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ func (self *StateManager) ProcessTransactions(coinbase *ethstate.StateObject, st
145145

146146
done:
147147
for i, tx := range txs {
148+
// If we are mining this block and validating we want to set the logs back to 0
149+
state.EmptyLogs()
150+
148151
txGas := new(big.Int).Set(tx.Gas)
149152

150153
cb := state.GetStateObject(coinbase.Address())
@@ -175,7 +178,7 @@ done:
175178
txGas.Sub(txGas, st.gas)
176179
cumulative := new(big.Int).Set(totalUsedGas.Add(totalUsedGas, txGas))
177180
//receipt := &Receipt{tx, ethutil.CopyBytes(state.Root().([]byte)), accumelative}
178-
receipt := &Receipt{ethutil.CopyBytes(state.Root().([]byte)), cumulative, LogsBloom(tx.logs).Bytes(), tx.logs}
181+
receipt := &Receipt{ethutil.CopyBytes(state.Root().([]byte)), cumulative, LogsBloom(state.Logs()).Bytes(), state.Logs()}
179182

180183
if i < len(block.Receipts()) {
181184
original := block.Receipts()[i]
@@ -238,7 +241,7 @@ func (sm *StateManager) Process(block *Block) (err error) {
238241

239242
txSha := DeriveSha(block.transactions)
240243
if bytes.Compare(txSha, block.TxSha) != 0 {
241-
return fmt.Errorf("Error validating transaction sha. Received %x, got %x", block.ReceiptSha, txSha)
244+
return fmt.Errorf("Error validating transaction sha. Received %x, got %x", block.TxSha, txSha)
242245
}
243246

244247
receipts, err := sm.ApplyDiff(state, parent, block)

‎ethchain/state_transition.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func (self *StateTransition) TransitionState() (err error) {
232232
} else {
233233
// Add default LOG. Default = big(sender.addr) + 1
234234
addr := ethutil.BigD(receiver.Address())
235-
tx.addLog(vm.Log{sender.Address(), [][]byte{addr.Add(addr, ethutil.Big1).Bytes()}, nil})
235+
self.state.AddLog(ethstate.Log{sender.Address(), [][]byte{ethutil.U256(addr.Add(addr, ethutil.Big1)).Bytes()}, nil})
236236
}
237237
}
238238

‎ethchain/transaction.go‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/ethereum/go-ethereum/ethcrypto"
99
"github.com/ethereum/go-ethereum/ethstate"
1010
"github.com/ethereum/go-ethereum/ethutil"
11-
"github.com/ethereum/go-ethereum/vm"
1211
"github.com/obscuren/secp256k1-go"
1312
)
1413

@@ -29,8 +28,6 @@ type Transaction struct {
2928
v byte
3029
r, s []byte
3130

32-
logs []vm.Log
33-
3431
// Indicates whether this tx is a contract creation transaction
3532
contractCreation bool
3633
}
@@ -57,10 +54,6 @@ func NewTransactionFromValue(val *ethutil.Value) *Transaction {
5754
return tx
5855
}
5956

60-
func (self *Transaction) addLog(log vm.Log) {
61-
self.logs = append(self.logs, log)
62-
}
63-
6457
func (self *Transaction) GasValue() *big.Int {
6558
return new(big.Int).Mul(self.Gas, self.GasPrice)
6659
}
@@ -212,7 +205,7 @@ type Receipt struct {
212205
PostState []byte
213206
CumulativeGasUsed *big.Int
214207
Bloom []byte
215-
Logs vm.Logs
208+
logs ethstate.Logs
216209
}
217210

218211
func NewRecieptFromValue(val *ethutil.Value) *Receipt {
@@ -229,12 +222,12 @@ func (self *Receipt) RlpValueDecode(decoder *ethutil.Value) {
229222

230223
it := decoder.Get(3).NewIterator()
231224
for it.Next() {
232-
self.Logs = append(self.Logs, vm.NewLogFromValue(it.Value()))
225+
self.logs = append(self.logs, ethstate.NewLogFromValue(it.Value()))
233226
}
234227
}
235228

236229
func (self *Receipt) RlpData() interface{} {
237-
return []interface{}{self.PostState, self.CumulativeGasUsed, self.Bloom, self.Logs.RlpData()}
230+
return []interface{}{self.PostState, self.CumulativeGasUsed, self.Bloom, self.logs.RlpData()}
238231
}
239232

240233
func (self *Receipt) RlpEncode() []byte {

‎ethchain/vm_env.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func (self *VMEnv) BlockHash() []byte { return self.block.Hash() }
3131
func (self *VMEnv) Value() *big.Int { return self.tx.Value }
3232
func (self *VMEnv) State() *ethstate.State { return self.state }
3333
func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit }
34-
func (self *VMEnv) AddLog(log vm.Log) {
35-
self.tx.addLog(log)
34+
func (self *VMEnv) AddLog(log ethstate.Log) {
35+
self.state.AddLog(log)
3636
}
3737
func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
3838
return vm.Transfer(from, to, amount)

‎ethminer/miner.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,8 @@ func (self *Miner) mineNewBlock() {
185185
self.ethereum.TxPool().RemoveSet(erroneous)
186186
self.txs = append(txs, unhandledTxs...)
187187

188-
self.block.SetReceipts(receipts)
189188
self.block.SetTransactions(txs)
189+
self.block.SetReceipts(receipts)
190190

191191
// Accumulate the rewards included for this block
192192
stateManager.AccumelateRewards(self.block.State(), self.block, parent)

‎ethpipe/vm_env.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (self *VMEnv) BlockHash() []byte { return self.block.Hash() }
3434
func (self *VMEnv) Value() *big.Int { return self.value }
3535
func (self *VMEnv) State() *ethstate.State { return self.state }
3636
func (self *VMEnv) GasLimit() *big.Int { return self.block.GasLimit }
37-
func (self *VMEnv) AddLog(vm.Log) {}
37+
func (self *VMEnv) AddLog(ethstate.Log) {}
3838
func (self *VMEnv) Transfer(from, to vm.Account, amount *big.Int) error {
3939
return vm.Transfer(from, to, amount)
4040
}

vm/log.go renamed to ethstate/log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package vm
1+
package ethstate
22

33
import "github.com/ethereum/go-ethereum/ethutil"
44

‎ethstate/state.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,27 @@ type State struct {
2424
manifest *Manifest
2525

2626
refund map[string]*big.Int
27+
28+
logs Logs
2729
}
2830

2931
// Create a new state from a given trie
3032
func New(trie *ethtrie.Trie) *State {
3133
return &State{Trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest(), refund: make(map[string]*big.Int)}
3234
}
3335

36+
func (self *State) EmptyLogs() {
37+
self.logs = nil
38+
}
39+
40+
func (self *State) AddLog(log Log) {
41+
self.logs = append(self.logs, log)
42+
}
43+
44+
func (self *State) Logs() Logs {
45+
return self.logs
46+
}
47+
3448
// Retrieve the balance from the given address or 0 if object not found
3549
func (self *State) GetBalance(addr []byte) *big.Int {
3650
stateObject := self.GetStateObject(addr)
@@ -202,6 +216,10 @@ func (self *State) Copy() *State {
202216
state.refund[addr] = refund
203217
}
204218

219+
logs := make(Logs, len(self.logs))
220+
copy(logs, self.logs)
221+
state.logs = logs
222+
205223
return state
206224
}
207225

@@ -216,6 +234,7 @@ func (self *State) Set(state *State) {
216234
self.Trie = state.Trie
217235
self.stateObjects = state.stateObjects
218236
self.refund = state.refund
237+
self.logs = state.logs
219238
}
220239

221240
func (s *State) Root() interface{} {

0 commit comments

Comments
 (0)