2020-02-07 20:00:01 +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 (
|
2020-04-19 23:26:23 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-02-07 20:00:01 +00:00
|
|
|
"github.com/btcsuite/btcd/rpcclient"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
|
|
|
|
"github.com/vulcanize/vulcanizedb/pkg/super_node/shared"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PayloadFetcher satisfies the PayloadFetcher interface for bitcoin
|
|
|
|
type PayloadFetcher struct {
|
|
|
|
// PayloadFetcher is thread-safe as long as the underlying client is thread-safe, since it has/modifies no other state
|
|
|
|
// http.Client is thread-safe
|
|
|
|
client *rpcclient.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStateDiffFetcher returns a PayloadFetcher
|
2020-02-10 15:00:55 +00:00
|
|
|
func NewPayloadFetcher(c *rpcclient.ConnConfig) (*PayloadFetcher, error) {
|
|
|
|
client, err := rpcclient.New(c, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-02-07 20:00:01 +00:00
|
|
|
}
|
2020-02-10 15:00:55 +00:00
|
|
|
return &PayloadFetcher{
|
|
|
|
client: client,
|
|
|
|
}, nil
|
2020-02-07 20:00:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FetchAt fetches the block payloads at the given block heights
|
|
|
|
func (fetcher *PayloadFetcher) FetchAt(blockHeights []uint64) ([]shared.RawChainData, error) {
|
|
|
|
blockPayloads := make([]shared.RawChainData, len(blockHeights))
|
|
|
|
for i, height := range blockHeights {
|
|
|
|
hash, err := fetcher.client.GetBlockHash(int64(height))
|
|
|
|
if err != nil {
|
2020-04-19 23:26:23 +00:00
|
|
|
return nil, fmt.Errorf("bitcoin PayloadFetcher GetBlockHash err at blockheight %d: %s", height, err.Error())
|
2020-02-07 20:00:01 +00:00
|
|
|
}
|
|
|
|
block, err := fetcher.client.GetBlock(hash)
|
|
|
|
if err != nil {
|
2020-04-19 23:26:23 +00:00
|
|
|
return nil, fmt.Errorf("bitcoin PayloadFetcher GetBlock err at blockheight %d: %s", height, err.Error())
|
2020-02-07 20:00:01 +00:00
|
|
|
}
|
|
|
|
blockPayloads[i] = BlockPayload{
|
2020-02-19 22:09:33 +00:00
|
|
|
BlockHeight: int64(height),
|
|
|
|
Header: &block.Header,
|
|
|
|
Txs: msgTxsToUtilTxs(block.Transactions),
|
2020-02-07 20:00:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return blockPayloads, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func msgTxsToUtilTxs(msgs []*wire.MsgTx) []*btcutil.Tx {
|
|
|
|
txs := make([]*btcutil.Tx, len(msgs))
|
|
|
|
for i, msg := range msgs {
|
|
|
|
tx := btcutil.NewTx(msg)
|
|
|
|
tx.SetIndex(i)
|
|
|
|
txs[i] = tx
|
|
|
|
}
|
|
|
|
return txs
|
|
|
|
}
|