mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Test purger, fix some nonces
- Test all the purger functions - Fix nonces set by til (previously til started with nonce 1 for pool l2txs, but the correct implementation is to start with nonce 0) - Rename L2DB.CheckNonces to L2DB.invalidateOldNoncesQuery - Rename L2DB.checkNoncesQuery to L2DB.InvalidateOldNonces Related https://github.com/hermeznetwork/hermez-node/issues/392 (Fix checkNoncesQuery) Resolve https://github.com/hermeznetwork/hermez-node/issues/396
This commit is contained in:
@@ -268,7 +268,7 @@ func (l2db *L2DB) GetPendingUniqueFromIdxs() ([]common.Idx, error) {
|
||||
return idxs, nil
|
||||
}
|
||||
|
||||
var checkNoncesQuery = fmt.Sprintf(`
|
||||
var invalidateOldNoncesQuery = fmt.Sprintf(`
|
||||
UPDATE tx_pool SET
|
||||
state = '%s',
|
||||
batch_num = %%d
|
||||
@@ -276,20 +276,22 @@ var checkNoncesQuery = fmt.Sprintf(`
|
||||
(NULL::::BIGINT, NULL::::BIGINT),
|
||||
(:idx, :nonce)
|
||||
) as updated_acc (idx, nonce)
|
||||
WHERE tx_pool.from_idx = updated_acc.idx AND tx_pool.nonce <= updated_acc.nonce;
|
||||
`, common.PoolL2TxStateInvalid)
|
||||
WHERE tx_pool.state = '%s' AND
|
||||
tx_pool.from_idx = updated_acc.idx AND
|
||||
tx_pool.nonce < updated_acc.nonce;
|
||||
`, common.PoolL2TxStateInvalid, common.PoolL2TxStatePending)
|
||||
|
||||
// CheckNonces invalidate txs with nonces that are smaller or equal than their
|
||||
// InvalidateOldNonces invalidate txs with nonces that are smaller or equal than their
|
||||
// respective accounts nonces. The state of the affected txs will be changed
|
||||
// from Pending to Invalid
|
||||
func (l2db *L2DB) CheckNonces(updatedAccounts []common.IdxNonce, batchNum common.BatchNum) (err error) {
|
||||
func (l2db *L2DB) InvalidateOldNonces(updatedAccounts []common.IdxNonce, batchNum common.BatchNum) (err error) {
|
||||
if len(updatedAccounts) == 0 {
|
||||
return nil
|
||||
}
|
||||
// Fill the batch_num in the query with Sprintf because we are using a
|
||||
// named query which works with slices, and doens't handle an extra
|
||||
// individual argument.
|
||||
query := fmt.Sprintf(checkNoncesQuery, batchNum)
|
||||
query := fmt.Sprintf(invalidateOldNoncesQuery, batchNum)
|
||||
if _, err := sqlx.NamedExec(l2db.db, query, updatedAccounts); err != nil {
|
||||
return tracerr.Wrap(err)
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/hermeznetwork/hermez-node/test/til"
|
||||
"github.com/hermeznetwork/tracerr"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
var l2DB *L2DB
|
||||
@@ -217,7 +218,7 @@ func TestGetPending(t *testing.T) {
|
||||
|
||||
func TestStartForging(t *testing.T) {
|
||||
// Generate txs
|
||||
const fakeBatchNum common.BatchNum = 33
|
||||
var fakeBatchNum common.BatchNum = 33
|
||||
err := prepareHistoryDB(historyDB)
|
||||
if err != nil {
|
||||
log.Error("Error prepare historyDB", err)
|
||||
@@ -243,13 +244,13 @@ func TestStartForging(t *testing.T) {
|
||||
fetchedTx, err := l2DB.GetTxAPI(id)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, common.PoolL2TxStateForging, fetchedTx.State)
|
||||
assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum)
|
||||
assert.Equal(t, &fakeBatchNum, fetchedTx.BatchNum)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoneForging(t *testing.T) {
|
||||
// Generate txs
|
||||
const fakeBatchNum common.BatchNum = 33
|
||||
var fakeBatchNum common.BatchNum = 33
|
||||
err := prepareHistoryDB(historyDB)
|
||||
if err != nil {
|
||||
log.Error("Error prepare historyDB", err)
|
||||
@@ -288,13 +289,13 @@ func TestDoneForging(t *testing.T) {
|
||||
fetchedTx, err := l2DB.GetTxAPI(id)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, common.PoolL2TxStateForged, fetchedTx.State)
|
||||
assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum)
|
||||
assert.Equal(t, &fakeBatchNum, fetchedTx.BatchNum)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidate(t *testing.T) {
|
||||
// Generate txs
|
||||
const fakeBatchNum common.BatchNum = 33
|
||||
var fakeBatchNum common.BatchNum = 33
|
||||
err := prepareHistoryDB(historyDB)
|
||||
if err != nil {
|
||||
log.Error("Error prepare historyDB", err)
|
||||
@@ -320,13 +321,13 @@ func TestInvalidate(t *testing.T) {
|
||||
fetchedTx, err := l2DB.GetTxAPI(id)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, common.PoolL2TxStateInvalid, fetchedTx.State)
|
||||
assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum)
|
||||
assert.Equal(t, &fakeBatchNum, fetchedTx.BatchNum)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckNonces(t *testing.T) {
|
||||
func TestInvalidateOldNonces(t *testing.T) {
|
||||
// Generate txs
|
||||
const fakeBatchNum common.BatchNum = 33
|
||||
var fakeBatchNum common.BatchNum = 33
|
||||
err := prepareHistoryDB(historyDB)
|
||||
if err != nil {
|
||||
log.Error("Error prepare historyDB", err)
|
||||
@@ -335,7 +336,7 @@ func TestCheckNonces(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
// Update Accounts currentNonce
|
||||
var updateAccounts []common.IdxNonce
|
||||
const currentNonce = common.Nonce(1)
|
||||
var currentNonce = common.Nonce(1)
|
||||
for i := range accs {
|
||||
updateAccounts = append(updateAccounts, common.IdxNonce{
|
||||
Idx: accs[i].Idx,
|
||||
@@ -345,21 +346,23 @@ func TestCheckNonces(t *testing.T) {
|
||||
// Add txs to DB
|
||||
var invalidTxIDs []common.TxID
|
||||
for i := range poolL2Txs {
|
||||
if poolL2Txs[i].Nonce <= currentNonce {
|
||||
if poolL2Txs[i].Nonce < currentNonce {
|
||||
invalidTxIDs = append(invalidTxIDs, poolL2Txs[i].TxID)
|
||||
}
|
||||
err := l2DB.AddTxTest(&poolL2Txs[i])
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
// sanity check
|
||||
require.Greater(t, len(invalidTxIDs), 0)
|
||||
|
||||
err = l2DB.CheckNonces(updateAccounts, fakeBatchNum)
|
||||
err = l2DB.InvalidateOldNonces(updateAccounts, fakeBatchNum)
|
||||
assert.NoError(t, err)
|
||||
// Fetch txs and check that they've been updated correctly
|
||||
for _, id := range invalidTxIDs {
|
||||
fetchedTx, err := l2DB.GetTxAPI(id)
|
||||
assert.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, common.PoolL2TxStateInvalid, fetchedTx.State)
|
||||
assert.Equal(t, fakeBatchNum, *fetchedTx.BatchNum)
|
||||
assert.Equal(t, &fakeBatchNum, fetchedTx.BatchNum)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user