forked from cerc-io/ipld-eth-server
emulate btc data streamer over http; misc fixes
This commit is contained in:
@@ -17,6 +17,7 @@
|
||||
package btc
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
@@ -62,7 +63,7 @@ func (pc *PayloadConverter) Convert(payload shared.RawChainData) (shared.Streame
|
||||
SignatureScript: in.SignatureScript,
|
||||
PreviousOutPointHash: in.PreviousOutPoint.Hash.String(),
|
||||
PreviousOutPointIndex: in.PreviousOutPoint.Index,
|
||||
TxWitness: in.Witness,
|
||||
TxWitness: convertBytesToHexArray(in.Witness),
|
||||
}
|
||||
}
|
||||
for i, out := range tx.MsgTx().TxOut {
|
||||
@@ -91,3 +92,11 @@ func (pc *PayloadConverter) Convert(payload shared.RawChainData) (shared.Streame
|
||||
TxMetaData: txMeta,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func convertBytesToHexArray(bytea [][]byte) []string {
|
||||
var strs []string
|
||||
for _, b := range bytea {
|
||||
strs = append(strs, hex.EncodeToString(b))
|
||||
}
|
||||
return strs
|
||||
}
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
// 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 (
|
||||
"bytes"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btcd/rpcclient"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/vulcanize/vulcanizedb/pkg/super_node/shared"
|
||||
)
|
||||
|
||||
// HTTPPayloadStreamer satisfies the PayloadStreamer interface for bitcoin over http endpoints (since bitcoin core doesn't support websockets)
|
||||
type HTTPPayloadStreamer struct {
|
||||
Config *rpcclient.ConnConfig
|
||||
lastHash []byte
|
||||
}
|
||||
|
||||
// NewHTTPPayloadStreamer creates a pointer to a new PayloadStreamer which satisfies the PayloadStreamer interface for bitcoin
|
||||
func NewHTTPPayloadStreamer(clientConfig *rpcclient.ConnConfig) *HTTPPayloadStreamer {
|
||||
return &HTTPPayloadStreamer{
|
||||
Config: clientConfig,
|
||||
}
|
||||
}
|
||||
|
||||
// Stream is the main loop for subscribing to data from the btc block notifications
|
||||
// Satisfies the shared.PayloadStreamer interface
|
||||
func (ps *HTTPPayloadStreamer) Stream(payloadChan chan shared.RawChainData) (shared.ClientSubscription, error) {
|
||||
logrus.Info("streaming block payloads from btc")
|
||||
client, err := rpcclient.New(ps.Config, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ticker := time.NewTicker(time.Second * 5)
|
||||
errChan := make(chan error)
|
||||
go func() {
|
||||
for {
|
||||
// start at
|
||||
select {
|
||||
case <-ticker.C:
|
||||
height, err := client.GetBlockCount()
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
continue
|
||||
}
|
||||
blockHash, err := client.GetBlockHash(height)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
continue
|
||||
}
|
||||
blockHashBytes := blockHash.CloneBytes()
|
||||
if bytes.Equal(blockHashBytes, ps.lastHash) {
|
||||
continue
|
||||
}
|
||||
ps.lastHash = blockHashBytes
|
||||
block, err := client.GetBlock(blockHash)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
continue
|
||||
}
|
||||
payloadChan <- BlockPayload{
|
||||
Header: &block.Header,
|
||||
Height: height,
|
||||
Txs: msgTxsToUtilTxs(block.Transactions),
|
||||
}
|
||||
default:
|
||||
}
|
||||
}
|
||||
}()
|
||||
return &HTTPClientSubscription{client: client, errChan: errChan}, nil
|
||||
}
|
||||
|
||||
// HTTPClientSubscription is a wrapper around the underlying bitcoind rpc client
|
||||
// to fit the shared.ClientSubscription interface
|
||||
type HTTPClientSubscription struct {
|
||||
client *rpcclient.Client
|
||||
errChan chan error
|
||||
}
|
||||
|
||||
// Unsubscribe satisfies the rpc.Subscription interface
|
||||
func (bcs *HTTPClientSubscription) Unsubscribe() {
|
||||
bcs.client.Shutdown()
|
||||
}
|
||||
|
||||
// Err() satisfies the rpc.Subscription interface
|
||||
func (bcs *HTTPClientSubscription) Err() <-chan error {
|
||||
return bcs.errChan
|
||||
}
|
||||
@@ -103,29 +103,10 @@ func (in *CIDIndexer) indexTransactionCID(tx *sqlx.Tx, transaction TxModelWithIn
|
||||
}
|
||||
|
||||
func (in *CIDIndexer) indexTxInput(tx *sqlx.Tx, txInput TxInput, txID int64) error {
|
||||
// resolve zero-value hash to null value (coinbase tx input with no referenced outputs)
|
||||
if txInput.PreviousOutPointHash == "0000000000000000000000000000000000000000000000000000000000000000" {
|
||||
_, err := tx.Exec(`INSERT INTO btc.tx_inputs (tx_id, index, witness, sig_script, outpoint_id)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (tx_id, index) DO UPDATE SET (witness, sig_script, outpoint_id) = ($3, $4, $5)`,
|
||||
txID, txInput.Index, pq.Array(txInput.TxWitness), txInput.SignatureScript, nil)
|
||||
return err
|
||||
}
|
||||
var referencedOutPutID int64
|
||||
if err := tx.Get(&referencedOutPutID, `SELECT tx_outputs.id FROM btc.transaction_cids, btc.tx_outputs
|
||||
WHERE tx_outputs.tx_id = transaction_cids.id
|
||||
AND tx_hash = $1
|
||||
AND tx_outputs.index = $2`, txInput.PreviousOutPointHash, txInput.PreviousOutPointIndex); err != nil {
|
||||
logrus.Errorf("btc indexer could not find the tx output (tx hash %s, output index %d) referenced in tx input %d of tx id %d", txInput.PreviousOutPointHash, txInput.PreviousOutPointIndex, txInput.Index, txID)
|
||||
return err
|
||||
}
|
||||
if referencedOutPutID == 0 {
|
||||
return fmt.Errorf("btc indexer could not find the tx output (tx hash %s, output index %d) referenced in tx input %d of tx id %d", txInput.PreviousOutPointHash, txInput.PreviousOutPointIndex, txInput.Index, txID)
|
||||
}
|
||||
_, err := tx.Exec(`INSERT INTO btc.tx_inputs (tx_id, index, witness, sig_script, outpoint_id)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (tx_id, index) DO UPDATE SET (witness, sig_script, outpoint_id) = ($3, $4, $5)`,
|
||||
txID, txInput.Index, pq.Array(txInput.TxWitness), txInput.SignatureScript, referencedOutPutID)
|
||||
_, err := tx.Exec(`INSERT INTO btc.tx_inputs (tx_id, index, witness, sig_script, outpoint_tx_hash, outpoint_index)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
ON CONFLICT (tx_id, index) DO UPDATE SET (witness, sig_script, outpoint_tx_hash, outpoint_index) = ($3, $4, $5, $6)`,
|
||||
txID, txInput.Index, pq.Array(txInput.TxWitness), txInput.SignatureScript, txInput.PreviousOutPointHash, txInput.PreviousOutPointIndex)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -43,8 +43,6 @@ var _ = Describe("Indexer", func() {
|
||||
|
||||
Describe("Index", func() {
|
||||
It("Indexes CIDs and related metadata into vulcanizedb", func() {
|
||||
err = repo.Index(&mocks.DummyCIDPayloadForFKReference)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
err = repo.Index(&mocks.MockCIDPayload)
|
||||
Expect(err).ToNot(HaveOccurred())
|
||||
pgStr := `SELECT * FROM btc.header_cids
|
||||
|
||||
@@ -29,7 +29,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
MockBlockHeight int32 = 1337
|
||||
MockBlockHeight int64 = 1337
|
||||
MockBlock = wire.MsgBlock{
|
||||
Header: wire.BlockHeader{
|
||||
Version: 1,
|
||||
|
||||
@@ -59,11 +59,10 @@ type TxInput struct {
|
||||
ID int64 `db:"id"`
|
||||
TxID int64 `db:"tx_id"`
|
||||
Index int64 `db:"index"`
|
||||
TxWitness [][]byte `db:"witness"`
|
||||
TxWitness []string `db:"witness"`
|
||||
SignatureScript []byte `db:"sig_script"`
|
||||
PreviousOutPointID int64 `db:"outpoint_id"`
|
||||
PreviousOutPointIndex uint32
|
||||
PreviousOutPointHash string
|
||||
PreviousOutPointIndex uint32 `db:"outpoint_tx_hash"`
|
||||
PreviousOutPointHash string `db:"outpoint_index"`
|
||||
}
|
||||
|
||||
// TxOutput is the db model for btc.tx_outputs table
|
||||
|
||||
@@ -55,7 +55,7 @@ func (fetcher *PayloadFetcher) FetchAt(blockHeights []uint64) ([]shared.RawChain
|
||||
return nil, err
|
||||
}
|
||||
blockPayloads[i] = BlockPayload{
|
||||
Height: int32(height),
|
||||
Height: int64(height),
|
||||
Header: &block.Header,
|
||||
Txs: msgTxsToUtilTxs(block.Transactions),
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ func (ps *PayloadStreamer) Stream(payloadChan chan shared.RawChainData) (shared.
|
||||
// Notification handler for block connections, forwards new block data to the payloadChan
|
||||
OnFilteredBlockConnected: func(height int32, header *wire.BlockHeader, txs []*btcutil.Tx) {
|
||||
payloadChan <- BlockPayload{
|
||||
Height: height,
|
||||
Height: int64(height),
|
||||
Header: header,
|
||||
Txs: txs,
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ import (
|
||||
|
||||
// BlockPayload packages the block and tx data received from block connection notifications
|
||||
type BlockPayload struct {
|
||||
Height int32
|
||||
Height int64
|
||||
Header *wire.BlockHeader
|
||||
Txs []*btcutil.Tx
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user