2019-04-15 16:29:49 +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/>.
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
package eth
|
2019-04-15 16:29:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-01-17 23:16:01 +00:00
|
|
|
"fmt"
|
2019-04-15 16:29:49 +00:00
|
|
|
|
2020-01-30 22:35:31 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/shared"
|
|
|
|
|
2019-04-15 16:29:49 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-04-18 12:39:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2020-01-30 22:35:31 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/ipfs"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/ipfs/dag_putters"
|
2019-04-15 16:29:49 +00:00
|
|
|
)
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// IPLDPublisher satisfies the IPLDPublisher for ethereum
|
|
|
|
type IPLDPublisher struct {
|
2020-01-30 22:35:31 +00:00
|
|
|
HeaderPutter shared.DagPutter
|
|
|
|
TransactionPutter shared.DagPutter
|
|
|
|
ReceiptPutter shared.DagPutter
|
|
|
|
StatePutter shared.DagPutter
|
|
|
|
StoragePutter shared.DagPutter
|
2019-04-15 16:29:49 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 17:48:31 +00:00
|
|
|
// NewIPLDPublisher creates a pointer to a new Publisher which satisfies the IPLDPublisher interface
|
2020-01-17 23:16:01 +00:00
|
|
|
func NewIPLDPublisher(ipfsPath string) (*IPLDPublisher, error) {
|
2019-04-15 16:29:49 +00:00
|
|
|
node, err := ipfs.InitIPFSNode(ipfsPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
return &IPLDPublisher{
|
2020-01-30 22:35:31 +00:00
|
|
|
HeaderPutter: dag_putters.NewEthBlockHeaderDagPutter(node),
|
|
|
|
TransactionPutter: dag_putters.NewEthTxsDagPutter(node),
|
|
|
|
ReceiptPutter: dag_putters.NewEthReceiptDagPutter(node),
|
|
|
|
StatePutter: dag_putters.NewEthStateDagPutter(node),
|
|
|
|
StoragePutter: dag_putters.NewEthStorageDagPutter(node),
|
2019-04-15 16:29:49 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
// Publish publishes an IPLDPayload to IPFS and returns the corresponding CIDPayload
|
2020-01-31 18:03:37 +00:00
|
|
|
func (pub *IPLDPublisher) Publish(payload shared.StreamedIPLDs) (shared.CIDsForIndexing, error) {
|
|
|
|
ipldPayload, ok := payload.(IPLDPayload)
|
2020-01-17 23:16:01 +00:00
|
|
|
if !ok {
|
2020-01-31 18:03:37 +00:00
|
|
|
return nil, fmt.Errorf("eth publisher expected payload type %T got %T", IPLDPayload{}, payload)
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2019-04-15 16:29:49 +00:00
|
|
|
// Process and publish headers
|
2020-01-30 22:35:31 +00:00
|
|
|
headerCid, err := pub.publishHeader(ipldPayload.Block.Header())
|
2020-01-16 23:21:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-04-15 16:29:49 +00:00
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
header := HeaderModel{
|
|
|
|
CID: headerCid,
|
|
|
|
ParentHash: ipldPayload.Block.ParentHash().String(),
|
|
|
|
BlockNumber: ipldPayload.Block.Number().String(),
|
|
|
|
BlockHash: ipldPayload.Block.Hash().String(),
|
|
|
|
TotalDifficulty: ipldPayload.TotalDifficulty.String(),
|
|
|
|
}
|
2019-04-17 18:26:48 +00:00
|
|
|
|
2019-04-30 17:48:31 +00:00
|
|
|
// Process and publish uncles
|
2020-01-26 19:55:26 +00:00
|
|
|
uncleCids := make([]UncleModel, 0, len(ipldPayload.Block.Uncles()))
|
2020-01-17 23:16:01 +00:00
|
|
|
for _, uncle := range ipldPayload.Block.Uncles() {
|
2020-01-30 22:35:31 +00:00
|
|
|
uncleCid, err := pub.publishHeader(uncle)
|
2020-01-16 23:21:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-04-30 17:48:31 +00:00
|
|
|
}
|
2020-01-26 19:55:26 +00:00
|
|
|
uncleCids = append(uncleCids, UncleModel{
|
|
|
|
CID: uncleCid,
|
|
|
|
ParentHash: uncle.ParentHash.String(),
|
|
|
|
BlockHash: uncle.Hash().String(),
|
2020-01-17 23:16:01 +00:00
|
|
|
})
|
2019-04-30 17:48:31 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
// Process and publish transactions
|
2020-01-30 22:35:31 +00:00
|
|
|
transactionCids, err := pub.publishTransactions(ipldPayload.Block.Body().Transactions, ipldPayload.TxMetaData)
|
2020-01-16 23:21:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-04-17 18:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process and publish receipts
|
2020-01-17 23:16:01 +00:00
|
|
|
receiptsCids, err := pub.publishReceipts(ipldPayload.Receipts, ipldPayload.ReceiptMetaData)
|
2020-01-16 23:21:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-04-17 18:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process and publish state leafs
|
2020-01-17 23:16:01 +00:00
|
|
|
stateNodeCids, err := pub.publishStateNodes(ipldPayload.StateNodes)
|
2020-01-16 23:21:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-04-17 18:26:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Process and publish storage leafs
|
2020-01-17 23:16:01 +00:00
|
|
|
storageNodeCids, err := pub.publishStorageNodes(ipldPayload.StorageNodes)
|
2020-01-16 23:21:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-04-17 18:26:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 03:50:12 +00:00
|
|
|
// Package CIDs and their metadata into a single struct
|
2019-04-17 18:26:48 +00:00
|
|
|
return &CIDPayload{
|
2020-01-17 23:16:01 +00:00
|
|
|
HeaderCID: header,
|
2019-08-29 19:57:58 +00:00
|
|
|
UncleCIDs: uncleCids,
|
2019-04-17 18:26:48 +00:00
|
|
|
TransactionCIDs: transactionCids,
|
2019-04-18 12:39:37 +00:00
|
|
|
ReceiptCIDs: receiptsCids,
|
2019-05-17 15:23:39 +00:00
|
|
|
StateNodeCIDs: stateNodeCids,
|
|
|
|
StorageNodeCIDs: storageNodeCids,
|
2019-04-17 18:26:48 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-01-30 22:35:31 +00:00
|
|
|
func (pub *IPLDPublisher) publishHeader(header *types.Header) (string, error) {
|
|
|
|
cids, err := pub.HeaderPutter.DagPut(header)
|
2019-04-17 18:26:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-01-30 22:35:31 +00:00
|
|
|
return cids[0], nil
|
2019-04-17 18:26:48 +00:00
|
|
|
}
|
2019-04-15 16:29:49 +00:00
|
|
|
|
2020-01-30 22:35:31 +00:00
|
|
|
func (pub *IPLDPublisher) publishTransactions(transactions types.Transactions, trxMeta []TxModel) ([]TxModel, error) {
|
|
|
|
transactionCids, err := pub.TransactionPutter.DagPut(transactions)
|
2019-04-15 16:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-21 19:12:35 +00:00
|
|
|
if len(transactionCids) != len(trxMeta) {
|
2019-04-15 16:29:49 +00:00
|
|
|
return nil, errors.New("expected one CID for each transaction")
|
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
mappedTrxCids := make([]TxModel, len(transactionCids))
|
2020-01-21 19:12:35 +00:00
|
|
|
for i, cid := range transactionCids {
|
2020-01-17 23:16:01 +00:00
|
|
|
mappedTrxCids[i] = TxModel{
|
2020-01-21 19:12:35 +00:00
|
|
|
CID: cid,
|
2020-01-26 19:55:26 +00:00
|
|
|
Index: trxMeta[i].Index,
|
2020-01-21 19:12:35 +00:00
|
|
|
TxHash: trxMeta[i].TxHash,
|
2020-01-17 23:16:01 +00:00
|
|
|
Src: trxMeta[i].Src,
|
|
|
|
Dst: trxMeta[i].Dst,
|
|
|
|
}
|
2019-04-15 16:29:49 +00:00
|
|
|
}
|
2019-04-17 18:26:48 +00:00
|
|
|
return mappedTrxCids, nil
|
|
|
|
}
|
2019-04-15 16:29:49 +00:00
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
func (pub *IPLDPublisher) publishReceipts(receipts types.Receipts, receiptMeta []ReceiptModel) (map[common.Hash]ReceiptModel, error) {
|
2019-04-17 18:26:48 +00:00
|
|
|
receiptsCids, err := pub.ReceiptPutter.DagPut(receipts)
|
2019-04-15 16:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-17 18:26:48 +00:00
|
|
|
if len(receiptsCids) != len(receipts) {
|
2019-04-15 16:29:49 +00:00
|
|
|
return nil, errors.New("expected one CID for each receipt")
|
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
// Map receipt cids to their transaction hashes
|
|
|
|
mappedRctCids := make(map[common.Hash]ReceiptModel, len(receiptsCids))
|
2019-04-17 18:26:48 +00:00
|
|
|
for i, rct := range receipts {
|
2020-01-17 23:16:01 +00:00
|
|
|
mappedRctCids[rct.TxHash] = ReceiptModel{
|
|
|
|
CID: receiptsCids[i],
|
|
|
|
Contract: receiptMeta[i].Contract,
|
|
|
|
Topic0s: receiptMeta[i].Topic0s,
|
2020-01-21 19:12:35 +00:00
|
|
|
Topic1s: receiptMeta[i].Topic1s,
|
|
|
|
Topic2s: receiptMeta[i].Topic2s,
|
|
|
|
Topic3s: receiptMeta[i].Topic3s,
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2019-04-15 16:29:49 +00:00
|
|
|
}
|
2019-04-17 18:26:48 +00:00
|
|
|
return mappedRctCids, nil
|
|
|
|
}
|
2019-04-15 16:29:49 +00:00
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
func (pub *IPLDPublisher) publishStateNodes(stateNodes []TrieNode) ([]StateNodeModel, error) {
|
|
|
|
stateNodeCids := make([]StateNodeModel, 0, len(stateNodes))
|
|
|
|
for _, node := range stateNodes {
|
2020-01-30 22:35:31 +00:00
|
|
|
cids, err := pub.StatePutter.DagPut(node.Value)
|
2019-04-15 16:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
stateNodeCids = append(stateNodeCids, StateNodeModel{
|
|
|
|
StateKey: node.Key.String(),
|
2020-01-30 22:35:31 +00:00
|
|
|
CID: cids[0],
|
2020-01-17 23:16:01 +00:00
|
|
|
Leaf: node.Leaf,
|
|
|
|
})
|
2019-04-15 16:29:49 +00:00
|
|
|
}
|
2019-05-17 04:08:53 +00:00
|
|
|
return stateNodeCids, nil
|
2019-04-17 18:26:48 +00:00
|
|
|
}
|
2019-04-15 16:29:49 +00:00
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
func (pub *IPLDPublisher) publishStorageNodes(storageNodes map[common.Hash][]TrieNode) (map[common.Hash][]StorageNodeModel, error) {
|
|
|
|
storageLeafCids := make(map[common.Hash][]StorageNodeModel)
|
2019-08-28 19:43:27 +00:00
|
|
|
for addrKey, storageTrie := range storageNodes {
|
2020-01-17 23:16:01 +00:00
|
|
|
storageLeafCids[addrKey] = make([]StorageNodeModel, 0, len(storageTrie))
|
2019-05-17 04:08:53 +00:00
|
|
|
for _, node := range storageTrie {
|
2020-01-30 22:35:31 +00:00
|
|
|
cids, err := pub.StoragePutter.DagPut(node.Value)
|
2019-04-15 16:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
// Map storage node cids to their state key hashes
|
|
|
|
storageLeafCids[addrKey] = append(storageLeafCids[addrKey], StorageNodeModel{
|
|
|
|
StorageKey: node.Key.Hex(),
|
2020-01-30 22:35:31 +00:00
|
|
|
CID: cids[0],
|
2020-01-17 23:16:01 +00:00
|
|
|
Leaf: node.Leaf,
|
2019-05-17 04:08:53 +00:00
|
|
|
})
|
2019-04-15 16:29:49 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-17 18:26:48 +00:00
|
|
|
return storageLeafCids, nil
|
2019-04-18 12:39:37 +00:00
|
|
|
}
|