2019-05-01 17:49:56 +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-05-01 17:49:56 +00:00
|
|
|
|
|
|
|
import (
|
2020-01-16 20:48:38 +00:00
|
|
|
"errors"
|
2020-01-17 23:16:01 +00:00
|
|
|
"fmt"
|
2020-02-27 21:07:33 +00:00
|
|
|
"math/big"
|
2019-05-01 17:49:56 +00:00
|
|
|
|
2019-05-21 19:27:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-08-31 11:32:01 +00:00
|
|
|
"github.com/ethereum/go-ethereum/statediff/indexer/ipfs"
|
2021-08-12 06:23:41 +00:00
|
|
|
"github.com/ethereum/go-ethereum/statediff/indexer/models"
|
|
|
|
"github.com/ethereum/go-ethereum/statediff/indexer/postgres"
|
2020-08-10 17:46:03 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2019-05-21 19:27:24 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-08-31 15:47:06 +00:00
|
|
|
"github.com/vulcanize/ipld-eth-server/pkg/shared"
|
2019-05-01 17:49:56 +00:00
|
|
|
)
|
|
|
|
|
2020-08-31 15:47:06 +00:00
|
|
|
// Fetcher interface for substituting mocks in tests
|
|
|
|
type Fetcher interface {
|
2020-10-20 14:42:09 +00:00
|
|
|
Fetch(cids CIDWrapper) (*IPLDs, error)
|
2020-08-31 15:47:06 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 18:04:07 +00:00
|
|
|
// IPLDFetcher satisfies the IPLDFetcher interface for ethereum
|
2020-08-10 17:46:03 +00:00
|
|
|
// It interfaces directly with PG-IPFS
|
2020-08-10 18:04:07 +00:00
|
|
|
type IPLDFetcher struct {
|
2020-08-10 17:46:03 +00:00
|
|
|
db *postgres.DB
|
2019-05-01 17:49:56 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 18:04:07 +00:00
|
|
|
// NewIPLDFetcher creates a pointer to a new IPLDFetcher
|
|
|
|
func NewIPLDFetcher(db *postgres.DB) *IPLDFetcher {
|
|
|
|
return &IPLDFetcher{
|
2020-08-10 17:46:03 +00:00
|
|
|
db: db,
|
2019-05-01 17:49:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// Fetch is the exported method for fetching and returning all the IPLDS specified in the CIDWrapper
|
2020-10-20 14:42:09 +00:00
|
|
|
func (f *IPLDFetcher) Fetch(cids CIDWrapper) (*IPLDs, error) {
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("fetching iplds")
|
2020-10-20 14:42:09 +00:00
|
|
|
iplds := new(IPLDs)
|
2020-08-31 15:47:06 +00:00
|
|
|
var ok bool
|
|
|
|
iplds.TotalDifficulty, ok = new(big.Int).SetString(cids.Header.TotalDifficulty, 10)
|
2020-02-27 21:07:33 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("eth fetcher: unable to set total difficulty")
|
|
|
|
}
|
2020-08-31 15:47:06 +00:00
|
|
|
iplds.BlockNumber = cids.BlockNumber
|
2020-08-10 17:46:03 +00:00
|
|
|
|
|
|
|
tx, err := f.db.Beginx()
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-08-10 17:46:03 +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-08-31 15:47:06 +00:00
|
|
|
iplds.Header, err = f.FetchHeader(tx, cids.Header)
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
2020-08-10 17:46:03 +00:00
|
|
|
return nil, fmt.Errorf("eth pg fetcher: header fetching error: %s", err.Error())
|
2019-06-18 17:28:57 +00:00
|
|
|
}
|
2020-08-31 15:47:06 +00:00
|
|
|
iplds.Uncles, err = f.FetchUncles(tx, cids.Uncles)
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
2020-08-10 17:46:03 +00:00
|
|
|
return nil, fmt.Errorf("eth pg fetcher: uncle fetching error: %s", err.Error())
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-08-31 15:47:06 +00:00
|
|
|
iplds.Transactions, err = f.FetchTrxs(tx, cids.Transactions)
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
2020-08-10 17:46:03 +00:00
|
|
|
return nil, fmt.Errorf("eth pg fetcher: transaction fetching error: %s", err.Error())
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-08-31 15:47:06 +00:00
|
|
|
iplds.Receipts, err = f.FetchRcts(tx, cids.Receipts)
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
2020-08-10 17:46:03 +00:00
|
|
|
return nil, fmt.Errorf("eth pg fetcher: receipt fetching error: %s", err.Error())
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-08-31 15:47:06 +00:00
|
|
|
iplds.StateNodes, err = f.FetchState(tx, cids.StateNodes)
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
2020-08-10 17:46:03 +00:00
|
|
|
return nil, fmt.Errorf("eth pg fetcher: state fetching error: %s", err.Error())
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-08-31 15:47:06 +00:00
|
|
|
iplds.StorageNodes, err = f.FetchStorage(tx, cids.StorageNodes)
|
2020-08-10 17:46:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("eth pg fetcher: storage fetching error: %s", err.Error())
|
|
|
|
}
|
|
|
|
return iplds, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
// FetchHeaders fetches headers
|
2021-08-12 06:23:41 +00:00
|
|
|
func (f *IPLDFetcher) FetchHeader(tx *sqlx.Tx, c models.HeaderModel) (ipfs.BlockModel, error) {
|
2020-02-23 23:14:29 +00:00
|
|
|
log.Debug("fetching header ipld")
|
2020-08-10 17:46:03 +00:00
|
|
|
headerBytes, err := shared.FetchIPLDByMhKey(tx, c.MhKey)
|
2020-02-23 23:14:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return ipfs.BlockModel{}, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-02-23 23:14:29 +00:00
|
|
|
return ipfs.BlockModel{
|
2020-08-10 17:46:03 +00:00
|
|
|
Data: headerBytes,
|
|
|
|
CID: c.CID,
|
2020-02-23 23:14:29 +00:00
|
|
|
}, nil
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
// FetchUncles fetches uncles
|
2021-08-12 06:23:41 +00:00
|
|
|
func (f *IPLDFetcher) FetchUncles(tx *sqlx.Tx, cids []models.UncleModel) ([]ipfs.BlockModel, error) {
|
2019-06-18 17:28:57 +00:00
|
|
|
log.Debug("fetching uncle iplds")
|
2020-08-10 17:46:03 +00:00
|
|
|
uncleIPLDs := make([]ipfs.BlockModel, len(cids))
|
2020-02-20 22:12:52 +00:00
|
|
|
for i, c := range cids {
|
2020-08-10 17:46:03 +00:00
|
|
|
uncleBytes, err := shared.FetchIPLDByMhKey(tx, c.MhKey)
|
2019-06-18 17:28:57 +00:00
|
|
|
if err != nil {
|
2020-01-16 20:48:38 +00:00
|
|
|
return nil, err
|
2019-06-18 17:28:57 +00:00
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
uncleIPLDs[i] = ipfs.BlockModel{
|
2020-08-10 17:46:03 +00:00
|
|
|
Data: uncleBytes,
|
|
|
|
CID: c.CID,
|
2020-02-20 22:13:19 +00:00
|
|
|
}
|
2020-02-20 22:12:52 +00:00
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
return uncleIPLDs, nil
|
2019-06-18 17:28:57 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
// FetchTrxs fetches transactions
|
2021-08-12 06:23:41 +00:00
|
|
|
func (f *IPLDFetcher) FetchTrxs(tx *sqlx.Tx, cids []models.TxModel) ([]ipfs.BlockModel, error) {
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("fetching transaction iplds")
|
2020-08-10 17:46:03 +00:00
|
|
|
trxIPLDs := make([]ipfs.BlockModel, len(cids))
|
2020-02-20 22:12:52 +00:00
|
|
|
for i, c := range cids {
|
2020-08-10 17:46:03 +00:00
|
|
|
txBytes, err := shared.FetchIPLDByMhKey(tx, c.MhKey)
|
2019-05-21 19:27:24 +00:00
|
|
|
if err != nil {
|
2020-01-16 20:48:38 +00:00
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
trxIPLDs[i] = ipfs.BlockModel{
|
2020-08-10 17:46:03 +00:00
|
|
|
Data: txBytes,
|
|
|
|
CID: c.CID,
|
2020-02-20 22:13:19 +00:00
|
|
|
}
|
2020-02-20 22:12:52 +00:00
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
return trxIPLDs, nil
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
// FetchRcts fetches receipts
|
2021-08-12 06:23:41 +00:00
|
|
|
func (f *IPLDFetcher) FetchRcts(tx *sqlx.Tx, cids []models.ReceiptModel) ([]ipfs.BlockModel, error) {
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("fetching receipt iplds")
|
2020-08-10 17:46:03 +00:00
|
|
|
rctIPLDs := make([]ipfs.BlockModel, len(cids))
|
2020-02-20 22:12:52 +00:00
|
|
|
for i, c := range cids {
|
2021-09-16 12:49:42 +00:00
|
|
|
rctBytes, err := shared.FetchIPLDByMhKey(tx, c.LeafMhKey)
|
2019-05-21 19:27:24 +00:00
|
|
|
if err != nil {
|
2020-01-16 20:48:38 +00:00
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2021-09-16 12:49:42 +00:00
|
|
|
//nodeVal, err := DecodeLeafNode(rctBytes)
|
2020-02-20 22:13:19 +00:00
|
|
|
rctIPLDs[i] = ipfs.BlockModel{
|
2020-08-10 17:46:03 +00:00
|
|
|
Data: rctBytes,
|
2021-09-16 12:49:42 +00:00
|
|
|
CID: c.LeafCID,
|
2020-02-20 22:13:19 +00:00
|
|
|
}
|
2020-02-20 22:12:52 +00:00
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
return rctIPLDs, nil
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
// FetchState fetches state nodes
|
2021-08-12 06:23:41 +00:00
|
|
|
func (f *IPLDFetcher) FetchState(tx *sqlx.Tx, cids []models.StateNodeModel) ([]StateNode, error) {
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("fetching state iplds")
|
2020-10-20 14:42:09 +00:00
|
|
|
stateNodes := make([]StateNode, 0, len(cids))
|
2020-04-30 21:26:32 +00:00
|
|
|
for _, stateNode := range cids {
|
|
|
|
if stateNode.CID == "" {
|
2019-05-21 19:27:24 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-08-10 17:46:03 +00:00
|
|
|
stateBytes, err := shared.FetchIPLDByMhKey(tx, stateNode.MhKey)
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-10-20 14:42:09 +00:00
|
|
|
stateNodes = append(stateNodes, StateNode{
|
2020-02-20 22:13:19 +00:00
|
|
|
IPLD: ipfs.BlockModel{
|
2020-08-10 17:46:03 +00:00
|
|
|
Data: stateBytes,
|
|
|
|
CID: stateNode.CID,
|
2020-02-20 22:13:19 +00:00
|
|
|
},
|
2020-03-11 18:41:59 +00:00
|
|
|
StateLeafKey: common.HexToHash(stateNode.StateKey),
|
|
|
|
Type: ResolveToNodeType(stateNode.NodeType),
|
|
|
|
Path: stateNode.Path,
|
2020-04-30 21:26:32 +00:00
|
|
|
})
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
return stateNodes, nil
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
// FetchStorage fetches storage nodes
|
2021-08-12 06:23:41 +00:00
|
|
|
func (f *IPLDFetcher) FetchStorage(tx *sqlx.Tx, cids []models.StorageNodeWithStateKeyModel) ([]StorageNode, error) {
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("fetching storage iplds")
|
2020-10-20 14:42:09 +00:00
|
|
|
storageNodes := make([]StorageNode, 0, len(cids))
|
2020-04-30 21:26:32 +00:00
|
|
|
for _, storageNode := range cids {
|
|
|
|
if storageNode.CID == "" || storageNode.StateKey == "" {
|
2019-05-21 19:27:24 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-08-10 17:46:03 +00:00
|
|
|
storageBytes, err := shared.FetchIPLDByMhKey(tx, storageNode.MhKey)
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-10-20 14:42:09 +00:00
|
|
|
storageNodes = append(storageNodes, StorageNode{
|
2020-02-20 22:13:19 +00:00
|
|
|
IPLD: ipfs.BlockModel{
|
2020-08-10 17:46:03 +00:00
|
|
|
Data: storageBytes,
|
|
|
|
CID: storageNode.CID,
|
2020-02-20 22:13:19 +00:00
|
|
|
},
|
2020-03-11 18:41:59 +00:00
|
|
|
StateLeafKey: common.HexToHash(storageNode.StateKey),
|
|
|
|
StorageLeafKey: common.HexToHash(storageNode.StorageKey),
|
|
|
|
Type: ResolveToNodeType(storageNode.NodeType),
|
|
|
|
Path: storageNode.Path,
|
2020-04-30 21:26:32 +00:00
|
|
|
})
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
return storageNodes, nil
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|