fix log trie fk bug

This commit is contained in:
i-norden 2021-12-17 16:44:44 -06:00
parent 167d89d4f4
commit bbfaea9be9
4 changed files with 27 additions and 22 deletions

View File

@ -299,7 +299,7 @@ type processArgs struct {
rctTrieNodes []*ipld.EthRctTrie rctTrieNodes []*ipld.EthRctTrie
txNodes []*ipld.EthTx txNodes []*ipld.EthTx
txTrieNodes []*ipld.EthTxTrie txTrieNodes []*ipld.EthTxTrie
logTrieNodes [][]*ipld.EthLogTrie logTrieNodes [][]node.Node
logLeafNodeCIDs [][]cid.Cid logLeafNodeCIDs [][]cid.Cid
rctLeafNodeCIDs []cid.Cid rctLeafNodeCIDs []cid.Cid
} }

View File

@ -3,6 +3,8 @@ package ipld
import ( import (
"fmt" "fmt"
node "github.com/ipfs/go-ipld-format"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
@ -91,13 +93,13 @@ func newLogTrie() *logTrie {
// getNodes invokes the localTrie, which computes the root hash of the // getNodes invokes the localTrie, which computes the root hash of the
// log trie and returns its database keys, to return a slice // log trie and returns its database keys, to return a slice
// of EthLogTrie nodes. // of EthLogTrie nodes.
func (rt *logTrie) getNodes() ([]*EthLogTrie, error) { func (rt *logTrie) getNodes() ([]node.Node, error) {
keys, err := rt.getKeys() keys, err := rt.getKeys()
if err != nil { if err != nil {
return nil, err return nil, err
} }
out := make([]*EthLogTrie, 0, len(keys)) out := make([]node.Node, 0, len(keys))
for _, k := range keys { for _, k := range keys {
n, err := rt.getNodeFromDB(k) n, err := rt.getNodeFromDB(k)
if err != nil { if err != nil {

View File

@ -23,6 +23,8 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
node "github.com/ipfs/go-ipld-format"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
@ -123,7 +125,7 @@ func FromBlockJSON(r io.Reader) (*EthHeader, []*EthTx, []*EthTxTrie, error) {
// FromBlockAndReceipts takes a block and processes it // FromBlockAndReceipts takes a block and processes it
// to return it a set of IPLD nodes for further processing. // to return it a set of IPLD nodes for further processing.
func FromBlockAndReceipts(block *types.Block, receipts []*types.Receipt) (*EthHeader, []*EthHeader, []*EthTx, []*EthTxTrie, []*EthReceipt, []*EthRctTrie, [][]*EthLogTrie, [][]cid.Cid, []cid.Cid, error) { func FromBlockAndReceipts(block *types.Block, receipts []*types.Receipt) (*EthHeader, []*EthHeader, []*EthTx, []*EthTxTrie, []*EthReceipt, []*EthRctTrie, [][]node.Node, [][]cid.Cid, []cid.Cid, error) {
// Process the header // Process the header
headerNode, err := NewEthHeader(block.Header()) headerNode, err := NewEthHeader(block.Header())
if err != nil { if err != nil {
@ -148,10 +150,10 @@ func FromBlockAndReceipts(block *types.Block, receipts []*types.Receipt) (*EthHe
} }
// Process the receipts and logs // Process the receipts and logs
rctNodes, tctTrieNodes, logTrieNodes, logLeafNodeCIDs, rctLeafNodeCIDs, err := processReceiptsAndLogs(receipts, rctNodes, tctTrieNodes, logTrieAndLogNodes, logLeafNodeCIDs, rctLeafNodeCIDs, err := processReceiptsAndLogs(receipts,
block.Header().ReceiptHash[:]) block.Header().ReceiptHash[:])
return headerNode, uncleNodes, txNodes, txTrieNodes, rctNodes, tctTrieNodes, logTrieNodes, logLeafNodeCIDs, rctLeafNodeCIDs, err return headerNode, uncleNodes, txNodes, txTrieNodes, rctNodes, tctTrieNodes, logTrieAndLogNodes, logLeafNodeCIDs, rctLeafNodeCIDs, err
} }
// processTransactions will take the found transactions in a parsed block body // processTransactions will take the found transactions in a parsed block body
@ -180,11 +182,11 @@ func processTransactions(txs []*types.Transaction, expectedTxRoot []byte) ([]*Et
// processReceiptsAndLogs will take in receipts // processReceiptsAndLogs will take in receipts
// to return IPLD node slices for eth-rct, eth-rct-trie, eth-log, eth-log-trie, eth-log-trie-CID, eth-rct-trie-CID // to return IPLD node slices for eth-rct, eth-rct-trie, eth-log, eth-log-trie, eth-log-trie-CID, eth-rct-trie-CID
func processReceiptsAndLogs(rcts []*types.Receipt, expectedRctRoot []byte) ([]*EthReceipt, []*EthRctTrie, [][]*EthLogTrie, [][]cid.Cid, []cid.Cid, error) { func processReceiptsAndLogs(rcts []*types.Receipt, expectedRctRoot []byte) ([]*EthReceipt, []*EthRctTrie, [][]node.Node, [][]cid.Cid, []cid.Cid, error) {
// Pre allocating memory. // Pre allocating memory.
ethRctNodes := make([]*EthReceipt, 0, len(rcts)) ethRctNodes := make([]*EthReceipt, 0, len(rcts))
ethLogleafNodeCids := make([][]cid.Cid, 0, len(rcts)) ethLogleafNodeCids := make([][]cid.Cid, 0, len(rcts))
ethLogTrieNodes := make([][]*EthLogTrie, 0, len(rcts)) ethLogTrieAndLogNodes := make([][]node.Node, 0, len(rcts))
receiptTrie := NewRctTrie() receiptTrie := NewRctTrie()
@ -195,7 +197,7 @@ func processReceiptsAndLogs(rcts []*types.Receipt, expectedRctRoot []byte) ([]*E
return nil, nil, nil, nil, nil, err return nil, nil, nil, nil, nil, err
} }
rct.LogRoot = logTrieHash rct.LogRoot = logTrieHash
ethLogTrieNodes = append(ethLogTrieNodes, logTrieNodes) ethLogTrieAndLogNodes = append(ethLogTrieAndLogNodes, logTrieNodes)
ethLogleafNodeCids = append(ethLogleafNodeCids, leafNodeCids) ethLogleafNodeCids = append(ethLogleafNodeCids, leafNodeCids)
ethRct, err := NewReceipt(rct) ethRct, err := NewReceipt(rct)
@ -235,14 +237,14 @@ func processReceiptsAndLogs(rcts []*types.Receipt, expectedRctRoot []byte) ([]*E
ethRctleafNodeCids[idx] = rln.Cid() ethRctleafNodeCids[idx] = rln.Cid()
} }
return ethRctNodes, rctTrieNodes, ethLogTrieNodes, ethLogleafNodeCids, ethRctleafNodeCids, err return ethRctNodes, rctTrieNodes, ethLogTrieAndLogNodes, ethLogleafNodeCids, ethRctleafNodeCids, err
} }
const keccak256Length = 32 const keccak256Length = 32
func processLogs(logs []*types.Log) ([]*EthLogTrie, []cid.Cid, common.Hash, error) { func processLogs(logs []*types.Log) ([]node.Node, []cid.Cid, common.Hash, error) {
logTr := newLogTrie() logTr := newLogTrie()
shortLogCIDs := make(map[uint64]cid.Cid, len(logs)) shortLog := make(map[uint64]*EthLog, len(logs))
for idx, log := range logs { for idx, log := range logs {
logRaw, err := rlp.EncodeToBytes(log) logRaw, err := rlp.EncodeToBytes(log)
if err != nil { if err != nil {
@ -259,14 +261,14 @@ func processLogs(logs []*types.Log) ([]*EthLogTrie, []cid.Cid, common.Hash, erro
if err != nil { if err != nil {
return nil, nil, common.Hash{}, err return nil, nil, common.Hash{}, err
} }
shortLogCIDs[uint64(idx)] = logNode.Cid() shortLog[uint64(idx)] = logNode
} }
if err = logTr.Add(idx, logRaw); err != nil { if err = logTr.Add(idx, logRaw); err != nil {
return nil, nil, common.Hash{}, err return nil, nil, common.Hash{}, err
} }
} }
logTrieNodes, err := logTr.getNodes() logTrieAndLogNodes, err := logTr.getNodes()
if err != nil { if err != nil {
return nil, nil, common.Hash{}, err return nil, nil, common.Hash{}, err
} }
@ -275,7 +277,6 @@ func processLogs(logs []*types.Log) ([]*EthLogTrie, []cid.Cid, common.Hash, erro
if err != nil { if err != nil {
return nil, nil, common.Hash{}, err return nil, nil, common.Hash{}, err
} }
leafNodeCids := make([]cid.Cid, len(logs)) leafNodeCids := make([]cid.Cid, len(logs))
for i, ln := range leafNodes { for i, ln := range leafNodes {
var idx uint var idx uint
@ -289,11 +290,13 @@ func processLogs(logs []*types.Log) ([]*EthLogTrie, []cid.Cid, common.Hash, erro
} }
// this is where we check which logs <= keccak256Length were actually internalized into parent branch node // 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 // and replace those that were with the cid.Cid for the raw log IPLD
for idx, lCID := range shortLogCIDs { for i, l := range shortLog {
if !leafNodeCids[idx].Defined() { if !leafNodeCids[i].Defined() {
leafNodeCids[idx] = lCID leafNodeCids[i] = l.Cid()
// if the leaf node was internalized, we append an IPLD for log itself to the list of IPLDs we need to publish
logTrieAndLogNodes = append(logTrieAndLogNodes, l)
} }
} }
return logTrieNodes, leafNodeCids, common.BytesToHash(logTr.rootHash()), err return logTrieAndLogNodes, leafNodeCids, common.BytesToHash(logTr.rootHash()), err
} }

View File

@ -86,7 +86,7 @@ func setup(t *testing.T, testBlock *types.Block, testReceipts types.Receipts) {
testBlock.Difficulty()) testBlock.Difficulty())
require.NoError(t, err) require.NoError(t, err)
defer func(){ defer func() {
if err := tx.Close(err); err != nil { if err := tx.Close(err); err != nil {
t.Fatal(err) t.Fatal(err)
} }