implemented get pool txs endpoint

This commit is contained in:
Mikelle
2021-03-29 13:39:05 +03:00
parent 6c1c157bc3
commit c84b3a4d0f
7 changed files with 189 additions and 0 deletions

View File

@@ -127,3 +127,41 @@ func (l2db *L2DB) GetTxAPI(txID common.TxID) (*PoolTxAPI, error) {
txID,
))
}
func (l2db *L2DB) GetPoolTxs(idx *common.Idx, state *common.PoolL2TxState) ([]*PoolTxAPI, error) {
cancel, err := l2db.apiConnCon.Acquire()
defer cancel()
if err != nil {
return nil, tracerr.Wrap(err)
}
defer l2db.apiConnCon.Release()
// Apply filters
nextIsAnd := false
queryStr := selectPoolTxAPI
var args []interface{}
if state != nil {
queryStr += "WHERE state = ? "
args = append(args, state)
nextIsAnd = true
}
if idx != nil {
if nextIsAnd {
queryStr += "AND ("
} else {
queryStr += "WHERE ("
}
queryStr += "tx_pool.from_idx = ? "
queryStr += "OR tx_pool.to_idx = ?) "
args = append(args, idx)
args = append(args, idx)
nextIsAnd = true
}
queryStr += "AND NOT external_delete;"
query := l2db.dbRead.Rebind(queryStr)
txs := []*PoolTxAPI{}
err = meddler.QueryAll(
l2db.dbRead, &txs,
query,
args...)
return txs, tracerr.Wrap(err)
}

View File

@@ -311,6 +311,27 @@ func TestGetPending(t *testing.T) {
}
}
func TestL2DB_GetPoolTxs(t *testing.T) {
err := prepareHistoryDB(historyDB)
if err != nil {
log.Error("Error prepare historyDB", err)
}
poolL2Txs, err := generatePoolL2Txs()
state := common.PoolL2TxState("pend")
idx := common.Idx(256)
var pendingTxs []*common.PoolL2Tx
for i := range poolL2Txs {
if poolL2Txs[i].FromIdx == idx || poolL2Txs[i].ToIdx == idx {
err := l2DB.AddTxTest(&poolL2Txs[i])
require.NoError(t, err)
pendingTxs = append(pendingTxs, &poolL2Txs[i])
}
}
fetchedTxs, err := l2DBWithACC.GetPoolTxs(&idx, &state)
require.NoError(t, err)
assert.Equal(t, len(pendingTxs), len(fetchedTxs))
}
func TestStartForging(t *testing.T) {
// Generate txs
var fakeBatchNum common.BatchNum = 33