arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/txselector/txselector_test.go
txselector/txselector_test.goRaw
  1package txselector
  2
  3import (
  4	"fmt"
  5	"io/ioutil"
  6	"testing"
  7
  8	"github.com/hermeznetwork/hermez-node/db/l2db"
  9	"github.com/hermeznetwork/hermez-node/db/statedb"
 10
 11	"github.com/stretchr/testify/assert"
 12	"github.com/stretchr/testify/require"
 13)
 14
 15/*
 16func initTestDB(l2 *l2db.L2DB, sdb *statedb.StateDB) *mock.MockDB {
 17	txs := []common.Tx{
 18		{
 19			FromIdx:     common.Idx(0),
 20			ToIdx:       common.Idx(1),
 21			TokenID:     1,
 22			Nonce:       1,
 23			AbsoluteFee: 1,
 24		},
 25		{
 26			FromIdx:     common.Idx(0),
 27			ToIdx:       common.Idx(1),
 28			TokenID:     1,
 29			Nonce:       2,
 30			AbsoluteFee: 3,
 31		},
 32		{
 33			FromIdx:     common.Idx(0),
 34			ToIdx:       common.Idx(1),
 35			TokenID:     1,
 36			Nonce:       4,
 37			AbsoluteFee: 6,
 38		},
 39		{
 40			FromIdx:     common.Idx(0),
 41			ToIdx:       common.Idx(1),
 42			TokenID:     1,
 43			Nonce:       4,
 44			AbsoluteFee: 4,
 45		},
 46		{
 47			ToIdx:       common.Idx(1),
 48			FromIdx:     common.Idx(0),
 49			TokenID:     1,
 50			Nonce:       1,
 51			AbsoluteFee: 4,
 52		},
 53		{
 54			ToIdx:       common.Idx(1),
 55			FromIdx:     common.Idx(0),
 56			TokenID:     1,
 57			Nonce:       2,
 58			AbsoluteFee: 3,
 59		},
 60		{
 61			ToIdx:       common.Idx(1),
 62			FromIdx:     common.Idx(0),
 63			TokenID:     1,
 64			Nonce:       3,
 65			AbsoluteFee: 5,
 66		},
 67		{
 68			// this tx will not be selected, as the ToEthAddr does not have an account
 69			FromIdx:     common.Idx(1),
 70			ToIdx:       common.Idx(2),
 71			TokenID:     1,
 72			Nonce:       4,
 73			AbsoluteFee: 5,
 74		},
 75	}
 76
 77	// n := 0
 78	nBatch := 0
 79	for i := 0; i < len(txs); i++ {
 80		// for i := 0; i < nBatch; i++ {
 81		//         for j := 0; j < len(txs)/nBatch; j++ {
 82		// store tx
 83		l2db.AddTx(uint64(nBatch), txs[i])
 84
 85		// store account if not yet
 86		accountID := getAccountID(txs[i].FromEthAddr, txs[i].TokenID)
 87		if _, ok := m.AccountDB[accountID]; !ok {
 88			account := common.Account{
 89				// EthAddr: txs[i].FromEthAddr,
 90				TokenID: txs[i].TokenID,
 91				Nonce:   0,
 92				Balance: big.NewInt(0),
 93			}
 94			m.AccountDB[accountID] = account
 95		}
 96		// n++
 97		// }
 98	}
 99
100	return m
101}
102*/
103
104func TestGetL2TxSelection(t *testing.T) {
105	dir, err := ioutil.TempDir("", "tmpdb")
106	require.Nil(t, err)
107	sdb, err := statedb.NewStateDB(dir, false, false, 0)
108	assert.Nil(t, err)
109	testL2DB := &l2db.L2DB{}
110	// initTestDB(testL2DB, sdb)
111
112	txsel, err := NewTxSelector(sdb, testL2DB, 3, 3, 3)
113	assert.Nil(t, err)
114	fmt.Println(txsel)
115
116	// txs, err := txsel.GetL2TxSelection(0)
117	// assert.Nil(t, err)
118	// for _, tx := range txs {
119	//         fmt.Println(tx.FromIdx, tx.ToIdx, tx.AbsoluteFee)
120	// }
121	// assert.Equal(t, 3, len(txs))
122	// assert.Equal(t, uint64(6), txs[0].AbsoluteFee)
123	// assert.Equal(t, uint64(5), txs[1].AbsoluteFee)
124	// assert.Equal(t, uint64(4), txs[2].AbsoluteFee)
125}