Skip to content

Commit e3253b5

Browse files
committed
core: fixed an issue with storing receipts
1 parent 27e0d2a commit e3253b5

5 files changed

Lines changed: 124 additions & 33 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ <h1>JevCoin <code id="contract_addr"></code></h1>
102102
var amount = parseInt( value.value );
103103
console.log("transact: ", to.value, " => ", amount)
104104

105-
contract.sendTransaction({from: eth.accounts[0]}).send( to.value, amount );
105+
contract.send.sendTransaction(to.value, amount ,{from: eth.accounts[0]});
106106

107107
to.value = "";
108108
value.value = "";

‎core/block_processor.go‎

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,6 @@ type BlockProcessor struct {
4040

4141
txpool *TxPool
4242

43-
// The last attempted block is mainly used for debugging purposes
44-
// This does not have to be a valid block and will be set during
45-
// 'Process' & canonical validation.
46-
lastAttemptedBlock *types.Block
47-
4843
events event.Subscription
4944

5045
eventMux *event.TypeMux
@@ -188,8 +183,6 @@ func (sm *BlockProcessor) Process(block *types.Block) (logs state.Logs, err erro
188183
}
189184

190185
func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs state.Logs, err error) {
191-
sm.lastAttemptedBlock = block
192-
193186
// Create a new state based on the parent's root (e.g., create copy)
194187
state := state.New(parent.Root(), sm.db)
195188

@@ -255,6 +248,12 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs st
255248
return
256249
}
257250

251+
// store the receipts
252+
err = putReceipts(sm.extraDb, block.Hash(), receipts)
253+
if err != nil {
254+
return nil, err
255+
}
256+
258257
// Calculate the td for this block
259258
//td = CalculateTD(block, parent)
260259
// Sync the current block's state to the database
@@ -268,23 +267,9 @@ func (sm *BlockProcessor) processWithParent(block, parent *types.Block) (logs st
268267
putTx(sm.extraDb, tx, block, uint64(i))
269268
}
270269

271-
receiptsRlp := block.Receipts().RlpEncode()
272-
sm.extraDb.Put(append(receiptsPre, block.Hash().Bytes()...), receiptsRlp)
273-
274270
return state.Logs(), nil
275271
}
276272

277-
func (self *BlockProcessor) GetBlockReceipts(bhash common.Hash) (receipts types.Receipts, err error) {
278-
var rdata []byte
279-
rdata, err = self.extraDb.Get(append(receiptsPre, bhash[:]...))
280-
281-
if err == nil {
282-
err = rlp.DecodeBytes(rdata, &receipts)
283-
}
284-
return
285-
286-
}
287-
288273
// See YP section 4.3.4. "Block Header Validity"
289274
// Validates a block. Returns an error if the block is invalid.
290275
func (sm *BlockProcessor) ValidateHeader(block, parent *types.Header, checkPow bool) error {
@@ -391,13 +376,25 @@ func (sm *BlockProcessor) VerifyUncles(statedb *state.StateDB, block, parent *ty
391376
return nil
392377
}
393378

379+
// GetBlockReceipts returns the receipts beloniging to the block hash
380+
func (sm *BlockProcessor) GetBlockReceipts(bhash common.Hash) (receipts types.Receipts, err error) {
381+
return getBlockReceipts(sm.extraDb, bhash)
382+
}
383+
384+
// GetLogs returns the logs of the given block. This method is using a two step approach
385+
// where it tries to get it from the (updated) method which gets them from the receipts or
386+
// the depricated way by re-processing the block.
394387
func (sm *BlockProcessor) GetLogs(block *types.Block) (logs state.Logs, err error) {
395-
if !sm.bc.HasBlock(block.Header().ParentHash) {
396-
return nil, ParentError(block.Header().ParentHash)
388+
receipts, err := sm.GetBlockReceipts(block.Hash())
389+
if err == nil && len(receipts) > 0 {
390+
// coalesce logs
391+
for _, receipt := range receipts {
392+
logs = append(logs, receipt.Logs()...)
393+
}
394+
return
397395
}
398396

399-
sm.lastAttemptedBlock = block
400-
397+
// TODO: remove backward compatibility
401398
var (
402399
parent = sm.bc.GetBlock(block.Header().ParentHash)
403400
state = state.New(parent.Root(), sm.db)
@@ -408,6 +405,16 @@ func (sm *BlockProcessor) GetLogs(block *types.Block) (logs state.Logs, err erro
408405
return state.Logs(), nil
409406
}
410407

408+
func getBlockReceipts(db common.Database, bhash common.Hash) (receipts types.Receipts, err error) {
409+
var rdata []byte
410+
rdata, err = db.Get(append(receiptsPre, bhash[:]...))
411+
412+
if err == nil {
413+
err = rlp.DecodeBytes(rdata, &receipts)
414+
}
415+
return
416+
}
417+
411418
func putTx(db common.Database, tx *types.Transaction, block *types.Block, i uint64) {
412419
rlpEnc, err := rlp.EncodeToBytes(tx)
413420
if err != nil {
@@ -431,3 +438,19 @@ func putTx(db common.Database, tx *types.Transaction, block *types.Block, i uint
431438
}
432439
db.Put(append(tx.Hash().Bytes(), 0x0001), rlpMeta)
433440
}
441+
442+
func putReceipts(db common.Database, hash common.Hash, receipts types.Receipts) error {
443+
storageReceipts := make([]*types.ReceiptForStorage, len(receipts))
444+
for i, receipt := range receipts {
445+
storageReceipts[i] = (*types.ReceiptForStorage)(receipt)
446+
}
447+
448+
bytes, err := rlp.EncodeToBytes(storageReceipts)
449+
if err != nil {
450+
return err
451+
}
452+
453+
db.Put(append(receiptsPre, hash[:]...), bytes)
454+
455+
return nil
456+
}

‎core/block_processor_test.go‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"testing"
66

77
"github.com/ethereum/go-ethereum/common"
8+
"github.com/ethereum/go-ethereum/core/state"
9+
"github.com/ethereum/go-ethereum/core/types"
810
"github.com/ethereum/go-ethereum/ethdb"
911
"github.com/ethereum/go-ethereum/event"
1012
"github.com/ethereum/go-ethereum/pow/ezp"
@@ -35,3 +37,33 @@ func TestNumber(t *testing.T) {
3537
t.Errorf("didn't expect block number error")
3638
}
3739
}
40+
41+
func TestPutReceipt(t *testing.T) {
42+
db, _ := ethdb.NewMemDatabase()
43+
44+
var addr common.Address
45+
addr[0] = 1
46+
var hash common.Hash
47+
hash[0] = 2
48+
49+
receipt := new(types.Receipt)
50+
receipt.SetLogs(state.Logs{&state.Log{
51+
Address: addr,
52+
Topics: []common.Hash{hash},
53+
Data: []byte("hi"),
54+
Number: 42,
55+
TxHash: hash,
56+
TxIndex: 0,
57+
BlockHash: hash,
58+
Index: 0,
59+
}})
60+
61+
putReceipts(db, hash, types.Receipts{receipt})
62+
receipts, err := getBlockReceipts(db, hash)
63+
if err != nil {
64+
t.Error("got err:", err)
65+
}
66+
if len(receipts) != 1 {
67+
t.Error("expected to get 1 receipt, got", len(receipts))
68+
}
69+
}

‎core/state/log.go‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,22 @@ func (self *Log) EncodeRLP(w io.Writer) error {
2929
}
3030

3131
func (self *Log) String() string {
32-
return fmt.Sprintf(`log: %x %x %x`, self.Address, self.Topics, self.Data)
32+
return fmt.Sprintf(`log: %x %x %x %x %d %x %d`, self.Address, self.Topics, self.Data, self.TxHash, self.TxIndex, self.BlockHash, self.Index)
3333
}
3434

3535
type Logs []*Log
3636

37-
func (self Logs) String() (ret string) {
38-
for _, log := range self {
39-
ret += fmt.Sprintf("%v", log)
40-
}
41-
42-
return "[" + ret + "]"
37+
type LogForStorage Log
38+
39+
func (self *LogForStorage) EncodeRLP(w io.Writer) error {
40+
return rlp.Encode(w, []interface{}{
41+
self.Address,
42+
self.Topics,
43+
self.Data,
44+
self.Number,
45+
self.TxHash,
46+
self.TxIndex,
47+
self.BlockHash,
48+
self.Index,
49+
})
4350
}

‎core/types/receipt.go‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,39 @@ func (self *Receipt) SetLogs(logs state.Logs) {
2626
self.logs = logs
2727
}
2828

29+
func (self *Receipt) Logs() state.Logs {
30+
return self.logs
31+
}
32+
2933
func (self *Receipt) EncodeRLP(w io.Writer) error {
3034
return rlp.Encode(w, []interface{}{self.PostState, self.CumulativeGasUsed, self.Bloom, self.logs})
3135
}
3236

37+
func (self *Receipt) DecodeRLP(s *rlp.Stream) error {
38+
var r struct {
39+
PostState []byte
40+
CumulativeGasUsed *big.Int
41+
Bloom Bloom
42+
Logs state.Logs
43+
}
44+
if err := s.Decode(&r); err != nil {
45+
return err
46+
}
47+
self.PostState, self.CumulativeGasUsed, self.Bloom, self.logs = r.PostState, r.CumulativeGasUsed, r.Bloom, r.Logs
48+
49+
return nil
50+
}
51+
52+
type ReceiptForStorage Receipt
53+
54+
func (self *ReceiptForStorage) EncodeRLP(w io.Writer) error {
55+
storageLogs := make([]*state.LogForStorage, len(self.logs))
56+
for i, log := range self.logs {
57+
storageLogs[i] = (*state.LogForStorage)(log)
58+
}
59+
return rlp.Encode(w, []interface{}{self.PostState, self.CumulativeGasUsed, self.Bloom, storageLogs})
60+
}
61+
3362
func (self *Receipt) RlpEncode() []byte {
3463
bytes, err := rlp.EncodeToBytes(self)
3564
if err != nil {

0 commit comments

Comments
 (0)