From c6d6d4138dcbb0a14df2333e19b01929c806d6ab Mon Sep 17 00:00:00 2001 From: Jakub Matys Date: Tue, 10 Jul 2018 17:04:07 +0200 Subject: [PATCH] Added new RPC tests --- bchain/coins/bch/bcashrpc_test.go | 24 +++ bchain/coins/btc/bitcoinrpc_test.go | 24 +++ bchain/coins/dash/dashrpc_test.go | 24 +++ bchain/coins/eth/ethrpc_test.go | 214 +--------------------- bchain/coins/namecoin/namecoinrpc_test.go | 12 ++ bchain/coins/vertcoin/vertcoinrpc_test.go | 12 ++ bchain/coins/zec/zcashrpc_test.go | 24 +++ bchain/tests/rpc/rpc.go | 78 +++++++- 8 files changed, 196 insertions(+), 216 deletions(-) diff --git a/bchain/coins/bch/bcashrpc_test.go b/bchain/coins/bch/bcashrpc_test.go index 195eabc1..b4ff9bf8 100644 --- a/bchain/coins/bch/bcashrpc_test.go +++ b/bchain/coins/bch/bcashrpc_test.go @@ -88,6 +88,18 @@ func TestBCashRPC_EstimateFee(t *testing.T) { tests.mainnet.TestEstimateFee(t) } +func TestBCashRPC_GetBestBlockHash(t *testing.T) { + tests.mainnet.TestGetBestBlockHash(t) +} + +func TestBCashRPC_GetBestBlockHeight(t *testing.T) { + tests.mainnet.TestGetBestBlockHeight(t) +} + +func TestBCashRPC_GetBlockHeader(t *testing.T) { + tests.mainnet.TestGetBlockHeader(t) +} + func TestBCashTestnetRPC_GetBlockHash(t *testing.T) { tests.testnet.TestGetBlockHash(t) } @@ -123,3 +135,15 @@ func TestBCashTestnetRPC_EstimateSmartFee(t *testing.T) { func TestBCashTestnetRPC_EstimateFee(t *testing.T) { tests.testnet.TestEstimateFee(t) } + +func TestBCashTestnetRPC_GetBestBlockHash(t *testing.T) { + tests.testnet.TestGetBestBlockHash(t) +} + +func TestBCashTestnetRPC_GetBestBlockHeight(t *testing.T) { + tests.testnet.TestGetBestBlockHeight(t) +} + +func TestBCashTestnetRPC_GetBlockHeader(t *testing.T) { + tests.testnet.TestGetBlockHeader(t) +} diff --git a/bchain/coins/btc/bitcoinrpc_test.go b/bchain/coins/btc/bitcoinrpc_test.go index a127fa49..b0f19ca0 100644 --- a/bchain/coins/btc/bitcoinrpc_test.go +++ b/bchain/coins/btc/bitcoinrpc_test.go @@ -87,6 +87,18 @@ func TestBitcoinRPC_EstimateFee(t *testing.T) { tests.mainnet.TestEstimateFee(t) } +func TestBitcoinRPC_GetBestBlockHash(t *testing.T) { + tests.mainnet.TestGetBestBlockHash(t) +} + +func TestBitcoinRPC_GetBestBlockHeight(t *testing.T) { + tests.mainnet.TestGetBestBlockHeight(t) +} + +func TestBitcoinRPC_GetBlockHeader(t *testing.T) { + tests.mainnet.TestGetBlockHeader(t) +} + func TestBitcoinTestnetRPC_GetBlockHash(t *testing.T) { tests.testnet.TestGetBlockHash(t) } @@ -122,3 +134,15 @@ func TestBitcoinTestnetRPC_EstimateSmartFee(t *testing.T) { func TestBitcoinTestnetRPC_EstimateFee(t *testing.T) { tests.testnet.TestEstimateFee(t) } + +func TestBitcoinTestnetRPC_GetBestBlockHash(t *testing.T) { + tests.testnet.TestGetBestBlockHash(t) +} + +func TestBitcoinTestnetRPC_GetBestBlockHeight(t *testing.T) { + tests.testnet.TestGetBestBlockHeight(t) +} + +func TestBitcoinTestnetRPC_GetBlockHeader(t *testing.T) { + tests.testnet.TestGetBlockHeader(t) +} diff --git a/bchain/coins/dash/dashrpc_test.go b/bchain/coins/dash/dashrpc_test.go index b406d21a..99b56a4b 100644 --- a/bchain/coins/dash/dashrpc_test.go +++ b/bchain/coins/dash/dashrpc_test.go @@ -80,6 +80,18 @@ func TestDashRPC_EstimateFee(t *testing.T) { tests.mainnet.TestEstimateFee(t) } +func TestDashRPC_GetBestBlockHash(t *testing.T) { + tests.mainnet.TestGetBestBlockHash(t) +} + +func TestDashRPC_GetBestBlockHeight(t *testing.T) { + tests.mainnet.TestGetBestBlockHeight(t) +} + +func TestDashRPC_GetBlockHeader(t *testing.T) { + tests.mainnet.TestGetBlockHeader(t) +} + func TestDashTestnetRPC_GetBlockHash(t *testing.T) { tests.testnet.TestGetBlockHash(t) } @@ -111,3 +123,15 @@ func TestDashTestnetRPC_EstimateSmartFee(t *testing.T) { func TestDashTestnetRPC_EstimateFee(t *testing.T) { tests.testnet.TestEstimateFee(t) } + +func TestDashTestnetRPC_GetBestBlockHash(t *testing.T) { + tests.testnet.TestGetBestBlockHash(t) +} + +func TestDashTestnetRPC_GetBestBlockHeight(t *testing.T) { + tests.testnet.TestGetBestBlockHeight(t) +} + +func TestDashTestnetRPC_GetBlockHeader(t *testing.T) { + tests.testnet.TestGetBlockHeader(t) +} diff --git a/bchain/coins/eth/ethrpc_test.go b/bchain/coins/eth/ethrpc_test.go index db71706a..fab3349f 100644 --- a/bchain/coins/eth/ethrpc_test.go +++ b/bchain/coins/eth/ethrpc_test.go @@ -8,10 +8,7 @@ import ( "encoding/json" "flag" "os" - "reflect" "testing" - - ethtypes "github.com/ethereum/go-ethereum/core/types" ) func getRPCClient(cfg json.RawMessage) (bchain.BlockChain, error) { @@ -48,221 +45,18 @@ func TestEthRPC_GetTransaction(t *testing.T) { rpcTest.TestGetTransaction(t) } -func TestEthRPC_getBestHeader(t *testing.T) { - type fields struct { - b *EthereumRPC - } - tests := []struct { - name string - fields fields - want *ethtypes.Header - wantErr bool - }{ - { - name: "1", - fields: fields{ - b: rpcTest.Client.(*EthereumRPC), - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - _, err := tt.fields.b.getBestHeader() - if (err != nil) != tt.wantErr { - t.Errorf("EthRPC.getBestHeader() error = %v, wantErr %v", err, tt.wantErr) - return - } - // the header is always different, do not compare what we got - }) - } -} - func TestEthRPC_GetBestBlockHash(t *testing.T) { - type fields struct { - b *EthereumRPC - } - tests := []struct { - name string - fields fields - want int - wantErr bool - }{ - { - name: "1", - fields: fields{ - b: rpcTest.Client.(*EthereumRPC), - }, - want: 66, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := tt.fields.b.GetBestBlockHash() - if (err != nil) != tt.wantErr { - t.Errorf("EthRPC.GetBestBlockHash() error = %v, wantErr %v", err, tt.wantErr) - return - } - // the hash is always different, compare only the length of hash - if len(got) != tt.want { - t.Errorf("EthRPC.GetBestBlockHash() = %v, len %v, want len %v", got, len(got), tt.want) - } - }) - } + rpcTest.TestGetBestBlockHash(t) } func TestEthRPC_GetBestBlockHeight(t *testing.T) { - type fields struct { - b *EthereumRPC - } - tests := []struct { - name string - fields fields - want uint32 - wantErr bool - }{ - { - name: "1", - fields: fields{ - b: rpcTest.Client.(*EthereumRPC), - }, - want: 1000000, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := tt.fields.b.GetBestBlockHeight() - if (err != nil) != tt.wantErr { - t.Errorf("EthRPC.GetBestBlockHeight() error = %v, wantErr %v", err, tt.wantErr) - return - } - if got < tt.want { - t.Errorf("EthRPC.GetBestBlockHeight() = %v, want at least %v", got, tt.want) - } - }) - } + rpcTest.TestGetBestBlockHeight(t) } func TestEthRPC_GetBlockHeader(t *testing.T) { - bh, err := rpcTest.Client.(*EthereumRPC).getBestHeader() - if err != nil { - panic(err) - } - type fields struct { - b *EthereumRPC - } - type args struct { - hash string - } - tests := []struct { - name string - fields fields - args args - want *bchain.BlockHeader - wantErr error - }{ - { - name: "2870000", - fields: fields{ - b: rpcTest.Client.(*EthereumRPC), - }, - args: args{ - hash: "eccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903895", - }, - want: &bchain.BlockHeader{ - Hash: "0xeccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903895", - Height: 2870000, - Confirmations: int(uint32(bh.Number.Uint64()) - 2870000 + 1), - }, - }, - { - name: "ErrBlockNotFound", - fields: fields{ - b: rpcTest.Client.(*EthereumRPC), - }, - args: args{ - hash: "0xeccd6b0031015a19cb7d4e10f28590ba65a6a54ad1baa322b50fe5ad16903896", - }, - wantErr: bchain.ErrBlockNotFound, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := tt.fields.b.GetBlockHeader(tt.args.hash) - if err != tt.wantErr { - t.Errorf("EthRPC.GetBlockHeader() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("EthRPC.GetBlockHeader() = %v, want %v", got, tt.want) - } - }) - } + rpcTest.TestGetBlockHeader(t) } func TestEthRPC_EstimateFee(t *testing.T) { - type fields struct { - b *EthereumRPC - } - type args struct { - blocks int - } - tests := []struct { - name string - fields fields - args args - want float64 - wantErr bool - }{ - { - name: "1", - fields: fields{ - b: rpcTest.Client.(*EthereumRPC), - }, - args: args{ - blocks: 10, - }, - want: 1., // check that there is some estimate - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := tt.fields.b.EstimateFee(tt.args.blocks) - if (err != nil) != tt.wantErr { - t.Errorf("EthRPC.EstimateFee() error = %v, wantErr %v", err, tt.wantErr) - return - } - if got < tt.want { - t.Errorf("EthRPC.EstimateFee() = %v, want %v", got, tt.want) - } - }) - } -} - -func TestEthRPC_GetMempool(t *testing.T) { - type fields struct { - b *EthereumRPC - } - tests := []struct { - name string - fields fields - want []string - wantErr bool - }{ - { - name: "1", - fields: fields{ - b: rpcTest.Client.(*EthereumRPC), - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := tt.fields.b.GetMempool() - if (err != nil) != tt.wantErr { - t.Errorf("EthRPC.GetMempool() error = %v, wantErr %v", err, tt.wantErr) - return - } - t.Logf("EthRPC.GetMempool() returned %v transactions", len(got)) - }) - } + rpcTest.TestEstimateFee(t) } diff --git a/bchain/coins/namecoin/namecoinrpc_test.go b/bchain/coins/namecoin/namecoinrpc_test.go index 59af97d6..5b458641 100644 --- a/bchain/coins/namecoin/namecoinrpc_test.go +++ b/bchain/coins/namecoin/namecoinrpc_test.go @@ -76,3 +76,15 @@ func TestNamecoinRPC_EstimateSmartFee(t *testing.T) { func TestNamecoinRPC_EstimateFee(t *testing.T) { rpcTest.TestEstimateFee(t) } + +func TestNamecoinRPC_GetBestBlockHash(t *testing.T) { + rpcTest.TestGetBestBlockHash(t) +} + +func TestNamecoinRPC_GetBestBlockHeight(t *testing.T) { + rpcTest.TestGetBestBlockHeight(t) +} + +func TestNamecoinRPC_GetBlockHeader(t *testing.T) { + rpcTest.TestGetBlockHeader(t) +} diff --git a/bchain/coins/vertcoin/vertcoinrpc_test.go b/bchain/coins/vertcoin/vertcoinrpc_test.go index 8aeadff9..dcf3de14 100644 --- a/bchain/coins/vertcoin/vertcoinrpc_test.go +++ b/bchain/coins/vertcoin/vertcoinrpc_test.go @@ -74,3 +74,15 @@ func TestVertcoinRPC_EstimateSmartFee(t *testing.T) { func TestVertcoinRPC_EstimateFee(t *testing.T) { rpcTest.TestEstimateFee(t) } + +func TestVertcoinRPC_GetBestBlockHash(t *testing.T) { + rpcTest.TestGetBestBlockHash(t) +} + +func TestVertcoinRPC_GetBestBlockHeight(t *testing.T) { + rpcTest.TestGetBestBlockHeight(t) +} + +func TestVertcoinRPC_GetBlockHeader(t *testing.T) { + rpcTest.TestGetBlockHeader(t) +} diff --git a/bchain/coins/zec/zcashrpc_test.go b/bchain/coins/zec/zcashrpc_test.go index e331625c..d65e68c8 100644 --- a/bchain/coins/zec/zcashrpc_test.go +++ b/bchain/coins/zec/zcashrpc_test.go @@ -79,6 +79,18 @@ func TestZCashRPC_EstimateFee(t *testing.T) { tests.mainnet.TestEstimateFee(t) } +func TestZCashRPC_GetBestBlockHash(t *testing.T) { + tests.mainnet.TestGetBestBlockHash(t) +} + +func TestZCashRPC_GetBestBlockHeight(t *testing.T) { + tests.mainnet.TestGetBestBlockHeight(t) +} + +func TestZCashRPC_GetBlockHeader(t *testing.T) { + tests.mainnet.TestGetBlockHeader(t) +} + func TestZCashTestnetRPC_GetBlockHash(t *testing.T) { tests.testnet.TestGetBlockHash(t) } @@ -110,3 +122,15 @@ func TestZCashTestnetRPC_EstimateSmartFee(t *testing.T) { func TestZCashTestnetRPC_EstimateFee(t *testing.T) { tests.testnet.TestEstimateFee(t) } + +func TestZCashTestnetRPC_GetBestBlockHash(t *testing.T) { + tests.mainnet.TestGetBestBlockHash(t) +} + +func TestZCashTestnetRPC_GetBestBlockHeight(t *testing.T) { + tests.mainnet.TestGetBestBlockHeight(t) +} + +func TestZCashTestnetRPC_GetBlockHeader(t *testing.T) { + tests.mainnet.TestGetBlockHeader(t) +} diff --git a/bchain/tests/rpc/rpc.go b/bchain/tests/rpc/rpc.go index 260b4754..6ebf298f 100644 --- a/bchain/tests/rpc/rpc.go +++ b/bchain/tests/rpc/rpc.go @@ -155,12 +155,11 @@ func (rt *Test) TestGetTransaction(t *testing.T) { return } // Confirmations is variable field, we just check if is set and reset it - if got.Confirmations > 0 { - got.Confirmations = 0 - } else { - t.Errorf("GetTransaction() has empty Confirmations field") + if got.Confirmations <= 0 { + t.Errorf("GetTransaction() got struct with invalid Confirmations field") continue } + got.Confirmations = 0 if !reflect.DeepEqual(got, want) { t.Errorf("GetTransaction() got %v, want %v", got, want) @@ -362,7 +361,7 @@ func (rt *Test) TestEstimateSmartFee(t *testing.T) { if err != nil { t.Error(err) } - if fee != -1 && (fee < 0 || fee > 1) { + if fee != -1 && fee < 0 { t.Errorf("EstimateSmartFee() returned unexpected fee rate: %f", fee) } } @@ -376,8 +375,75 @@ func (rt *Test) TestEstimateFee(t *testing.T) { if err != nil { t.Error(err) } - if fee != -1 && (fee < 0 || fee > 1) { + if fee != -1 && fee < 0 { t.Errorf("EstimateFee() returned unexpected fee rate: %f", fee) } } } + +func (rt *Test) TestGetBestBlockHash(t *testing.T) { + for i := 0; i < 3; i++ { + hash, err := rt.Client.GetBestBlockHash() + if err != nil { + t.Fatal(err) + } + + blk, err := rt.Client.GetBlock(hash, 0) + if err != nil { + t.Fatal(err) + } + + // we expect no next block + _, err = rt.Client.GetBlock("", blk.Height+1) + if err != nil { + if err != bchain.ErrBlockNotFound { + t.Error(err) + } + return + } + } + t.Error("GetBestBlockHash() didn't get the best hash") +} + +func (rt *Test) TestGetBestBlockHeight(t *testing.T) { + for i := 0; i < 3; i++ { + height, err := rt.Client.GetBestBlockHeight() + if err != nil { + t.Fatal(err) + } + + // we expect no next block + _, err = rt.Client.GetBlock("", height+1) + if err != nil { + if err != bchain.ErrBlockNotFound { + t.Error(err) + } + return + } + } + t.Error("GetBestBlockHeigh() didn't get the the best heigh") +} + +func (rt *Test) TestGetBlockHeader(t *testing.T) { + want := &bchain.BlockHeader{ + Hash: rt.TestData.BlockHash, + Height: rt.TestData.BlockHeight, + } + + got, err := rt.Client.GetBlockHeader(rt.TestData.BlockHash) + if err != nil { + t.Fatal(err) + } + + // Confirmations is variable field, we just check if is set and reset it + if got.Confirmations <= 0 { + t.Fatalf("GetBlockHeader() got struct with invalid Confirmations field") + } + got.Confirmations = 0 + + got.Prev, got.Next = "", "" + + if !reflect.DeepEqual(got, want) { + t.Errorf("GetBlockHeader() got=%v, want=%v", got, want) + } +}