diff --git a/statediff/indexer/ipfs/ipld/eth_log_trie.go b/statediff/indexer/ipfs/ipld/eth_log_trie.go index 2e36f0a68..d6b5e945f 100644 --- a/statediff/indexer/ipfs/ipld/eth_log_trie.go +++ b/statediff/indexer/ipfs/ipld/eth_log_trie.go @@ -114,14 +114,8 @@ func (rt *logTrie) getNodeFromDB(key []byte) (*EthLogTrie, error) { if err != nil { return nil, err } - - c, err := RawdataToCid(MEthLogTrie, rawdata, multihash.KECCAK_256) - if err != nil { - return nil, err - } - tn := &TrieNode{ - cid: c, + cid: keccak256ToCid(MEthLogTrie, key), rawdata: rawdata, } return &EthLogTrie{TrieNode: tn}, nil diff --git a/statediff/indexer/ipfs/ipld/eth_parser.go b/statediff/indexer/ipfs/ipld/eth_parser.go index 0b4780f8a..15fad5e72 100644 --- a/statediff/indexer/ipfs/ipld/eth_parser.go +++ b/statediff/indexer/ipfs/ipld/eth_parser.go @@ -238,14 +238,30 @@ func processReceiptsAndLogs(rcts []*types.Receipt, expectedRctRoot []byte) ([]*E return ethRctNodes, rctTrieNodes, ethLogTrieNodes, ethLogleafNodeCids, ethRctleafNodeCids, err } +const keccak256Length = 32 + func processLogs(logs []*types.Log) ([]*EthLogTrie, []cid.Cid, common.Hash, error) { logTr := newLogTrie() + shortLogCIDs := make(map[uint64]cid.Cid, len(logs)) for idx, log := range logs { - ethLog, err := NewLog(log) + logRaw, err := rlp.EncodeToBytes(log) if err != nil { return nil, nil, common.Hash{}, err } - if err = logTr.Add(idx, ethLog.RawData()); err != nil { + // if len(logRaw) <= keccak256Length it is possible this value's "leaf node" + // will be stored in its parent branch but only if len(partialPathOfTheNode) + len(logRaw) <= keccak256Length + // But we can't tell what the partial path will be until the trie is Commit()-ed + // So wait until we collect all the leaf nodes, and if we are missing any at the indexes we note in shortLogCIDs + // we know that these "leaf nodes" were internalized into their parent branch node and we move forward with + // using the cid.Cid we cached in shortLogCIDs + if len(logRaw) <= keccak256Length { + logNode, err := NewLog(log) + if err != nil { + return nil, nil, common.Hash{}, err + } + shortLogCIDs[uint64(idx)] = logNode.Cid() + } + if err = logTr.Add(idx, logRaw); err != nil { return nil, nil, common.Hash{}, err } } @@ -260,7 +276,7 @@ func processLogs(logs []*types.Log) ([]*EthLogTrie, []cid.Cid, common.Hash, erro return nil, nil, common.Hash{}, err } - leafNodeCids := make([]cid.Cid, len(leafNodes)) + leafNodeCids := make([]cid.Cid, len(logs)) for i, ln := range leafNodes { var idx uint @@ -271,6 +287,13 @@ func processLogs(logs []*types.Log) ([]*EthLogTrie, []cid.Cid, common.Hash, erro } leafNodeCids[idx] = ln.Cid() } + // this is where we check which logs <= keccak256Length were actually internalized into parent branch node + // and replace those that were with the cid.Cid for the raw log IPLD + for idx, lCID := range shortLogCIDs { + if !leafNodeCids[idx].Defined() { + leafNodeCids[idx] = lCID + } + } return logTrieNodes, leafNodeCids, common.BytesToHash(logTr.rootHash()), err } diff --git a/statediff/indexer/ipfs/ipld/eth_receipt_trie.go b/statediff/indexer/ipfs/ipld/eth_receipt_trie.go index fc1480703..b31b14d6b 100644 --- a/statediff/indexer/ipfs/ipld/eth_receipt_trie.go +++ b/statediff/indexer/ipfs/ipld/eth_receipt_trie.go @@ -166,14 +166,8 @@ func (rt *rctTrie) getNodeFromDB(key []byte) (*EthRctTrie, error) { if err != nil { return nil, err } - - cid, err := RawdataToCid(MEthTxReceiptTrie, rawdata, multihash.KECCAK_256) - if err != nil { - return nil, err - } - tn := &TrieNode{ - cid: cid, + cid: keccak256ToCid(MEthTxReceiptTrie, key), rawdata: rawdata, } diff --git a/statediff/indexer/ipfs/ipld/eth_tx_trie.go b/statediff/indexer/ipfs/ipld/eth_tx_trie.go index 7e79ff164..4edc7dd7b 100644 --- a/statediff/indexer/ipfs/ipld/eth_tx_trie.go +++ b/statediff/indexer/ipfs/ipld/eth_tx_trie.go @@ -135,12 +135,8 @@ func (tt *txTrie) getNodes() ([]*EthTxTrie, error) { if err != nil { return nil, err } - c, err := RawdataToCid(MEthTxTrie, rawdata, multihash.KECCAK_256) - if err != nil { - return nil, err - } tn := &TrieNode{ - cid: c, + cid: keccak256ToCid(MEthTxTrie, k), rawdata: rawdata, } out = append(out, &EthTxTrie{TrieNode: tn})