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"
|
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2019-04-15 16:29:49 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
"github.com/vulcanize/eth-block-extractor/pkg/wrappers/rlp"
|
|
|
|
)
|
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
// Publisher is the interface for publishing an IPLD payload
|
2019-04-15 16:29:49 +00:00
|
|
|
type Publisher interface {
|
|
|
|
Publish(payload *IPLDPayload) (*CIDPayload, error)
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
// IPLDPublisher is the underlying struct for the Publisher interface
|
2019-04-15 16:29:49 +00:00
|
|
|
type IPLDPublisher struct {
|
|
|
|
Node *ipfs.IPFS
|
|
|
|
HeaderPutter *eth_block_header.BlockHeaderDagPutter
|
|
|
|
TransactionPutter *eth_block_transactions.BlockTransactionsDagPutter
|
|
|
|
ReceiptPutter *eth_block_receipts.EthBlockReceiptDagPutter
|
|
|
|
StatePutter *eth_state_trie.StateTrieDagPutter
|
|
|
|
StoragePutter *eth_storage_trie.StorageTrieDagPutter
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
// CID payload is a struct to hold all the CIDs and their meta data
|
2019-04-15 16:29:49 +00:00
|
|
|
type CIDPayload struct {
|
|
|
|
BlockNumber string
|
|
|
|
BlockHash string
|
|
|
|
HeaderCID string
|
|
|
|
TransactionCIDs map[common.Hash]string
|
|
|
|
ReceiptCIDs map[common.Hash]string
|
|
|
|
StateLeafCIDs map[common.Hash]string
|
|
|
|
StorageLeafCIDs map[common.Hash]map[common.Hash]string
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
// NewIPLDPublisher creates a pointer to a new IPLDPublisher which satisfies the Publisher interface
|
2019-04-15 16:29:49 +00:00
|
|
|
func NewIPLDPublisher(ipfsPath string) (*IPLDPublisher, error) {
|
|
|
|
node, err := ipfs.InitIPFSNode(ipfsPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
decoder := rlp.RlpDecoder{}
|
|
|
|
return &IPLDPublisher{
|
|
|
|
Node: node,
|
|
|
|
HeaderPutter: eth_block_header.NewBlockHeaderDagPutter(node, decoder),
|
|
|
|
TransactionPutter: eth_block_transactions.NewBlockTransactionsDagPutter(node),
|
|
|
|
ReceiptPutter: eth_block_receipts.NewEthBlockReceiptDagPutter(node),
|
|
|
|
StatePutter: eth_state_trie.NewStateTrieDagPutter(node),
|
|
|
|
StoragePutter: eth_storage_trie.NewStorageTrieDagPutter(node),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
// Publish publishes an IPLDPayload to IPFS and returns the corresponding CIDPayload
|
2019-04-15 16:29:49 +00:00
|
|
|
func (pub *IPLDPublisher) Publish(payload *IPLDPayload) (*CIDPayload, error) {
|
|
|
|
// 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
|
|
|
|
|
|
|
// Process and publish transactions
|
|
|
|
transactionCids, err := pub.publishTransactions(payload.BlockBody)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process and publish receipts
|
|
|
|
receiptsCids, err := pub.publishReceipts(payload.Receipts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process and publish state leafs
|
|
|
|
stateLeafCids, err := pub.publishStateLeafs(payload.StateLeafs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Process and publish storage leafs
|
|
|
|
storageLeafCids, err := pub.publishStorageLeafs(payload.StorageLeafs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Package CIDs into a single struct
|
|
|
|
return &CIDPayload{
|
|
|
|
BlockHash: payload.BlockHash.Hex(),
|
|
|
|
BlockNumber: payload.BlockNumber.String(),
|
|
|
|
HeaderCID: headerCid,
|
|
|
|
TransactionCIDs: transactionCids,
|
|
|
|
ReceiptCIDs: receiptsCids,
|
|
|
|
StateLeafCIDs: stateLeafCids,
|
|
|
|
StorageLeafCIDs: storageLeafCids,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pub *IPLDPublisher) publishHeaders(headerRLP []byte) (string, error) {
|
|
|
|
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-17 18:26:48 +00:00
|
|
|
func (pub *IPLDPublisher) publishTransactions(blockBody *types.Body) (map[common.Hash]string, error) {
|
|
|
|
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-17 18:26:48 +00:00
|
|
|
mappedTrxCids := make(map[common.Hash]string, len(transactionCids))
|
|
|
|
for i, trx := range blockBody.Transactions {
|
|
|
|
mappedTrxCids[trx.Hash()] = 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-17 18:26:48 +00:00
|
|
|
func (pub *IPLDPublisher) publishReceipts(receipts types.Receipts) (map[common.Hash]string, error) {
|
|
|
|
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-04-17 18:26:48 +00:00
|
|
|
mappedRctCids := make(map[common.Hash]string, len(receiptsCids))
|
|
|
|
for i, rct := range receipts {
|
|
|
|
mappedRctCids[rct.TxHash] = 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-04-17 18:26:48 +00:00
|
|
|
func (pub *IPLDPublisher) publishStateLeafs(stateLeafs map[common.Hash][]byte) (map[common.Hash]string, error) {
|
2019-04-15 16:29:49 +00:00
|
|
|
stateLeafCids := make(map[common.Hash]string)
|
2019-04-17 18:26:48 +00:00
|
|
|
for addr, leaf := range stateLeafs {
|
2019-04-15 16:29:49 +00:00
|
|
|
stateLeafCid, err := pub.StatePutter.DagPut(leaf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(stateLeafCid) != 1 {
|
|
|
|
return nil, errors.New("single CID expected to be returned for state leaf")
|
|
|
|
}
|
|
|
|
stateLeafCids[addr] = stateLeafCid[0]
|
|
|
|
}
|
2019-04-17 18:26:48 +00:00
|
|
|
return stateLeafCids, nil
|
|
|
|
}
|
2019-04-15 16:29:49 +00:00
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
func (pub *IPLDPublisher) publishStorageLeafs(storageLeafs map[common.Hash]map[common.Hash][]byte) (map[common.Hash]map[common.Hash]string, error) {
|
2019-04-15 16:29:49 +00:00
|
|
|
storageLeafCids := make(map[common.Hash]map[common.Hash]string)
|
2019-04-17 18:26:48 +00:00
|
|
|
for addr, storageTrie := range storageLeafs {
|
2019-04-15 16:29:49 +00:00
|
|
|
storageLeafCids[addr] = make(map[common.Hash]string)
|
|
|
|
for key, leaf := range storageTrie {
|
|
|
|
storageLeafCid, err := pub.StoragePutter.DagPut(leaf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(storageLeafCid) != 1 {
|
|
|
|
return nil, errors.New("single CID expected to be returned for storage leaf")
|
|
|
|
}
|
|
|
|
storageLeafCids[addr][key] = storageLeafCid[0]
|
|
|
|
}
|
|
|
|
}
|
2019-04-17 18:26:48 +00:00
|
|
|
return storageLeafCids, nil
|
2019-04-15 16:29:49 +00:00
|
|
|
}
|