Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and Test the syncing of blocks on sequencer #51

Open
wants to merge 10 commits into
base: develop
Choose a base branch
from
2 changes: 2 additions & 0 deletions contracts/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ POSEIDON4ELEMENTS=
MAXTX=100
# Number of levels
NLEVEL=5
# Sybil Contract Address
SYBILCONTRACTADDRESS=
Shailu-s marked this conversation as resolved.
Show resolved Hide resolved
50 changes: 50 additions & 0 deletions contracts/script/TestSyncEvents.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import "forge-std/Script.sol";
import {Sybil} from "../src/mvp/Sybil.sol";

contract TestSyncEvents is Script {
struct TxParams {
uint48 fromIdx;
uint40 loadAmountF;
uint40 amountF;
uint48 toIdx;
}

function run() external {
// Existing deployed Sybil contract address
address sybilContractAddress = vm.envAddress("SYBILCONTRACTADDRESS");
uint256 maxTx = vm.envUint("MAXTX");
uint256 nLevel = vm.envUint("NLEVEL");

vm.startBroadcast();
// Using the Deployed Sybil contract
Sybil sybilContract = Sybil(sybilContractAddress);

console2.log("Sybil contract is deployed at:", address(sybilContract));

TxParams memory params1 = validCreateAccountDeposit();
uint256 loadAmount1 = _float2Fix(params1.loadAmountF);

sybilContract.createAccountDeposit{value: loadAmount1}(
params1.loadAmountF
);

vm.stopBroadcast();
}

function validCreateAccountDeposit() public pure returns (TxParams memory) {
return TxParams({fromIdx: 0, loadAmountF: 1, amountF: 0, toIdx: 0});
}

function _float2Fix(uint40 floatVal) internal pure returns (uint256) {
uint256 m = floatVal & 0x7FFFFFFFF;
uint256 e = floatVal >> 35;

uint256 exp = 10 ** e;
uint256 fix = m * exp;

return fix;
}
}
2 changes: 1 addition & 1 deletion contracts/test/Sybil.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
pragma solidity 0.8 .23;

import "forge-std/Test.sol";
import "../src/Sybil.sol";
import "../src/sybil.sol";
import "./utils/Constants.sol";
import "./types/TransactionTypes.sol";
import "../src/VerifierRollupStub.sol";
Expand Down
4 changes: 2 additions & 2 deletions sequencer/cfg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Keep = 256

[Web3]
### Url of the web3 ethereum-node RPC server. Only geth is officially supported
URL = "https://rpc.thanos-sepolia.tokamak.network"
URL = "https://1rpc.io/sepolia"

[Synchronizer]
### Interval between attempts to synchronize a new block from an ethereum node
Expand All @@ -18,7 +18,7 @@ StatsUpdateFrequencyDivider = 100

[SmartContracts]
## Smart contract address of the rollup contract
Rollup = "0xd2Fcf423780E5D4e6F8fD4F3c7aa931454DFB9c4"
Rollup = "0xA27212B7608e5a73e6a807DEC84B3190AaB12f7C"

