Browse Source

Imp api get full-batches

feature/sql-semaphore1
Arnau B 4 years ago
parent
commit
e0e3299d9f
4 changed files with 70 additions and 19 deletions
  1. +4
    -1
      api/api_test.go
  2. +11
    -5
      api/batch.go
  3. +44
    -2
      api/batch_test.go
  4. +11
    -11
      api/txshistory_test.go

+ 4
- 1
api/api_test.go

@ -40,6 +40,7 @@ type testCommon struct {
blocks []common.Block blocks []common.Block
tokens []historydb.TokenWithUSD tokens []historydb.TokenWithUSD
batches []testBatch batches []testBatch
fullBatches []testFullBatch
coordinators []historydb.CoordinatorAPI coordinators []historydb.CoordinatorAPI
usrAddr string usrAddr string
usrBjj string usrBjj string
@ -361,10 +362,12 @@ func TestMain(m *testing.M) {
// Set testCommon // Set testCommon
usrTxs, allTxs := genTestTxs(sortedTxs, usrIdxs, accs, tokensUSD, blocks) usrTxs, allTxs := genTestTxs(sortedTxs, usrIdxs, accs, tokensUSD, blocks)
poolTxsToSend, poolTxsToReceive := genTestPoolTx(accs, []babyjub.PrivateKey{privK}, tokensUSD) // NOTE: pool txs are not inserted to the DB here. In the test they will be posted and getted. poolTxsToSend, poolTxsToReceive := genTestPoolTx(accs, []babyjub.PrivateKey{privK}, tokensUSD) // NOTE: pool txs are not inserted to the DB here. In the test they will be posted and getted.
testBatches, fullBatches := genTestBatches(blocks, batches, allTxs)
tc = testCommon{ tc = testCommon{
blocks: blocks, blocks: blocks,
tokens: tokensUSD, tokens: tokensUSD,
batches: genTestBatches(blocks, batches),
batches: testBatches,
fullBatches: fullBatches,
coordinators: coordinators, coordinators: coordinators,
usrAddr: ethAddrToHez(usrAddr), usrAddr: ethAddrToHez(usrAddr),
usrBjj: bjjToString(usrBjj), usrBjj: bjjToString(usrBjj),

+ 11
- 5
api/batch.go

@ -84,8 +84,8 @@ func getBatch(c *gin.Context) {
} }
type fullBatch struct { type fullBatch struct {
Batch *historydb.BatchAPI
Txs []historydb.TxAPI
Batch *historydb.BatchAPI `json:"batch"`
Txs []historydb.TxAPI `json:"transactions"`
} }
func getFullBatch(c *gin.Context) { func getFullBatch(c *gin.Context) {
@ -105,9 +105,15 @@ func getFullBatch(c *gin.Context) {
retSQLErr(err, c) retSQLErr(err, c)
return return
} }
// Fetch txs from historyDB
// TODO
txs := []historydb.TxAPI{}
// Fetch txs forged in the batch from historyDB
maxTxsPerBatch := uint(2048) //nolint:gomnd
txs, _, err := h.GetHistoryTxs(
nil, nil, nil, nil, batchNum, nil, nil, &maxTxsPerBatch, historydb.OrderAsc,
)
if err != nil {
retSQLErr(err, c)
return
}
// JSON response // JSON response
c.JSON(http.StatusOK, fullBatch{ c.JSON(http.StatusOK, fullBatch{
Batch: batch, Batch: batch,

+ 44
- 2
api/batch_test.go

@ -49,7 +49,16 @@ func (t testBatchesResponse) Len() int {
return len(t.Batches) return len(t.Batches)
} }
func genTestBatches(blocks []common.Block, cBatches []common.Batch) []testBatch {
type testFullBatch struct {
Batch testBatch `json:"batch"`
Txs []testTx `json:"transactions"`
}
func genTestBatches(
blocks []common.Block,
cBatches []common.Batch,
txs []testTx,
) ([]testBatch, []testFullBatch) {
tBatches := []testBatch{} tBatches := []testBatch{}
for _, cBatch := range cBatches { for _, cBatch := range cBatches {
block := common.Block{} block := common.Block{}
@ -84,7 +93,20 @@ func genTestBatches(blocks []common.Block, cBatches []common.Batch) []testBatch
} }
tBatches = append(tBatches, tBatch) tBatches = append(tBatches, tBatch)
} }
return tBatches
fullBatches := []testFullBatch{}
for i := 0; i < len(tBatches); i++ {
forgedTxs := []testTx{}
for j := 0; j < len(txs); j++ {
if txs[j].BatchNum != nil && *txs[j].BatchNum == tBatches[i].BatchNum {
forgedTxs = append(forgedTxs, txs[j])
}
}
fullBatches = append(fullBatches, testFullBatch{
Batch: tBatches[i],
Txs: forgedTxs,
})
}
return tBatches, fullBatches
} }
func TestGetBatches(t *testing.T) { func TestGetBatches(t *testing.T) {
@ -228,6 +250,26 @@ func TestGetBatch(t *testing.T) {
assert.NoError(t, doBadReq("GET", endpoint+"99999", nil, 404)) assert.NoError(t, doBadReq("GET", endpoint+"99999", nil, 404))
} }
func TestGetFullBatch(t *testing.T) {
endpoint := apiURL + "full-batches/"
for _, fullBatch := range tc.fullBatches {
fetchedFullBatch := testFullBatch{}
assert.NoError(
t, doGoodReq(
"GET",
endpoint+strconv.Itoa(int(fullBatch.Batch.BatchNum)),
nil, &fetchedFullBatch,
),
)
assertBatch(t, fullBatch.Batch, fetchedFullBatch.Batch)
assertTxs(t, fullBatch.Txs, fetchedFullBatch.Txs)
}
// 400
assert.NoError(t, doBadReq("GET", endpoint+"foo", nil, 400))
// 404
assert.NoError(t, doBadReq("GET", endpoint+"99999", nil, 404))
}
func assertBatches(t *testing.T, expected, actual []testBatch) { func assertBatches(t *testing.T, expected, actual []testBatch) {
assert.Equal(t, len(expected), len(actual)) assert.Equal(t, len(expected), len(actual))
for i := 0; i < len(expected); i++ { for i := 0; i < len(expected); i++ {

+ 11
- 11
api/txshistory_test.go

@ -310,7 +310,7 @@ func TestGetHistoryTxs(t *testing.T) {
path := fmt.Sprintf("%s?limit=%d&fromItem=", endpoint, limit) path := fmt.Sprintf("%s?limit=%d&fromItem=", endpoint, limit)
err := doGoodReqPaginated(path, historydb.OrderAsc, &testTxsResponse{}, appendIter) err := doGoodReqPaginated(path, historydb.OrderAsc, &testTxsResponse{}, appendIter)
assert.NoError(t, err) assert.NoError(t, err)
assertHistoryTxAPIs(t, tc.allTxs, fetchedTxs)
assertTxs(t, tc.allTxs, fetchedTxs)
// Uncomment once tx generation for tests is fixed // Uncomment once tx generation for tests is fixed
// // Get by ethAddr // // Get by ethAddr
// fetchedTxs = []testTx{} // fetchedTxs = []testTx{}
@ -321,7 +321,7 @@ func TestGetHistoryTxs(t *testing.T) {
// ) // )
// err = doGoodReqPaginated(path, historydb.OrderAsc, &testTxsResponse{}, appendIter) // err = doGoodReqPaginated(path, historydb.OrderAsc, &testTxsResponse{}, appendIter)
// assert.NoError(t, err) // assert.NoError(t, err)
// assertHistoryTxAPIs(t, tc.usrTxs, fetchedTxs)
// assertTxs(t, tc.usrTxs, fetchedTxs)
// // Get by bjj // // Get by bjj
// fetchedTxs = []testTx{} // fetchedTxs = []testTx{}
// limit = 6 // limit = 6
@ -331,7 +331,7 @@ func TestGetHistoryTxs(t *testing.T) {
// ) // )
// err = doGoodReqPaginated(path, historydb.OrderAsc, &testTxsResponse{}, appendIter) // err = doGoodReqPaginated(path, historydb.OrderAsc, &testTxsResponse{}, appendIter)
// assert.NoError(t, err) // assert.NoError(t, err)
// assertHistoryTxAPIs(t, tc.usrTxs, fetchedTxs)
// assertTxs(t, tc.usrTxs, fetchedTxs)
// Get by tokenID // Get by tokenID
fetchedTxs = []testTx{} fetchedTxs = []testTx{}
limit = 5 limit = 5
@ -348,7 +348,7 @@ func TestGetHistoryTxs(t *testing.T) {
tokenIDTxs = append(tokenIDTxs, tc.allTxs[i]) tokenIDTxs = append(tokenIDTxs, tc.allTxs[i])
} }
} }
assertHistoryTxAPIs(t, tokenIDTxs, fetchedTxs)
assertTxs(t, tokenIDTxs, fetchedTxs)
// idx // idx
fetchedTxs = []testTx{} fetchedTxs = []testTx{}
limit = 4 limit = 4
@ -366,7 +366,7 @@ func TestGetHistoryTxs(t *testing.T) {
idxTxs = append(idxTxs, tc.allTxs[i]) idxTxs = append(idxTxs, tc.allTxs[i])
} }
} }
assertHistoryTxAPIs(t, idxTxs, fetchedTxs)
assertTxs(t, idxTxs, fetchedTxs)
// batchNum // batchNum
fetchedTxs = []testTx{} fetchedTxs = []testTx{}
limit = 3 limit = 3
@ -384,7 +384,7 @@ func TestGetHistoryTxs(t *testing.T) {
batchNumTxs = append(batchNumTxs, tc.allTxs[i]) batchNumTxs = append(batchNumTxs, tc.allTxs[i])
} }
} }
assertHistoryTxAPIs(t, batchNumTxs, fetchedTxs)
assertTxs(t, batchNumTxs, fetchedTxs)
// type // type
txTypes := []common.TxType{ txTypes := []common.TxType{
// Uncomment once test gen is fixed // Uncomment once test gen is fixed
@ -414,7 +414,7 @@ func TestGetHistoryTxs(t *testing.T) {
txTypeTxs = append(txTypeTxs, tc.allTxs[i]) txTypeTxs = append(txTypeTxs, tc.allTxs[i])
} }
} }
assertHistoryTxAPIs(t, txTypeTxs, fetchedTxs)
assertTxs(t, txTypeTxs, fetchedTxs)
} }
// Multiple filters // Multiple filters
fetchedTxs = []testTx{} fetchedTxs = []testTx{}
@ -433,7 +433,7 @@ func TestGetHistoryTxs(t *testing.T) {
} }
} }
} }
assertHistoryTxAPIs(t, mixedTxs, fetchedTxs)
assertTxs(t, mixedTxs, fetchedTxs)
// All, in reverse order // All, in reverse order
fetchedTxs = []testTx{} fetchedTxs = []testTx{}
limit = 5 limit = 5
@ -444,7 +444,7 @@ func TestGetHistoryTxs(t *testing.T) {
for i := 0; i < len(tc.allTxs); i++ { for i := 0; i < len(tc.allTxs); i++ {
flipedTxs = append(flipedTxs, tc.allTxs[len(tc.allTxs)-1-i]) flipedTxs = append(flipedTxs, tc.allTxs[len(tc.allTxs)-1-i])
} }
assertHistoryTxAPIs(t, flipedTxs, fetchedTxs)
assertTxs(t, flipedTxs, fetchedTxs)
// 400 // 400
path = fmt.Sprintf( path = fmt.Sprintf(
"%s?accountIndex=%s&hermezEthereumAddress=%s", "%s?accountIndex=%s&hermezEthereumAddress=%s",
@ -474,7 +474,7 @@ func TestGetHistoryTx(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
fetchedTxs = append(fetchedTxs, fetchedTx) fetchedTxs = append(fetchedTxs, fetchedTx)
} }
assertHistoryTxAPIs(t, tc.allTxs, fetchedTxs)
assertTxs(t, tc.allTxs, fetchedTxs)
// 400 // 400
err := doBadReq("GET", endpoint+"0x001", nil, 400) err := doBadReq("GET", endpoint+"0x001", nil, 400)
assert.NoError(t, err) assert.NoError(t, err)
@ -483,7 +483,7 @@ func TestGetHistoryTx(t *testing.T) {
assert.NoError(t, err) assert.NoError(t, err)
} }
func assertHistoryTxAPIs(t *testing.T, expected, actual []testTx) {
func assertTxs(t *testing.T, expected, actual []testTx) {
require.Equal(t, len(expected), len(actual)) require.Equal(t, len(expected), len(actual))
for i := 0; i < len(actual); i++ { //nolint len(actual) won't change within the loop for i := 0; i < len(actual); i++ { //nolint len(actual) won't change within the loop
actual[i].ItemID = 0 actual[i].ItemID = 0

Loading…
Cancel
Save