Merge pull request #246 from hermeznetwork/feature/api-full-batch Imp api get full-batches
@@ -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 @@
// 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),
@@ -84,8 +84,8 @@ c.JSON(http.StatusOK, batch)
} 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 @@ if err != nil {
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,
@@ -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 @@ SlotNum: cBatch.SlotNum,
} 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) {@@ -221,6 +243,26 @@ nil, &fetchedBatch,
), ) assertBatch(t, batch, fetchedBatch) + } + // 400 + assert.NoError(t, doBadReq("GET", endpoint+"foo", nil, 400)) + // 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))
@@ -310,7 +310,7 @@ limit := 8
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 @@ // endpoint, tc.usrAddr, limit,
// ) // 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 @@ // endpoint, tc.usrBjj, limit,
// ) // 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 @@ if tc.allTxs[i].Token.TokenID == tokenID {
tokenIDTxs = append(tokenIDTxs, tc.allTxs[i]) } } - assertHistoryTxAPIs(t, tokenIDTxs, fetchedTxs) + assertTxs(t, tokenIDTxs, fetchedTxs) // idx fetchedTxs = []testTx{} limit = 4@@ -366,7 +366,7 @@ tc.allTxs[i].ToIdx[6:] == idx[6:] {
idxTxs = append(idxTxs, tc.allTxs[i]) } } - assertHistoryTxAPIs(t, idxTxs, fetchedTxs) + assertTxs(t, idxTxs, fetchedTxs) // batchNum fetchedTxs = []testTx{} limit = 3@@ -384,7 +384,7 @@ *tc.allTxs[i].BatchNum == *batchNum {
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 @@ if tc.allTxs[i].Type == txType {
txTypeTxs = append(txTypeTxs, tc.allTxs[i]) } } - assertHistoryTxAPIs(t, txTypeTxs, fetchedTxs) + assertTxs(t, txTypeTxs, fetchedTxs) } // Multiple filters fetchedTxs = []testTx{}@@ -433,7 +433,7 @@ mixedTxs = append(mixedTxs, tc.allTxs[i])
} } } - assertHistoryTxAPIs(t, mixedTxs, fetchedTxs) + assertTxs(t, mixedTxs, fetchedTxs) // All, in reverse order fetchedTxs = []testTx{} limit = 5@@ -444,7 +444,7 @@ flipedTxs := []testTx{}
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 @@ err := doGoodReq("GET", endpoint+tx.TxID.String(), nil, &fetchedTx)
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 @@ err = doBadReq("GET", endpoint+"0x00000000000001e240004700", nil, 404)
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