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/>.
|
|
|
|
|
|
|
|
package ipfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-04-18 12:39:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2019-08-05 17:56:15 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2019-05-17 15:23:39 +00:00
|
|
|
"github.com/ipfs/go-ipfs/plugin/loader"
|
2019-04-15 16:29:49 +00:00
|
|
|
|
|
|
|
"github.com/vulcanize/eth-block-extractor/pkg/ipfs"
|
|
|
|
"github.com/vulcanize/eth-block-extractor/pkg/ipfs/eth_block_header"
|
|
|
|
"github.com/vulcanize/eth-block-extractor/pkg/ipfs/eth_block_receipts"
|
|
|
|
"github.com/vulcanize/eth-block-extractor/pkg/ipfs/eth_block_transactions"
|
|
|
|
"github.com/vulcanize/eth-block-extractor/pkg/ipfs/eth_state_trie"
|
|
|
|
"github.com/vulcanize/eth-block-extractor/pkg/ipfs/eth_storage_trie"
|
2019-08-05 17:56:15 +00:00
|
|
|
rlp2 "github.com/vulcanize/eth-block-extractor/pkg/wrappers/rlp"
|
2019-04-15 16:29:49 +00:00
|
|
|
)
|
|
|
|
|
2019-04-30 17:48:31 +00:00
|
|
|
// IPLDPublisher is the interface for publishing an IPLD payload
|
|
|
|
type IPLDPublisher interface {
|
2019-04-15 16:29:49 +00:00
|
|
|
Publish(payload *IPLDPayload) (*CIDPayload, error)
|
|
|
|
}
|
|
|
|
|
2019-04-30 17:48:31 +00:00
|
|
|
// Publisher is the underlying struct for the IPLDPublisher interface
|
|
|
|
type Publisher struct {
|
2019-08-26 02:13:40 +00:00
|
|
|
HeaderPutter ipfs.DagPutter
|
|
|
|
TransactionPutter ipfs.DagPutter
|
|
|
|
ReceiptPutter ipfs.DagPutter
|
|
|
|
StatePutter ipfs.DagPutter
|
|
|
|
StoragePutter ipfs.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
|
|
|
|
func NewIPLDPublisher(ipfsPath string) (*Publisher, error) {
|
2019-06-10 22:06:38 +00:00
|
|
|
l, err := loader.NewPluginLoader("")
|
2019-05-17 15:23:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = l.Initialize()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = l.Inject()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-15 16:29:49 +00:00
|
|
|
node, err := ipfs.InitIPFSNode(ipfsPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-30 17:48:31 +00:00
|
|
|
return &Publisher{
|
2019-08-05 17:56:15 +00:00
|
|
|
HeaderPutter: eth_block_header.NewBlockHeaderDagPutter(node, rlp2.RlpDecoder{}),
|
2019-04-15 16:29:49 +00:00
|
|
|
TransactionPutter: eth_block_transactions.NewBlockTransactionsDagPutter(node),
|
2019-04-18 12:39:37 +00:00
|
|
|
ReceiptPutter: eth_block_receipts.NewEthBlockReceiptDagPutter(node),
|
|
|
|
StatePutter: eth_state_trie.NewStateTrieDagPutter(node),
|
|
|
|
StoragePutter: eth_storage_trie.NewStorageTrieDagPutter(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
|
2019-04-30 17:48:31 +00:00
|
|
|
func (pub *Publisher) Publish(payload *IPLDPayload) (*CIDPayload, error) {
|
2019-04-15 16:29:49 +00:00
|
|
|
// Process and publish headers
|
2019-04-17 18:26:48 +00:00
|
|
|
headerCid, err := pub.publishHeaders(payload.HeaderRLP)
|
2019-04-15 16:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-17 18:26:48 +00:00
|
|
|
|
2019-04-30 17:48:31 +00:00
|
|
|
// Process and publish uncles
|
|
|
|
uncleCids := make(map[common.Hash]string)
|
|
|
|
for _, uncle := range payload.BlockBody.Uncles {
|
2019-08-05 17:56:15 +00:00
|
|
|
uncleRlp, err := rlp.EncodeToBytes(uncle)
|
2019-04-30 17:48:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cid, err := pub.publishHeaders(uncleRlp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
uncleCids[uncle.Hash()] = cid
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
// Process and publish transactions
|
2019-04-30 17:48:31 +00:00
|
|
|
transactionCids, err := pub.publishTransactions(payload.BlockBody, payload.TrxMetaData)
|
2019-04-17 18:26:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process and publish receipts
|
2019-04-30 17:48:31 +00:00
|
|
|
receiptsCids, err := pub.publishReceipts(payload.Receipts, payload.ReceiptMetaData)
|
2019-04-17 18:26:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process and publish state leafs
|
2019-05-17 15:23:39 +00:00
|
|
|
stateNodeCids, err := pub.publishStateNodes(payload.StateNodes)
|
2019-04-17 18:26:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process and publish storage leafs
|
2019-05-17 15:23:39 +00:00
|
|
|
storageNodeCids, err := pub.publishStorageNodes(payload.StorageNodes)
|
2019-04-17 18:26:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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{
|
2019-05-17 04:08:53 +00:00
|
|
|
BlockHash: payload.BlockHash,
|
2019-04-18 12:39:37 +00:00
|
|
|
BlockNumber: payload.BlockNumber.String(),
|
|
|
|
HeaderCID: headerCid,
|
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
|
|
|
|
}
|
|
|
|
|
2019-04-30 17:48:31 +00:00
|
|
|
func (pub *Publisher) publishHeaders(headerRLP []byte) (string, error) {
|
2019-04-17 18:26:48 +00:00
|
|
|
headerCids, err := pub.HeaderPutter.DagPut(headerRLP)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-04-15 16:29:49 +00:00
|
|
|
if len(headerCids) != 1 {
|
2019-04-17 18:26:48 +00:00
|
|
|
return "", errors.New("single CID expected to be returned for header")
|
2019-04-15 16:29:49 +00:00
|
|
|
}
|
2019-04-17 18:26:48 +00:00
|
|
|
return headerCids[0], nil
|
|
|
|
}
|
2019-04-15 16:29:49 +00:00
|
|
|
|
2019-04-30 17:48:31 +00:00
|
|
|
func (pub *Publisher) publishTransactions(blockBody *types.Body, trxMeta []*TrxMetaData) (map[common.Hash]*TrxMetaData, error) {
|
2019-04-17 18:26:48 +00:00
|
|
|
transactionCids, err := pub.TransactionPutter.DagPut(blockBody)
|
2019-04-15 16:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-04-17 18:26:48 +00:00
|
|
|
if len(transactionCids) != len(blockBody.Transactions) {
|
2019-04-15 16:29:49 +00:00
|
|
|
return nil, errors.New("expected one CID for each transaction")
|
|
|
|
}
|
2019-04-30 17:48:31 +00:00
|
|
|
mappedTrxCids := make(map[common.Hash]*TrxMetaData, len(transactionCids))
|
2019-04-17 18:26:48 +00:00
|
|
|
for i, trx := range blockBody.Transactions {
|
2019-04-30 17:48:31 +00:00
|
|
|
mappedTrxCids[trx.Hash()] = trxMeta[i]
|
|
|
|
mappedTrxCids[trx.Hash()].CID = transactionCids[i]
|
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
|
|
|
|
2019-04-30 17:48:31 +00:00
|
|
|
func (pub *Publisher) publishReceipts(receipts types.Receipts, receiptMeta []*ReceiptMetaData) (map[common.Hash]*ReceiptMetaData, 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")
|
|
|
|
}
|
2019-06-20 15:59:10 +00:00
|
|
|
// Keep receipts associated with their transaction
|
2019-04-30 17:48:31 +00:00
|
|
|
mappedRctCids := make(map[common.Hash]*ReceiptMetaData, len(receiptsCids))
|
2019-04-17 18:26:48 +00:00
|
|
|
for i, rct := range receipts {
|
2019-04-30 17:48:31 +00:00
|
|
|
mappedRctCids[rct.TxHash] = receiptMeta[i]
|
|
|
|
mappedRctCids[rct.TxHash].CID = receiptsCids[i]
|
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
|
|
|
|
2019-05-17 04:08:53 +00:00
|
|
|
func (pub *Publisher) publishStateNodes(stateNodes map[common.Hash]StateNode) (map[common.Hash]StateNodeCID, error) {
|
|
|
|
stateNodeCids := make(map[common.Hash]StateNodeCID)
|
2019-08-28 19:43:27 +00:00
|
|
|
for addrKey, node := range stateNodes {
|
2019-05-17 04:08:53 +00:00
|
|
|
stateNodeCid, err := pub.StatePutter.DagPut(node.Value)
|
2019-04-15 16:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-17 04:08:53 +00:00
|
|
|
if len(stateNodeCid) != 1 {
|
2019-04-15 16:29:49 +00:00
|
|
|
return nil, errors.New("single CID expected to be returned for state leaf")
|
|
|
|
}
|
2019-08-28 19:43:27 +00:00
|
|
|
stateNodeCids[addrKey] = StateNodeCID{
|
2019-05-17 04:08:53 +00:00
|
|
|
CID: stateNodeCid[0],
|
|
|
|
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
|
|
|
|
2019-05-17 04:08:53 +00:00
|
|
|
func (pub *Publisher) publishStorageNodes(storageNodes map[common.Hash][]StorageNode) (map[common.Hash][]StorageNodeCID, error) {
|
|
|
|
storageLeafCids := make(map[common.Hash][]StorageNodeCID)
|
2019-08-28 19:43:27 +00:00
|
|
|
for addrKey, storageTrie := range storageNodes {
|
|
|
|
storageLeafCids[addrKey] = make([]StorageNodeCID, 0, len(storageTrie))
|
2019-05-17 04:08:53 +00:00
|
|
|
for _, node := range storageTrie {
|
|
|
|
storageNodeCid, err := pub.StoragePutter.DagPut(node.Value)
|
2019-04-15 16:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-17 04:08:53 +00:00
|
|
|
if len(storageNodeCid) != 1 {
|
2019-04-15 16:29:49 +00:00
|
|
|
return nil, errors.New("single CID expected to be returned for storage leaf")
|
|
|
|
}
|
2019-08-28 19:43:27 +00:00
|
|
|
storageLeafCids[addrKey] = append(storageLeafCids[addrKey], StorageNodeCID{
|
2019-05-21 19:27:24 +00:00
|
|
|
Key: node.Key.Hex(),
|
2019-05-17 04:08:53 +00:00
|
|
|
CID: storageNodeCid[0],
|
|
|
|
Leaf: node.Leaf,
|
|
|
|
})
|
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
|
|
|
}
|