-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5d1ff5
commit 6278ae4
Showing
9 changed files
with
67 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,7 +47,7 @@ const ( | |
|
||
bytePerKb = 1000 | ||
|
||
btcPerSatoshi = 1E-8 | ||
btcPerSatoshi = 1e-8 | ||
) | ||
|
||
var ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,48 @@ | ||
// Copyright (c) 2014-2017 The btcsuite developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package main | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"github.com/btcsuite/btcd/rpcclient" | ||
"log" | ||
) | ||
|
||
func main() { | ||
const ( | ||
url = "http://localhost:8332" | ||
rpcUser = "yourrpcuser" | ||
rpcPass = "yourrpcpass" | ||
) | ||
|
||
// populate request set | ||
reqs := []string{ | ||
`{}`, | ||
`[]`, | ||
`[1]`, | ||
`[1,2,3]`, | ||
`{"foo": "boo"}`, // should be an invalid request | ||
`{"jsonrpc": "1.0", "foo": "boo", "id": "1"}`, | ||
`{"jsonrpc": "1.0", "method": "getblockcount", "params": [], "id": "1"}`, | ||
`{"jsonrpc": "1.0", "method": "getblockcount", "params": "a", "id": "1"}`, // should be invalid since params is neither an array nor a json object. | ||
`[ | ||
{"jsonrpc": "2.0", "method": "getblockcount", "params": [], "id": "1"}, | ||
{"jsonrpc": "2.0", "method": "decodescript", "params": ["ac"]}, | ||
{"jsonrpc": "2.0", "method": "getbestblockhash", "params": [], "id": "2"}, | ||
{"foo": "boo"}, | ||
{"jsonrpc": "2.0", "method": "getblockcount", "id": "9"} /**/ | ||
]`, // should produce invalid request for the `{"foo": "boo"}`. | ||
// Connect to local bitcoin core RPC server using HTTP POST mode. | ||
connCfg := &rpcclient.ConnConfig{ | ||
Host: "localhost:8332", | ||
User: "yourrpcuser", | ||
Pass: "yourrpcpass", | ||
DisableConnectOnNew: true, | ||
HTTPPostMode: true, // Bitcoin core only supports HTTP POST mode | ||
DisableTLS: true, // Bitcoin core does not provide TLS by default | ||
} | ||
|
||
// Connect to local btcd RPC server using websockets. | ||
|
||
client := http.Client{} | ||
|
||
for _, jsonReq := range reqs { | ||
bodyReader := bytes.NewReader([]byte(jsonReq)) | ||
httpReq, err := http.NewRequest("POST", url, bodyReader) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
httpReq.Close = true | ||
httpReq.Header.Set("Content-Type", "application/json") | ||
httpReq.SetBasicAuth(rpcUser, rpcPass) | ||
resp, err := client.Do(httpReq) | ||
if err != nil { | ||
fmt.Println("request:", jsonReq, "response:", err) | ||
return | ||
} | ||
|
||
respBytes, err := ioutil.ReadAll(resp.Body) | ||
resp.Body.Close() | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
|
||
fmt.Println("request:", jsonReq, "response:", string(respBytes)) | ||
// Notice the notification parameter is nil since notifications are | ||
// not supported in HTTP POST mode. | ||
client, err := rpcclient.New(connCfg, nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
defer client.Shutdown() | ||
|
||
// Get the current block count. | ||
batchClient := client.Batch() | ||
|
||
// batch mode requires async requests | ||
blockCount := batchClient.GetBlockCountAsync() | ||
block1 := batchClient.GetBlockHashAsync(1) | ||
batchClient.GetBlockHashAsync(2) | ||
batchClient.GetBlockHashAsync(3) | ||
block4 := batchClient.GetBlockHashAsync(4) | ||
difficulty := batchClient.GetDifficultyAsync() | ||
|
||
batchClient.Send() | ||
//result, err | ||
fmt.Println(blockCount.Receive()) | ||
fmt.Println(block1.Receive()) | ||
fmt.Println(block4.Receive()) | ||
fmt.Println(difficulty.Receive()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters