2020-04-27 20:36:52 +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 eth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-05-19 20:09:30 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2020-04-27 20:36:52 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core/state"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
"github.com/ethereum/go-ethereum/statediff"
|
|
|
|
"github.com/jmoiron/sqlx"
|
2020-05-12 01:49:42 +00:00
|
|
|
"github.com/multiformats/go-multihash"
|
2020-04-27 20:36:52 +00:00
|
|
|
|
2020-06-22 18:12:32 +00:00
|
|
|
"github.com/vulcanize/ipfs-blockchain-watcher/pkg/ipfs/ipld"
|
|
|
|
"github.com/vulcanize/ipfs-blockchain-watcher/pkg/postgres"
|
|
|
|
"github.com/vulcanize/ipfs-blockchain-watcher/pkg/shared"
|
2020-04-27 20:36:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// IPLDPublisherAndIndexer satisfies the IPLDPublisher interface for ethereum
|
|
|
|
// It interfaces directly with the public.blocks table of PG-IPFS rather than going through an ipfs intermediary
|
|
|
|
// It publishes and indexes IPLDs together in a single sqlx.Tx
|
|
|
|
type IPLDPublisherAndIndexer struct {
|
2020-04-30 01:00:50 +00:00
|
|
|
indexer *CIDIndexer
|
2020-04-27 20:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewIPLDPublisherAndIndexer creates a pointer to a new IPLDPublisherAndIndexer which satisfies the IPLDPublisher interface
|
|
|
|
func NewIPLDPublisherAndIndexer(db *postgres.DB) *IPLDPublisherAndIndexer {
|
|
|
|
return &IPLDPublisherAndIndexer{
|
2020-04-30 01:00:50 +00:00
|
|
|
indexer: NewCIDIndexer(db),
|
2020-04-27 20:36:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish publishes an IPLDPayload to IPFS and returns the corresponding CIDPayload
|
|
|
|
func (pub *IPLDPublisherAndIndexer) Publish(payload shared.ConvertedData) (shared.CIDsForIndexing, error) {
|
|
|
|
ipldPayload, ok := payload.(ConvertedPayload)
|
|
|
|
if !ok {
|
2020-05-12 01:49:42 +00:00
|
|
|
return nil, fmt.Errorf("eth IPLDPublisherAndIndexer expected payload type %T got %T", ConvertedPayload{}, payload)
|
2020-04-27 20:36:52 +00:00
|
|
|
}
|
|
|
|
// Generate the iplds
|
|
|
|
headerNode, uncleNodes, txNodes, txTrieNodes, rctNodes, rctTrieNodes, err := ipld.FromBlockAndReceipts(ipldPayload.Block, ipldPayload.Receipts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Begin new db tx
|
2020-04-30 01:00:50 +00:00
|
|
|
tx, err := pub.indexer.db.Beginx()
|
2020-04-27 20:36:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-01 21:25:58 +00:00
|
|
|
defer func() {
|
|
|
|
if p := recover(); p != nil {
|
|
|
|
shared.Rollback(tx)
|
|
|
|
panic(p)
|
|
|
|
} else if err != nil {
|
|
|
|
shared.Rollback(tx)
|
|
|
|
} else {
|
|
|
|
err = tx.Commit()
|
|
|
|
}
|
|
|
|
}()
|
2020-04-27 20:36:52 +00:00
|
|
|
|
|
|
|
// Publish trie nodes
|
|
|
|
for _, node := range txTrieNodes {
|
2020-04-30 01:00:50 +00:00
|
|
|
if err := shared.PublishIPLD(tx, node); err != nil {
|
2020-04-27 20:36:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, node := range rctTrieNodes {
|
2020-04-30 01:00:50 +00:00
|
|
|
if err := shared.PublishIPLD(tx, node); err != nil {
|
2020-04-27 20:36:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish and index header
|
2020-04-30 01:00:50 +00:00
|
|
|
if err := shared.PublishIPLD(tx, headerNode); err != nil {
|
2020-04-27 20:36:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-30 03:02:47 +00:00
|
|
|
reward := CalcEthBlockReward(ipldPayload.Block.Header(), ipldPayload.Block.Uncles(), ipldPayload.Block.Transactions(), ipldPayload.Receipts)
|
2020-04-27 20:36:52 +00:00
|
|
|
header := HeaderModel{
|
|
|
|
CID: headerNode.Cid().String(),
|
|
|
|
ParentHash: ipldPayload.Block.ParentHash().String(),
|
|
|
|
BlockNumber: ipldPayload.Block.Number().String(),
|
|
|
|
BlockHash: ipldPayload.Block.Hash().String(),
|
|
|
|
TotalDifficulty: ipldPayload.TotalDifficulty.String(),
|
|
|
|
Reward: reward.String(),
|
|
|
|
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-04-30 01:00:50 +00:00
|
|
|
headerID, err := pub.indexer.indexHeaderCID(tx, header)
|
2020-04-27 20:36:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish and index uncles
|
|
|
|
for _, uncleNode := range uncleNodes {
|
2020-04-30 01:00:50 +00:00
|
|
|
if err := shared.PublishIPLD(tx, uncleNode); err != nil {
|
2020-04-27 20:36:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-30 03:02:47 +00:00
|
|
|
uncleReward := CalcUncleMinerReward(ipldPayload.Block.Number().Int64(), uncleNode.Number.Int64())
|
2020-04-27 20:36:52 +00:00
|
|
|
uncle := UncleModel{
|
|
|
|
CID: uncleNode.Cid().String(),
|
|
|
|
ParentHash: uncleNode.ParentHash.String(),
|
|
|
|
BlockHash: uncleNode.Hash().String(),
|
|
|
|
Reward: uncleReward.String(),
|
|
|
|
}
|
2020-04-30 01:00:50 +00:00
|
|
|
if err := pub.indexer.indexUncleCID(tx, uncle, headerID); err != nil {
|
2020-04-27 20:36:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish and index txs and receipts
|
|
|
|
for i, txNode := range txNodes {
|
2020-04-30 01:00:50 +00:00
|
|
|
if err := shared.PublishIPLD(tx, txNode); err != nil {
|
2020-04-27 20:36:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rctNode := rctNodes[i]
|
2020-04-30 01:00:50 +00:00
|
|
|
if err := shared.PublishIPLD(tx, rctNode); err != nil {
|
2020-04-27 20:36:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
txModel := ipldPayload.TxMetaData[i]
|
|
|
|
txModel.CID = txNode.Cid().String()
|
2020-04-30 01:00:50 +00:00
|
|
|
txID, err := pub.indexer.indexTransactionCID(tx, txModel, headerID)
|
2020-04-27 20:36:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rctModel := ipldPayload.ReceiptMetaData[i]
|
|
|
|
rctModel.CID = rctNode.Cid().String()
|
2020-04-30 01:00:50 +00:00
|
|
|
if err := pub.indexer.indexReceiptCID(tx, rctModel, txID); err != nil {
|
2020-04-27 20:36:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish and index state and storage
|
2020-05-01 21:25:58 +00:00
|
|
|
err = pub.publishAndIndexStateAndStorage(tx, ipldPayload, headerID)
|
2020-04-27 20:36:52 +00:00
|
|
|
|
|
|
|
// This IPLDPublisher does both publishing and indexing, we do not need to pass anything forward to the indexer
|
2020-05-02 05:02:05 +00:00
|
|
|
return nil, err // return err variable explicitly so that we return the err = tx.Commit() assignment in the defer
|
2020-04-27 20:36:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (pub *IPLDPublisherAndIndexer) publishAndIndexStateAndStorage(tx *sqlx.Tx, ipldPayload ConvertedPayload, headerID int64) error {
|
|
|
|
// Publish and index state and storage
|
|
|
|
for _, stateNode := range ipldPayload.StateNodes {
|
2020-05-12 01:49:42 +00:00
|
|
|
stateCIDStr, err := shared.PublishRaw(tx, ipld.MEthStateTrie, multihash.KECCAK_256, stateNode.Value)
|
2020-04-27 20:36:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
stateModel := StateNodeModel{
|
|
|
|
Path: stateNode.Path,
|
|
|
|
StateKey: stateNode.LeafKey.String(),
|
2020-05-12 01:49:42 +00:00
|
|
|
CID: stateCIDStr,
|
2020-04-27 20:36:52 +00:00
|
|
|
NodeType: ResolveFromNodeType(stateNode.Type),
|
|
|
|
}
|
2020-04-30 01:00:50 +00:00
|
|
|
stateID, err := pub.indexer.indexStateCID(tx, stateModel, headerID)
|
2020-04-27 20:36:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-12 01:49:42 +00:00
|
|
|
// If we have a leaf, decode and index the account data and any associated storage diffs
|
2020-04-27 20:36:52 +00:00
|
|
|
if stateNode.Type == statediff.Leaf {
|
|
|
|
var i []interface{}
|
|
|
|
if err := rlp.DecodeBytes(stateNode.Value, &i); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(i) != 2 {
|
2020-05-12 01:49:42 +00:00
|
|
|
return fmt.Errorf("eth IPLDPublisherAndIndexer expected state leaf node rlp to decode into two elements")
|
2020-04-27 20:36:52 +00:00
|
|
|
}
|
|
|
|
var account state.Account
|
|
|
|
if err := rlp.DecodeBytes(i[1].([]byte), &account); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
accountModel := StateAccountModel{
|
|
|
|
Balance: account.Balance.String(),
|
|
|
|
Nonce: account.Nonce,
|
|
|
|
CodeHash: account.CodeHash,
|
|
|
|
StorageRoot: account.Root.String(),
|
|
|
|
}
|
2020-04-30 01:00:50 +00:00
|
|
|
if err := pub.indexer.indexStateAccount(tx, accountModel, stateID); err != nil {
|
2020-04-27 20:36:52 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-19 20:09:30 +00:00
|
|
|
for _, storageNode := range ipldPayload.StorageNodes[common.Bytes2Hex(stateNode.Path)] {
|
2020-05-12 01:49:42 +00:00
|
|
|
storageCIDStr, err := shared.PublishRaw(tx, ipld.MEthStorageTrie, multihash.KECCAK_256, storageNode.Value)
|
2020-04-27 20:36:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
storageModel := StorageNodeModel{
|
|
|
|
Path: storageNode.Path,
|
|
|
|
StorageKey: storageNode.LeafKey.Hex(),
|
2020-05-12 01:49:42 +00:00
|
|
|
CID: storageCIDStr,
|
2020-04-27 20:36:52 +00:00
|
|
|
NodeType: ResolveFromNodeType(storageNode.Type),
|
|
|
|
}
|
2020-04-30 01:00:50 +00:00
|
|
|
if err := pub.indexer.indexStorageCID(tx, storageModel, stateID); err != nil {
|
2020-04-27 20:36:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Index satisfies the shared.CIDIndexer interface
|
|
|
|
func (pub *IPLDPublisherAndIndexer) Index(cids shared.CIDsForIndexing) error {
|
|
|
|
return nil
|
|
|
|
}
|