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"
|
2022-04-25 11:37:11 +00:00
|
|
|
"strconv"
|
2019-05-01 17:49:56 +00:00
|
|
|
|
2022-09-14 05:20:16 +00:00
|
|
|
"github.com/cerc-io/ipld-eth-server/v4/pkg/shared"
|
2019-05-21 19:27:24 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2021-08-12 06:23:41 +00:00
|
|
|
"github.com/ethereum/go-ethereum/statediff/indexer/models"
|
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"
|
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 {
|
2022-03-10 09:53:03 +00:00
|
|
|
db *sqlx.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
|
2022-03-10 09:53:03 +00:00
|
|
|
func NewIPLDFetcher(db *sqlx.DB) *IPLDFetcher {
|
2020-08-10 18:04:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-05-27 13:46:10 +00:00
|
|
|
// FetchHeader fetches header
|
2022-03-10 12:35:19 +00:00
|
|
|
func (f *IPLDFetcher) FetchHeader(tx *sqlx.Tx, c models.HeaderModel) (models.IPLDModel, error) {
|
2020-02-23 23:14:29 +00:00
|
|
|
log.Debug("fetching header ipld")
|
2022-04-25 11:37:11 +00:00
|
|
|
blockNumber, err := strconv.ParseUint(c.BlockNumber, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return models.IPLDModel{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
headerBytes, err := shared.FetchIPLDByMhKeyAndBlockNumber(tx, c.MhKey, blockNumber)
|
2020-02-23 23:14:29 +00:00
|
|
|
if err != nil {
|
2022-03-10 12:35:19 +00:00
|
|
|
return models.IPLDModel{}, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2022-03-10 12:35:19 +00:00
|
|
|
return models.IPLDModel{
|
2022-05-18 12:18:17 +00:00
|
|
|
BlockNumber: c.BlockNumber,
|
|
|
|
Data: headerBytes,
|
|
|
|
Key: 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
|
2022-03-10 12:35:19 +00:00
|
|
|
func (f *IPLDFetcher) FetchUncles(tx *sqlx.Tx, cids []models.UncleModel) ([]models.IPLDModel, error) {
|
2019-06-18 17:28:57 +00:00
|
|
|
log.Debug("fetching uncle iplds")
|
2022-03-10 12:35:19 +00:00
|
|
|
uncleIPLDs := make([]models.IPLDModel, len(cids))
|
2020-02-20 22:12:52 +00:00
|
|
|
for i, c := range cids {
|
2022-04-25 11:37:11 +00:00
|
|
|
blockNumber, err := strconv.ParseUint(c.BlockNumber, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
uncleBytes, err := shared.FetchIPLDByMhKeyAndBlockNumber(tx, c.MhKey, blockNumber)
|
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
|
|
|
}
|
2022-03-10 12:35:19 +00:00
|
|
|
uncleIPLDs[i] = models.IPLDModel{
|
2022-05-18 12:18:17 +00:00
|
|
|
BlockNumber: c.BlockNumber,
|
|
|
|
Data: uncleBytes,
|
|
|
|
Key: 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
|
2022-03-10 12:35:19 +00:00
|
|
|
func (f *IPLDFetcher) FetchTrxs(tx *sqlx.Tx, cids []models.TxModel) ([]models.IPLDModel, error) {
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("fetching transaction iplds")
|
2022-03-10 12:35:19 +00:00
|
|
|
trxIPLDs := make([]models.IPLDModel, len(cids))
|
2020-02-20 22:12:52 +00:00
|
|
|
for i, c := range cids {
|
2022-04-25 11:37:11 +00:00
|
|
|
blockNumber, err := strconv.ParseUint(c.BlockNumber, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
txBytes, err := shared.FetchIPLDByMhKeyAndBlockNumber(tx, c.MhKey, blockNumber)
|
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
|
|
|
}
|
2022-03-10 12:35:19 +00:00
|
|
|
trxIPLDs[i] = models.IPLDModel{
|
2022-05-18 12:18:17 +00:00
|
|
|
BlockNumber: c.BlockNumber,
|
|
|
|
Data: txBytes,
|
|
|
|
Key: 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
|
2022-03-10 12:35:19 +00:00
|
|
|
func (f *IPLDFetcher) FetchRcts(tx *sqlx.Tx, cids []models.ReceiptModel) ([]models.IPLDModel, error) {
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("fetching receipt iplds")
|
2022-03-10 12:35:19 +00:00
|
|
|
rctIPLDs := make([]models.IPLDModel, len(cids))
|
2020-02-20 22:12:52 +00:00
|
|
|
for i, c := range cids {
|
2022-04-25 11:37:11 +00:00
|
|
|
blockNumber, err := strconv.ParseUint(c.BlockNumber, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rctBytes, err := shared.FetchIPLDByMhKeyAndBlockNumber(tx, c.LeafMhKey, blockNumber)
|
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)
|
2022-03-10 12:35:19 +00:00
|
|
|
rctIPLDs[i] = models.IPLDModel{
|
2022-05-18 12:18:17 +00:00
|
|
|
BlockNumber: c.BlockNumber,
|
|
|
|
Data: rctBytes,
|
|
|
|
Key: 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
|
|
|
|
}
|
2022-04-25 11:37:11 +00:00
|
|
|
blockNumber, err := strconv.ParseUint(stateNode.BlockNumber, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stateBytes, err := shared.FetchIPLDByMhKeyAndBlockNumber(tx, stateNode.MhKey, blockNumber)
|
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{
|
2022-03-10 12:35:19 +00:00
|
|
|
IPLD: models.IPLDModel{
|
2022-05-18 12:18:17 +00:00
|
|
|
BlockNumber: stateNode.BlockNumber,
|
|
|
|
Data: stateBytes,
|
|
|
|
Key: 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
|
|
|
|
}
|
2022-04-25 11:37:11 +00:00
|
|
|
blockNumber, err := strconv.ParseUint(storageNode.BlockNumber, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
storageBytes, err := shared.FetchIPLDByMhKeyAndBlockNumber(tx, storageNode.MhKey, blockNumber)
|
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{
|
2022-03-10 12:35:19 +00:00
|
|
|
IPLD: models.IPLDModel{
|
2022-05-18 12:18:17 +00:00
|
|
|
BlockNumber: storageNode.BlockNumber,
|
|
|
|
Data: storageBytes,
|
|
|
|
Key: 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
|
|
|
}
|