From 22ffd932921f36c389ee10463f99409f8dbb60b8 Mon Sep 17 00:00:00 2001 From: Mikelle Date: Tue, 30 Mar 2021 12:10:13 +0300 Subject: [PATCH] implemented fromIdx and toIdx in transaction-pool request --- api/parsers.go | 12 ++++++++++++ api/swagger.yml | 10 ++++++++-- api/txspool.go | 12 +++++++++--- api/txspool_test.go | 2 +- db/l2db/apiqueries.go | 25 +++++++++++++++++++++---- db/l2db/l2db_test.go | 2 +- 6 files changed, 52 insertions(+), 11 deletions(-) diff --git a/api/parsers.go b/api/parsers.go index f606a87..6968552 100644 --- a/api/parsers.go +++ b/api/parsers.go @@ -172,6 +172,18 @@ func parseIdx(c querier) (*common.Idx, error) { return stringToIdx(idxStr, name) } +func parseFromIdx(c querier) (*common.Idx, error) { + const name = "fromAccountIndex" + idxStr := c.Query(name) + return stringToIdx(idxStr, name) +} + +func parseToIdx(c querier) (*common.Idx, error) { + const name = "toAccountIndex" + idxStr := c.Query(name) + return stringToIdx(idxStr, name) +} + func parseExitFilters(c querier) (*common.TokenID, *ethCommon.Address, *babyjub.PublicKeyComp, *common.Idx, error) { // TokenID tid, err := parseQueryUint("tokenId", nil, 0, maxUint32, c) diff --git a/api/swagger.yml b/api/swagger.yml index 3efdd74..9dd3caf 100644 --- a/api/swagger.yml +++ b/api/swagger.yml @@ -427,10 +427,16 @@ paths: description: State of the transactions, e.g. "pend" schema: $ref: '#/components/schemas/PoolL2TransactionState' - - name: accountIndex + - name: fromAccountIndex + in: query + required: false + description: Id of the from account + schema: + $ref: '#/components/schemas/AccountIndex' + - name: toAccountIndex in: query required: false - description: Id of the account + description: Id of the to account schema: $ref: '#/components/schemas/AccountIndex' responses: diff --git a/api/txspool.go b/api/txspool.go index 6452b1f..2ea3c39 100644 --- a/api/txspool.go +++ b/api/txspool.go @@ -56,8 +56,14 @@ func (a *API) getPoolTx(c *gin.Context) { } func (a *API) getPoolTxs(c *gin.Context) { - // Get idx - idx, err := parseIdx(c) + // Get from idx + fromIdx, err := parseFromIdx(c) + if err != nil { + retBadReq(err, c) + return + } + // Get to idx + toIdx, err := parseToIdx(c) if err != nil { retBadReq(err, c) return @@ -69,7 +75,7 @@ func (a *API) getPoolTxs(c *gin.Context) { return } // Fetch txs from l2DB - txs, err := a.l2.GetPoolTxs(idx, state) + txs, err := a.l2.GetPoolTxs(fromIdx, toIdx, state) if err != nil { retSQLErr(err, c) return diff --git a/api/txspool_test.go b/api/txspool_test.go index b3d23c3..e102ce7 100644 --- a/api/txspool_test.go +++ b/api/txspool_test.go @@ -233,7 +233,7 @@ func TestPoolTxs(t *testing.T) { fetchedTxs := testPoolTxsResponse{} require.NoError(t, doGoodReq( "GET", - endpoint+"?accountIndex=hez:ETH:263", + endpoint+"?fromAccountIndex=hez:ETH:263", nil, &fetchedTxs)) assert.Equal(t, 1, len(fetchedTxs.Txs)) assert.Equal(t, "hez:ETH:263", fetchedTxs.Txs[0].FromIdx) diff --git a/db/l2db/apiqueries.go b/db/l2db/apiqueries.go index 937b9e1..a04e342 100644 --- a/db/l2db/apiqueries.go +++ b/db/l2db/apiqueries.go @@ -129,7 +129,7 @@ func (l2db *L2DB) GetTxAPI(txID common.TxID) (*PoolTxAPI, error) { } // GetPoolTxs return Txs from the pool -func (l2db *L2DB) GetPoolTxs(idx *common.Idx, state *common.PoolL2TxState) ([]*PoolTxAPI, error) { +func (l2db *L2DB) GetPoolTxs(fromIdx, toIdx *common.Idx, state *common.PoolL2TxState) ([]*PoolTxAPI, error) { cancel, err := l2db.apiConnCon.Acquire() defer cancel() if err != nil { @@ -145,7 +145,8 @@ func (l2db *L2DB) GetPoolTxs(idx *common.Idx, state *common.PoolL2TxState) ([]*P args = append(args, state) nextIsAnd = true } - if idx != nil { + + if fromIdx != nil && toIdx != nil { if nextIsAnd { queryStr += "AND (" } else { @@ -153,8 +154,24 @@ func (l2db *L2DB) GetPoolTxs(idx *common.Idx, state *common.PoolL2TxState) ([]*P } queryStr += "tx_pool.from_idx = ? " queryStr += "OR tx_pool.to_idx = ?) " - args = append(args, idx) - args = append(args, idx) + args = append(args, fromIdx) + args = append(args, toIdx) + } else if fromIdx != nil { + if nextIsAnd { + queryStr += "AND " + } else { + queryStr += "WHERE " + } + queryStr += "tx_pool.from_idx = ?" + args = append(args, fromIdx) + } else if toIdx != nil { + if nextIsAnd { + queryStr += "AND " + } else { + queryStr += "WHERE " + } + queryStr += "tx_pool.to_idx = ?" + args = append(args, toIdx) } queryStr += "AND NOT external_delete;" query := l2db.dbRead.Rebind(queryStr) diff --git a/db/l2db/l2db_test.go b/db/l2db/l2db_test.go index 4988990..ec36c51 100644 --- a/db/l2db/l2db_test.go +++ b/db/l2db/l2db_test.go @@ -328,7 +328,7 @@ func TestL2DB_GetPoolTxs(t *testing.T) { pendingTxs = append(pendingTxs, &poolL2Txs[i]) } } - fetchedTxs, err := l2DBWithACC.GetPoolTxs(&idx, &state) + fetchedTxs, err := l2DBWithACC.GetPoolTxs(&idx, &idx, &state) require.NoError(t, err) assert.Equal(t, len(pendingTxs), len(fetchedTxs)) }