@@ -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}
0 commit comments