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 (
|
2020-01-17 23:16:01 +00:00
|
|
|
"fmt"
|
2019-04-15 16:29:49 +00:00
|
|
|
|
2020-03-22 00:50:12 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
|
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-03-22 00:50:12 +00:00
|
|
|
"github.com/ethereum/go-ethereum/statediff"
|
2020-02-20 22:13:19 +00:00
|
|
|
|
|
|
|
common2 "github.com/vulcanize/vulcanizedb/pkg/eth/converters/common"
|
2020-01-30 22:35:31 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/ipfs"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/ipfs/dag_putters"
|
2020-03-17 18:05:19 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/ipfs/ipld"
|
2020-02-20 22:13:19 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/shared"
|
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-04-17 21:59:25 +00:00
|
|
|
HeaderPutter ipfs.DagPutter
|
|
|
|
TransactionPutter ipfs.DagPutter
|
|
|
|
TransactionTriePutter ipfs.DagPutter
|
|
|
|
ReceiptPutter ipfs.DagPutter
|
|
|
|
ReceiptTriePutter ipfs.DagPutter
|
|
|
|
StatePutter ipfs.DagPutter
|
|
|
|
StoragePutter ipfs.DagPutter
|
2019-04-15 16:29:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-27 20:36:52 +00:00
|
|
|
// NewIPLDPublisher creates a pointer to a new IPLDPublisher 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-03-17 18:05:19 +00:00
|
|
|
HeaderPutter: dag_putters.NewEthBlockHeaderDagPutter(node),
|
|
|
|
TransactionPutter: dag_putters.NewEthTxsDagPutter(node),
|
|
|
|
TransactionTriePutter: dag_putters.NewEthTxTrieDagPutter(node),
|
|
|
|
ReceiptPutter: dag_putters.NewEthReceiptDagPutter(node),
|
|
|
|
ReceiptTriePutter: dag_putters.NewEthRctTrieDagPutter(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-02-20 22:12:52 +00:00
|
|
|
func (pub *IPLDPublisher) Publish(payload shared.ConvertedData) (shared.CIDsForIndexing, error) {
|
|
|
|
ipldPayload, ok := payload.(ConvertedPayload)
|
2020-01-17 23:16:01 +00:00
|
|
|
if !ok {
|
2020-02-20 22:12:52 +00:00
|
|
|
return nil, fmt.Errorf("eth publisher expected payload type %T got %T", ConvertedPayload{}, payload)
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2020-03-17 18:05:19 +00:00
|
|
|
// Generate the nodes for publishing
|
|
|
|
headerNode, uncleNodes, txNodes, txTrieNodes, rctNodes, rctTrieNodes, err := ipld.FromBlockAndReceipts(ipldPayload.Block, ipldPayload.Receipts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-15 16:29:49 +00:00
|
|
|
// Process and publish headers
|
2020-03-17 18:05:19 +00:00
|
|
|
headerCid, err := pub.publishHeader(headerNode)
|
2020-01-16 23:21:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-04-15 16:29:49 +00:00
|
|
|
}
|
2020-02-27 21:07:33 +00:00
|
|
|
reward := common2.CalcEthBlockReward(ipldPayload.Block.Header(), ipldPayload.Block.Uncles(), ipldPayload.Block.Transactions(), ipldPayload.Receipts)
|
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(),
|
2020-02-20 22:13:19 +00:00
|
|
|
Reward: reward.String(),
|
2020-03-22 00:50:12 +00:00
|
|
|
Bloom: ipldPayload.Block.Bloom().Bytes(),
|
|
|
|
StateRoot: ipldPayload.Block.Root().String(),
|
|
|
|
RctRoot: ipldPayload.Block.ReceiptHash().String(),
|
|
|
|
TxRoot: ipldPayload.Block.TxHash().String(),
|
|
|
|
UncleRoot: ipldPayload.Block.UncleHash().String(),
|
|
|
|
Timestamp: ipldPayload.Block.Time(),
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2019-04-17 18:26:48 +00:00
|
|
|
|
2019-04-30 17:48:31 +00:00
|
|
|
// Process and publish uncles
|
2020-03-17 18:05:19 +00:00
|
|
|
uncleCids := make([]UncleModel, len(uncleNodes))
|
|
|
|
for i, uncle := range uncleNodes {
|
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-02-20 22:13:19 +00:00
|
|
|
uncleReward := common2.CalcUncleMinerReward(ipldPayload.Block.Number().Int64(), uncle.Number.Int64())
|
2020-03-17 18:05:19 +00:00
|
|
|
uncleCids[i] = UncleModel{
|
2020-01-26 19:55:26 +00:00
|
|
|
CID: uncleCid,
|
|
|
|
ParentHash: uncle.ParentHash.String(),
|
|
|
|
BlockHash: uncle.Hash().String(),
|
2020-02-20 22:13:19 +00:00
|
|
|
Reward: uncleReward.String(),
|
2020-03-17 18:05:19 +00:00
|
|
|
}
|
2019-04-30 17:48:31 +00:00
|
|
|
}
|
|
|
|
|
2019-04-17 18:26:48 +00:00
|
|
|
// Process and publish transactions
|
2020-03-17 18:05:19 +00:00
|
|
|
transactionCids, err := pub.publishTransactions(txNodes, txTrieNodes, 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-03-17 18:05:19 +00:00
|
|
|
receiptsCids, err := pub.publishReceipts(rctNodes, rctTrieNodes, 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-03-22 00:50:12 +00:00
|
|
|
stateNodeCids, stateAccounts, 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,
|
2020-03-22 00:50:12 +00:00
|
|
|
StateAccounts: stateAccounts,
|
2019-04-17 18:26:48 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:05:19 +00:00
|
|
|
func (pub *IPLDPublisher) generateBlockNodes(body *types.Block, receipts types.Receipts) (*ipld.EthHeader,
|
|
|
|
[]*ipld.EthHeader, []*ipld.EthTx, []*ipld.EthTxTrie, []*ipld.EthReceipt, []*ipld.EthRctTrie, error) {
|
|
|
|
return ipld.FromBlockAndReceipts(body, receipts)
|
2019-04-17 18:26:48 +00:00
|
|
|
}
|
2019-04-15 16:29:49 +00:00
|
|
|
|
2020-03-17 18:05:19 +00:00
|
|
|
func (pub *IPLDPublisher) publishHeader(header *ipld.EthHeader) (string, error) {
|
|
|
|
return pub.HeaderPutter.DagPut(header)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pub *IPLDPublisher) publishTransactions(transactions []*ipld.EthTx, txTrie []*ipld.EthTxTrie, trxMeta []TxModel) ([]TxModel, error) {
|
|
|
|
trxCids := make([]TxModel, len(transactions))
|
|
|
|
for i, tx := range transactions {
|
|
|
|
cid, err := pub.TransactionPutter.DagPut(tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
trxCids[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
|
|
|
}
|
2020-03-17 18:05:19 +00:00
|
|
|
for _, txNode := range txTrie {
|
|
|
|
// We don't do anything with the tx trie cids atm
|
|
|
|
if _, err := pub.TransactionTriePutter.DagPut(txNode); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return trxCids, nil
|
2019-04-17 18:26:48 +00:00
|
|
|
}
|
2019-04-15 16:29:49 +00:00
|
|
|
|
2020-03-17 18:05:19 +00:00
|
|
|
func (pub *IPLDPublisher) publishReceipts(receipts []*ipld.EthReceipt, receiptTrie []*ipld.EthRctTrie, receiptMeta []ReceiptModel) (map[common.Hash]ReceiptModel, error) {
|
|
|
|
rctCids := make(map[common.Hash]ReceiptModel)
|
2019-04-17 18:26:48 +00:00
|
|
|
for i, rct := range receipts {
|
2020-03-17 18:05:19 +00:00
|
|
|
cid, err := pub.ReceiptPutter.DagPut(rct)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rctCids[rct.TxHash] = ReceiptModel{
|
2020-04-02 03:34:06 +00:00
|
|
|
CID: cid,
|
2020-04-04 19:56:11 +00:00
|
|
|
Contract: receiptMeta[i].Contract,
|
2020-04-03 20:15:06 +00:00
|
|
|
ContractHash: receiptMeta[i].ContractHash,
|
2020-04-02 03:34:06 +00:00
|
|
|
Topic0s: receiptMeta[i].Topic0s,
|
|
|
|
Topic1s: receiptMeta[i].Topic1s,
|
|
|
|
Topic2s: receiptMeta[i].Topic2s,
|
|
|
|
Topic3s: receiptMeta[i].Topic3s,
|
|
|
|
LogContracts: receiptMeta[i].LogContracts,
|
2020-01-17 23:16:01 +00:00
|
|
|
}
|
2019-04-15 16:29:49 +00:00
|
|
|
}
|
2020-03-17 18:05:19 +00:00
|
|
|
for _, rctNode := range receiptTrie {
|
|
|
|
// We don't do anything with the rct trie cids atm
|
|
|
|
if _, err := pub.ReceiptTriePutter.DagPut(rctNode); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rctCids, nil
|
2019-04-17 18:26:48 +00:00
|
|
|
}
|
2019-04-15 16:29:49 +00:00
|
|
|
|
2020-03-22 00:50:12 +00:00
|
|
|
func (pub *IPLDPublisher) publishStateNodes(stateNodes []TrieNode) ([]StateNodeModel, map[common.Hash]StateAccountModel, error) {
|
2020-01-17 23:16:01 +00:00
|
|
|
stateNodeCids := make([]StateNodeModel, 0, len(stateNodes))
|
2020-03-22 00:50:12 +00:00
|
|
|
stateAccounts := make(map[common.Hash]StateAccountModel)
|
2020-03-17 18:05:19 +00:00
|
|
|
for _, stateNode := range stateNodes {
|
|
|
|
node, err := ipld.FromStateTrieRLP(stateNode.Value)
|
|
|
|
if err != nil {
|
2020-03-22 00:50:12 +00:00
|
|
|
return nil, nil, err
|
2020-03-17 18:05:19 +00:00
|
|
|
}
|
|
|
|
cid, err := pub.StatePutter.DagPut(node)
|
2019-04-15 16:29:49 +00:00
|
|
|
if err != nil {
|
2020-03-22 00:50:12 +00:00
|
|
|
return nil, nil, err
|
2019-04-15 16:29:49 +00:00
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
stateNodeCids = append(stateNodeCids, StateNodeModel{
|
2020-03-17 18:05:19 +00:00
|
|
|
Path: stateNode.Path,
|
|
|
|
StateKey: stateNode.LeafKey.String(),
|
|
|
|
CID: cid,
|
|
|
|
NodeType: ResolveFromNodeType(stateNode.Type),
|
2020-01-17 23:16:01 +00:00
|
|
|
})
|
2020-03-22 00:50:12 +00:00
|
|
|
// If we have a leaf, decode the account to extract additional metadata for indexing
|
|
|
|
if stateNode.Type == statediff.Leaf {
|
|
|
|
var i []interface{}
|
|
|
|
if err := rlp.DecodeBytes(stateNode.Value, &i); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if len(i) != 2 {
|
|
|
|
return nil, nil, fmt.Errorf("IPLDPublisher expected state leaf node rlp to decode into two elements")
|
|
|
|
}
|
|
|
|
var account state.Account
|
|
|
|
if err := rlp.DecodeBytes(i[1].([]byte), &account); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
// Map state account to the state path hash
|
|
|
|
statePathHash := crypto.Keccak256Hash(stateNode.Path)
|
|
|
|
stateAccounts[statePathHash] = StateAccountModel{
|
|
|
|
Balance: account.Balance.String(),
|
|
|
|
Nonce: account.Nonce,
|
|
|
|
CodeHash: account.CodeHash,
|
|
|
|
StorageRoot: account.Root.String(),
|
|
|
|
}
|
|
|
|
}
|
2019-04-15 16:29:49 +00:00
|
|
|
}
|
2020-03-22 00:50:12 +00:00
|
|
|
return stateNodeCids, stateAccounts, 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)
|
2020-03-11 18:41:59 +00:00
|
|
|
for pathHash, storageTrie := range storageNodes {
|
|
|
|
storageLeafCids[pathHash] = make([]StorageNodeModel, 0, len(storageTrie))
|
2020-03-17 18:05:19 +00:00
|
|
|
for _, storageNode := range storageTrie {
|
|
|
|
node, err := ipld.FromStorageTrieRLP(storageNode.Value)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cid, err := pub.StoragePutter.DagPut(node)
|
2019-04-15 16:29:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-22 00:50:12 +00:00
|
|
|
// Map storage node cids to the state path hash
|
2020-03-11 18:41:59 +00:00
|
|
|
storageLeafCids[pathHash] = append(storageLeafCids[pathHash], StorageNodeModel{
|
2020-03-17 18:05:19 +00:00
|
|
|
Path: storageNode.Path,
|
|
|
|
StorageKey: storageNode.LeafKey.Hex(),
|
|
|
|
CID: cid,
|
|
|
|
NodeType: ResolveFromNodeType(storageNode.Type),
|
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
|
|
|
}
|