Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bgld.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
45 changes: 45 additions & 0 deletions blockheader_json_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}