Add socket.io unit tests

This commit is contained in:
Martin Boehm 2018-10-19 16:36:22 +02:00
parent c852214407
commit 017c6b3736
5 changed files with 151 additions and 33 deletions

View File

@ -413,6 +413,8 @@ func testTxCache(t *testing.T, d *RocksDB, b *bchain.Block, tx *bchain.Tx) {
if b.Height != height {
t.Fatalf("GetTx: got height %v, expected %v", height, b.Height)
}
// Confirmations are not stored in the DB, set them from input tx
gtx.Confirmations = tx.Confirmations
if fmt.Sprint(gtx) != fmt.Sprint(tx) {
t.Errorf("GetTx: %v, want %v", gtx, tx)
}

View File

@ -1,4 +1,4 @@
// build unittest
// +build unittest
package server
@ -15,9 +15,12 @@ import (
"os"
"strings"
"testing"
"time"
"github.com/golang/glog"
"github.com/jakm/btcutil/chaincfg"
"github.com/martinboehm/golang-socketio"
"github.com/martinboehm/golang-socketio/transport"
)
func TestMain(m *testing.M) {
@ -60,9 +63,11 @@ func setupPublicHTTPServer(t *testing.T) (*PublicServer, string) {
&btc.Configuration{BlockAddressesToKeep: 1})
d, is, path := setupRocksDB(t, parser)
// setup internal state and match BestHeight to test data
is.Coin = "Fakecoin"
is.CoinLabel = "Fake Coin"
is.CoinShortcut = "FAKE"
is.BestHeight = 225494
metrics, err := common.GetMetrics("Fakecoin")
if err != nil {
@ -74,7 +79,8 @@ func setupPublicHTTPServer(t *testing.T) (*PublicServer, string) {
glog.Fatal("fakechain: ", err)
}
txCache, err := db.NewTxCache(d, chain, metrics, is, true)
// caching is switched off because test transactions do not have hex data
txCache, err := db.NewTxCache(d, chain, metrics, is, false)
if err != nil {
glog.Fatal("txCache: ", err)
}
@ -326,7 +332,7 @@ func httpTests(t *testing.T, ts *httptest.Server) {
status: http.StatusOK,
contentType: "application/json; charset=utf-8",
body: []string{
`{"txid":"05e2e48aeabdd9b75def7b48d756ba304713c2aba7b522bf9dbc893fc4231b07","vin":[{"txid":"effd9ef509383d536b1c8af5bf434c8efbf521a4f2befd4022bbd68694b4ac75","vout":2,"n":0,"scriptSig":{"hex":""},"addresses":["2NEVv9LJmAnY99W1pFoc5UJjVdypBqdnvu1"],"value":"0.00009876"}],"vout":[{"value":"0.00009","n":0,"scriptPubKey":{"hex":"a914e921fc4912a315078f370d959f2c4f7b6d2a683c87","addresses":["2NEVv9LJmAnY99W1pFoc5UJjVdypBqdnvu1"]}}],"blockheight":0,"confirmations":0,"time":22549400002,"blocktime":22549400002,"valueOut":"0.00009","valueIn":"0.00009876","fees":"0.00000876","hex":""}`,
`{"txid":"05e2e48aeabdd9b75def7b48d756ba304713c2aba7b522bf9dbc893fc4231b07","vin":[{"txid":"effd9ef509383d536b1c8af5bf434c8efbf521a4f2befd4022bbd68694b4ac75","vout":2,"n":0,"scriptSig":{"hex":""},"addresses":["2NEVv9LJmAnY99W1pFoc5UJjVdypBqdnvu1"],"value":"0.00009876"}],"vout":[{"value":"0.00009","n":0,"scriptPubKey":{"hex":"a914e921fc4912a315078f370d959f2c4f7b6d2a683c87","addresses":["2NEVv9LJmAnY99W1pFoc5UJjVdypBqdnvu1"]}}],"blockhash":"00000000eb0443fd7dc4a1ed5c686a8e995057805f9a161d9a5a77a95e72b7b6","blockheight":225494,"confirmations":1,"time":22549400002,"blocktime":22549400002,"valueOut":"0.00009","valueIn":"0.00009876","fees":"0.00000876","hex":""}`,
},
},
{
@ -344,7 +350,7 @@ func httpTests(t *testing.T, ts *httptest.Server) {
status: http.StatusOK,
contentType: "application/json; charset=utf-8",
body: []string{
`{"hex":"","txid":"00b2c06055e5e90e9c82bd4181fde310104391a7fa4f289b1704e5d90caa3840","version":0,"locktime":0,"vin":null,"vout":[{"ValueSat":100000000,"value":0,"n":0,"scriptPubKey":{"hex":"76a914010d39800f86122416e28f485029acf77507169288ac","addresses":null}},{"ValueSat":12345,"value":0,"n":1,"scriptPubKey":{"hex":"76a9148bdf0aa3c567aa5975c2e61321b8bebbe7293df688ac","addresses":null}}],"time":22549300000,"blocktime":22549300000}`,
`{"hex":"","txid":"00b2c06055e5e90e9c82bd4181fde310104391a7fa4f289b1704e5d90caa3840","version":0,"locktime":0,"vin":null,"vout":[{"ValueSat":100000000,"value":0,"n":0,"scriptPubKey":{"hex":"76a914010d39800f86122416e28f485029acf77507169288ac","addresses":null}},{"ValueSat":12345,"value":0,"n":1,"scriptPubKey":{"hex":"76a9148bdf0aa3c567aa5975c2e61321b8bebbe7293df688ac","addresses":null}}],"confirmations":2,"time":22549300000,"blocktime":22549300000}`,
},
},
{
@ -362,10 +368,11 @@ func httpTests(t *testing.T, ts *httptest.Server) {
status: http.StatusOK,
contentType: "application/json; charset=utf-8",
body: []string{
`{"page":1,"totalPages":1,"itemsOnPage":1000,"hash":"0000000076fbbed90fd75b0e18856aa35baa984e9c9d444cf746ad85e94e2997","previousblockhash":"","nextblockhash":"","height":225493,"confirmations":0,"size":1234567,"time":1534858021,"version":0,"merkleroot":"","nonce":0,"bits":"","difficulty":0,"TxCount":2,"txs":[{"txid":"00b2c06055e5e90e9c82bd4181fde310104391a7fa4f289b1704e5d90caa3840","vin":[],"vout":[{"value":"1","n":0,"scriptPubKey":{"hex":"","addresses":["mfcWp7DB6NuaZsExybTTXpVgWz559Np4Ti"]}},{"value":"0.00012345","n":1,"scriptPubKey":{"hex":"","addresses":["mtGXQvBowMkBpnhLckhxhbwYK44Gs9eEtz"]}}],"blockhash":"0000000076fbbed90fd75b0e18856aa35baa984e9c9d444cf746ad85e94e2997","blockheight":225493,"confirmations":2,"time":1534858021,"blocktime":1534858021,"valueOut":"1.00012345","valueIn":"0","fees":"0","hex":""},{"txid":"effd9ef509383d536b1c8af5bf434c8efbf521a4f2befd4022bbd68694b4ac75","vin":[],"vout":[{"value":"12345.67890123","n":0,"scriptPubKey":{"hex":"","addresses":["mv9uLThosiEnGRbVPS7Vhyw6VssbVRsiAw"]}},{"value":"0.00000001","n":1,"scriptPubKey":{"hex":"","addresses":["2Mz1CYoppGGsLNUGF2YDhTif6J661JitALS"]}},{"value":"0.00009876","n":2,"scriptPubKey":{"hex":"","addresses":["2NEVv9LJmAnY99W1pFoc5UJjVdypBqdnvu1"]}}],"blockhash":"0000000076fbbed90fd75b0e18856aa35baa984e9c9d444cf746ad85e94e2997","blockheight":225493,"confirmations":2,"time":1534858021,"blocktime":1534858021,"valueOut":"12345.679","valueIn":"0","fees":"0","hex":""}]}`,
`{"page":1,"totalPages":1,"itemsOnPage":1000,"hash":"0000000076fbbed90fd75b0e18856aa35baa984e9c9d444cf746ad85e94e2997","previousblockhash":"","nextblockhash":"","height":225493,"confirmations":2,"size":1234567,"time":1534858021,"version":0,"merkleroot":"","nonce":0,"bits":"","difficulty":0,"TxCount":2,"txs":[{"txid":"00b2c06055e5e90e9c82bd4181fde310104391a7fa4f289b1704e5d90caa3840","vin":[],"vout":[{"value":"1","n":0,"scriptPubKey":{"hex":"","addresses":["mfcWp7DB6NuaZsExybTTXpVgWz559Np4Ti"]}},{"value":"0.00012345","n":1,"scriptPubKey":{"hex":"","addresses":["mtGXQvBowMkBpnhLckhxhbwYK44Gs9eEtz"]}}],"blockhash":"0000000076fbbed90fd75b0e18856aa35baa984e9c9d444cf746ad85e94e2997","blockheight":225493,"confirmations":2,"time":1534858021,"blocktime":1534858021,"valueOut":"1.00012345","valueIn":"0","fees":"0","hex":""},{"txid":"effd9ef509383d536b1c8af5bf434c8efbf521a4f2befd4022bbd68694b4ac75","vin":[],"vout":[{"value":"12345.67890123","n":0,"scriptPubKey":{"hex":"","addresses":["mv9uLThosiEnGRbVPS7Vhyw6VssbVRsiAw"]}},{"value":"0.00000001","n":1,"scriptPubKey":{"hex":"","addresses":["2Mz1CYoppGGsLNUGF2YDhTif6J661JitALS"]}},{"value":"0.00009876","n":2,"scriptPubKey":{"hex":"","addresses":["2NEVv9LJmAnY99W1pFoc5UJjVdypBqdnvu1"]}}],"blockhash":"0000000076fbbed90fd75b0e18856aa35baa984e9c9d444cf746ad85e94e2997","blockheight":225493,"confirmations":2,"time":1534858021,"blocktime":1534858021,"valueOut":"12345.679","valueIn":"0","fees":"0","hex":""}]}`,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp, err := http.DefaultClient.Do(tt.r)
@ -394,6 +401,107 @@ func httpTests(t *testing.T, ts *httptest.Server) {
}
}
func socketioTests(t *testing.T, ts *httptest.Server) {
type socketioReq struct {
Method string `json:"method"`
Params []interface{} `json:"params"`
}
url := strings.Replace(ts.URL, "http://", "ws://", 1) + "/socket.io/"
s, err := gosocketio.Dial(url, transport.GetDefaultWebsocketTransport())
if err != nil {
t.Fatal(err)
}
defer s.Close()
tests := []struct {
name string
req socketioReq
want string
}{
{
name: "getInfo",
req: socketioReq{"getInfo", []interface{}{}},
want: `{"result":{"blocks":225494,"testnet":true,"network":"fakecoin","subversion":"/Fakecoin:0.0.1/","coin_name":"Fakecoin","about":"Blockbook - blockchain indexer for TREZOR wallet https://trezor.io/. Do not use for any other purpose."}}`,
},
{
name: "estimateFee",
req: socketioReq{"estimateFee", []interface{}{17}},
want: `{"result":0.000034}`,
},
{
name: "estimateSmartFee",
req: socketioReq{"estimateSmartFee", []interface{}{19, true}},
want: `{"result":0.000019}`,
},
{
name: "getAddressTxids",
req: socketioReq{"getAddressTxids", []interface{}{
[]string{"mtGXQvBowMkBpnhLckhxhbwYK44Gs9eEtz"},
map[string]interface{}{
"start": 2000000,
"end": 0,
"queryMempool": false,
},
}},
want: `{"result":["7c3be24063f268aaa1ed81b64776798f56088757641a34fb156c4f51ed2e9d25","00b2c06055e5e90e9c82bd4181fde310104391a7fa4f289b1704e5d90caa3840"]}`,
},
{
name: "getAddressTxids limited range",
req: socketioReq{"getAddressTxids", []interface{}{
[]string{"mtGXQvBowMkBpnhLckhxhbwYK44Gs9eEtz"},
map[string]interface{}{
"start": 225494,
"end": 225494,
"queryMempool": false,
},
}},
want: `{"result":["7c3be24063f268aaa1ed81b64776798f56088757641a34fb156c4f51ed2e9d25"]}`,
},
{
name: "getAddressHistory",
req: socketioReq{"getAddressHistory", []interface{}{
[]string{"mtGXQvBowMkBpnhLckhxhbwYK44Gs9eEtz"},
map[string]interface{}{
"start": 2000000,
"end": 0,
"queryMempool": false,
"from": 0,
"to": 5,
},
}},
want: `{"result":{"totalCount":2,"items":[{"addresses":{"mtGXQvBowMkBpnhLckhxhbwYK44Gs9eEtz":{"inputIndexes":[1],"outputIndexes":[]}},"satoshis":-12345,"confirmations":1,"tx":{"hex":"","height":225494,"blockTimestamp":22549400000,"version":0,"hash":"7c3be24063f268aaa1ed81b64776798f56088757641a34fb156c4f51ed2e9d25","inputs":[{"txid":"effd9ef509383d536b1c8af5bf434c8efbf521a4f2befd4022bbd68694b4ac75","outputIndex":0,"script":"","sequence":0,"address":"mv9uLThosiEnGRbVPS7Vhyw6VssbVRsiAw","satoshis":1234567890123},{"txid":"00b2c06055e5e90e9c82bd4181fde310104391a7fa4f289b1704e5d90caa3840","outputIndex":1,"script":"","sequence":0,"address":"mtGXQvBowMkBpnhLckhxhbwYK44Gs9eEtz","satoshis":12345}],"inputSatoshis":1234567902468,"outputs":[{"satoshis":317283951061,"script":"76a914ccaaaf374e1b06cb83118453d102587b4273d09588ac","address":"mzB8cYrfRwFRFAGTDzV8LkUQy5BQicxGhX"},{"satoshis":917283951061,"script":"76a9148d802c045445df49613f6a70ddd2e48526f3701f88ac","address":"mtR97eM2HPWVM6c8FGLGcukgaHHQv7THoL"}],"outputSatoshis":1234567902122,"feeSatoshis":346}},{"addresses":{"mtGXQvBowMkBpnhLckhxhbwYK44Gs9eEtz":{"inputIndexes":[],"outputIndexes":[1]}},"satoshis":12345,"confirmations":2,"tx":{"hex":"","height":225493,"blockTimestamp":22549300000,"version":0,"hash":"00b2c06055e5e90e9c82bd4181fde310104391a7fa4f289b1704e5d90caa3840","inputs":[],"outputs":[{"satoshis":100000000,"script":"76a914010d39800f86122416e28f485029acf77507169288ac","address":"mfcWp7DB6NuaZsExybTTXpVgWz559Np4Ti"},{"satoshis":12345,"script":"76a9148bdf0aa3c567aa5975c2e61321b8bebbe7293df688ac","address":"mtGXQvBowMkBpnhLckhxhbwYK44Gs9eEtz"}],"outputSatoshis":100012345}}]}}`,
},
{
name: "getBlockHeader",
req: socketioReq{"getBlockHeader", []interface{}{225493}},
want: `{"result":{"hash":"0000000076fbbed90fd75b0e18856aa35baa984e9c9d444cf746ad85e94e2997","version":0,"confirmations":0,"height":0,"chainWork":"","nextHash":"","merkleRoot":"","time":0,"medianTime":0,"nonce":0,"bits":"","difficulty":0}}`,
},
{
name: "getDetailedTransaction",
req: socketioReq{"getDetailedTransaction", []interface{}{"3d90d15ed026dc45e19ffb52875ed18fa9e8012ad123d7f7212176e2b0ebdb71"}},
want: `{"result":{"hex":"","height":225494,"blockTimestamp":22549400001,"version":0,"hash":"3d90d15ed026dc45e19ffb52875ed18fa9e8012ad123d7f7212176e2b0ebdb71","inputs":[{"txid":"7c3be24063f268aaa1ed81b64776798f56088757641a34fb156c4f51ed2e9d25","outputIndex":0,"script":"","sequence":0,"address":"mzB8cYrfRwFRFAGTDzV8LkUQy5BQicxGhX","satoshis":317283951061},{"txid":"effd9ef509383d536b1c8af5bf434c8efbf521a4f2befd4022bbd68694b4ac75","outputIndex":1,"script":"","sequence":0,"address":"2Mz1CYoppGGsLNUGF2YDhTif6J661JitALS","satoshis":1}],"inputSatoshis":317283951062,"outputs":[{"satoshis":118641975500,"script":"76a914b434eb0c1a3b7a02e8a29cc616e791ef1e0bf51f88ac","address":"mwwoKQE5Lb1G4picHSHDQKg8jw424PF9SC"},{"satoshis":198641975500,"script":"76a9143f8ba3fda3ba7b69f5818086e12223c6dd25e3c888ac","address":"mmJx9Y8ayz9h14yd9fgCW1bUKoEpkBAquP"}],"outputSatoshis":317283951000,"feeSatoshis":62}}`,
},
{
name: "sendTransaction",
req: socketioReq{"sendTransaction", []interface{}{"010000000001019d64f0c72a0d206001decbffaa722eb1044534c"}},
want: `{"error":{"message":"Not implemented"}}`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
resp, err := s.Ack("message", tt.req, time.Second*3)
if err != nil {
t.Errorf("Socketio error %v", err)
}
if resp != tt.want {
t.Errorf("Socketio resp %v, want %v", resp, tt.want)
}
})
}
}
func Test_PublicServer_UTXO(t *testing.T) {
s, dbpath := setupPublicHTTPServer(t)
defer closeAndDestroyPublicServer(t, s, dbpath)
@ -403,4 +511,6 @@ func Test_PublicServer_UTXO(t *testing.T) {
defer ts.Close()
httpTests(t, ts)
socketioTests(t, ts)
}

View File

@ -12,9 +12,8 @@ import (
"strings"
"time"
"github.com/juju/errors"
"github.com/golang/glog"
"github.com/juju/errors"
"github.com/martinboehm/golang-socketio"
"github.com/martinboehm/golang-socketio/transport"
)
@ -240,7 +239,7 @@ type addressHistoryIndexes struct {
}
type txInputs struct {
Txid *string `json:"txid"`
Txid *string `json:"txid"`
OutputIndex int `json:"outputIndex"`
Script *string `json:"script"`
// ScriptAsm *string `json:"scriptAsm"`
@ -389,9 +388,6 @@ func (s *SocketIoServer) getAddressHistory(addr []string, opts *addrOpts) (res r
}
for txi := opts.From; txi < to; txi++ {
tx, err := s.api.GetTransaction(txids[txi], false)
// for i, txid := range txids {
// if i >= opts.From && i < opts.To {
// tx, err := s.api.GetTransaction(txid, bestheight, false)
if err != nil {
return res, err
}

View File

@ -57,10 +57,11 @@ func AddressToPubKeyHex(addr string, parser bchain.BlockChainParser) string {
func GetTestUTXOBlock1(parser bchain.BlockChainParser) *bchain.Block {
return &bchain.Block{
BlockHeader: bchain.BlockHeader{
Height: 225493,
Hash: "0000000076fbbed90fd75b0e18856aa35baa984e9c9d444cf746ad85e94e2997",
Size: 1234567,
Time: 1534858021,
Height: 225493,
Hash: "0000000076fbbed90fd75b0e18856aa35baa984e9c9d444cf746ad85e94e2997",
Size: 1234567,
Time: 1534858021,
Confirmations: 2,
},
Txs: []bchain.Tx{
bchain.Tx{
@ -81,8 +82,9 @@ func GetTestUTXOBlock1(parser bchain.BlockChainParser) *bchain.Block {
ValueSat: *SatB1T1A2,
},
},
Blocktime: 22549300000,
Time: 22549300000,
Blocktime: 22549300000,
Time: 22549300000,
Confirmations: 2,
},
bchain.Tx{
Txid: TxidB1T2,
@ -109,8 +111,9 @@ func GetTestUTXOBlock1(parser bchain.BlockChainParser) *bchain.Block {
ValueSat: *SatB1T2A5,
},
},
Blocktime: 22549300001,
Time: 22549300001,
Blocktime: 22549300001,
Time: 22549300001,
Confirmations: 2,
},
},
}
@ -119,10 +122,11 @@ func GetTestUTXOBlock1(parser bchain.BlockChainParser) *bchain.Block {
func GetTestUTXOBlock2(parser bchain.BlockChainParser) *bchain.Block {
return &bchain.Block{
BlockHeader: bchain.BlockHeader{
Height: 225494,
Hash: "00000000eb0443fd7dc4a1ed5c686a8e995057805f9a161d9a5a77a95e72b7b6",
Size: 2345678,
Time: 1534859123,
Height: 225494,
Hash: "00000000eb0443fd7dc4a1ed5c686a8e995057805f9a161d9a5a77a95e72b7b6",
Size: 2345678,
Time: 1534859123,
Confirmations: 1,
},
Txs: []bchain.Tx{
bchain.Tx{
@ -155,8 +159,9 @@ func GetTestUTXOBlock2(parser bchain.BlockChainParser) *bchain.Block {
ValueSat: *SatB2T1A7,
},
},
Blocktime: 22549400000,
Time: 22549400000,
Blocktime: 22549400000,
Time: 22549400000,
Confirmations: 1,
},
bchain.Tx{
Txid: TxidB2T2,
@ -188,8 +193,9 @@ func GetTestUTXOBlock2(parser bchain.BlockChainParser) *bchain.Block {
ValueSat: *SatB2T2A9,
},
},
Blocktime: 22549400001,
Time: 22549400001,
Blocktime: 22549400001,
Time: 22549400001,
Confirmations: 1,
},
// transaction from the same address in the previous block
bchain.Tx{
@ -210,8 +216,9 @@ func GetTestUTXOBlock2(parser bchain.BlockChainParser) *bchain.Block {
ValueSat: *SatB2T3A5,
},
},
Blocktime: 22549400002,
Time: 22549400002,
Blocktime: 22549400002,
Time: 22549400002,
Confirmations: 1,
},
// mining transaction
bchain.Tx{
@ -235,8 +242,9 @@ func GetTestUTXOBlock2(parser bchain.BlockChainParser) *bchain.Block {
ValueSat: *SatZero,
},
},
Blocktime: 22549400003,
Time: 22549400003,
Blocktime: 22549400003,
Time: 22549400003,
Confirmations: 1,
},
},
}

View File

@ -158,11 +158,13 @@ func (c *fakeBlockChain) GetTransactionForMempool(txid string) (v *bchain.Tx, er
}
func (c *fakeBlockChain) EstimateSmartFee(blocks int, conservative bool) (v big.Int, err error) {
return v, errors.New("Not implemented")
v.SetInt64(int64(blocks) * 100)
return
}
func (c *fakeBlockChain) EstimateFee(blocks int) (v big.Int, err error) {
return v, errors.New("Not implemented")
v.SetInt64(int64(blocks) * 200)
return
}
func (c *fakeBlockChain) SendRawTransaction(tx string) (v string, err error) {