Skip to content

Commit 9d0ec30

Browse files
authored
Merge pull request #170 from vulcanize/v1.10.13-statediff-0.0.29
log processing bug fix
2 parents 34da680 + a945cb1 commit 9d0ec30

20 files changed

+436
-46
lines changed

statediff/indexer/indexer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ type processArgs struct {
299299
rctTrieNodes []*ipld.EthRctTrie
300300
txNodes []*ipld.EthTx
301301
txTrieNodes []*ipld.EthTxTrie
302-
logTrieNodes [][]*ipld.EthLogTrie
302+
logTrieNodes [][]node.Node
303303
logLeafNodeCIDs [][]cid.Cid
304304
rctLeafNodeCIDs []cid.Cid
305305
}

statediff/indexer/indexer_test.go

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var (
4949
mockBlock *types.Block
5050
headerCID, trx1CID, trx2CID, trx3CID, trx4CID, trx5CID cid.Cid
5151
rct1CID, rct2CID, rct3CID, rct4CID, rct5CID cid.Cid
52+
rctLeaf1, rctLeaf2, rctLeaf3, rctLeaf4, rctLeaf5 []byte
5253
state1CID, state2CID, storageCID cid.Cid
5354
)
5455

@@ -124,14 +125,49 @@ func init() {
124125
trx3CID, _ = ipld.RawdataToCid(ipld.MEthTx, tx3, multihash.KECCAK_256)
125126
trx4CID, _ = ipld.RawdataToCid(ipld.MEthTx, tx4, multihash.KECCAK_256)
126127
trx5CID, _ = ipld.RawdataToCid(ipld.MEthTx, tx5, multihash.KECCAK_256)
127-
rct1CID, _ = ipld.RawdataToCid(ipld.MEthTxReceipt, rct1, multihash.KECCAK_256)
128-
rct2CID, _ = ipld.RawdataToCid(ipld.MEthTxReceipt, rct2, multihash.KECCAK_256)
129-
rct3CID, _ = ipld.RawdataToCid(ipld.MEthTxReceipt, rct3, multihash.KECCAK_256)
130-
rct4CID, _ = ipld.RawdataToCid(ipld.MEthTxReceipt, rct4, multihash.KECCAK_256)
131-
rct5CID, _ = ipld.RawdataToCid(ipld.MEthTxReceipt, rct5, multihash.KECCAK_256)
128+
/*
129+
rct1Node, _ := ipld.NewReceipt(rcts[0])
130+
rct2Node, _ := ipld.NewReceipt(rcts[1])
131+
rct3Node, _ := ipld.NewReceipt(rcts[2])
132+
rct4Node, _ := ipld.NewReceipt(rcts[3])
133+
rct5Node, _ := ipld.NewReceipt(rcts[4])
134+
*/
132135
state1CID, _ = ipld.RawdataToCid(ipld.MEthStateTrie, mocks.ContractLeafNode, multihash.KECCAK_256)
133136
state2CID, _ = ipld.RawdataToCid(ipld.MEthStateTrie, mocks.AccountLeafNode, multihash.KECCAK_256)
134137
storageCID, _ = ipld.RawdataToCid(ipld.MEthStorageTrie, mocks.StorageLeafNode, multihash.KECCAK_256)
138+
139+
receiptTrie := ipld.NewRctTrie()
140+
141+
receiptTrie.Add(0, rct1)
142+
receiptTrie.Add(1, rct2)
143+
receiptTrie.Add(2, rct3)
144+
receiptTrie.Add(3, rct4)
145+
receiptTrie.Add(4, rct5)
146+
147+
rctLeafNodes, keys, _ := receiptTrie.GetLeafNodes()
148+
149+
rctleafNodeCids := make([]cid.Cid, len(rctLeafNodes))
150+
orderedRctLeafNodes := make([][]byte, len(rctLeafNodes))
151+
for i, rln := range rctLeafNodes {
152+
var idx uint
153+
154+
r := bytes.NewReader(keys[i].TrieKey)
155+
rlp.Decode(r, &idx)
156+
rctleafNodeCids[idx] = rln.Cid()
157+
orderedRctLeafNodes[idx] = rln.RawData()
158+
}
159+
160+
rct1CID = rctleafNodeCids[0]
161+
rct2CID = rctleafNodeCids[1]
162+
rct3CID = rctleafNodeCids[2]
163+
rct4CID = rctleafNodeCids[3]
164+
rct5CID = rctleafNodeCids[4]
165+
166+
rctLeaf1 = orderedRctLeafNodes[0]
167+
rctLeaf2 = orderedRctLeafNodes[1]
168+
rctLeaf3 = orderedRctLeafNodes[2]
169+
rctLeaf4 = orderedRctLeafNodes[3]
170+
rctLeaf5 = orderedRctLeafNodes[4]
135171
}
136172

137173
func setup(t *testing.T) {
@@ -416,7 +452,7 @@ func TestPublishAndIndexer(t *testing.T) {
416452

417453
switch c {
418454
case rct1CID.String():
419-
shared.ExpectEqual(t, data, rct1)
455+
shared.ExpectEqual(t, data, rctLeaf1)
420456
var postStatus uint64
421457
pgStr = `SELECT post_status FROM eth.receipt_cids WHERE leaf_cid = $1`
422458
err = db.Get(&postStatus, pgStr, c)
@@ -425,7 +461,7 @@ func TestPublishAndIndexer(t *testing.T) {
425461
}
426462
shared.ExpectEqual(t, postStatus, mocks.ExpectedPostStatus)
427463
case rct2CID.String():
428-
shared.ExpectEqual(t, data, rct2)
464+
shared.ExpectEqual(t, data, rctLeaf2)
429465
var postState string
430466
pgStr = `SELECT post_state FROM eth.receipt_cids WHERE leaf_cid = $1`
431467
err = db.Get(&postState, pgStr, c)
@@ -434,7 +470,7 @@ func TestPublishAndIndexer(t *testing.T) {
434470
}
435471
shared.ExpectEqual(t, postState, mocks.ExpectedPostState1)
436472
case rct3CID.String():
437-
shared.ExpectEqual(t, data, rct3)
473+
shared.ExpectEqual(t, data, rctLeaf3)
438474
var postState string
439475
pgStr = `SELECT post_state FROM eth.receipt_cids WHERE leaf_cid = $1`
440476
err = db.Get(&postState, pgStr, c)
@@ -443,7 +479,7 @@ func TestPublishAndIndexer(t *testing.T) {
443479
}
444480
shared.ExpectEqual(t, postState, mocks.ExpectedPostState2)
445481
case rct4CID.String():
446-
shared.ExpectEqual(t, data, rct4)
482+
shared.ExpectEqual(t, data, rctLeaf4)
447483
var postState string
448484
pgStr = `SELECT post_state FROM eth.receipt_cids WHERE leaf_cid = $1`
449485
err = db.Get(&postState, pgStr, c)
@@ -452,7 +488,7 @@ func TestPublishAndIndexer(t *testing.T) {
452488
}
453489
shared.ExpectEqual(t, postState, mocks.ExpectedPostState3)
454490
case rct5CID.String():
455-
shared.ExpectEqual(t, data, rct5)
491+
shared.ExpectEqual(t, data, rctLeaf5)
456492
var postState string
457493
pgStr = `SELECT post_state FROM eth.receipt_cids WHERE leaf_cid = $1`
458494
err = db.Get(&postState, pgStr, c)

statediff/indexer/ipfs/ipld/eth_log_trie.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package ipld
33
import (
44
"fmt"
55

6+
node "github.com/ipfs/go-ipld-format"
7+
68
"github.com/ethereum/go-ethereum/core/types"
79
"github.com/ethereum/go-ethereum/rlp"
810
"github.com/ipfs/go-cid"
@@ -91,13 +93,13 @@ func newLogTrie() *logTrie {
9193
// getNodes invokes the localTrie, which computes the root hash of the
9294
// log trie and returns its database keys, to return a slice
9395
// of EthLogTrie nodes.
94-
func (rt *logTrie) getNodes() ([]*EthLogTrie, error) {
96+
func (rt *logTrie) getNodes() ([]node.Node, error) {
9597
keys, err := rt.getKeys()
9698
if err != nil {
9799
return nil, err
98100
}
99101

100-
out := make([]*EthLogTrie, 0, len(keys))
102+
out := make([]node.Node, 0, len(keys))
101103
for _, k := range keys {
102104
n, err := rt.getNodeFromDB(k)
103105
if err != nil {
@@ -114,14 +116,8 @@ func (rt *logTrie) getNodeFromDB(key []byte) (*EthLogTrie, error) {
114116
if err != nil {
115117
return nil, err
116118
}
117-
118-
c, err := RawdataToCid(MEthLogTrie, rawdata, multihash.KECCAK_256)
119-
if err != nil {
120-
return nil, err
121-
}
122-
123119
tn := &TrieNode{
124-
cid: c,
120+
cid: keccak256ToCid(MEthLogTrie, key),
125121
rawdata: rawdata,
126122
}
127123
return &EthLogTrie{TrieNode: tn}, nil

statediff/indexer/ipfs/ipld/eth_parser.go

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import (
2323
"io"
2424
"io/ioutil"
2525

26+
node "github.com/ipfs/go-ipld-format"
27+
2628
"github.com/ethereum/go-ethereum/common"
2729
"github.com/ethereum/go-ethereum/core/types"
2830
"github.com/ethereum/go-ethereum/rlp"
@@ -123,7 +125,7 @@ func FromBlockJSON(r io.Reader) (*EthHeader, []*EthTx, []*EthTxTrie, error) {
123125

124126
// FromBlockAndReceipts takes a block and processes it
125127
// to return it a set of IPLD nodes for further processing.
126-
func FromBlockAndReceipts(block *types.Block, receipts []*types.Receipt) (*EthHeader, []*EthHeader, []*EthTx, []*EthTxTrie, []*EthReceipt, []*EthRctTrie, [][]*EthLogTrie, [][]cid.Cid, []cid.Cid, error) {
128+
func FromBlockAndReceipts(block *types.Block, receipts []*types.Receipt) (*EthHeader, []*EthHeader, []*EthTx, []*EthTxTrie, []*EthReceipt, []*EthRctTrie, [][]node.Node, [][]cid.Cid, []cid.Cid, error) {
127129
// Process the header
128130
headerNode, err := NewEthHeader(block.Header())
129131
if err != nil {
@@ -148,10 +150,10 @@ func FromBlockAndReceipts(block *types.Block, receipts []*types.Receipt) (*EthHe
148150
}
149151

150152
// Process the receipts and logs
151-
rctNodes, tctTrieNodes, logTrieNodes, logLeafNodeCIDs, rctLeafNodeCIDs, err := processReceiptsAndLogs(receipts,
153+
rctNodes, tctTrieNodes, logTrieAndLogNodes, logLeafNodeCIDs, rctLeafNodeCIDs, err := processReceiptsAndLogs(receipts,
152154
block.Header().ReceiptHash[:])
153155

154-
return headerNode, uncleNodes, txNodes, txTrieNodes, rctNodes, tctTrieNodes, logTrieNodes, logLeafNodeCIDs, rctLeafNodeCIDs, err
156+
return headerNode, uncleNodes, txNodes, txTrieNodes, rctNodes, tctTrieNodes, logTrieAndLogNodes, logLeafNodeCIDs, rctLeafNodeCIDs, err
155157
}
156158

157159
// processTransactions will take the found transactions in a parsed block body
@@ -180,11 +182,11 @@ func processTransactions(txs []*types.Transaction, expectedTxRoot []byte) ([]*Et
180182

181183
// processReceiptsAndLogs will take in receipts
182184
// to return IPLD node slices for eth-rct, eth-rct-trie, eth-log, eth-log-trie, eth-log-trie-CID, eth-rct-trie-CID
183-
func processReceiptsAndLogs(rcts []*types.Receipt, expectedRctRoot []byte) ([]*EthReceipt, []*EthRctTrie, [][]*EthLogTrie, [][]cid.Cid, []cid.Cid, error) {
185+
func processReceiptsAndLogs(rcts []*types.Receipt, expectedRctRoot []byte) ([]*EthReceipt, []*EthRctTrie, [][]node.Node, [][]cid.Cid, []cid.Cid, error) {
184186
// Pre allocating memory.
185187
ethRctNodes := make([]*EthReceipt, 0, len(rcts))
186188
ethLogleafNodeCids := make([][]cid.Cid, 0, len(rcts))
187-
ethLogTrieNodes := make([][]*EthLogTrie, 0, len(rcts))
189+
ethLogTrieAndLogNodes := make([][]node.Node, 0, len(rcts))
188190

189191
receiptTrie := NewRctTrie()
190192

@@ -195,7 +197,7 @@ func processReceiptsAndLogs(rcts []*types.Receipt, expectedRctRoot []byte) ([]*E
195197
return nil, nil, nil, nil, nil, err
196198
}
197199
rct.LogRoot = logTrieHash
198-
ethLogTrieNodes = append(ethLogTrieNodes, logTrieNodes)
200+
ethLogTrieAndLogNodes = append(ethLogTrieAndLogNodes, logTrieNodes)
199201
ethLogleafNodeCids = append(ethLogleafNodeCids, leafNodeCids)
200202

201203
ethRct, err := NewReceipt(rct)
@@ -235,22 +237,38 @@ func processReceiptsAndLogs(rcts []*types.Receipt, expectedRctRoot []byte) ([]*E
235237
ethRctleafNodeCids[idx] = rln.Cid()
236238
}
237239

238-
return ethRctNodes, rctTrieNodes, ethLogTrieNodes, ethLogleafNodeCids, ethRctleafNodeCids, err
240+
return ethRctNodes, rctTrieNodes, ethLogTrieAndLogNodes, ethLogleafNodeCids, ethRctleafNodeCids, err
239241
}
240242

241-
func processLogs(logs []*types.Log) ([]*EthLogTrie, []cid.Cid, common.Hash, error) {
243+
const keccak256Length = 32
244+
245+
func processLogs(logs []*types.Log) ([]node.Node, []cid.Cid, common.Hash, error) {
242246
logTr := newLogTrie()
247+
shortLog := make(map[uint64]*EthLog, len(logs))
243248
for idx, log := range logs {
244-
ethLog, err := NewLog(log)
249+
logRaw, err := rlp.EncodeToBytes(log)
245250
if err != nil {
246251
return nil, nil, common.Hash{}, err
247252
}
248-
if err = logTr.Add(idx, ethLog.RawData()); err != nil {
253+
// if len(logRaw) <= keccak256Length it is possible this value's "leaf node"
254+
// will be stored in its parent branch but only if len(partialPathOfTheNode) + len(logRaw) <= keccak256Length
255+
// But we can't tell what the partial path will be until the trie is Commit()-ed
256+
// So wait until we collect all the leaf nodes, and if we are missing any at the indexes we note in shortLogCIDs
257+
// we know that these "leaf nodes" were internalized into their parent branch node and we move forward with
258+
// using the cid.Cid we cached in shortLogCIDs
259+
if len(logRaw) <= keccak256Length {
260+
logNode, err := NewLog(log)
261+
if err != nil {
262+
return nil, nil, common.Hash{}, err
263+
}
264+
shortLog[uint64(idx)] = logNode
265+
}
266+
if err = logTr.Add(idx, logRaw); err != nil {
249267
return nil, nil, common.Hash{}, err
250268
}
251269
}
252270

253-
logTrieNodes, err := logTr.getNodes()
271+
logTrieAndLogNodes, err := logTr.getNodes()
254272
if err != nil {
255273
return nil, nil, common.Hash{}, err
256274
}
@@ -259,8 +277,7 @@ func processLogs(logs []*types.Log) ([]*EthLogTrie, []cid.Cid, common.Hash, erro
259277
if err != nil {
260278
return nil, nil, common.Hash{}, err
261279
}
262-
263-
leafNodeCids := make([]cid.Cid, len(leafNodes))
280+
leafNodeCids := make([]cid.Cid, len(logs))
264281
for i, ln := range leafNodes {
265282
var idx uint
266283

@@ -271,6 +288,15 @@ func processLogs(logs []*types.Log) ([]*EthLogTrie, []cid.Cid, common.Hash, erro
271288
}
272289
leafNodeCids[idx] = ln.Cid()
273290
}
291+
// this is where we check which logs <= keccak256Length were actually internalized into parent branch node
292+
// and replace those that were with the cid.Cid for the raw log IPLD
293+
for i, l := range shortLog {
294+
if !leafNodeCids[i].Defined() {
295+
leafNodeCids[i] = l.Cid()
296+
// if the leaf node was internalized, we append an IPLD for log itself to the list of IPLDs we need to publish
297+
logTrieAndLogNodes = append(logTrieAndLogNodes, l)
298+
}
299+
}
274300

275-
return logTrieNodes, leafNodeCids, common.BytesToHash(logTr.rootHash()), err
301+
return logTrieAndLogNodes, leafNodeCids, common.BytesToHash(logTr.rootHash()), err
276302
}

statediff/indexer/ipfs/ipld/eth_receipt_trie.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,8 @@ func (rt *rctTrie) getNodeFromDB(key []byte) (*EthRctTrie, error) {
166166
if err != nil {
167167
return nil, err
168168
}
169-
170-
cid, err := RawdataToCid(MEthTxReceiptTrie, rawdata, multihash.KECCAK_256)
171-
if err != nil {
172-
return nil, err
173-
}
174-
175169
tn := &TrieNode{
176-
cid: cid,
170+
cid: keccak256ToCid(MEthTxReceiptTrie, key),
177171
rawdata: rawdata,
178172
}
179173

statediff/indexer/ipfs/ipld/eth_tx_trie.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,8 @@ func (tt *txTrie) getNodes() ([]*EthTxTrie, error) {
135135
if err != nil {
136136
return nil, err
137137
}
138-
c, err := RawdataToCid(MEthTxTrie, rawdata, multihash.KECCAK_256)
139-
if err != nil {
140-
return nil, err
141-
}
142138
tn := &TrieNode{
143-
cid: c,
139+
cid: keccak256ToCid(MEthTxTrie, k),
144140
rawdata: rawdata,
145141
}
146142
out = append(out, &EthTxTrie{TrieNode: tn})
4.35 KB
Binary file not shown.
5.75 KB
Binary file not shown.
3.95 KB
Binary file not shown.
15.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)