[Coordinator]
## Ethereum address that the coordinator is using to forge batches
Expand Down
8 changes: 4 additions & 4 deletions sequencer/common/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (
maxBalanceBytes = 24

// AccountIdxBytesLen idx bytes
AccountIdxBytesLen = 3
AccountIdxBytesLen = 6

// maxAccountIdxValue is the maximum value that AccountIdx can have (24 bits:
// maxAccountIdxValue=2**24-1)
Expand Down Expand Up @@ -65,7 +65,7 @@ func (idx AccountIdx) Bytes() ([NLevelsAsBytes]byte, error) {
var idxBytes [4]byte
binary.BigEndian.PutUint32(idxBytes[:], uint32(idx))
var b [NLevelsAsBytes]byte
copy(b[:], idxBytes[4-NLevelsAsBytes:])
copy(b[:], idxBytes[7-NLevelsAsBytes:])
return b, nil
}

Expand All @@ -75,8 +75,8 @@ func AccountIdxFromBytes(b []byte) (AccountIdx, error) {
return 0, Wrap(fmt.Errorf("can not parse Idx, bytes len %d, expected %d",
len(b), AccountIdxBytesLen))
}
var idxBytes [4]byte
copy(idxBytes[4-NLevelsAsBytes:], b[:])
var idxBytes [7]byte
copy(idxBytes[7-NLevelsAsBytes:], b[:])
idx := binary.BigEndian.Uint32(idxBytes[:])
return AccountIdx(idx), nil
}
Expand Down
2 changes: 1 addition & 1 deletion sequencer/common/ethrollup.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
// RollupConstL1UserTotalBytes [20 bytes] fromEthAddr + [32 bytes] fromBjj-compressed + [6
// bytes] fromIdx + [5 bytes] depositAmountFloat40 + [5 bytes] amountFloat40 + [4 bytes]
// tokenId + [6 bytes] toIdx
RollupConstL1UserTotalBytes = 78
RollupConstL1UserTotalBytes = 42
// RollupConstMaxL1UserTx Maximum L1-user transactions allowed to be queued in a batch
RollupConstMaxL1UserTx = 128
// RollupConstMaxL1Tx Maximum L1 transactions allowed to be queued in a batch
Expand Down
105 changes: 41 additions & 64 deletions sequencer/common/l1tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,37 +198,63 @@ func (tx L1Tx) TxCompressedData(chainID uint64) (*big.Int, error) {
return bi, nil
}

// L1UserTxFromBytes decodes a L1Tx from []byte
// L1UserTxFromEvent decodes L1UserTxEvent data
type L1UserTxEvent struct {
QueueIndex uint32
Position uint8
L1UserTx *L1Tx
}

// L1UserTxFromEventData decodes the event data from L1UserTxEvent
func L1UserTxFromEventData(queueIndex uint32, position uint8, txData []byte) (*L1UserTxEvent, error) {
// Parse the l1UserTx bytes
tx, err := L1UserTxFromBytes(txData)
if err != nil {
return nil, Wrap(err)
}

return &L1UserTxEvent{
QueueIndex: queueIndex,
Position: position,
L1UserTx: tx,
}, nil
}

