110 lines
2.6 KiB
Go
110 lines
2.6 KiB
Go
package deeponion
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
"github.com/golang/glog"
|
|
"github.com/juju/errors"
|
|
"spacecruft.org/spacecruft/blockbook/bchain"
|
|
"spacecruft.org/spacecruft/blockbook/bchain/coins/btc"
|
|
)
|
|
|
|
// DeepOnionRPC is an interface to JSON-RPC bitcoind service.
|
|
type DeepOnionRPC struct {
|
|
*btc.BitcoinRPC
|
|
}
|
|
|
|
// NewDeepOnionRPC returns new DeepOnionRPC instance.
|
|
func NewDeepOnionRPC(config json.RawMessage, pushHandler func(bchain.NotificationType)) (bchain.BlockChain, error) {
|
|
b, err := btc.NewBitcoinRPC(config, pushHandler)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
s := &DeepOnionRPC{
|
|
b.(*btc.BitcoinRPC),
|
|
}
|
|
s.RPCMarshaler = btc.JSONMarshalerV2{}
|
|
s.ChainConfig.SupportsEstimateFee = false
|
|
|
|
return s, nil
|
|
}
|
|
|
|
// Initialize initializes DeepOnionRPC instance.
|
|
func (b *DeepOnionRPC) Initialize() error {
|
|
ci, err := b.GetChainInfo()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
chainName := ci.Chain
|
|
|
|
glog.Info("Chain name ", chainName)
|
|
params := GetChainParams(chainName)
|
|
|
|
// always create parser
|
|
b.Parser = NewDeepOnionParser(params, b.ChainConfig)
|
|
|
|
// parameters for getInfo request
|
|
if params.Net == MainnetMagic {
|
|
b.Testnet = false
|
|
b.Network = "livenet"
|
|
} else {
|
|
b.Testnet = true
|
|
b.Network = "testnet"
|
|
}
|
|
|
|
glog.Info("rpc: block chain ", params.Name)
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetBlock returns block with given hash.
|
|
func (s *DeepOnionRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) {
|
|
var err error
|
|
if hash == "" && height > 0 {
|
|
hash, err = s.GetBlockHash(height)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
glog.V(1).Info("rpc: getblock (verbosity=1) ", hash)
|
|
|
|
res := btc.ResGetBlockThin{}
|
|
req := btc.CmdGetBlock{Method: "getblock"}
|
|
req.Params.BlockHash = hash
|
|
req.Params.Verbosity = 1
|
|
err = s.Call(&req, &res)
|
|
|
|
if err != nil {
|
|
return nil, errors.Annotatef(err, "hash %v", hash)
|
|
}
|
|
if res.Error != nil {
|
|
return nil, errors.Annotatef(res.Error, "hash %v", hash)
|
|
}
|
|
|
|
txs := make([]bchain.Tx, 0, len(res.Result.Txids))
|
|
for _, txid := range res.Result.Txids {
|
|
tx, err := s.GetTransaction(txid)
|
|
if err != nil {
|
|
if err == bchain.ErrTxNotFound {
|
|
glog.Errorf("rpc: getblock: skipping transanction in block %s due error: %s", hash, err)
|
|
continue
|
|
}
|
|
return nil, err
|
|
}
|
|
txs = append(txs, *tx)
|
|
}
|
|
block := &bchain.Block{
|
|
BlockHeader: res.Result.BlockHeader,
|
|
Txs: txs,
|
|
}
|
|
return block, nil
|
|
}
|
|
|
|
// GetTransactionForMempool returns a transaction by the transaction ID.
|
|
// It could be optimized for mempool, i.e. without block time and confirmations
|
|
func (s *DeepOnionRPC) GetTransactionForMempool(txid string) (*bchain.Tx, error) {
|
|
return s.GetTransaction(txid)
|
|
}
|