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/>.
|
|
|
|
|
|
|
|
package ipfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
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"
|
2019-05-01 17:49:56 +00:00
|
|
|
)
|
|
|
|
|
2019-06-06 03:50:12 +00:00
|
|
|
// IPLDFetcher is an interface for fetching IPLDs
|
2019-05-21 19:27:24 +00:00
|
|
|
type IPLDFetcher interface {
|
2019-10-08 19:51:38 +00:00
|
|
|
FetchIPLDs(cids CIDWrapper) (*IPLDWrapper, error)
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EthIPLDFetcher is used to fetch ETH IPLD objects from IPFS
|
|
|
|
type EthIPLDFetcher 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
|
2019-05-21 19:27:24 +00:00
|
|
|
func NewIPLDFetcher(ipfsPath string) (*EthIPLDFetcher, error) {
|
2019-05-01 17:49:56 +00:00
|
|
|
blockService, err := InitIPFSBlockService(ipfsPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-05-21 19:27:24 +00:00
|
|
|
return &EthIPLDFetcher{
|
2019-05-01 17:49:56 +00:00
|
|
|
BlockService: blockService,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-10-08 19:51:38 +00:00
|
|
|
// FetchIPLDs is the exported method for fetching and returning all the IPLDS specified in the CIDWrapper
|
|
|
|
func (f *EthIPLDFetcher) FetchIPLDs(cids CIDWrapper) (*IPLDWrapper, error) {
|
2019-07-02 17:38:12 +00:00
|
|
|
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("fetching iplds")
|
2019-08-05 17:56:15 +00:00
|
|
|
blocks := &IPLDWrapper{
|
2019-06-18 17:28:57 +00:00
|
|
|
BlockNumber: cids.BlockNumber,
|
2019-05-21 19:27:24 +00:00
|
|
|
Headers: make([]blocks.Block, 0),
|
2019-06-18 17:28:57 +00:00
|
|
|
Uncles: make([]blocks.Block, 0),
|
2019-05-21 19:27:24 +00:00
|
|
|
Transactions: make([]blocks.Block, 0),
|
|
|
|
Receipts: make([]blocks.Block, 0),
|
|
|
|
StateNodes: make(map[common.Hash]blocks.Block),
|
|
|
|
StorageNodes: make(map[common.Hash]map[common.Hash]blocks.Block),
|
|
|
|
}
|
|
|
|
|
2019-10-08 19:51:38 +00:00
|
|
|
headersErr := f.fetchHeaders(cids, blocks)
|
|
|
|
if headersErr != nil {
|
|
|
|
return nil, headersErr
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2019-10-08 19:51:38 +00:00
|
|
|
unclesErr := f.fetchUncles(cids, blocks)
|
|
|
|
if unclesErr != nil {
|
|
|
|
return nil, unclesErr
|
2019-06-18 17:28:57 +00:00
|
|
|
}
|
2019-10-08 19:51:38 +00:00
|
|
|
trxsErr := f.fetchTrxs(cids, blocks)
|
|
|
|
if trxsErr != nil {
|
|
|
|
return nil, trxsErr
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2019-10-08 19:51:38 +00:00
|
|
|
rctsErr := f.fetchRcts(cids, blocks)
|
|
|
|
if rctsErr != nil {
|
|
|
|
return nil, rctsErr
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2019-10-08 19:51:38 +00:00
|
|
|
storageErr := f.fetchStorage(cids, blocks)
|
|
|
|
if storageErr != nil {
|
|
|
|
return nil, storageErr
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2019-10-08 19:51:38 +00:00
|
|
|
stateErr := f.fetchState(cids, blocks)
|
|
|
|
if stateErr != nil {
|
|
|
|
return nil, stateErr
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return blocks, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchHeaders fetches headers
|
|
|
|
// It uses the f.fetchBatch method
|
2019-08-05 17:56:15 +00:00
|
|
|
func (f *EthIPLDFetcher) fetchHeaders(cids CIDWrapper, blocks *IPLDWrapper) error {
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("fetching header iplds")
|
2019-05-21 19:27:24 +00:00
|
|
|
headerCids := make([]cid.Cid, 0, len(cids.Headers))
|
|
|
|
for _, c := range cids.Headers {
|
|
|
|
dc, err := cid.Decode(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
headerCids = append(headerCids, dc)
|
|
|
|
}
|
|
|
|
blocks.Headers = f.fetchBatch(headerCids)
|
|
|
|
if len(blocks.Headers) != len(headerCids) {
|
|
|
|
log.Errorf("ipfs fetcher: number of header blocks returned (%d) does not match number expected (%d)", len(blocks.Headers), len(headerCids))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-18 17:28:57 +00:00
|
|
|
// fetchUncles fetches uncles
|
|
|
|
// It uses the f.fetchBatch method
|
2019-08-05 17:56:15 +00:00
|
|
|
func (f *EthIPLDFetcher) fetchUncles(cids CIDWrapper, blocks *IPLDWrapper) error {
|
2019-06-18 17:28:57 +00:00
|
|
|
log.Debug("fetching uncle iplds")
|
|
|
|
uncleCids := make([]cid.Cid, 0, len(cids.Uncles))
|
|
|
|
for _, c := range cids.Uncles {
|
|
|
|
dc, err := cid.Decode(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
uncleCids = append(uncleCids, dc)
|
|
|
|
}
|
|
|
|
blocks.Uncles = f.fetchBatch(uncleCids)
|
|
|
|
if len(blocks.Uncles) != len(uncleCids) {
|
|
|
|
log.Errorf("ipfs fetcher: number of uncle blocks returned (%d) does not match number expected (%d)", len(blocks.Uncles), len(uncleCids))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-21 19:27:24 +00:00
|
|
|
// fetchTrxs fetches transactions
|
|
|
|
// It uses the f.fetchBatch method
|
2019-08-05 17:56:15 +00:00
|
|
|
func (f *EthIPLDFetcher) fetchTrxs(cids CIDWrapper, blocks *IPLDWrapper) error {
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("fetching transaction iplds")
|
2019-05-21 19:27:24 +00:00
|
|
|
trxCids := make([]cid.Cid, 0, len(cids.Transactions))
|
|
|
|
for _, c := range cids.Transactions {
|
|
|
|
dc, err := cid.Decode(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
trxCids = append(trxCids, dc)
|
|
|
|
}
|
|
|
|
blocks.Transactions = f.fetchBatch(trxCids)
|
|
|
|
if len(blocks.Transactions) != len(trxCids) {
|
|
|
|
log.Errorf("ipfs fetcher: number of transaction blocks returned (%d) does not match number expected (%d)", len(blocks.Transactions), len(trxCids))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchRcts fetches receipts
|
|
|
|
// It uses the f.fetchBatch method
|
2019-08-05 17:56:15 +00:00
|
|
|
func (f *EthIPLDFetcher) fetchRcts(cids CIDWrapper, blocks *IPLDWrapper) error {
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("fetching receipt iplds")
|
2019-05-21 19:27:24 +00:00
|
|
|
rctCids := make([]cid.Cid, 0, len(cids.Receipts))
|
|
|
|
for _, c := range cids.Receipts {
|
|
|
|
dc, err := cid.Decode(c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rctCids = append(rctCids, dc)
|
|
|
|
}
|
|
|
|
blocks.Receipts = f.fetchBatch(rctCids)
|
|
|
|
if len(blocks.Receipts) != len(rctCids) {
|
|
|
|
log.Errorf("ipfs fetcher: number of receipt blocks returned (%d) does not match number expected (%d)", len(blocks.Receipts), len(rctCids))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchState fetches state nodes
|
|
|
|
// It uses the single f.fetch method instead of the batch fetch, because it
|
|
|
|
// needs to maintain the data's relation to state keys
|
2019-08-05 17:56:15 +00:00
|
|
|
func (f *EthIPLDFetcher) fetchState(cids CIDWrapper, blocks *IPLDWrapper) error {
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("fetching state iplds")
|
2019-05-21 19:27:24 +00:00
|
|
|
for _, stateNode := range cids.StateNodes {
|
|
|
|
if stateNode.CID == "" || stateNode.Key == "" {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-08 19:51:38 +00:00
|
|
|
dc, decodeErr := cid.Decode(stateNode.CID)
|
|
|
|
if decodeErr != nil {
|
|
|
|
return decodeErr
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2019-10-08 19:51:38 +00:00
|
|
|
block, fetchErr := f.fetch(dc)
|
|
|
|
if fetchErr != nil {
|
|
|
|
return fetchErr
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
blocks.StateNodes[common.HexToHash(stateNode.Key)] = block
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchStorage fetches storage nodes
|
|
|
|
// 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
|
2019-08-23 21:34:54 +00:00
|
|
|
func (f *EthIPLDFetcher) fetchStorage(cids CIDWrapper, blks *IPLDWrapper) error {
|
2019-06-07 13:42:10 +00:00
|
|
|
log.Debug("fetching storage iplds")
|
2019-05-21 19:27:24 +00:00
|
|
|
for _, storageNode := range cids.StorageNodes {
|
|
|
|
if storageNode.CID == "" || storageNode.Key == "" || storageNode.StateKey == "" {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-08 19:51:38 +00:00
|
|
|
dc, decodeErr := cid.Decode(storageNode.CID)
|
|
|
|
if decodeErr != nil {
|
|
|
|
return decodeErr
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2019-10-08 19:51:38 +00:00
|
|
|
blk, fetchErr := f.fetch(dc)
|
|
|
|
if fetchErr != nil {
|
|
|
|
return fetchErr
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
2019-08-23 21:34:54 +00:00
|
|
|
if blks.StorageNodes[common.HexToHash(storageNode.StateKey)] == nil {
|
|
|
|
blks.StorageNodes[common.HexToHash(storageNode.StateKey)] = make(map[common.Hash]blocks.Block)
|
|
|
|
}
|
|
|
|
blks.StorageNodes[common.HexToHash(storageNode.StateKey)][common.HexToHash(storageNode.Key)] = blk
|
2019-05-21 19:27:24 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch is used to fetch a single cid
|
|
|
|
func (f *EthIPLDFetcher) 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
|
2019-05-21 19:27:24 +00:00
|
|
|
func (f *EthIPLDFetcher) 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
|
|
|
|
}
|