mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Add txselector base
This commit is contained in:
40
txselector/common/tmp.go
Normal file
40
txselector/common/tmp.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"math/big"
|
||||
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
)
|
||||
|
||||
// WIP this will be from hermeznetwork/common
|
||||
type Tx struct {
|
||||
FromAx [32]byte
|
||||
FromAy [32]byte
|
||||
FromEthAddr ethCommon.Address
|
||||
ToAx [32]byte
|
||||
ToAy [32]byte
|
||||
ToEthAddr ethCommon.Address
|
||||
OnChain bool
|
||||
RqOffset []byte
|
||||
NewAccount bool
|
||||
TokenID uint32
|
||||
LoadAmount [3]byte
|
||||
Amount [3]byte
|
||||
Nonce uint64
|
||||
UserFee uint8
|
||||
UserFeeAbsolute uint64
|
||||
R8x [32]byte
|
||||
R8y [32]byte
|
||||
S [32]byte
|
||||
RqTxData [32]byte
|
||||
}
|
||||
|
||||
// WIP this will be from hermeznetwork/common
|
||||
type Account struct {
|
||||
EthAddr ethCommon.Address
|
||||
TokenID uint32
|
||||
Idx uint32
|
||||
Nonce uint64
|
||||
Balance *big.Int
|
||||
// Ax, Ay
|
||||
}
|
||||
33
txselector/mock/mock.go
Normal file
33
txselector/mock/mock.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package mock
|
||||
|
||||
import (
|
||||
"github.com/hermeznetwork/hermez-node/tx-selector/common"
|
||||
)
|
||||
|
||||
type MockDB struct {
|
||||
Txs map[uint64][]common.Tx
|
||||
|
||||
// AccountDB is the LocalAccountDB copy of the original AccountDB
|
||||
AccountDB map[[36]byte]common.Account // [36]byte is tx.ToEthAddr + tx.TokenID
|
||||
|
||||
PendingRegistersDB map[[36]byte]common.Account // [36]byte is tx.ToEthAddr + tx.TokenID
|
||||
}
|
||||
|
||||
func New() *MockDB {
|
||||
return &MockDB{
|
||||
Txs: make(map[uint64][]common.Tx),
|
||||
AccountDB: make(map[[36]byte]common.Account),
|
||||
PendingRegistersDB: make(map[[36]byte]common.Account),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockDB) AddTx(batchID uint64, tx common.Tx) {
|
||||
if _, ok := m.Txs[batchID]; !ok {
|
||||
m.Txs[batchID] = []common.Tx{}
|
||||
}
|
||||
m.Txs[batchID] = append(m.Txs[batchID], tx)
|
||||
}
|
||||
|
||||
func (m *MockDB) GetTxs(batchID uint64) []common.Tx {
|
||||
return m.Txs[batchID]
|
||||
}
|
||||
142
txselector/txselector.go
Normal file
142
txselector/txselector.go
Normal file
@@ -0,0 +1,142 @@
|
||||
package txselector
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/hermeznetwork/hermez-node/tx-selector/common"
|
||||
"github.com/hermeznetwork/hermez-node/tx-selector/mock"
|
||||
)
|
||||
|
||||
// txs implements the interface Sort for an array of Tx
|
||||
type txs []common.Tx
|
||||
|
||||
func (t txs) Len() int {
|
||||
return len(t)
|
||||
}
|
||||
func (t txs) Swap(i, j int) {
|
||||
t[i], t[j] = t[j], t[i]
|
||||
}
|
||||
func (t txs) Less(i, j int) bool {
|
||||
return t[i].UserFeeAbsolute > t[j].UserFeeAbsolute
|
||||
}
|
||||
|
||||
type TxSelector struct {
|
||||
// NMax is the maximum L1-user-tx for a batch
|
||||
NMax uint64
|
||||
// MMax is the maximum L1-operator-tx for a batch
|
||||
MMax uint64
|
||||
// PMax is the maximum L2-tx for a batch
|
||||
PMax uint64
|
||||
// DB is a pointer to the database interface
|
||||
DB *mock.MockDB
|
||||
}
|
||||
|
||||
func NewTxSelector(db *mock.MockDB, nMax, mMax, pMax uint64) *TxSelector {
|
||||
return &TxSelector{
|
||||
NMax: nMax,
|
||||
MMax: mMax,
|
||||
PMax: pMax,
|
||||
DB: db,
|
||||
}
|
||||
}
|
||||
|
||||
func (txsel *TxSelector) updateLocalAccountDB(batchId uint64) error {
|
||||
// if batchID > max(localAccountDB.BatchID) + 1
|
||||
// make a checkpoint of AccountDB at BatchID to a localAccountDB
|
||||
// use localAccountDB[inputBatchID-1]
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (txsel *TxSelector) GetL2TxSelection(batchID uint64) ([]common.Tx, error) {
|
||||
err := txsel.updateLocalAccountDB(batchID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// get pending l2-tx from tx-pool
|
||||
txsRaw := txsel.DB.GetTxs(batchID)
|
||||
|
||||
// discard the txs that don't have an Account in the AccountDB
|
||||
var validTxs txs
|
||||
for _, tx := range txsRaw {
|
||||
accountID := getAccountID(tx.ToEthAddr, tx.TokenID)
|
||||
if _, ok := txsel.DB.AccountDB[accountID]; ok {
|
||||
validTxs = append(validTxs, tx)
|
||||
}
|
||||
}
|
||||
|
||||
// get most profitable L2-tx (len<NMax)
|
||||
txs := txsel.getL2Profitable(validTxs)
|
||||
|
||||
// apply L2-tx to local AccountDB, make checkpoint tagged with BatchID
|
||||
// update balances
|
||||
// update nonces
|
||||
|
||||
// return selected txs
|
||||
return txs, nil
|
||||
}
|
||||
|
||||
func (txsel *TxSelector) GetL1L2TxSelection(batchID uint64, l1txs []common.Tx) ([]common.Tx, []common.Tx, []common.Tx, error) {
|
||||
err := txsel.updateLocalAccountDB(batchID)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
// apply l1-user-tx to localAccountDB
|
||||
// create new leaves
|
||||
// update balances
|
||||
// update nonces
|
||||
|
||||
// get pending l2-tx from tx-pool
|
||||
txsRaw := txsel.DB.GetTxs(batchID)
|
||||
|
||||
// discard the txs that don't have an Account in the AccountDB
|
||||
// neither appear in the PendingRegistersDB
|
||||
var validTxs txs
|
||||
for _, tx := range txsRaw {
|
||||
accountID := getAccountID(tx.ToEthAddr, tx.TokenID)
|
||||
exist := txsel.checkIfAccountExist(accountID)
|
||||
if exist {
|
||||
validTxs = append(validTxs, tx)
|
||||
}
|
||||
}
|
||||
|
||||
// get most profitable L2-tx (len<NMax)
|
||||
l2txs := txsel.getL2Profitable(validTxs)
|
||||
|
||||
// prepare (from the selected l2txs) pending to register from the PendingRegistersDB
|
||||
var pendingRegisters []common.Account
|
||||
for _, tx := range l2txs {
|
||||
accountID := getAccountID(tx.ToEthAddr, tx.TokenID)
|
||||
if toRegister, ok := txsel.DB.PendingRegistersDB[accountID]; ok {
|
||||
pendingRegisters = append(pendingRegisters, toRegister)
|
||||
}
|
||||
}
|
||||
|
||||
// create L1-operator-tx for each L2-tx selected in which the recipient does not have account
|
||||
l1OperatorTxs := txsel.createL1OperatorTxForL2Tx(pendingRegisters) // only with the L2-tx selected ones
|
||||
|
||||
return l1txs, l2txs, l1OperatorTxs, nil
|
||||
}
|
||||
|
||||
func (txsel *TxSelector) checkIfAccountExist(accountID [36]byte) bool {
|
||||
// check if account exist in AccountDB
|
||||
if _, ok := txsel.DB.AccountDB[accountID]; ok {
|
||||
return true
|
||||
}
|
||||
// check if account is pending to register
|
||||
if _, ok := txsel.DB.PendingRegistersDB[accountID]; ok {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (txsel *TxSelector) getL2Profitable(txs txs) txs {
|
||||
sort.Sort(txs)
|
||||
return txs[:txsel.PMax]
|
||||
}
|
||||
func (txsel *TxSelector) createL1OperatorTxForL2Tx(accounts []common.Account) txs {
|
||||
//
|
||||
return txs{}
|
||||
}
|
||||
116
txselector/txselector_test.go
Normal file
116
txselector/txselector_test.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package txselector
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
"github.com/hermeznetwork/hermez-node/tx-selector/common"
|
||||
"github.com/hermeznetwork/hermez-node/tx-selector/mock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func initMockDB() *mock.MockDB {
|
||||
m := mock.New()
|
||||
|
||||
txs := []common.Tx{
|
||||
{
|
||||
FromEthAddr: ethCommon.HexToAddress("0x859c3d0d5aD917F146fF6654A4C676f1ddeCE26a"),
|
||||
ToEthAddr: ethCommon.HexToAddress("0x6950E814B82d276DB5Fa7f34253CfeE1387fe03E"),
|
||||
TokenID: 1,
|
||||
Nonce: 1,
|
||||
UserFeeAbsolute: 1,
|
||||
},
|
||||
{
|
||||
FromEthAddr: ethCommon.HexToAddress("0x859c3d0d5aD917F146fF6654A4C676f1ddeCE26a"),
|
||||
ToEthAddr: ethCommon.HexToAddress("0x6950E814B82d276DB5Fa7f34253CfeE1387fe03E"),
|
||||
TokenID: 1,
|
||||
Nonce: 2,
|
||||
UserFeeAbsolute: 3,
|
||||
},
|
||||
{
|
||||
FromEthAddr: ethCommon.HexToAddress("0x859c3d0d5aD917F146fF6654A4C676f1ddeCE26a"),
|
||||
ToEthAddr: ethCommon.HexToAddress("0x6950E814B82d276DB5Fa7f34253CfeE1387fe03E"),
|
||||
TokenID: 1,
|
||||
Nonce: 4,
|
||||
UserFeeAbsolute: 6,
|
||||
},
|
||||
{
|
||||
FromEthAddr: ethCommon.HexToAddress("0x859c3d0d5aD917F146fF6654A4C676f1ddeCE26a"),
|
||||
ToEthAddr: ethCommon.HexToAddress("0x6950E814B82d276DB5Fa7f34253CfeE1387fe03E"),
|
||||
TokenID: 1,
|
||||
Nonce: 4,
|
||||
UserFeeAbsolute: 4,
|
||||
},
|
||||
{
|
||||
FromEthAddr: ethCommon.HexToAddress("0x6950E814B82d276DB5Fa7f34253CfeE1387fe03E"),
|
||||
ToEthAddr: ethCommon.HexToAddress("0x859c3d0d5aD917F146fF6654A4C676f1ddeCE26a"),
|
||||
TokenID: 1,
|
||||
Nonce: 1,
|
||||
UserFeeAbsolute: 4,
|
||||
},
|
||||
{
|
||||
FromEthAddr: ethCommon.HexToAddress("0x6950E814B82d276DB5Fa7f34253CfeE1387fe03E"),
|
||||
ToEthAddr: ethCommon.HexToAddress("0x859c3d0d5aD917F146fF6654A4C676f1ddeCE26a"),
|
||||
TokenID: 1,
|
||||
Nonce: 2,
|
||||
UserFeeAbsolute: 3,
|
||||
},
|
||||
{
|
||||
FromEthAddr: ethCommon.HexToAddress("0x6950E814B82d276DB5Fa7f34253CfeE1387fe03E"),
|
||||
ToEthAddr: ethCommon.HexToAddress("0x859c3d0d5aD917F146fF6654A4C676f1ddeCE26a"),
|
||||
TokenID: 1,
|
||||
Nonce: 3,
|
||||
UserFeeAbsolute: 5,
|
||||
},
|
||||
{
|
||||
// this tx will not be selected, as the ToEthAddr does not have an account
|
||||
FromEthAddr: ethCommon.HexToAddress("0x6950E814B82d276DB5Fa7f34253CfeE1387fe03E"),
|
||||
ToEthAddr: ethCommon.HexToAddress("0x4a2CFDF534725D8D6e07Af97B237Fff19BDb3c93"),
|
||||
TokenID: 1,
|
||||
Nonce: 4,
|
||||
UserFeeAbsolute: 5,
|
||||
},
|
||||
}
|
||||
|
||||
// n := 0
|
||||
nBatch := 0
|
||||
for i := 0; i < len(txs); i++ {
|
||||
// for i := 0; i < nBatch; i++ {
|
||||
// for j := 0; j < len(txs)/nBatch; j++ {
|
||||
// store tx
|
||||
m.AddTx(uint64(nBatch), txs[i])
|
||||
|
||||
// store account if not yet
|
||||
accountID := getAccountID(txs[i].FromEthAddr, txs[i].TokenID)
|
||||
if _, ok := m.AccountDB[accountID]; !ok {
|
||||
account := common.Account{
|
||||
EthAddr: txs[i].FromEthAddr,
|
||||
TokenID: txs[i].TokenID,
|
||||
Nonce: 0,
|
||||
Balance: big.NewInt(0),
|
||||
}
|
||||
m.AccountDB[accountID] = account
|
||||
}
|
||||
// n++
|
||||
// }
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func TestGetL2TxSelection(t *testing.T) {
|
||||
mockDB := initMockDB()
|
||||
txsel := NewTxSelector(mockDB, 3, 3, 3)
|
||||
|
||||
txs, err := txsel.GetL2TxSelection(0)
|
||||
assert.Nil(t, err)
|
||||
for _, tx := range txs {
|
||||
fmt.Println(tx.FromEthAddr.String(), tx.ToEthAddr.String(), tx.UserFeeAbsolute)
|
||||
}
|
||||
assert.Equal(t, 3, len(txs))
|
||||
assert.Equal(t, uint64(6), txs[0].UserFeeAbsolute)
|
||||
assert.Equal(t, uint64(5), txs[1].UserFeeAbsolute)
|
||||
assert.Equal(t, uint64(4), txs[2].UserFeeAbsolute)
|
||||
}
|
||||
16
txselector/utils.go
Normal file
16
txselector/utils.go
Normal file
@@ -0,0 +1,16 @@
|
||||
package txselector
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
||||
ethCommon "github.com/ethereum/go-ethereum/common"
|
||||
)
|
||||
|
||||
func getAccountID(addr ethCommon.Address, tokenID uint32) [36]byte {
|
||||
var tokenIDBytes [4]byte
|
||||
binary.LittleEndian.PutUint32(tokenIDBytes[:], tokenID)
|
||||
accountIDBytes := append(addr[:], tokenIDBytes[:]...)
|
||||
var accountID [36]byte
|
||||
copy(accountID[:], accountIDBytes[:36])
|
||||
return accountID
|
||||
}
|
||||
Reference in New Issue
Block a user