bt解析 开源 java_修复开源项目 btcd RPC 实现比特币获取区块的问题
btcd 提供的 RPC 接口中,獲取區塊和交易詳情接口 GetBlockVerboseTx 存在一個 BUG, 具體可以查看 issue :
getblock 接口詳情如下
getblock "blockhash" ( verbosity )
If verbosity is 0, returns a string that is serialized, hex-encoded data for block 'hash'.
If verbosity is 1, returns an Object with information about block .
If verbosity is 2, returns an Object with information about block and information about each transaction.
Arguments:
1. "blockhash" (string, required) The block hash
2. verbosity (numeric, optional, default=1) 0 for hex encoded data, 1 for a json object, and 2 for json object with transaction data
我沒有在 btcd 提 pull request, 故把修改過的文件提交到這個 repo, 使用 btcd 時直接覆蓋對應文件即可。
修改的代碼
在 btcd 中,獲取區塊和區塊中所有交易詳情的 RPC 方法是:
func (c *Client) GetBlockVerboseTx(blockHash *chainhash.Hash) (*btcjson.GetBlockVerboseResult, error) {
return c.GetBlockVerboseTxAsync(blockHash).Receive()
}
接著我們再看看 GetBlockVerboseTxAsync 方法:
func (c *Client) GetBlockVerboseTxAsync(blockHash *chainhash.Hash) FutureGetBlockVerboseResult {
hash := ""
if blockHash != nil {
hash = blockHash.String()
}
cmd := btcjson.NewGetBlockCmd(hash, btcjson.Bool(true), btcjson.Bool(true))
return c.sendCmd(cmd)
}
其中 NewGetBlockCmd 函數接受三個參數,后面兩個為布爾類型,顯然是不對的,getblock 接口格式為 getblock "blockhash" ( verbosity )
因為我只是做了小修改再加上對 btcd 源碼不怎么熟悉,為了不引入新的 bug ,我在這里只添加代碼,不刪除,不修改 ...
func (c *Client) GetBlockVerboseTxM(blockHash *chainhash.Hash) (*btcjson.GetBlockVerboseResult, error) {
return c.GetBlockVerboseTxAsyncM(blockHash).Receive()
}
...
func (c *Client) GetBlockVerboseTxAsyncM(blockHash *chainhash.Hash) FutureGetBlockVerboseResult {
hash := ""
if blockHash != nil {
hash = blockHash.String()
}
cmd := btcjson.NewGetBlockCmdM(hash, 2)
return c.sendCmd(cmd)
}
在 rpcclient/chain.go 修改,不刪除原來的方法,追加修改的兩個方法。接下來繼續看看 NewGetBlockCmdM 方法,在 btcjson/chainsvrcmds.go 文件中:
func NewGetBlockCmdM(hash string, verbose int) *GetBlockCmdM {
return &GetBlockCmdM{
Hash: hash,
Verbose: &verbose,
}
}
...
// GetBlockCmdM defines the getblock JSON-RPC command.
type GetBlockCmdM struct {
Hash string
Verbose *int `jsonrpcdefault:"1"`
}
...
,通過 getblock "blockhash" 2 在比特幣節點的返回數據中,Unmarshal 后的結構體 GetBlockVerboseResult Tx 字段類型應該改為 TxRawResult,在 btcjson/chainsvrresults.go 文件中:
...
type GetBlockVerboseResult struct {
Hash string `json:"hash"`
Confirmations uint64 `json:"confirmations"`
StrippedSize int32 `json:"strippedsize"`
Size int32 `json:"size"`
Weight int32 `json:"weight"`
Height int64 `json:"height"`
Version int32 `json:"version"`
VersionHex string `json:"versionHex"`
MerkleRoot string `json:"merkleroot"`
Tx []TxRawResult `json:"tx,omitempty"`
RawTx []TxRawResult `json:"rawtx,omitempty"`
Time int64 `json:"time"`
Nonce uint32 `json:"nonce"`
Bits string `json:"bits"`
Difficulty float64 `json:"difficulty"`
PreviousHash string `json:"previousblockhash"`
NextHash string `json:"nextblockhash,omitempty"`
}
最后,在 btcjson/chainsvrcmds.go 文件中修改 getblock 命令和修改后的方法綁定:
MustRegisterCmd("getblock", (*GetBlockCmdM)(nil), flags)
fix verbocity in getblock command for btcd
When i call getblock by btcd rpcclient, the generated rpc command looks like this:
{
"jsonrpc":"1.0",
"method":"getblock",
"params":
["0000000000000000015ed7a2934f8ecb39eda15459376fff64284e1a87688d5d",true,true],
"id":1
}
the right generation sendCmd should look like this:
{
"jsonrpc":"1.0",
"method":"getblock",
"params":["0000000000000000015ed7a2934f8ecb39eda15459376fff64284e1a87688d5d",2],
"id":1
}
Original "getblock" command accept 2 arguments:
blockhash
verbosity (numeric, optional, default=1) 0 for hex encoded data, 1 for a json object, and 2 for json object with transaction data
getblock "blockhash" ( verbosity )
If verbosity is 0, returns a string that is serialized, hex-encoded data for block 'hash'.
If verbosity is 1, returns an Object with information about block .
If verbosity is 2, returns an Object with information about block and information about each transaction.
Arguments:
1. "blockhash" (string, required) The block hash
2. verbosity (numeric, optional, default=1) 0 for hex encoded data, 1 for a json object, and 2 for json object with transaction data
總結
以上是生活随笔為你收集整理的bt解析 开源 java_修复开源项目 btcd RPC 实现比特币获取区块的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 乒乓球比赛赛程_国乒今年最后一站比赛延期
- 下一篇: 220v变12v初级线圈匝数_12V升2