2020-01-30 22:46:08 +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 btc
|
2020-02-07 20:00:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/ipfs/go-block-format"
|
|
|
|
"github.com/ipfs/go-blockservice"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-06-22 18:12:32 +00:00
|
|
|
"github.com/vulcanize/ipfs-blockchain-watcher/pkg/ipfs"
|
|
|
|
"github.com/vulcanize/ipfs-blockchain-watcher/pkg/shared"
|
2020-02-07 20:00:01 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errUnexpectedNumberOfIPLDs = errors.New("ipfs batch fetch returned unexpected number of IPLDs")
|
|
|
|
)
|
|
|
|
|
|
|
|
// IPLDFetcher satisfies the IPLDFetcher interface for ethereum
|
|
|
|
type IPLDFetcher struct {
|
|
|
|
BlockService blockservice.BlockService
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewIPLDFetcher creates a pointer to a new IPLDFetcher
|
2020-04-30 21:26:32 +00:00
|
|
|
// It interfaces with PG-IPFS through an internalized IPFS node interface
|
2020-02-07 20:00:01 +00:00
|
|
|
func NewIPLDFetcher(ipfsPath string) (*IPLDFetcher, error) {
|
|
|
|
blockService, err := ipfs.InitIPFSBlockService(ipfsPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &IPLDFetcher{
|
|
|
|
BlockService: blockService,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-02-07 20:00:01 +00:00
|
|
|
cidWrapper, ok := cids.(*CIDWrapper)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("btc fetcher: expected cids type %T got %T", &CIDWrapper{}, cids)
|
|
|
|
}
|
|
|
|
log.Debug("fetching iplds")
|
2020-02-20 22:12:52 +00:00
|
|
|
iplds := IPLDs{}
|
2020-02-07 20:00:01 +00:00
|
|
|
iplds.BlockNumber = cidWrapper.BlockNumber
|
|
|
|
var err error
|
2020-02-23 23:14:29 +00:00
|
|
|
iplds.Header, err = f.FetchHeader(cidWrapper.Header)
|
2020-02-07 20:00:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
iplds.Transactions, err = f.FetchTrxs(cidWrapper.Transactions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return iplds, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2020-02-07 20:00:01 +00:00
|
|
|
}
|
2020-02-23 23:14:29 +00:00
|
|
|
return ipfs.BlockModel{
|
|
|
|
Data: header.RawData(),
|
|
|
|
CID: header.Cid().String(),
|
|
|
|
}, nil
|
2020-02-07 20:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FetchTrxs fetches transactions
|
|
|
|
// It uses the f.fetchBatch method
|
2020-02-20 22:13:19 +00:00
|
|
|
func (f *IPLDFetcher) FetchTrxs(cids []TxModel) ([]ipfs.BlockModel, error) {
|
2020-02-07 20:00:01 +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-02-07 20:00:01 +00:00
|
|
|
dc, err := cid.Decode(c.CID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-20 22:12:52 +00:00
|
|
|
trxCids[i] = dc
|
2020-02-07 20:00:01 +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-02-07 20:00:01 +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
|
2020-02-07 20:00:01 +00:00
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
return trxIPLDs, nil
|
2020-02-07 20:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// fetch is used to fetch a single cid
|
|
|
|
func (f *IPLDFetcher) fetch(cid cid.Cid) (blocks.Block, error) {
|
|
|
|
return f.BlockService.GetBlock(context.Background(), cid)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetchBatch is used to fetch a batch of IPFS data blocks by cid
|
|
|
|
// 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
|
|
|
|
func (f *IPLDFetcher) fetchBatch(cids []cid.Cid) []blocks.Block {
|
|
|
|
fetchedBlocks := make([]blocks.Block, 0, len(cids))
|
|
|
|
blockChan := f.BlockService.GetBlocks(context.Background(), cids)
|
|
|
|
for block := range blockChan {
|
|
|
|
fetchedBlocks = append(fetchedBlocks, block)
|
|
|
|
}
|
|
|
|
return fetchedBlocks
|
|
|
|
}
|