From e0e3299d9f619abf73d63fb02be7c48a9d8c1c65 Mon Sep 17 00:00:00 2001 From: Arnau B Date: Fri, 30 Oct 2020 16:26:33 +0100 Subject: [PATCH] Imp api get full-batches --- api/api_test.go | 5 ++++- api/batch.go | 16 ++++++++++----- api/batch_test.go | 46 ++++++++++++++++++++++++++++++++++++++++-- api/txshistory_test.go | 22 ++++++++++---------- 4 files changed, 70 insertions(+), 19 deletions(-) diff --git a/api/api_test.go b/api/api_test.go index 5e813ed..749af0a 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -40,6 +40,7 @@ type testCommon struct { blocks []common.Block tokens []historydb.TokenWithUSD batches []testBatch + fullBatches []testFullBatch coordinators []historydb.CoordinatorAPI usrAddr string usrBjj string @@ -361,10 +362,12 @@ func TestMain(m *testing.M) { // Set testCommon 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. + testBatches, fullBatches := genTestBatches(blocks, batches, allTxs) tc = testCommon{ blocks: blocks, tokens: tokensUSD, - batches: genTestBatches(blocks, batches), + batches: testBatches, + fullBatches: fullBatches, coordinators: coordinators, usrAddr: ethAddrToHez(usrAddr), usrBjj: bjjToString(usrBjj), diff --git a/api/batch.go b/api/batch.go index 72031a4..181609e 100644 --- a/api/batch.go +++ b/api/batch.go @@ -84,8 +84,8 @@ func getBatch(c *gin.Context) { } type fullBatch struct { - Batch *historydb.BatchAPI - Txs []historydb.TxAPI + Batch *historydb.BatchAPI `json:"batch"` + Txs []historydb.TxAPI `json:"transactions"` } func getFullBatch(c *gin.Context) { @@ -105,9 +105,15 @@ func getFullBatch(c *gin.Context) { retSQLErr(err, c) 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 c.JSON(http.StatusOK, fullBatch{ Batch: batch, diff --git a/api/batch_test.go b/api/batch_test.go index 56b4734..e94aa00 100644 --- a/api/batch_test.go +++ b/api/batch_test.go @@ -49,7 +49,16 @@ func (t testBatchesResponse) Len() int { 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{} for _, cBatch := range cBatches { block := common.Block{} @@ -84,7 +93,20 @@ func genTestBatches(blocks []common.Block, cBatches []common.Batch) []testBatch } 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) { @@ -228,6 +250,26 @@ func TestGetBatch(t *testing.T) { 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) { assert.Equal(t, len(expected), len(actual)) for i := 0; i < len(expected); i++ { diff --git a/api/txshistory_test.go b/api/txshistory_test.go index 1d3cb73..6751967 100644 --- a/api/txshistory_test.go +++ b/api/txshistory_test.go @@ -310,7 +310,7 @@ func TestGetHistoryTxs(t *testing.T) { path := fmt.Sprintf("%s?limit=%d&fromItem=", endpoint, limit) err := doGoodReqPaginated(path, historydb.OrderAsc, &testTxsResponse{}, appendIter) assert.NoError(t, err) - assertHistoryTxAPIs(t, tc.allTxs, fetchedTxs) + assertTxs(t, tc.allTxs, fetchedTxs) // Uncomment once tx generation for tests is fixed // // Get by ethAddr // fetchedTxs = []testTx{} @@ -321,7 +321,7 @@ func TestGetHistoryTxs(t *testing.T) { // ) // err = doGoodReqPaginated(path, historydb.OrderAsc, &testTxsResponse{}, appendIter) // assert.NoError(t, err) - // assertHistoryTxAPIs(t, tc.usrTxs, fetchedTxs) + // assertTxs(t, tc.usrTxs, fetchedTxs) // // Get by bjj // fetchedTxs = []testTx{} // limit = 6 @@ -331,7 +331,7 @@ func TestGetHistoryTxs(t *testing.T) { // ) // err = doGoodReqPaginated(path, historydb.OrderAsc, &testTxsResponse{}, appendIter) // assert.NoError(t, err) - // assertHistoryTxAPIs(t, tc.usrTxs, fetchedTxs) + // assertTxs(t, tc.usrTxs, fetchedTxs) // Get by tokenID fetchedTxs = []testTx{} limit = 5 @@ -348,7 +348,7 @@ func TestGetHistoryTxs(t *testing.T) { tokenIDTxs = append(tokenIDTxs, tc.allTxs[i]) } } - assertHistoryTxAPIs(t, tokenIDTxs, fetchedTxs) + assertTxs(t, tokenIDTxs, fetchedTxs) // idx fetchedTxs = []testTx{} limit = 4 @@ -366,7 +366,7 @@ func TestGetHistoryTxs(t *testing.T) { idxTxs = append(idxTxs, tc.allTxs[i]) } } - assertHistoryTxAPIs(t, idxTxs, fetchedTxs) + assertTxs(t, idxTxs, fetchedTxs) // batchNum fetchedTxs = []testTx{} limit = 3 @@ -384,7 +384,7 @@ func TestGetHistoryTxs(t *testing.T) { batchNumTxs = append(batchNumTxs, tc.allTxs[i]) } } - assertHistoryTxAPIs(t, batchNumTxs, fetchedTxs) + assertTxs(t, batchNumTxs, fetchedTxs) // type txTypes := []common.TxType{ // Uncomment once test gen is fixed @@ -414,7 +414,7 @@ func TestGetHistoryTxs(t *testing.T) { txTypeTxs = append(txTypeTxs, tc.allTxs[i]) } } - assertHistoryTxAPIs(t, txTypeTxs, fetchedTxs) + assertTxs(t, txTypeTxs, fetchedTxs) } // Multiple filters fetchedTxs = []testTx{} @@ -433,7 +433,7 @@ func TestGetHistoryTxs(t *testing.T) { } } } - assertHistoryTxAPIs(t, mixedTxs, fetchedTxs) + assertTxs(t, mixedTxs, fetchedTxs) // All, in reverse order fetchedTxs = []testTx{} limit = 5 @@ -444,7 +444,7 @@ func TestGetHistoryTxs(t *testing.T) { for i := 0; i < len(tc.allTxs); i++ { flipedTxs = append(flipedTxs, tc.allTxs[len(tc.allTxs)-1-i]) } - assertHistoryTxAPIs(t, flipedTxs, fetchedTxs) + assertTxs(t, flipedTxs, fetchedTxs) // 400 path = fmt.Sprintf( "%s?accountIndex=%s&hermezEthereumAddress=%s", @@ -474,7 +474,7 @@ func TestGetHistoryTx(t *testing.T) { assert.NoError(t, err) fetchedTxs = append(fetchedTxs, fetchedTx) } - assertHistoryTxAPIs(t, tc.allTxs, fetchedTxs) + assertTxs(t, tc.allTxs, fetchedTxs) // 400 err := doBadReq("GET", endpoint+"0x001", nil, 400) assert.NoError(t, err) @@ -483,7 +483,7 @@ func TestGetHistoryTx(t *testing.T) { 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)) for i := 0; i < len(actual); i++ { //nolint len(actual) won't change within the loop actual[i].ItemID = 0