github.com/trezor/blockbook@v0.4.1-0.20240328132726-e9a08582ee2c/bchain/coins/dash/dashrpc.go (about) 1 package dash 2 3 import ( 4 "encoding/json" 5 6 "github.com/golang/glog" 7 "github.com/juju/errors" 8 "github.com/trezor/blockbook/bchain" 9 "github.com/trezor/blockbook/bchain/coins/btc" 10 ) 11 12 const firstBlockWithSpecialTransactions = 1028160 13 14 // DashRPC is an interface to JSON-RPC bitcoind service 15 type DashRPC struct { 16 *btc.BitcoinRPC 17 } 18 19 // NewDashRPC returns new DashRPC instance 20 func NewDashRPC(config json.RawMessage, pushHandler func(bchain.NotificationType)) (bchain.BlockChain, error) { 21 b, err := btc.NewBitcoinRPC(config, pushHandler) 22 if err != nil { 23 return nil, err 24 } 25 26 s := &DashRPC{ 27 b.(*btc.BitcoinRPC), 28 } 29 s.RPCMarshaler = btc.JSONMarshalerV1{} 30 31 return s, nil 32 } 33 34 // Initialize initializes DashRPC instance. 35 func (b *DashRPC) Initialize() error { 36 ci, err := b.GetChainInfo() 37 if err != nil { 38 return err 39 } 40 chainName := ci.Chain 41 42 params := GetChainParams(chainName) 43 44 // always create parser 45 b.Parser = NewDashParser(params, b.ChainConfig) 46 47 // parameters for getInfo request 48 if params.Net == MainnetMagic { 49 b.Testnet = false 50 b.Network = "livenet" 51 } else { 52 b.Testnet = true 53 b.Network = "testnet" 54 } 55 56 glog.Info("rpc: block chain ", params.Name) 57 58 return nil 59 } 60 61 // GetBlock returns block with given hash 62 func (b *DashRPC) GetBlock(hash string, height uint32) (*bchain.Block, error) { 63 if hash == "" && height < firstBlockWithSpecialTransactions { 64 return b.BitcoinRPC.GetBlock(hash, height) 65 } 66 67 var err error 68 if hash == "" && height > 0 { 69 hash, err = b.GetBlockHash(height) 70 if err != nil { 71 return nil, err 72 } 73 } 74 75 glog.V(1).Info("rpc: getblock (verbosity=1) ", hash) 76 77 res := btc.ResGetBlockThin{} 78 req := btc.CmdGetBlock{Method: "getblock"} 79 req.Params.BlockHash = hash 80 req.Params.Verbosity = 1 81 err = b.Call(&req, &res) 82 83 if err != nil { 84 return nil, errors.Annotatef(err, "hash %v", hash) 85 } 86 if res.Error != nil { 87 return nil, errors.Annotatef(res.Error, "hash %v", hash) 88 } 89 90 txs := make([]bchain.Tx, 0, len(res.Result.Txids)) 91 for _, txid := range res.Result.Txids { 92 tx, err := b.GetTransaction(txid) 93 if err != nil { 94 if err == bchain.ErrTxNotFound { 95 glog.Errorf("rpc: getblock: skipping transanction in block %s due error: %s", hash, err) 96 continue 97 } 98 return nil, err 99 } 100 txs = append(txs, *tx) 101 } 102 block := &bchain.Block{ 103 BlockHeader: res.Result.BlockHeader, 104 Txs: txs, 105 } 106 return block, nil 107 } 108 109 // GetTransactionForMempool returns a transaction by the transaction ID. 110 // It could be optimized for mempool, i.e. without block time and confirmations 111 func (b *DashRPC) GetTransactionForMempool(txid string) (*bchain.Tx, error) { 112 return b.GetTransaction(txid) 113 }