Skip to content
Snippets Groups Projects
Unverified Commit 52d95908 authored by Silas Davis's avatar Silas Davis
Browse files

RPC test for BlockchainInfo

parent e827eb17
No related branches found
No related tags found
No related merge requests found
......@@ -23,7 +23,7 @@ import (
)
func TestCompatibleConsensus(t *testing.T) {
// TODO: [ben] expand by constructing and elemetary testing for each listed
// TODO: [ben] expand by constructing and elementary testing for each listed
// compatible consensus engine
for _, listedConsensus := range compatibleConsensus {
......
......@@ -550,12 +550,12 @@ func (pipe *erisMintPipe) BroadcastTxSync(tx txs.Tx) (*rpc_tm_types.ResultBroadc
func (pipe *erisMintPipe) BlockchainInfo(minHeight, maxHeight,
maxBlockLookback int) (*rpc_tm_types.ResultBlockchainInfo, error) {
height := pipe.consensusEngine.Height()
latestHeight := pipe.consensusEngine.Height()
if maxHeight < 1 {
maxHeight = height
maxHeight = latestHeight
} else {
maxHeight = imath.MinInt(height, maxHeight)
maxHeight = imath.MinInt(latestHeight, maxHeight)
}
if minHeight < 1 {
minHeight = imath.MaxInt(1, maxHeight-maxBlockLookback)
......@@ -567,5 +567,5 @@ func (pipe *erisMintPipe) BlockchainInfo(minHeight, maxHeight,
blockMetas = append(blockMetas, blockMeta)
}
return &rpc_tm_types.ResultBlockchainInfo{height, blockMetas}, nil
return &rpc_tm_types.ResultBlockchainInfo{latestHeight, blockMetas}, nil
}
......@@ -57,6 +57,13 @@ func TestHTTPNameReg(t *testing.T) {
testNameReg(t, "HTTP")
}
func TestHTTPBlockchainInfo(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
testBlockchainInfo(t, "HTTP")
}
//--------------------------------------------------------------------------------
// Test the JSONRPC client
......@@ -102,3 +109,10 @@ func TestJSONNameReg(t *testing.T) {
}
testNameReg(t, "JSONRPC")
}
func TestJSONBlockchainInfo(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
testBlockchainInfo(t, "JSONRPC")
}
......@@ -153,12 +153,12 @@ func testCall(t *testing.T, typ string) {
t.Fatalf("Problem broadcasting transaction: %v", err)
}
assert.Equal(t, uint8(1), receipt.CreatesContract, "This transaction should"+
" create a contract")
" create a contract")
assert.NotEqual(t, 0, len(receipt.TxHash), "Receipt should contain a"+
" transaction hash")
" transaction hash")
contractAddr := receipt.ContractAddr
assert.NotEqual(t, 0, len(contractAddr), "Transactions claims to have"+
" created a contract but the contract address is empty")
" created a contract but the contract address is empty")
// run a call through the contract
data := []byte{}
......@@ -303,3 +303,39 @@ Subscribe:
}
}
}
func testBlockchainInfo(t *testing.T, typ string) {
client := clients[typ]
wsc := newWSClient(t)
nBlocks := 4
waitNBlocks(t, wsc, nBlocks)
time.Sleep(time.Millisecond * 200)
resp, err := edbcli.BlockchainInfo(client, 0, 0)
if err != nil {
t.Fatalf("Failed to get blockchain info: %v", err)
}
//TODO: [Silas] reintroduce this when Tendermint changes logic to fire
// NewBlock after saving a block
// see https://github.com/tendermint/tendermint/issues/273
//assert.Equal(t, 4, resp.LastHeight, "Last height should be 4 after waiting for first 4 blocks")
assert.Equal(t, nBlocks, len(resp.BlockMetas),
"Should see 4 BlockMetas after waiting for first 4 blocks")
lastBlockHash := resp.BlockMetas[nBlocks-1].Hash
for i := nBlocks - 2; i >= 0; i-- {
assert.Equal(t, lastBlockHash, resp.BlockMetas[i].Header.LastBlockHash,
"Blockchain should be a hash tree!")
lastBlockHash = resp.BlockMetas[i].Hash
}
resp, err = edbcli.BlockchainInfo(client, 1, 2)
if err != nil {
t.Fatalf("Failed to get blockchain info: %v", err)
}
assert.Equal(t, 2, len(resp.BlockMetas),
"Should see 2 BlockMetas after extracting 2 blocks")
fmt.Printf("%v\n", resp)
}
......@@ -96,7 +96,7 @@ func waitNBlocks(t *testing.T, wsc *client.WSClient, n int) {
runThenWaitForBlock(t, wsc,
func(block *tm_types.Block) bool {
i++
return i <= n
return i >= n
},
func() {})
}
......@@ -156,7 +156,6 @@ func waitForEvent(t *testing.T, wsc *client.WSClient, eventid string,
event, ok := (*result).(*ctypes.ResultEvent)
if ok && event.Event == eventid {
goodCh <- event.Data
break LOOP
}
case err := <-wsc.ErrorsCh:
errCh <- err
......@@ -167,11 +166,10 @@ func waitForEvent(t *testing.T, wsc *client.WSClient, eventid string,
}
}()
// wait for an event or timeout
timeout := time.NewTimer(timeoutSeconds * time.Second)
for {
select {
case <-timeout.C:
// wait for an event or timeout
case <-time.After(timeoutSeconds * time.Second*3):
return waitForEventError{timeout: true}
case eventData := <-goodCh:
// run the check
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment