mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Compare commits
1 Commits
feature/ge
...
feature/ap
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3c1729556c |
@@ -60,7 +60,6 @@ func NewAPI(
|
||||
// Transaction
|
||||
v1.POST("/transactions-pool", a.postPoolTx)
|
||||
v1.GET("/transactions-pool/:id", a.getPoolTx)
|
||||
v1.GET("/transactions-pool", a.getPoolTxs)
|
||||
}
|
||||
|
||||
// Add explorer endpoints
|
||||
|
||||
@@ -161,6 +161,8 @@ var SetBlockchain = `
|
||||
> block
|
||||
> batch
|
||||
> block
|
||||
ForceTransfer(0) D-B: 77777700000000000
|
||||
> block
|
||||
`
|
||||
|
||||
type testCommon struct {
|
||||
@@ -362,6 +364,12 @@ func TestMain(m *testing.M) {
|
||||
commonL1Txs = append(commonL1Txs, batch.L1CoordinatorTxs...)
|
||||
}
|
||||
}
|
||||
// Add unforged L1 tx
|
||||
unforgedTx := blocksData[len(blocksData)-1].Rollup.L1UserTxs[0]
|
||||
if unforgedTx.BatchNum != nil {
|
||||
panic("Unforged tx batch num should be nil")
|
||||
}
|
||||
commonL1Txs = append(commonL1Txs, unforgedTx)
|
||||
|
||||
// Generate Coordinators and add them to HistoryDB
|
||||
const nCoords = 10
|
||||
|
||||
@@ -109,7 +109,7 @@ func (a *API) getFullBatch(c *gin.Context) {
|
||||
// Fetch txs forged in the batch from historyDB
|
||||
maxTxsPerBatch := uint(2048) //nolint:gomnd
|
||||
txs, _, err := a.h.GetTxsAPI(
|
||||
nil, nil, nil, nil, nil, batchNum, nil, nil, &maxTxsPerBatch, historydb.OrderAsc,
|
||||
nil, nil, nil, nil, batchNum, nil, nil, nil, &maxTxsPerBatch, historydb.OrderAsc,
|
||||
)
|
||||
if err != nil && tracerr.Unwrap(err) != sql.ErrNoRows {
|
||||
retSQLErr(err, c)
|
||||
|
||||
@@ -96,32 +96,6 @@ func parseQueryBJJ(c querier) (*babyjub.PublicKeyComp, error) {
|
||||
return hezStringToBJJ(bjjStr, name)
|
||||
}
|
||||
|
||||
func parseQueryPoolL2TxState(c querier) (*common.PoolL2TxState, error) {
|
||||
const name = "state"
|
||||
stateStr := c.Query(name)
|
||||
if stateStr == "" {
|
||||
return nil, nil
|
||||
}
|
||||
switch common.PoolL2TxState(stateStr) {
|
||||
case common.PoolL2TxStatePending:
|
||||
ret := common.PoolL2TxStatePending
|
||||
return &ret, nil
|
||||
case common.PoolL2TxStateForged:
|
||||
ret := common.PoolL2TxStateForged
|
||||
return &ret, nil
|
||||
case common.PoolL2TxStateForging:
|
||||
ret := common.PoolL2TxStateForging
|
||||
return &ret, nil
|
||||
case common.PoolL2TxStateInvalid:
|
||||
ret := common.PoolL2TxStateInvalid
|
||||
return &ret, nil
|
||||
}
|
||||
return nil, tracerr.Wrap(fmt.Errorf(
|
||||
"invalid %s, %s is not a valid option. Check the valid options in the docmentation",
|
||||
name, stateStr,
|
||||
))
|
||||
}
|
||||
|
||||
func parseQueryTxType(c querier) (*common.TxType, error) {
|
||||
const name = "type"
|
||||
typeStr := c.Query(name)
|
||||
@@ -172,18 +146,6 @@ 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)
|
||||
@@ -219,47 +181,6 @@ func parseExitFilters(c querier) (*common.TokenID, *ethCommon.Address, *babyjub.
|
||||
return tokenID, addr, bjj, idx, nil
|
||||
}
|
||||
|
||||
func parseTxsHistoryFilters(c querier) (*common.TokenID, *ethCommon.Address,
|
||||
*babyjub.PublicKeyComp, *common.Idx, *common.Idx, error) {
|
||||
// TokenID
|
||||
tid, err := parseQueryUint("tokenId", nil, 0, maxUint32, c)
|
||||
if err != nil {
|
||||
return nil, nil, nil, nil, nil, tracerr.Wrap(err)
|
||||
}
|
||||
var tokenID *common.TokenID
|
||||
if tid != nil {
|
||||
tokenID = new(common.TokenID)
|
||||
*tokenID = common.TokenID(*tid)
|
||||
}
|
||||
// Hez Eth addr
|
||||
addr, err := parseQueryHezEthAddr(c)
|
||||
if err != nil {
|
||||
return nil, nil, nil, nil, nil, tracerr.Wrap(err)
|
||||
}
|
||||
// BJJ
|
||||
bjj, err := parseQueryBJJ(c)
|
||||
if err != nil {
|
||||
return nil, nil, nil, nil, nil, tracerr.Wrap(err)
|
||||
}
|
||||
if addr != nil && bjj != nil {
|
||||
return nil, nil, nil, nil, nil, tracerr.Wrap(errors.New("bjj and hezEthereumAddress params are incompatible"))
|
||||
}
|
||||
// from Idx
|
||||
fromIdx, err := parseFromIdx(c)
|
||||
if err != nil {
|
||||
return nil, nil, nil, nil, nil, tracerr.Wrap(err)
|
||||
}
|
||||
// to Idx
|
||||
toIdx, err := parseToIdx(c)
|
||||
if err != nil {
|
||||
return nil, nil, nil, nil, nil, tracerr.Wrap(err)
|
||||
}
|
||||
if (fromIdx != nil || toIdx != nil) && (addr != nil || bjj != nil || tokenID != nil) {
|
||||
return nil, nil, nil, nil, nil, tracerr.Wrap(errors.New("accountIndex is incompatible with BJJ, hezEthereumAddress and tokenId"))
|
||||
}
|
||||
return tokenID, addr, bjj, fromIdx, toIdx, nil
|
||||
}
|
||||
|
||||
func parseTokenFilters(c querier) ([]common.TokenID, []string, string, error) {
|
||||
idsStr := c.Query("ids")
|
||||
symbolsStr := c.Query("symbols")
|
||||
|
||||
@@ -415,55 +415,6 @@ paths:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Error500'
|
||||
get:
|
||||
tags:
|
||||
- Coordinator
|
||||
summary: Get transactions that are in the pool.
|
||||
operationId: getPoolTxs
|
||||
parameters:
|
||||
- name: state
|
||||
in: query
|
||||
required: false
|
||||
description: State of the transactions, e.g. "pend"
|
||||
schema:
|
||||
$ref: '#/components/schemas/PoolL2TransactionState'
|
||||
- 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 to account
|
||||
schema:
|
||||
$ref: '#/components/schemas/AccountIndex'
|
||||
responses:
|
||||
'200':
|
||||
description: Successful operation.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PoolL2Transactions'
|
||||
'400':
|
||||
description: Bad request.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Error400'
|
||||
'404':
|
||||
description: Not found.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Error404'
|
||||
'500':
|
||||
description: Internal server error
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Error500'
|
||||
'/transactions-pool/{id}':
|
||||
get:
|
||||
tags:
|
||||
@@ -536,16 +487,10 @@ paths:
|
||||
required: false
|
||||
schema:
|
||||
$ref: '#/components/schemas/BJJ'
|
||||
- name: fromAccountIndex
|
||||
- name: accountIndex
|
||||
in: query
|
||||
required: false
|
||||
description: Only get transactions sent from a specific account. Incompatible with the queries `tokenId`, `hezEthereumAddress` and `BJJ`.
|
||||
schema:
|
||||
$ref: '#/components/schemas/AccountIndex'
|
||||
- name: toAccountIndex
|
||||
in: query
|
||||
required: false
|
||||
description: Only get transactions sent to a specific account. Incompatible with the queries `tokenId`, `hezEthereumAddress` and `BJJ`.
|
||||
description: Only get transactions sent from or to a specific account. Incompatible with the queries `tokenId`, `hezEthereumAddress` and `BJJ`.
|
||||
schema:
|
||||
$ref: '#/components/schemas/AccountIndex'
|
||||
- name: batchNum
|
||||
@@ -584,6 +529,15 @@ paths:
|
||||
type: integer
|
||||
minimum: 1
|
||||
maximum: 2049
|
||||
- name: includePendingL1s
|
||||
in: query
|
||||
required: false
|
||||
description: |
|
||||
If set to true L1 transactions that have been added to the smart contract queue but haven't been forged yet are returned.
|
||||
Warning: the correctness of the order is not guaranteed when using this filter, as the unforged transactions may change their position
|
||||
once they are forged.
|
||||
schema:
|
||||
type: boolean
|
||||
responses:
|
||||
'200':
|
||||
description: Successful operation.
|
||||
@@ -1494,14 +1448,6 @@ components:
|
||||
- requestFee
|
||||
- requestNonce
|
||||
- token
|
||||
PoolL2Transactions:
|
||||
type: object
|
||||
properties:
|
||||
transactions:
|
||||
type: array
|
||||
description: List of pool l2 transactions
|
||||
items:
|
||||
$ref: '#/components/schemas/PoolL2Transaction'
|
||||
TransactionId:
|
||||
type: string
|
||||
description: Identifier for transactions. Used for any kind of transaction (both L1 and L2). More info on how the identifiers are built [here](https://idocs.hermez.io/#/spec/architecture/db/README?id=txid)
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
func (a *API) getHistoryTxs(c *gin.Context) {
|
||||
// Get query parameters
|
||||
tokenID, addr, bjj, fromIdx, toIdx, err := parseTxsHistoryFilters(c)
|
||||
tokenID, addr, bjj, idx, err := parseExitFilters(c)
|
||||
if err != nil {
|
||||
retBadReq(err, c)
|
||||
return
|
||||
@@ -26,6 +26,14 @@ func (a *API) getHistoryTxs(c *gin.Context) {
|
||||
retBadReq(err, c)
|
||||
return
|
||||
}
|
||||
// IncludePendingL1s
|
||||
includePendingL1s := new(bool)
|
||||
*includePendingL1s = false
|
||||
includePendingL1s, err = parseQueryBool("includePendingL1s", includePendingL1s, c)
|
||||
if err != nil {
|
||||
retBadReq(err, c)
|
||||
return
|
||||
}
|
||||
// Pagination
|
||||
fromItem, order, limit, err := parsePagination(c)
|
||||
if err != nil {
|
||||
@@ -35,7 +43,7 @@ func (a *API) getHistoryTxs(c *gin.Context) {
|
||||
|
||||
// Fetch txs from historyDB
|
||||
txs, pendingItems, err := a.h.GetTxsAPI(
|
||||
addr, bjj, tokenID, fromIdx, toIdx, batchNum, txType, fromItem, limit, order,
|
||||
addr, bjj, tokenID, idx, batchNum, txType, includePendingL1s, fromItem, limit, order,
|
||||
)
|
||||
if err != nil {
|
||||
retSQLErr(err, c)
|
||||
|
||||
@@ -71,7 +71,7 @@ func (t txsSort) Less(i, j int) bool {
|
||||
}
|
||||
// i is forged
|
||||
if jsf.BatchNum == nil {
|
||||
return false // j is not forged
|
||||
return true // j is not forged
|
||||
}
|
||||
// Both are forged
|
||||
if *isf.BatchNum == *jsf.BatchNum {
|
||||
@@ -111,7 +111,7 @@ func genTestTxs(
|
||||
) []testTx {
|
||||
txs := []testTx{}
|
||||
// common.L1Tx ==> testTx
|
||||
for _, l1 := range l1s {
|
||||
for i, l1 := range l1s {
|
||||
token := getTokenByID(l1.TokenID, tokens)
|
||||
// l1.FromEthAddr and l1.FromBJJ can't be nil
|
||||
fromEthAddr := string(apitypes.NewHezEthAddr(l1.FromEthAddr))
|
||||
@@ -137,15 +137,26 @@ func genTestTxs(
|
||||
},
|
||||
Token: token,
|
||||
}
|
||||
|
||||
// set BatchNum for user txs
|
||||
if tx.L1Info.ToForgeL1TxsNum != nil {
|
||||
// WARNING: this is an asumption, and the test input data can brake it easily
|
||||
// WARNING: this works just because the way "common" txs are generated using til
|
||||
// any change on the test set could break this
|
||||
bn := common.BatchNum(*tx.L1Info.ToForgeL1TxsNum + 2)
|
||||
tx.BatchNum = &bn
|
||||
}
|
||||
// If FromIdx is not nil
|
||||
idxStr := idxToHez(l1.EffectiveFromIdx, token.Symbol)
|
||||
tx.FromIdx = &idxStr
|
||||
if i == len(l1s)-1 {
|
||||
// Last tx of the L1 set is supposed to be unforged as per the til set.
|
||||
// Unforged txs have some special propperties
|
||||
tx.L1Info.DepositAmountSuccess = false
|
||||
tx.L1Info.AmountSuccess = false
|
||||
tx.BatchNum = nil
|
||||
idxStrUnforged := idxToHez(l1.FromIdx, token.Symbol)
|
||||
tx.FromIdx = &idxStrUnforged
|
||||
}
|
||||
// If tx has a normal ToIdx (>255), set FromEthAddr and FromBJJ
|
||||
if l1.ToIdx >= common.UserThreshold {
|
||||
// find account
|
||||
@@ -261,12 +272,26 @@ func TestGetHistoryTxs(t *testing.T) {
|
||||
fetchedTxs = append(fetchedTxs, tmp.(testTx))
|
||||
}
|
||||
}
|
||||
// Get all (no filters)
|
||||
// Get all (no filters, excluding unforged txs)
|
||||
limit := 20
|
||||
path := fmt.Sprintf("%s?limit=%d", endpoint, limit)
|
||||
err := doGoodReqPaginated(path, historydb.OrderAsc, &testTxsResponse{}, appendIter)
|
||||
assert.NoError(t, err)
|
||||
forgedTxs := []testTx{}
|
||||
for i := 0; i < len(tc.txs); i++ {
|
||||
if tc.txs[i].BatchNum != nil {
|
||||
forgedTxs = append(forgedTxs, tc.txs[i])
|
||||
}
|
||||
}
|
||||
assertTxs(t, forgedTxs, fetchedTxs)
|
||||
|
||||
// Get all, including unforged txs
|
||||
fetchedTxs = []testTx{}
|
||||
path = fmt.Sprintf("%s?limit=%d&includePendingL1s=true", endpoint, limit)
|
||||
err = doGoodReqPaginated(path, historydb.OrderAsc, &testTxsResponse{}, appendIter)
|
||||
assert.NoError(t, err)
|
||||
assertTxs(t, tc.txs, fetchedTxs)
|
||||
|
||||
// Get by ethAddr
|
||||
account := tc.accounts[2]
|
||||
fetchedTxs = []testTx{}
|
||||
@@ -285,7 +310,7 @@ func TestGetHistoryTxs(t *testing.T) {
|
||||
(tx.FromEthAddr != nil && *tx.FromEthAddr == string(account.EthAddr)) ||
|
||||
(tx.ToEthAddr != nil && *tx.ToEthAddr == string(account.EthAddr)) ||
|
||||
(tx.FromBJJ != nil && *tx.FromBJJ == string(account.PublicKey)) ||
|
||||
(tx.ToBJJ != nil && *tx.ToBJJ == string(account.PublicKey)) {
|
||||
(tx.ToBJJ != nil && *tx.ToBJJ == string(account.PublicKey)) && tx.BatchNum != nil {
|
||||
accountTxs = append(accountTxs, tx)
|
||||
}
|
||||
}
|
||||
@@ -312,7 +337,7 @@ func TestGetHistoryTxs(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
tokenIDTxs := []testTx{}
|
||||
for i := 0; i < len(tc.txs); i++ {
|
||||
if tc.txs[i].Token.TokenID == tokenID {
|
||||
if tc.txs[i].BatchNum != nil && tc.txs[i].Token.TokenID == tokenID {
|
||||
tokenIDTxs = append(tokenIDTxs, tc.txs[i])
|
||||
}
|
||||
}
|
||||
@@ -324,13 +349,16 @@ func TestGetHistoryTxs(t *testing.T) {
|
||||
idx, err := stringToIdx(idxStr, "")
|
||||
assert.NoError(t, err)
|
||||
path = fmt.Sprintf(
|
||||
"%s?fromAccountIndex=%s&toAccountIndex=%s&limit=%d",
|
||||
endpoint, idxStr, idxStr, limit,
|
||||
"%s?accountIndex=%s&limit=%d",
|
||||
endpoint, idxStr, limit,
|
||||
)
|
||||
err = doGoodReqPaginated(path, historydb.OrderAsc, &testTxsResponse{}, appendIter)
|
||||
assert.NoError(t, err)
|
||||
idxTxs := []testTx{}
|
||||
for i := 0; i < len(tc.txs); i++ {
|
||||
if tc.txs[i].BatchNum == nil {
|
||||
continue
|
||||
}
|
||||
var fromIdx *common.Idx
|
||||
if tc.txs[i].FromIdx != nil {
|
||||
fromIdx, err = stringToIdx(*tc.txs[i].FromIdx, "")
|
||||
@@ -388,7 +416,7 @@ func TestGetHistoryTxs(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
txTypeTxs := []testTx{}
|
||||
for i := 0; i < len(tc.txs); i++ {
|
||||
if tc.txs[i].Type == txType {
|
||||
if tc.txs[i].Type == txType && tc.txs[i].BatchNum != nil {
|
||||
txTypeTxs = append(txTypeTxs, tc.txs[i])
|
||||
}
|
||||
}
|
||||
@@ -420,7 +448,9 @@ func TestGetHistoryTxs(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
flipedTxs := []testTx{}
|
||||
for i := 0; i < len(tc.txs); i++ {
|
||||
flipedTxs = append(flipedTxs, tc.txs[len(tc.txs)-1-i])
|
||||
if tc.txs[len(tc.txs)-1-i].BatchNum != nil {
|
||||
flipedTxs = append(flipedTxs, tc.txs[len(tc.txs)-1-i])
|
||||
}
|
||||
}
|
||||
assertTxs(t, flipedTxs, fetchedTxs)
|
||||
// Empty array
|
||||
@@ -431,8 +461,8 @@ func TestGetHistoryTxs(t *testing.T) {
|
||||
assertTxs(t, []testTx{}, fetchedTxs)
|
||||
// 400
|
||||
path = fmt.Sprintf(
|
||||
"%s?fromAccountIndex=%s&toAccountIndex=%s&hezEthereumAddress=%s",
|
||||
endpoint, idx, idx, account.EthAddr,
|
||||
"%s?accountIndex=%s&hezEthereumAddress=%s",
|
||||
endpoint, idx, account.EthAddr,
|
||||
)
|
||||
err = doBadReq("GET", path, nil, 400)
|
||||
assert.NoError(t, err)
|
||||
|
||||
@@ -55,41 +55,6 @@ func (a *API) getPoolTx(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, tx)
|
||||
}
|
||||
|
||||
func (a *API) getPoolTxs(c *gin.Context) {
|
||||
// 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
|
||||
}
|
||||
// Get state
|
||||
state, err := parseQueryPoolL2TxState(c)
|
||||
if err != nil {
|
||||
retBadReq(err, c)
|
||||
return
|
||||
}
|
||||
// Fetch txs from l2DB
|
||||
txs, err := a.l2.GetPoolTxs(fromIdx, toIdx, state)
|
||||
if err != nil {
|
||||
retSQLErr(err, c)
|
||||
return
|
||||
}
|
||||
|
||||
// Build successful response
|
||||
type txsResponse struct {
|
||||
Txs []*l2db.PoolTxAPI `json:"transactions"`
|
||||
}
|
||||
c.JSON(http.StatusOK, &txsResponse{
|
||||
Txs: txs,
|
||||
})
|
||||
}
|
||||
|
||||
type receivedPoolTx struct {
|
||||
TxID common.TxID `json:"id" binding:"required"`
|
||||
Type common.TxType `json:"type" binding:"required"`
|
||||
|
||||
@@ -47,10 +47,6 @@ type testPoolTxReceive struct {
|
||||
Token historydb.TokenWithUSD `json:"token"`
|
||||
}
|
||||
|
||||
type testPoolTxsResponse struct {
|
||||
Txs []testPoolTxReceive `json:"transactions"`
|
||||
}
|
||||
|
||||
// testPoolTxSend is a struct to be used as a JSON body
|
||||
// when testing POST /transactions-pool
|
||||
type testPoolTxSend struct {
|
||||
@@ -229,24 +225,6 @@ func TestPoolTxs(t *testing.T) {
|
||||
err = doBadReq("POST", endpoint, jsonTxReader, 400)
|
||||
require.NoError(t, err)
|
||||
// GET
|
||||
// get by idx
|
||||
fetchedTxs := testPoolTxsResponse{}
|
||||
require.NoError(t, doGoodReq(
|
||||
"GET",
|
||||
endpoint+"?fromAccountIndex=hez:ETH:263",
|
||||
nil, &fetchedTxs))
|
||||
assert.Equal(t, 1, len(fetchedTxs.Txs))
|
||||
assert.Equal(t, "hez:ETH:263", fetchedTxs.Txs[0].FromIdx)
|
||||
// get by state
|
||||
require.NoError(t, doGoodReq(
|
||||
"GET",
|
||||
endpoint+"?state=pend",
|
||||
nil, &fetchedTxs))
|
||||
assert.Equal(t, 4, len(fetchedTxs.Txs))
|
||||
for _, v := range fetchedTxs.Txs {
|
||||
assert.Equal(t, common.PoolL2TxStatePending, v.State)
|
||||
}
|
||||
// GET
|
||||
endpoint += "/"
|
||||
for _, tx := range tc.poolTxsToReceive {
|
||||
fetchedTx := testPoolTxReceive{}
|
||||
|
||||
@@ -456,7 +456,8 @@ func (hdb *HistoryDB) GetTxAPI(txID common.TxID) (*TxAPI, error) {
|
||||
// and pagination info
|
||||
func (hdb *HistoryDB) GetTxsAPI(
|
||||
ethAddr *ethCommon.Address, bjj *babyjub.PublicKeyComp,
|
||||
tokenID *common.TokenID, fromIdx, toIdx *common.Idx, batchNum *uint, txType *common.TxType,
|
||||
tokenID *common.TokenID, idx *common.Idx, batchNum *uint, txType *common.TxType,
|
||||
includePendingL1s *bool,
|
||||
fromItem, limit *uint, order string,
|
||||
) ([]TxAPI, uint64, error) {
|
||||
// Warning: amount_success and deposit_amount_success have true as default for
|
||||
@@ -508,32 +509,14 @@ func (hdb *HistoryDB) GetTxsAPI(
|
||||
nextIsAnd = true
|
||||
}
|
||||
// idx filter
|
||||
if fromIdx != nil && toIdx != nil {
|
||||
if idx != nil {
|
||||
if nextIsAnd {
|
||||
queryStr += "AND "
|
||||
} else {
|
||||
queryStr += "WHERE "
|
||||
}
|
||||
queryStr += "(tx.effective_from_idx = ? "
|
||||
queryStr += "OR tx.to_idx = ?) "
|
||||
args = append(args, fromIdx, toIdx)
|
||||
nextIsAnd = true
|
||||
} else if fromIdx != nil {
|
||||
if nextIsAnd {
|
||||
queryStr += "AND "
|
||||
} else {
|
||||
queryStr += "WHERE "
|
||||
}
|
||||
queryStr += "tx.effective_from_idx = ? "
|
||||
nextIsAnd = true
|
||||
} else if toIdx != nil {
|
||||
if nextIsAnd {
|
||||
queryStr += "AND "
|
||||
} else {
|
||||
queryStr += "WHERE "
|
||||
}
|
||||
queryStr += "tx.to_idx = ? "
|
||||
args = append(args, toIdx)
|
||||
queryStr += "(tx.effective_from_idx = ? OR tx.to_idx = ?) "
|
||||
args = append(args, idx, idx)
|
||||
nextIsAnd = true
|
||||
}
|
||||
// batchNum filter
|
||||
@@ -572,12 +555,16 @@ func (hdb *HistoryDB) GetTxsAPI(
|
||||
args = append(args, fromItem)
|
||||
nextIsAnd = true
|
||||
}
|
||||
if nextIsAnd {
|
||||
queryStr += "AND "
|
||||
} else {
|
||||
queryStr += "WHERE "
|
||||
|
||||
// Include pending L1 txs? (deafault false)
|
||||
if includePendingL1s == nil || (includePendingL1s != nil && !*includePendingL1s) {
|
||||
if nextIsAnd {
|
||||
queryStr += "AND "
|
||||
} else {
|
||||
queryStr += "WHERE "
|
||||
}
|
||||
queryStr += "tx.batch_num IS NOT NULL "
|
||||
}
|
||||
queryStr += "tx.batch_num IS NOT NULL "
|
||||
|
||||
// pagination
|
||||
queryStr += "ORDER BY tx.item_id "
|
||||
|
||||
@@ -127,57 +127,3 @@ func (l2db *L2DB) GetTxAPI(txID common.TxID) (*PoolTxAPI, error) {
|
||||
txID,
|
||||
))
|
||||
}
|
||||
|
||||
// GetPoolTxs return Txs from the pool
|
||||
func (l2db *L2DB) GetPoolTxs(fromIdx, toIdx *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 fromIdx != nil && toIdx != nil {
|
||||
if nextIsAnd {
|
||||
queryStr += "AND ("
|
||||
} else {
|
||||
queryStr += "WHERE ("
|
||||
}
|
||||
queryStr += "tx_pool.from_idx = ? "
|
||||
queryStr += "OR tx_pool.to_idx = ?) "
|
||||
args = append(args, fromIdx, 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)
|
||||
txs := []*PoolTxAPI{}
|
||||
err = meddler.QueryAll(
|
||||
l2db.dbRead, &txs,
|
||||
query,
|
||||
args...)
|
||||
return txs, tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
@@ -311,28 +311,6 @@ 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()
|
||||
require.NoError(t, err)
|
||||
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, &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
|
||||
|
||||
@@ -245,15 +245,15 @@ func (c *EthereumClient) EthBlockByNumber(ctx context.Context, number int64) (*c
|
||||
if number == -1 {
|
||||
blockNum = nil
|
||||
}
|
||||
header, err := c.client.HeaderByNumber(ctx, blockNum)
|
||||
block, err := c.client.BlockByNumber(ctx, blockNum)
|
||||
if err != nil {
|
||||
return nil, tracerr.Wrap(err)
|
||||
}
|
||||
b := &common.Block{
|
||||
Num: header.Number.Int64(),
|
||||
Timestamp: time.Unix(int64(header.Time), 0),
|
||||
ParentHash: header.ParentHash,
|
||||
Hash: header.Hash(),
|
||||
Num: block.Number().Int64(),
|
||||
Timestamp: time.Unix(int64(block.Time()), 0),
|
||||
ParentHash: block.ParentHash(),
|
||||
Hash: block.Hash(),
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user