Extend ethclient test, implement new TxID spec

- Implement new TxID spec that distinguishes L1UserTx and L1CoordinatorTx
- Replace some type []*Foo by []Foo
- Fix HistoryDB & L2DB bug: in case of error, a rollback was applied and the returned error was nil
- Reorder inserts in historydb.NewHistoryDB() to follow foreign key dependencies
- Add initial synchronizer test with test.Client (for now, only tested l1UserTxs, blocks, addToken)
- Update L1UserTx event in test.Client
This commit is contained in:
Eduard S
2020-10-02 13:11:23 +02:00
parent 3d7b71e1fd
commit 0277210c39
21 changed files with 380 additions and 240 deletions

View File

@@ -81,26 +81,41 @@ func NewL1Tx(l1Tx *L1Tx) (*L1Tx, error) {
}
l1Tx.Type = txType
var txid [TxIDLen]byte
if !l1Tx.UserOrigin {
txid[0] = TxIDPrefixL1CoordTx
txID, err := l1Tx.CalcTxID()
if err != nil {
return nil, err
}
var toForgeL1TxsNumBytes [8]byte
var toForge uint64 = 0
if l1Tx.ToForgeL1TxsNum != nil {
toForge = uint64(*l1Tx.ToForgeL1TxsNum)
}
binary.BigEndian.PutUint64(toForgeL1TxsNumBytes[:], toForge)
copy(txid[1:9], toForgeL1TxsNumBytes[:])
var positionBytes [2]byte
binary.BigEndian.PutUint16(positionBytes[:], uint16(l1Tx.Position))
copy(txid[9:11], positionBytes[:])
l1Tx.TxID = TxID(txid)
l1Tx.TxID = *txID
return l1Tx, nil
}
func (l1Tx *L1Tx) CalcTxID() (*TxID, error) {
var txID TxID
if l1Tx.UserOrigin {
if l1Tx.ToForgeL1TxsNum == nil {
return nil, fmt.Errorf("L1Tx.UserOrigin == true && L1Tx.ToForgeL1TxsNum == nil")
}
txID[0] = TxIDPrefixL1UserTx
var toForgeL1TxsNumBytes [8]byte
binary.BigEndian.PutUint64(toForgeL1TxsNumBytes[:], uint64(*l1Tx.ToForgeL1TxsNum))
copy(txID[1:9], toForgeL1TxsNumBytes[:])
} else {
if l1Tx.BatchNum == nil {
return nil, fmt.Errorf("L1Tx.UserOrigin == false && L1Tx.BatchNum == nil")
}
txID[0] = TxIDPrefixL1CoordTx
var batchNumBytes [8]byte
binary.BigEndian.PutUint64(batchNumBytes[:], uint64(*l1Tx.BatchNum))
copy(txID[1:9], batchNumBytes[:])
}
var positionBytes [2]byte
binary.BigEndian.PutUint16(positionBytes[:], uint16(l1Tx.Position))
copy(txID[9:11], positionBytes[:])
return &txID, nil
}
// Tx returns a *Tx from the L1Tx
func (tx *L1Tx) Tx() *Tx {
f := new(big.Float).SetInt(tx.Amount)

View File

@@ -15,23 +15,40 @@ import (
"github.com/stretchr/testify/require"
)
func TestNewL1Tx(t *testing.T) {
toForge := new(int64)
*toForge = 123456
fromIdx := new(Idx)
*fromIdx = 300
func TestNewL1UserTx(t *testing.T) {
toForge := int64(123456)
fromIdx := Idx(300)
l1Tx := &L1Tx{
ToForgeL1TxsNum: toForge,
ToForgeL1TxsNum: &toForge,
Position: 71,
UserOrigin: true,
ToIdx: 301,
TokenID: 5,
Amount: big.NewInt(1),
LoadAmount: big.NewInt(2),
FromIdx: fromIdx,
FromIdx: &fromIdx,
}
l1Tx, err := NewL1Tx(l1Tx)
assert.Nil(t, err)
assert.Equal(t, "0x01000000000001e240004700", l1Tx.TxID.String())
assert.Equal(t, "0x00000000000001e240004700", l1Tx.TxID.String())
}
func TestNewL1CoordinatorTx(t *testing.T) {
fromIdx := Idx(300)
batchNum := BatchNum(51966)
l1Tx := &L1Tx{
Position: 88,
UserOrigin: false,
ToIdx: 301,
TokenID: 5,
Amount: big.NewInt(1),
LoadAmount: big.NewInt(2),
FromIdx: &fromIdx,
BatchNum: &batchNum,
}
l1Tx, err := NewL1Tx(l1Tx)
assert.Nil(t, err)
assert.Equal(t, "0x01000000000000cafe005800", l1Tx.TxID.String())
}
func TestL1TxByteParsers(t *testing.T) {

View File

@@ -111,10 +111,10 @@ func (tx *L2Tx) PoolL2Tx() *PoolL2Tx {
// L2TxsToPoolL2Txs returns an array of []*PoolL2Tx from an array of []*L2Tx,
// where the PoolL2Tx only have the parameters of a L2Tx filled.
func L2TxsToPoolL2Txs(txs []*L2Tx) []*PoolL2Tx {
var r []*PoolL2Tx
func L2TxsToPoolL2Txs(txs []*L2Tx) []PoolL2Tx {
var r []PoolL2Tx
for _, tx := range txs {
r = append(r, tx.PoolL2Tx())
r = append(r, *tx.PoolL2Tx())
}
return r
}

View File

@@ -250,15 +250,15 @@ func (tx *PoolL2Tx) Tx() (*Tx, error) {
}, nil
}
// PoolL2TxsToL2Txs returns an array of []*L2Tx from an array of []*PoolL2Tx
func PoolL2TxsToL2Txs(txs []*PoolL2Tx) ([]*L2Tx, error) {
var r []*L2Tx
// PoolL2TxsToL2Txs returns an array of []L2Tx from an array of []PoolL2Tx
func PoolL2TxsToL2Txs(txs []PoolL2Tx) ([]L2Tx, error) {
var r []L2Tx
for _, poolTx := range txs {
tx, err := poolTx.L2Tx()
if err != nil {
return nil, err
}
r = append(r, tx)
r = append(r, *tx)
}
return r, nil
}

View File

@@ -31,8 +31,8 @@ type AuctionVars struct {
AllocationRatio AllocationRatio
}
// WithdrawalDelayerVars contains the Withdrawal Delayer smart contract variables
type WithdrawalDelayerVars struct {
// WithdrawDelayerVars contains the Withdrawal Delayer smart contract variables
type WithdrawDelayerVars struct {
HermezRollupAddress eth.Address
HermezGovernanceDAOAddress eth.Address
WhiteHackGroupAddress eth.Address

View File

@@ -12,6 +12,11 @@ import (
)
const (
// TXIDPrefixL1UserTx is the prefix that determines that the TxID is
// for a L1UserTx
//nolinter:gomnd
TxIDPrefixL1UserTx = byte(0)
// TXIDPrefixL1CoordTx is the prefix that determines that the TxID is
// for a L1CoordinatorTx
//nolinter:gomnd