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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/ipfs"
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/ipfs/dag_putters"
|
2020-03-17 18:05:19 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/ipfs/ipld"
|
2020-01-30 22:46:08 +00:00
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/shared"
|
|
|
|
)
|
|
|
|
|
|
|
|
// IPLDPublisher satisfies the IPLDPublisher for ethereum
|
|
|
|
type IPLDPublisher struct {
|
2020-03-17 18:05:19 +00:00
|
|
|
HeaderPutter shared.DagPutter
|
|
|
|
TransactionPutter shared.DagPutter
|
|
|
|
TransactionTriePutter shared.DagPutter
|
2020-01-30 22:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewIPLDPublisher creates a pointer to a new Publisher which satisfies the IPLDPublisher interface
|
|
|
|
func NewIPLDPublisher(ipfsPath string) (*IPLDPublisher, error) {
|
|
|
|
node, err := ipfs.InitIPFSNode(ipfsPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &IPLDPublisher{
|
2020-03-17 18:05:19 +00:00
|
|
|
HeaderPutter: dag_putters.NewBtcHeaderDagPutter(node),
|
|
|
|
TransactionPutter: dag_putters.NewBtcTxDagPutter(node),
|
|
|
|
TransactionTriePutter: dag_putters.NewBtcTxTrieDagPutter(node),
|
2020-01-30 22:46:08 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish publishes an IPLDPayload to IPFS and returns the corresponding CIDPayload
|
2020-02-20 22:12:52 +00:00
|
|
|
func (pub *IPLDPublisher) Publish(payload shared.ConvertedData) (shared.CIDsForIndexing, error) {
|
|
|
|
ipldPayload, ok := payload.(ConvertedPayload)
|
2020-01-30 22:46:08 +00:00
|
|
|
if !ok {
|
2020-02-20 22:12:52 +00:00
|
|
|
return nil, fmt.Errorf("eth publisher expected payload type %T got %T", &ConvertedPayload{}, payload)
|
2020-01-30 22:46:08 +00:00
|
|
|
}
|
2020-03-17 18:05:19 +00:00
|
|
|
// Generate nodes
|
|
|
|
headerNode, txNodes, txTrieNodes, err := ipld.FromHeaderAndTxs(ipldPayload.Header, ipldPayload.Txs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-30 22:46:08 +00:00
|
|
|
// Process and publish headers
|
2020-03-17 18:05:19 +00:00
|
|
|
headerCid, err := pub.publishHeader(headerNode)
|
2020-01-30 22:46:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
header := HeaderModel{
|
|
|
|
CID: headerCid,
|
|
|
|
ParentHash: ipldPayload.Header.PrevBlock.String(),
|
2020-02-19 22:09:33 +00:00
|
|
|
BlockNumber: strconv.Itoa(int(ipldPayload.BlockPayload.BlockHeight)),
|
2020-01-30 22:46:08 +00:00
|
|
|
BlockHash: ipldPayload.Header.BlockHash().String(),
|
2020-02-02 21:58:07 +00:00
|
|
|
Timestamp: ipldPayload.Header.Timestamp.UnixNano(),
|
|
|
|
Bits: ipldPayload.Header.Bits,
|
2020-01-30 22:46:08 +00:00
|
|
|
}
|
|
|
|
// Process and publish transactions
|
2020-03-17 18:05:19 +00:00
|
|
|
transactionCids, err := pub.publishTransactions(txNodes, txTrieNodes, ipldPayload.TxMetaData)
|
2020-01-30 22:46:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Package CIDs and their metadata into a single struct
|
|
|
|
return &CIDPayload{
|
|
|
|
HeaderCID: header,
|
|
|
|
TransactionCIDs: transactionCids,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-03-17 18:05:19 +00:00
|
|
|
func (pub *IPLDPublisher) publishHeader(header *ipld.BtcHeader) (string, error) {
|
|
|
|
cid, err := pub.HeaderPutter.DagPut(header)
|
2020-01-30 22:46:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-03-17 18:05:19 +00:00
|
|
|
return cid, nil
|
2020-01-30 22:46:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-17 18:05:19 +00:00
|
|
|
func (pub *IPLDPublisher) publishTransactions(transactions []*ipld.BtcTx, txTrie []*ipld.BtcTxTrie, trxMeta []TxModelWithInsAndOuts) ([]TxModelWithInsAndOuts, error) {
|
|
|
|
txCids := make([]TxModelWithInsAndOuts, len(transactions))
|
|
|
|
for i, tx := range transactions {
|
|
|
|
cid, err := pub.TransactionPutter.DagPut(tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
txCids[i] = TxModelWithInsAndOuts{
|
2020-01-30 22:46:08 +00:00
|
|
|
CID: cid,
|
|
|
|
Index: trxMeta[i].Index,
|
|
|
|
TxHash: trxMeta[i].TxHash,
|
2020-02-04 21:20:49 +00:00
|
|
|
SegWit: trxMeta[i].SegWit,
|
2020-01-30 22:46:08 +00:00
|
|
|
WitnessHash: trxMeta[i].WitnessHash,
|
2020-02-02 21:58:07 +00:00
|
|
|
TxInputs: trxMeta[i].TxInputs,
|
|
|
|
TxOutputs: trxMeta[i].TxOutputs,
|
2020-01-30 22:46:08 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-17 18:05:19 +00:00
|
|
|
for _, txNode := range txTrie {
|
|
|
|
// We don't do anything with the tx trie cids atm
|
|
|
|
if _, err := pub.TransactionTriePutter.DagPut(txNode); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return txCids, nil
|
2020-01-30 22:46:08 +00:00
|
|
|
}
|