diff --git a/bgld.go b/bgld.go index 75dde24..9da6a1c 100644 --- a/bgld.go +++ b/bgld.go @@ -112,12 +112,12 @@ type BlockHeader struct { Time int64 Mediantime int64 Nonce uint32 - Bits uint32 + Bits string Difficulty float64 Chainwork string Txes int `json:"nTx"` - Previousblockhash string `json:"omitempty"` - Nextblockhash string `json:"omitempty"` + Previousblockhash string `json:"previousblockhash,omitempty"` + Nextblockhash string `json:"nextblockhash,omitempty"` } func (b *Bgld) GetBlockheader(blockHash string) (*BlockHeader, error) { diff --git a/blockheader_json_test.go b/blockheader_json_test.go new file mode 100644 index 0000000..410d2a1 --- /dev/null +++ b/blockheader_json_test.go @@ -0,0 +1,45 @@ +package bgld + +import ( + "fmt" + "net/http" + "net/http/httptest" + "net/url" + "strconv" + "testing" +) + +func TestGetBlockheaderDecodesBitgesellCoreResponse(t *testing.T) { + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, `{"result":{"hash":"0000000000000000000000000000000000000000000000000000000000000001","confirmations":2,"height":1,"version":536870912,"versionHex":"20000000","merkleroot":"4d5e6f","time":1700000000,"mediantime":1700000000,"nonce":42,"bits":"1d00ffff","difficulty":1,"chainwork":"0000000000000000000000000000000000000000000000000000000000000002","nTx":1,"previousblockhash":"0000000000000000000000000000000000000000000000000000000000000000","nextblockhash":"0000000000000000000000000000000000000000000000000000000000000002"},"error":null,"id":1}`) + }) + ts := httptest.NewServer(handler) + defer ts.Close() + + serverURL, err := url.Parse(ts.URL) + if err != nil { + t.Fatal(err) + } + port, err := strconv.Atoi(serverURL.Port()) + if err != nil { + t.Fatal(err) + } + client, err := New(serverURL.Hostname(), port, "user", "pass", false) + if err != nil { + t.Fatal(err) + } + + header, err := client.GetBlockheader("0000000000000000000000000000000000000000000000000000000000000001") + if err != nil { + t.Fatalf("GetBlockheader returned error: %v", err) + } + if header.Bits != "1d00ffff" { + t.Fatalf("Bits = %q, want %q", header.Bits, "1d00ffff") + } + if header.Previousblockhash != "0000000000000000000000000000000000000000000000000000000000000000" { + t.Fatalf("Previousblockhash was not decoded: %q", header.Previousblockhash) + } + if header.Nextblockhash != "0000000000000000000000000000000000000000000000000000000000000002" { + t.Fatalf("Nextblockhash was not decoded: %q", header.Nextblockhash) + } +}