Skip to content

sample code in readme is misleading, 500 Internal Server Error #8

@huahuayu

Description

@huahuayu

Hi I have a bitcoind run in testnet, I found the sample code is misleading, people who run it the first time will get 500 internal server error, the full reproduce step is as follow:

1. btcd config:

# Generated by https://jlopp.github.io/bitcoin-core-config-generator/

# This config should be placed in following path:
# ~/.bitcoin/bitcoin.conf

# [core]
# Specify the location of the configuration file. To use non-default location, create a default location config file containing this setting.
conf=~/.bitcoind/bitcoin.conf
# Specify a non-default location to store blockchain and other data.
# datadir=/mnt/bitcoin/bitcoind/testnet

# [debug]
# Run this node on the Bitcoin Test Network.
testnet=1

# [rpc]
# Accept command line and JSON-RPC commands.
server=1
# Accept public REST requests.
rest=1
# RPC user
rpcuser=rpcuser
# RPC password
rpcpassword=rpcpass
# RPC allow ip (allow all)
rpcallowip=0.0.0.0/0
# RPC bind
rpcbind=0.0.0.0

2. check the port listend

port 18332 for testnet rpc connection is listening

~ netstat -apnt
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.1.1:53            0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      -
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:18332           0.0.0.0:*               LISTEN      16025/bitcoind
tcp        0      0 0.0.0.0:18333           0.0.0.0:*               LISTEN      16025/bitcoind

3. check the port from local

telnet successful

pro:go-bitcoind shiming$ telnet 10.211.55.7 18332
Trying 10.211.55.7...
Connected to ubuntu.shared.
Escape character is '^]'.
Connection closed by foreign host.

4. run bitcoind

bitcoind -debug=1

5. run sample code

package main

import (
	"github.com/toorop/go-bitcoind"
	"log"
)

const (
	SERVER_HOST        = "10.211.55.7"
	SERVER_PORT        = 18332
	USER               = "rpcuser"
	PASSWD             = "rpcpass"
	USESSL             = false
	WALLET_PASSPHRASE  = "WalletPassphrase"
)

func main() {
	bc, err := bitcoind.New(SERVER_HOST, SERVER_PORT, USER, PASSWD, USESSL)
	if err != nil {
		log.Fatalln(err)
	}

	//walletpassphrase
	err = bc.WalletPassphrase(WALLET_PASSPHRASE, 3600)
	log.Println(err)

	// backupwallet
	err = bc.BackupWallet("/tmp/wallet.dat")
	log.Println(err)


	// dumpprivkey
	privKey, err := bc.DumpPrivKey("1KU5DX7jKECLxh1nYhmQ7CahY7GMNMVLP3")
	log.Println(err, privKey)

}

6. get err

2018/10/06 11:26:25 HTTP error: 500 Internal Server Error
2018/10/06 11:26:25 <nil>
2018/10/06 11:26:25 HTTP error: 500 Internal Server Error 

7. bitcoind debug.log

those rows begin with "Received a POST request for" is the log, but no detail logs, dosen't explain the 500 internal server error. But at least it is connected

......
2018-10-06 03:58:35 sending getheaders (933 bytes) peer=0
2018-10-06 03:58:35 Received a POST request for / from 10.211.55.2:62473
2018-10-06 03:58:35 ThreadRPCServer method=walletpassphrase
2018-10-06 03:58:35 Received a POST request for / from 10.211.55.2:62473
2018-10-06 03:58:35 ThreadRPCServer method=backupwallet
2018-10-06 03:58:35 copied wallet.dat to /tmp/wallet.dat
2018-10-06 03:58:35 Received a POST request for / from 10.211.55.2:62473
2018-10-06 03:58:35 ThreadRPCServer method=dumpprivkey
2018-10-06 03:58:35 tor: Error connecting to Tor control socket
2018-10-06 03:58:35 tor: Not connected to Tor control port 127.0.0.1:9051, trying to reconnect
2018-10-06 03:58:36 received: headers (162003 bytes) peer=0
......

8. my sample code suggestion

looking into the issue by add trace info and invoke another simple client methods, I found it actually successful connected, my code is as below

package main

import (
	"fmt"
	"github.com/toorop/go-bitcoind"
	"log"
)

const (
	SERVER_HOST        = "10.211.55.7"
	SERVER_PORT        = 18332
	USER               = "rpcuser"
	PASSWD             = "rpcpass"
	USESSL             = false
	WALLET_PASSPHRASE  = "WalletPassphrase"
)

func main() {
	bc, err := bitcoind.New(SERVER_HOST, SERVER_PORT, USER, PASSWD, USESSL)
	if err != nil {
		log.Fatalln(err)
	}else{
		fmt.Println("connect successful")
	}

	difficult, err := bc.GetDifficulty()
	if err != nil {
		log.Fatalln(err)
	}else{
		fmt.Printf("difficult:%v\n",difficult)
	}

	count, err := bc.GetBlockCount()
	if err != nil{
		log.Fatalln(err)
	}else{
		fmt.Printf("count:%v\n",count)
	}


	////walletpassphrase
	//err = bc.WalletPassphrase(WALLET_PASSPHRASE, 3600)
	//log.Println(err)
	//
	//// backupwallet
	//err = bc.BackupWallet("/tmp/wallet.dat")
	//log.Println(err)
	//
	//
	//// dumpprivkey
	//privKey, err := bc.DumpPrivKey("1KU5DX7jKECLxh1nYhmQ7CahY7GMNMVLP3")
	//log.Println(err, privKey)

}

/*
output:
connect successful
difficult:1
count:306017
*/

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions