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 (
|
|
|
|
"context"
|
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"
|
2019-05-01 17:49:56 +00:00
|
|
|
"github.com/ipfs/go-block-format"
|
|
|
|
"github.com/ipfs/go-blockservice"
|
|
|
|
"github.com/ipfs/go-cid"
|
2019-05-21 19:27:24 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-01-17 23:16:01 +00:00
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/ipfs"
|
2020-02-20 22:12:52 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/shared"
|
2019-05-01 17:49:56 +00:00
|
|
|
)
|
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
var (
|
|
|
|
errUnexpectedNumberOfIPLDs = errors.New("ipfs batch fetch returned unexpected number of IPLDs")
|
|
|
|
)
|
|
|
|
|
2020-01-17 23:16:01 +00:00
|
|
|
// IPLDFetcher satisfies the IPLDFetcher interface for ethereum
|
|
|
|
type IPLDFetcher struct {
|
2019-05-01 17:49:56 +00:00
|
|
|
BlockService blockservice.BlockService
|
|
|
|
}
|
|
|
|
|
2019-05-17 04:08:53 +00:00
|
|
|
// NewIPLDFetcher creates a pointer to a new IPLDFetcher
|
2020-01-17 23:16:01 +00:00
|
|
|
func NewIPLDFetcher(ipfsPath string) (*IPLDFetcher, error) {
|
|
|
|
blockService, err := ipfs.InitIPFSBlockService(ipfsPath)
|
2019-05-01 17:49:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
return &IPLDFetcher{
|
2019-05-01 17:49:56 +00:00
|
|
|
BlockService: blockService,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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-02-20 22:12:52 +00:00
|
|
|
func (f *IPLDFetcher) Fetch(cids shared.CIDsForFetching) (shared.IPLDs, error) {
|
2020-01-17 23:16:01 +00:00
|
|
|
cidWrapper, ok := cids.(*CIDWrapper)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("eth fetcher: expected cids type %T got %T", &CIDWrapper{}, cids)
|
|
|
|
}
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("fetching iplds")
|
2020-02-27 21:07:33 +00:00
|
|
|
var err error
|
2020-02-20 22:12:52 +00:00
|
|
|
iplds := IPLDs{}
|
2020-02-27 21:07:33 +00:00
|
|
|
iplds.TotalDifficulty, ok = new(big.Int).SetString(cidWrapper.Header.TotalDifficulty, 10)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("eth fetcher: unable to set total difficulty")
|
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
iplds.BlockNumber = cidWrapper.BlockNumber
|
2020-02-23 23:14:29 +00:00
|
|
|
iplds.Header, err = f.FetchHeader(cidWrapper.Header)
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
iplds.Uncles, err = f.FetchUncles(cidWrapper.Uncles)
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-06-18 17:28:57 +00:00
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
iplds.Transactions, err = f.FetchTrxs(cidWrapper.Transactions)
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
iplds.Receipts, err = f.FetchRcts(cidWrapper.Receipts)
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
iplds.StateNodes, err = f.FetchState(cidWrapper.StateNodes)
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-17 23:16:01 +00:00
|
|
|
iplds.StorageNodes, err = f.FetchStorage(cidWrapper.StorageNodes)
|
2020-01-16 20:48:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
return iplds, nil
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
// FetchHeaders fetches headers
|
2020-02-23 23:14:29 +00:00
|
|
|
// It uses the f.fetch method
|
|
|
|
func (f *IPLDFetcher) FetchHeader(c HeaderModel) (ipfs.BlockModel, error) {
|
|
|
|
log.Debug("fetching header ipld")
|
|
|
|
dc, err := cid.Decode(c.CID)
|
|
|
|
if err != nil {
|
|
|
|
return ipfs.BlockModel{}, err
|
2020-02-20 22:12:52 +00:00
|
|
|
}
|
2020-02-23 23:14:29 +00:00
|
|
|
header, err := f.fetch(dc)
|
|
|
|
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{
|
|
|
|
Data: header.RawData(),
|
|
|
|
CID: header.Cid().String(),
|
|
|
|
}, nil
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 20:48:38 +00:00
|
|
|
// FetchUncles fetches uncles
|
2019-06-18 17:28:57 +00:00
|
|
|
// It uses the f.fetchBatch method
|
2020-02-20 22:13:19 +00:00
|
|
|
func (f *IPLDFetcher) FetchUncles(cids []UncleModel) ([]ipfs.BlockModel, error) {
|
2019-06-18 17:28:57 +00:00
|
|
|
log.Debug("fetching uncle iplds")
|
2020-02-20 22:12:52 +00:00
|
|
|
uncleCids := make([]cid.Cid, len(cids))
|
|
|
|
for i, c := range cids {
|
2020-01-17 23:16:01 +00:00
|
|
|
dc, err := cid.Decode(c.CID)
|
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:12:52 +00:00
|
|
|
uncleCids[i] = dc
|
2019-06-18 17:28:57 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
uncles := f.fetchBatch(uncleCids)
|
2020-02-20 22:13:19 +00:00
|
|
|
uncleIPLDs := make([]ipfs.BlockModel, len(uncles))
|
2020-02-20 22:12:52 +00:00
|
|
|
for i, uncle := range uncles {
|
2020-02-20 22:13:19 +00:00
|
|
|
uncleIPLDs[i] = ipfs.BlockModel{
|
|
|
|
Data: uncle.RawData(),
|
|
|
|
CID: uncle.Cid().String(),
|
|
|
|
}
|
2020-02-20 22:12:52 +00:00
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
if len(uncleIPLDs) != len(uncleCids) {
|
2020-01-16 20:48:38 +00:00
|
|
|
log.Errorf("ipfs fetcher: number of uncle blocks returned (%d) does not match number expected (%d)", len(uncles), len(uncleCids))
|
2020-02-20 22:13:19 +00:00
|
|
|
return uncleIPLDs, errUnexpectedNumberOfIPLDs
|
2019-06-18 17:28:57 +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
|
2019-05-21 19:27:24 +00:00
|
|
|
// It uses the f.fetchBatch method
|
2020-02-20 22:13:19 +00:00
|
|
|
func (f *IPLDFetcher) FetchTrxs(cids []TxModel) ([]ipfs.BlockModel, error) {
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("fetching transaction iplds")
|
2020-02-20 22:12:52 +00:00
|
|
|
trxCids := make([]cid.Cid, len(cids))
|
|
|
|
for i, c := range cids {
|
2020-01-17 23:16:01 +00:00
|
|
|
dc, err := cid.Decode(c.CID)
|
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:12:52 +00:00
|
|
|
trxCids[i] = dc
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
trxs := f.fetchBatch(trxCids)
|
2020-02-20 22:13:19 +00:00
|
|
|
trxIPLDs := make([]ipfs.BlockModel, len(trxs))
|
2020-02-20 22:12:52 +00:00
|
|
|
for i, trx := range trxs {
|
2020-02-20 22:13:19 +00:00
|
|
|
trxIPLDs[i] = ipfs.BlockModel{
|
|
|
|
Data: trx.RawData(),
|
|
|
|
CID: trx.Cid().String(),
|
|
|
|
}
|
2020-02-20 22:12:52 +00:00
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
if len(trxIPLDs) != len(trxCids) {
|
2020-01-16 20:48:38 +00:00
|
|
|
log.Errorf("ipfs fetcher: number of transaction blocks returned (%d) does not match number expected (%d)", len(trxs), len(trxCids))
|
2020-02-20 22:13:19 +00:00
|
|
|
return trxIPLDs, errUnexpectedNumberOfIPLDs
|
2019-05-21 19:27:24 +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
|
2019-05-21 19:27:24 +00:00
|
|
|
// It uses the f.fetchBatch method
|
2020-02-20 22:13:19 +00:00
|
|
|
func (f *IPLDFetcher) FetchRcts(cids []ReceiptModel) ([]ipfs.BlockModel, error) {
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("fetching receipt iplds")
|
2020-02-20 22:12:52 +00:00
|
|
|
rctCids := make([]cid.Cid, len(cids))
|
|
|
|
for i, c := range cids {
|
2020-01-17 23:16:01 +00:00
|
|
|
dc, err := cid.Decode(c.CID)
|
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:12:52 +00:00
|
|
|
rctCids[i] = dc
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
rcts := f.fetchBatch(rctCids)
|
2020-02-20 22:13:19 +00:00
|
|
|
rctIPLDs := make([]ipfs.BlockModel, len(rcts))
|
2020-02-20 22:12:52 +00:00
|
|
|
for i, rct := range rcts {
|
2020-02-20 22:13:19 +00:00
|
|
|
rctIPLDs[i] = ipfs.BlockModel{
|
|
|
|
Data: rct.RawData(),
|
|
|
|
CID: rct.Cid().String(),
|
|
|
|
}
|
2020-02-20 22:12:52 +00:00
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
if len(rctIPLDs) != len(rctCids) {
|
2020-01-16 20:48:38 +00:00
|
|
|
log.Errorf("ipfs fetcher: number of receipt blocks returned (%d) does not match number expected (%d)", len(rcts), len(rctCids))
|
2020-02-20 22:13:19 +00:00
|
|
|
return rctIPLDs, errUnexpectedNumberOfIPLDs
|
2019-05-21 19:27:24 +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
|
2019-05-21 19:27:24 +00:00
|
|
|
// It uses the single f.fetch method instead of the batch fetch, because it
|
|
|
|
// needs to maintain the data's relation to state keys
|
2020-02-20 22:12:52 +00:00
|
|
|
func (f *IPLDFetcher) FetchState(cids []StateNodeModel) ([]StateNode, error) {
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("fetching state iplds")
|
2020-04-30 21:26:32 +00:00
|
|
|
stateNodes := make([]StateNode, 0, len(cids))
|
|
|
|
for _, stateNode := range cids {
|
|
|
|
if stateNode.CID == "" {
|
2019-05-21 19:27:24 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
dc, err := cid.Decode(stateNode.CID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
state, err := f.fetch(dc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-04-30 21:26:32 +00:00
|
|
|
stateNodes = append(stateNodes, StateNode{
|
2020-02-20 22:13:19 +00:00
|
|
|
IPLD: ipfs.BlockModel{
|
|
|
|
Data: state.RawData(),
|
|
|
|
CID: state.Cid().String(),
|
|
|
|
},
|
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
|
2019-05-21 19:27:24 +00:00
|
|
|
// It uses the single f.fetch method instead of the batch fetch, because it
|
|
|
|
// needs to maintain the data's relation to state and storage keys
|
2020-02-20 22:12:52 +00:00
|
|
|
func (f *IPLDFetcher) FetchStorage(cids []StorageNodeWithStateKeyModel) ([]StorageNode, error) {
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("fetching storage iplds")
|
2020-04-30 21:26:32 +00:00
|
|
|
storageNodes := make([]StorageNode, 0, len(cids))
|
|
|
|
for _, storageNode := range cids {
|
|
|
|
if storageNode.CID == "" || storageNode.StateKey == "" {
|
2019-05-21 19:27:24 +00:00
|
|
|
continue
|
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
dc, err := cid.Decode(storageNode.CID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-01-16 20:48:38 +00:00
|
|
|
storage, err := f.fetch(dc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2020-04-30 21:26:32 +00:00
|
|
|
storageNodes = append(storageNodes, StorageNode{
|
2020-02-20 22:13:19 +00:00
|
|
|
IPLD: ipfs.BlockModel{
|
|
|
|
Data: storage.RawData(),
|
|
|
|
CID: storage.Cid().String(),
|
|
|
|
},
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// fetch is used to fetch a single cid
|
2020-01-17 23:16:01 +00:00
|
|
|
func (f *IPLDFetcher) fetch(cid cid.Cid) (blocks.Block, error) {
|
2019-05-01 17:49:56 +00:00
|
|
|
return f.BlockService.GetBlock(context.Background(), cid)
|
|
|
|
}
|
|
|
|
|
2019-05-21 19:27:24 +00:00
|
|
|
// fetchBatch is used to fetch a batch of IPFS data blocks by cid
|
2019-05-01 17:49:56 +00:00
|
|
|
// There is no guarantee all are fetched, and no error in such a case, so
|
|
|
|
// downstream we will need to confirm which CIDs were fetched in the result set
|
2020-01-17 23:16:01 +00:00
|
|
|
func (f *IPLDFetcher) fetchBatch(cids []cid.Cid) []blocks.Block {
|
2019-05-01 17:49:56 +00:00
|
|
|
fetchedBlocks := make([]blocks.Block, 0, len(cids))
|
|
|
|
blockChan := f.BlockService.GetBlocks(context.Background(), cids)
|
|
|
|
for block := range blockChan {
|
|
|
|
fetchedBlocks = append(fetchedBlocks, block)
|
|
|
|
}
|
|
|
|
return fetchedBlocks
|
|
|
|
}
|