// L1UserTxFromBytes decodes the l1UserTx bytes from the event
func L1UserTxFromBytes(b []byte) (*L1Tx, error) {
// The bytes should contain: ethAddress(20) + fromIdx(6) + loadAmountF(5) + amountF(5) + toIdx(6)
if len(b) != RollupConstL1UserTotalBytes {
return nil,
Wrap(fmt.Errorf("cannot parse L1Tx bytes, expected length %d, current: %d",
68, len(b)))
return nil, Wrap(fmt.Errorf("invalid L1UserTx length: got %d, want %d", len(b), RollupConstL1UserTotalBytes))
}

tx := &L1Tx{
UserOrigin: true,
}
var err error

// Parse ethAddress (20 bytes)
tx.FromEthAddr = ethCommon.BytesToAddress(b[0:20])

pkCompB := b[20:52]
pkCompL := SwapEndianness(pkCompB)
copy(tx.FromBJJ[:], pkCompL)
fromIdx, err := AccountIdxFromBytes(b[52:55])
// Parse fromIdx (6 bytes)
var err error
tx.FromIdx, err = AccountIdxFromBytes(b[20:26])
if err != nil {
return nil, Wrap(err)
}
tx.FromIdx = fromIdx
tx.DepositAmount, err = Float40FromBytes(b[58:63]).BigInt()

// Parse loadAmountF (5 bytes)
tx.DepositAmount, err = Float40FromBytes(b[26:31]).BigInt()
if err != nil {
return nil, Wrap(err)
}
tx.Amount, err = Float40FromBytes(b[63:68]).BigInt()

// Parse amountF (5 bytes)
tx.Amount, err = Float40FromBytes(b[31:36]).BigInt()
if err != nil {
return nil, Wrap(err)
}
tx.ToIdx, err = AccountIdxFromBytes(b[72:75])

// Parse toIdx (6 bytes)
tx.ToIdx, err = AccountIdxFromBytes(b[36:42])
if err != nil {
return nil, Wrap(err)
}
Expand All @@ -245,12 +271,12 @@ func L1TxFromDataAvailability(b []byte, nLevels uint32) (*L1Tx, error) {
amountBytes := b[idxLen*2 : idxLen*2+Float40BytesLength]

l1tx := L1Tx{}
fromIdx, err := AccountIdxFromBytes(ethCommon.LeftPadBytes(fromIdxBytes, 3))
fromIdx, err := AccountIdxFromBytes(ethCommon.LeftPadBytes(fromIdxBytes, 6))
if err != nil {
return nil, Wrap(err)
}
l1tx.FromIdx = fromIdx
toIdx, err := AccountIdxFromBytes(ethCommon.LeftPadBytes(toIdxBytes, 3))
toIdx, err := AccountIdxFromBytes(ethCommon.LeftPadBytes(toIdxBytes, 6))
if err != nil {
return nil, Wrap(err)
}
Expand Down Expand Up @@ -313,55 +339,6 @@ func L1CoordinatorTxFromBytes(b []byte, chainID *big.Int, tokamakAddress ethComm
return tx, nil
}

// BytesGeneric returns the generic representation of a L1Tx. This method is
// used to compute the []byte representation of a L1UserTx, and also to compute
// the L1TxData for the ZKInputs (at the HashGlobalInputs), using this method
// for L1CoordinatorTxs & L1UserTxs (for the ZKInputs case).
func (tx *L1Tx) BytesGeneric() ([]byte, error) {
var b [RollupConstL1UserTotalBytes]byte
copy(b[0:20], tx.FromEthAddr.Bytes())
if tx.FromBJJ != EmptyBJJComp {
pkCompL := tx.FromBJJ
pkCompB := SwapEndianness(pkCompL[:])
copy(b[20:52], pkCompB[:])
}
fromIdxBytes, err := tx.FromIdx.Bytes()
if err != nil {
return nil, Wrap(err)
}
copy(b[52:55], fromIdxBytes[:])

depositAmountFloat40, err := NewFloat40(tx.DepositAmount)
if err != nil {
return nil, Wrap(err)
}
depositAmountFloat40Bytes, err := depositAmountFloat40.Bytes()
if err != nil {
return nil, Wrap(err)
}
copy(b[58:63], depositAmountFloat40Bytes)

amountFloat40, err := NewFloat40(tx.Amount)
if err != nil {
return nil, Wrap(err)
}
amountFloat40Bytes, err := amountFloat40.Bytes()
if err != nil {
return nil, Wrap(err)
}
copy(b[63:68], amountFloat40Bytes)

//TODO: We can update this for better memory management.
// copy(b[68:72], tx.TokenID.Bytes())

toIdxBytes, err := tx.ToIdx.Bytes()
if err != nil {
return nil, Wrap(err)
}
copy(b[72:75], toIdxBytes[:])
return b[:], nil
}

// // BytesDataAvailability encodes a L1Tx into []byte for the Data Availability
// // [ fromIdx | toIdx | amountFloat40 | Fee ]
func (tx *L1Tx) BytesDataAvailability(nLevels uint32) ([]byte, error) {
Expand Down
4 changes: 2 additions & 2 deletions sequencer/common/l2tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ func L2TxFromBytesDataAvailability(b []byte, nLevels int) (*L2Tx, error) {
tx := &L2Tx{}
var err error

var paddedFromIdxBytes [3]byte
copy(paddedFromIdxBytes[3-idxLen:], b[0:idxLen])
var paddedFromIdxBytes [6]byte
copy(paddedFromIdxBytes[6-idxLen:], b[0:idxLen])
tx.FromIdx, err = AccountIdxFromBytes(paddedFromIdxBytes[:])
if err != nil {
return nil, Wrap(err)
Expand Down
2 changes: 1 addition & 1 deletion sequencer/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

const (
// BytesLength to represent the depth of merkle tree
NLevelsAsBytes = 3
NLevelsAsBytes = 6
)

// EthAddrToBigInt returns a *big.Int from a given ethereum common.Address.
Expand Down
8 changes: 4 additions & 4 deletions sequencer/common/vouch.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ func (idx VouchIdx) Bytes() ([2 * NLevelsAsBytes]byte, error) {
if idx > maxVouchIdxValue {
return [2 * NLevelsAsBytes]byte{}, Wrap(ErrIdxOverflow)
}
var idxBytes [8]byte
var idxBytes [14]byte
binary.BigEndian.PutUint64(idxBytes[:], uint64(idx))
var b [2 * NLevelsAsBytes]byte
copy(b[:], idxBytes[8-2*NLevelsAsBytes:])
copy(b[:], idxBytes[14-2*NLevelsAsBytes:])
return b, nil
}

Expand All @@ -51,8 +51,8 @@ func VouchIdxFromBytes(b []byte) (VouchIdx, error) {
return 0, Wrap(fmt.Errorf("can not parse Idx, bytes len %d, expected %d",
len(b), VouchIdxBytesLen))
}
var idxBytes [8]byte
copy(idxBytes[8-2*NLevelsAsBytes:], b[:])
var idxBytes [14]byte
copy(idxBytes[14-2*NLevelsAsBytes:], b[:])
idx := binary.BigEndian.Uint64(idxBytes[:])
return VouchIdx(idx), nil
}
Expand Down
1 change: 0 additions & 1 deletion sequencer/database/historydb/historydb.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,6 @@ func (hdb *HistoryDB) GetAllL1CoordinatorTxs() ([]common.L1Tx, error) {
return database.SlicePtrsToSlice(txs).([]common.L1Tx), common.Wrap(err)
}


// GetUnforgedL1UserTxs gets L1 User Txs to be forged in the L1Batch with toForgeL1TxsNum.
func (hdb *HistoryDB) GetUnforgedL1UserTxs(toForgeL1TxsNum int64) ([]common.L1Tx, error) {
var txs []*common.L1Tx
Expand Down
10 changes: 6 additions & 4 deletions sequencer/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import (
const SyncTime = 24 * 60 * time.Minute

// block number of the Smart contract to sync from
const RollupStartBlockNum = 379608
const RollupStartBlockNum = 7553896
Shailu-s marked this conversation as resolved.
Show resolved Hide resolved

// Node is the Hermez Node
type Node struct {
Expand Down Expand Up @@ -532,15 +532,17 @@ func (n *Node) StartSynchronizer() {
go func() {
var err error
var lastBlock *common.Block
waitDuration := time.Duration(0)
waitDuration := time.Duration(10 * time.Second)
Shailu-s marked this conversation as resolved.
Show resolved Hide resolved
ticker := time.NewTicker(waitDuration)
defer ticker.Stop()
for {
select {
case <-n.ctx.Done():
log.Info("Synchronizer done")
n.wg.Done()
return
case <-time.After(waitDuration):
if lastBlock, waitDuration, err = n.syncLoopFn(n.ctx,
case <-ticker.C:
if _, _, err = n.syncLoopFn(n.ctx,
lastBlock); err != nil {
if n.ctx.Err() != nil {
continue
Expand Down
4 changes: 2 additions & 2 deletions sequencer/synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ func (s *Synchronizer) Sync(ctx context.Context,
if lastSavedBlock == nil {
// Get lastSavedBlock from History DB
lastSavedBlock, err = s.historyDB.GetLastBlock()
// TODO: Change blocknum here
// lastSavedBlock.Num = 7553897
Shailu-s marked this conversation as resolved.
Show resolved Hide resolved
if err != nil && common.Unwrap(err) != sql.ErrNoRows {
log.Errorw("Sync GetLastBlock", "err", err)
return nil, nil, common.Wrap(err)
Expand Down Expand Up @@ -347,12 +349,10 @@ func (s *Synchronizer) Sync(ctx context.Context,
Block: *ethBlock,
Rollup: *rollupData,
}

err = s.historyDB.AddBlockSCData(blockData)
if err != nil {
return nil, nil, common.Wrap(err)
}

batchesLen := len(rollupData.Batches)
if batchesLen == 0 {
s.stats.UpdateSync(ethBlock, nil, nil, nil)
Expand Down