2023-06-14 12:43:34 +00:00
|
|
|
// VulcanizeDB
|
|
|
|
// Copyright © 2019 Vulcanize
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package ipld
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FromBlockAndReceipts takes a block and processes it
|
|
|
|
// to return it a set of IPLD nodes for further processing.
|
2024-06-28 09:44:58 +00:00
|
|
|
func FromBlockAndReceipts(block *types.Block, receipts []*types.Receipt) ([]IPLD, []IPLD, [][]IPLD, []IPLD, error) {
|
2023-06-14 12:43:34 +00:00
|
|
|
// Process the txs
|
|
|
|
txNodes, err := processTransactions(block.Transactions())
|
|
|
|
if err != nil {
|
2024-06-28 09:44:58 +00:00
|
|
|
return nil, nil, nil, nil, err
|
|
|
|
}
|
|
|
|
withdrawalNodes, err := processWithdrawals(block.Withdrawals())
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, nil, err
|
2023-06-14 12:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process the receipts and logs
|
|
|
|
rctNodes, logNodes, err := processReceiptsAndLogs(receipts)
|
|
|
|
|
2024-06-28 09:44:58 +00:00
|
|
|
return txNodes, rctNodes, logNodes, withdrawalNodes, err
|
2023-06-14 12:43:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// processTransactions will take the found transactions in a parsed block body
|
|
|
|
// to return IPLD node slices for eth-tx
|
2024-06-28 09:44:58 +00:00
|
|
|
func processTransactions(txs []*types.Transaction) ([]IPLD, error) {
|
|
|
|
var ethTxNodes []IPLD
|
2023-06-14 12:43:34 +00:00
|
|
|
for _, tx := range txs {
|
2024-07-13 11:09:39 +00:00
|
|
|
ethTx, err := EncodeTx(tx)
|
2023-06-14 12:43:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ethTxNodes = append(ethTxNodes, ethTx)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ethTxNodes, nil
|
|
|
|
}
|
|
|
|
|
2024-06-28 09:44:58 +00:00
|
|
|
func processWithdrawals(withdrawals []*types.Withdrawal) ([]IPLD, error) {
|
|
|
|
var withdrawalNodes []IPLD
|
|
|
|
for _, withdrawal := range withdrawals {
|
2024-07-13 11:09:39 +00:00
|
|
|
ethW, err := EncodeWithdrawal(withdrawal)
|
2024-06-28 09:44:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
withdrawalNodes = append(withdrawalNodes, ethW)
|
|
|
|
}
|
|
|
|
|
|
|
|
return withdrawalNodes, nil
|
|
|
|
}
|
|
|
|
|
2023-06-14 12:43:34 +00:00
|
|
|
// processReceiptsAndLogs will take in receipts
|
|
|
|
// to return IPLD node slices for eth-rct and eth-log
|
2024-06-28 09:44:58 +00:00
|
|
|
func processReceiptsAndLogs(rcts []*types.Receipt) ([]IPLD, [][]IPLD, error) {
|
2023-06-14 12:43:34 +00:00
|
|
|
// Pre allocating memory.
|
2024-06-28 09:44:58 +00:00
|
|
|
ethRctNodes := make([]IPLD, len(rcts))
|
|
|
|
ethLogNodes := make([][]IPLD, len(rcts))
|
2023-06-14 12:43:34 +00:00
|
|
|
|
|
|
|
for idx, rct := range rcts {
|
|
|
|
logNodes, err := processLogs(rct.Logs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2024-07-13 11:09:39 +00:00
|
|
|
ethRct, err := EncodeReceipt(rct)
|
2023-06-14 12:43:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ethRctNodes[idx] = ethRct
|
|
|
|
ethLogNodes[idx] = logNodes
|
|
|
|
}
|
|
|
|
|
|
|
|
return ethRctNodes, ethLogNodes, nil
|
|
|
|
}
|
|
|
|
|
2024-06-28 09:44:58 +00:00
|
|
|
func processLogs(logs []*types.Log) ([]IPLD, error) {
|
|
|
|
logNodes := make([]IPLD, len(logs))
|
2023-06-14 12:43:34 +00:00
|
|
|
for idx, log := range logs {
|
2024-07-13 11:09:39 +00:00
|
|
|
logNode, err := EncodeLog(log)
|
2023-06-14 12:43:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
logNodes[idx] = logNode
|
|
|
|
}
|
|
|
|
return logNodes, nil
|
|
|
|
}
|