Skip to content

Commit

Permalink
Add more test cases for chain API
Browse files Browse the repository at this point in the history
  • Loading branch information
leventeliu committed Feb 20, 2019
1 parent 64b9f7e commit f8720f2
Showing 1 changed file with 92 additions and 22 deletions.
114 changes: 92 additions & 22 deletions blockproducer/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,37 +187,107 @@ func TestChain(t *testing.T) {
So(err, ShouldBeNil)
})

Convey("When chain state objects are injected", func() {
rpcService := &ChainRPCService{chain: chain}
Convey("When chain service are created over the chain instance", func() {
var rpcService = &ChainRPCService{chain: chain}
err = rpcService.QuerySQLChainProfile(
&types.QuerySQLChainProfileReq{DBID: dbid1},
&types.QuerySQLChainProfileResp{})
So(err, ShouldNotBeNil)
resp := &types.FetchLastIrreversibleBlockResp{}

var fetchBlockResp = &types.FetchLastIrreversibleBlockResp{}
err = rpcService.FetchLastIrreversibleBlock(
&types.FetchLastIrreversibleBlockReq{Address: addr1}, resp)
&types.FetchLastIrreversibleBlockReq{Address: addr1}, fetchBlockResp)
So(err, ShouldBeNil)
So(resp.SQLChains, ShouldBeEmpty)
So(fetchBlockResp.SQLChains, ShouldBeEmpty)

var (
queryBalanceReq = &types.QueryAccountTokenBalanceReq{Addr: addr2, TokenType: 0}
queryBalanceResp = &types.QueryAccountTokenBalanceResp{}
)
err = rpcService.QueryAccountTokenBalance(queryBalanceReq, queryBalanceResp)
So(err, ShouldBeNil)
So(queryBalanceResp.OK, ShouldBeFalse)

Convey("Chain APIs should return correct result of state objects", func() {
var loaded bool
_, loaded = chain.immutable.loadOrStoreProviderObject(addr1, &types.ProviderProfile{})
So(loaded, ShouldBeFalse)
_, loaded = chain.immutable.loadOrStoreSQLChainObject(dbid1, &types.SQLChainProfile{
Miners: []*types.MinerInfo{&types.MinerInfo{Address: addr2}},
})
So(loaded, ShouldBeFalse)
_, loaded = chain.immutable.loadOrStoreAccountObject(addr2, &types.Account{
Address: addr2,
TokenBalance: [types.SupportTokenNumber]uint64{100, 100, 100, 100, 100},
})
So(loaded, ShouldBeFalse)
chain.immutable.commit()

var loaded bool
_, loaded = chain.immutable.loadOrStoreProviderObject(addr1, &types.ProviderProfile{})
So(loaded, ShouldBeFalse)
_, loaded = chain.immutable.loadOrStoreSQLChainObject(dbid1, &types.SQLChainProfile{
Miners: []*types.MinerInfo{&types.MinerInfo{Address: addr2}},
err = rpcService.QuerySQLChainProfile(
&types.QuerySQLChainProfileReq{DBID: dbid1},
&types.QuerySQLChainProfileResp{})
So(err, ShouldBeNil)
err = rpcService.FetchLastIrreversibleBlock(
&types.FetchLastIrreversibleBlockReq{Address: addr2}, fetchBlockResp)
So(err, ShouldBeNil)
So(fetchBlockResp.SQLChains, ShouldNotBeEmpty)

err = rpcService.QueryAccountTokenBalance(queryBalanceReq, queryBalanceResp)
So(err, ShouldBeNil)
So(queryBalanceResp.OK, ShouldBeTrue)
So(queryBalanceResp.Balance, ShouldEqual, 100)
})
So(loaded, ShouldBeFalse)
_, loaded = chain.immutable.loadOrStoreAccountObject(addr2, &types.Account{})
So(loaded, ShouldBeFalse)
chain.immutable.commit()

err = rpcService.QuerySQLChainProfile(
&types.QuerySQLChainProfileReq{DBID: dbid1},
&types.QuerySQLChainProfileResp{})
So(err, ShouldBeNil)
err = rpcService.FetchLastIrreversibleBlock(
&types.FetchLastIrreversibleBlockReq{Address: addr2}, resp)
So(err, ShouldBeNil)
So(resp.SQLChains, ShouldNotBeEmpty)
Convey("Chain APIs should return correct result of tx state", func() {
var tx pi.Transaction
tx, err = newTransfer(1, priv1, addr1, addr2, 1)
So(err, ShouldBeNil)
So(tx, ShouldNotBeNil)

var (
req = &types.QueryTxStateReq{Hash: tx.Hash()}
resp = &types.QueryTxStateResp{}
)
err = rpcService.QueryTxState(req, resp)
So(err, ShouldBeNil)
So(resp.State, ShouldEqual, pi.TransactionStateNotFound)

chain.headBranch.addTx(tx)
err = rpcService.QueryTxState(req, resp)
So(err, ShouldBeNil)
So(resp.State, ShouldEqual, pi.TransactionStatePending)

delete(chain.headBranch.unpacked, req.Hash)
chain.headBranch.packed[req.Hash] = tx
err = rpcService.QueryTxState(req, resp)
So(err, ShouldBeNil)
So(resp.State, ShouldEqual, pi.TransactionStatePacked)

delete(chain.headBranch.packed, req.Hash)
_, err = chain.storage.Writer().Exec(`INSERT INTO "indexed_transactions" (
"block_height",
"tx_index",
"hash",
"block_hash",
"timestamp",
"tx_type",
"address",
"raw"
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
1,
0,
tx.Hash().String(),
"",
tx.GetTimestamp().UnixNano(),
tx.GetTransactionType(),
tx.GetAccountAddress().String(),
"",
)
So(err, ShouldBeNil)
err = rpcService.QueryTxState(req, resp)
So(err, ShouldBeNil)
So(resp.State, ShouldEqual, pi.TransactionStateConfirmed)
})
})

Convey("When transfer transactions are added", func() {
Expand Down

0 comments on commit f8720f2

Please sign in to comment.