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 (
|
|
|
|
"fmt"
|
|
|
|
|
2020-08-10 17:46:03 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
2020-02-07 20:00:01 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2020-06-22 18:12:32 +00:00
|
|
|
"github.com/vulcanize/ipfs-blockchain-watcher/pkg/ipfs"
|
2020-08-10 17:46:03 +00:00
|
|
|
"github.com/vulcanize/ipfs-blockchain-watcher/pkg/postgres"
|
2020-06-22 18:12:32 +00:00
|
|
|
"github.com/vulcanize/ipfs-blockchain-watcher/pkg/shared"
|
2020-02-07 20:00:01 +00:00
|
|
|
)
|
|
|
|
|
2020-08-10 17:46:03 +00:00
|
|
|
// IPLDPGFetcher satisfies the IPLDFetcher interface for ethereum
|
|
|
|
// it interfaces directly with PG-IPFS instead of going through a node-interface or remote node
|
|
|
|
type IPLDPGFetcher struct {
|
|
|
|
db *postgres.DB
|
2020-02-07 20:00:01 +00:00
|
|
|
}
|
|
|
|
|
2020-08-10 17:46:03 +00:00
|
|
|
// NewIPLDPGFetcher creates a pointer to a new IPLDPGFetcher
|
|
|
|
func NewIPLDPGFetcher(db *postgres.DB) *IPLDPGFetcher {
|
|
|
|
return &IPLDPGFetcher{
|
|
|
|
db: db,
|
2020-02-07 20:00:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch is the exported method for fetching and returning all the IPLDS specified in the CIDWrapper
|
2020-08-10 17:46:03 +00:00
|
|
|
func (f *IPLDPGFetcher) 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
|
2020-08-10 17:46:03 +00:00
|
|
|
|
|
|
|
tx, err := f.db.Beginx()
|
2020-02-07 20:00:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
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()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
iplds.Header, err = f.FetchHeader(tx, cidWrapper.Header)
|
2020-02-07 20:00:01 +00:00
|
|
|
if err != nil {
|
2020-08-10 17:46:03 +00:00
|
|
|
return nil, fmt.Errorf("btc pg fetcher: header fetching error: %s", err.Error())
|
|
|
|
}
|
|
|
|
iplds.Transactions, err = f.FetchTrxs(tx, cidWrapper.Transactions)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("btc pg fetcher: transaction fetching error: %s", err.Error())
|
2020-02-07 20:00:01 +00:00
|
|
|
}
|
2020-08-10 17:46:03 +00:00
|
|
|
return iplds, err
|
2020-02-07 20:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FetchHeaders fetches headers
|
2020-08-10 17:46:03 +00:00
|
|
|
func (f *IPLDPGFetcher) FetchHeader(tx *sqlx.Tx, c 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
|
2020-02-07 20:00:01 +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
|
2020-02-07 20:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FetchTrxs fetches transactions
|
2020-08-10 17:46:03 +00:00
|
|
|
func (f *IPLDPGFetcher) FetchTrxs(tx *sqlx.Tx, cids []TxModel) ([]ipfs.BlockModel, error) {
|
2020-02-07 20:00:01 +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
|
|
|
trxBytes, err := shared.FetchIPLDByMhKey(tx, c.MhKey)
|
2020-02-07 20:00:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-02-20 22:13:19 +00:00
|
|
|
trxIPLDs[i] = ipfs.BlockModel{
|
2020-08-10 17:46:03 +00:00
|
|
|
Data: trxBytes,
|
|
|
|
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
|
2020-02-07 20:00:01 +00:00
|
|
|
}
|