Skip to content

Commit

Permalink
added global exit root support and state methods implementations (0xP…
Browse files Browse the repository at this point in the history
…olygonHermez#801)

* added global exit root support and state methods implementations

* returned GetLastBatch method

* renamed misc -> sync_info
  • Loading branch information
Mikelle authored Jun 27, 2022
1 parent 7b64ba6 commit 94d765e
Show file tree
Hide file tree
Showing 9 changed files with 472 additions and 344 deletions.
2 changes: 1 addition & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func Test_Defaults(t *testing.T) {
},
{
path: "Sequencerv2.WaitBlocksToUpdateGER",
expectedValue: uint32(10),
expectedValue: uint64(10),
},
{
path: "Sequencerv2.LastTimeBatchMaxWaitPeriod",
Expand Down
13 changes: 13 additions & 0 deletions db/migrations/0002.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ CREATE TABLE statev2.block ( --L1 block
CREATE TABLE statev2.batch ( --batch abstraction: will be created through trusted state
batch_num BIGINT PRIMARY KEY,
global_exit_root VARCHAR,
local_exit_root VARCHAR,
state_root VARCHAR,
timestamp TIMESTAMP,
raw_txs_data VARCHAR
);
Expand Down Expand Up @@ -61,3 +63,14 @@ CREATE TABLE statev2.exit_root
rollup_exit_root BYTEA,
global_exit_root BYTEA
);

CREATE TABLE statev2.sync_info
(
last_batch_num_seen BIGINT,
last_batch_num_consolidated BIGINT,
init_sync_batch BIGINT
);

-- Insert default values into sync_info table
INSERT INTO statev2.sync_info (last_batch_num_seen, last_batch_num_consolidated, init_sync_batch)VALUES (0, 0, 0);

2 changes: 1 addition & 1 deletion ethermanv2/types/sequence.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// processed.
type Sequence struct {
GlobalExitRoot common.Hash
Timestamp uint64
Timestamp int64
ForceBatchesNum uint64
Txs []types.Transaction
}
2 changes: 1 addition & 1 deletion sequencerv2/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Config struct {
LastL1InteractionTimeMaxWaitPeriod types.Duration `mapstructure:"LastL1InteractionTimeMaxWaitPeriod"`

// WaitBlocksToUpdateGER is number of blocks for sequencer to wait
WaitBlocksToUpdateGER uint32 `mapstructure:"WaitBlocksToUpdateGER"`
WaitBlocksToUpdateGER uint64 `mapstructure:"WaitBlocksToUpdateGER"`

// LastTimeBatchMaxWaitPeriod is time after which new batch should be closed
LastTimeBatchMaxWaitPeriod types.Duration `mapstructure:"LastTimeBatchMaxWaitPeriod"`
Expand Down
20 changes: 7 additions & 13 deletions sequencerv2/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/ethereum/go-ethereum/core/types"
ethmanTypes "github.com/hermeznetwork/hermez-core/ethermanv2/types"
"github.com/hermeznetwork/hermez-core/pool"
"github.com/hermeznetwork/hermez-core/state"
"github.com/hermeznetwork/hermez-core/state/runtime"
"github.com/hermeznetwork/hermez-core/statev2"
"github.com/jackc/pgx/v4"
)

// Consumer interfaces required by the package.
Expand All @@ -33,19 +33,13 @@ type etherman interface {

// stateInterface gathers the methods required to interact with the state.
type stateInterface interface {
GetLastBatchNumber(ctx context.Context, txBundleID string) (uint64, error)
GetLastBatchNumberSeenOnEthereum(ctx context.Context, txBundleID string) (uint64, error)
GetLastVirtualBatchNum(ctx context.Context) (uint64, error)
GetLastBatchNumberSeenOnEthereum(ctx context.Context) (uint64, error)
GetLatestGlobalExitRoot(ctx context.Context, dbTx pgx.Tx) (*statev2.GlobalExitRoot, error)

SetGenesis(ctx context.Context, genesis state.Genesis, txBundleID string) error
SetLastBatchNumberSeenOnEthereum(ctx context.Context, batchNumber uint64, txBundleID string) error
SetLastBatchNumberConsolidatedOnEthereum(ctx context.Context, batchNumber uint64, txBundleID string) error
SetInitSyncBatch(ctx context.Context, batchNumber uint64, txBundleID string) error

AddBlock(ctx context.Context, block *state.Block, txBundleID string) error

ProcessBatchAndStoreLastTx(ctx context.Context, txs []types.Transaction) *runtime.ExecutionResult
ProcessBatch(ctx context.Context, txs []types.Transaction) (*statev2.ProcessBatchResponse, error)
GetLastSendSequenceTime(ctx context.Context) (time.Time, error)
GetNumberOfBlocksSinceLastGERUpdate(ctx context.Context) (uint32, error)
GetNumberOfBlocksSinceLastGERUpdate(ctx context.Context) (uint64, error)
GetLastBatchTime(ctx context.Context) (time.Time, error)
}

Expand Down
32 changes: 24 additions & 8 deletions sequencerv2/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ func (s *Sequencer) tryToProcessTx(ctx context.Context, ticker *time.Ticker) {
// 2. Check if current sequence should be closed
if s.shouldCloseSequenceInProgress(ctx) {
s.closedSequences = append(s.closedSequences, s.sequenceInProgress)
s.sequenceInProgress = s.newSequence()
newSequence, err := s.newSequence(ctx)
if err != nil {
log.Errorf("failed to create new sequence, err: %v", err)
s.closedSequences = s.closedSequences[:len(s.closedSequences)-1]
return
}
s.sequenceInProgress = newSequence
}

// 3. Check if current sequence should be sent
Expand Down Expand Up @@ -108,10 +114,10 @@ func (s *Sequencer) tryToProcessTx(ctx context.Context, ticker *time.Ticker) {

// 5. Process tx
s.sequenceInProgress.Txs = append(s.sequenceInProgress.Txs, tx.Transaction)
res := s.state.ProcessBatchAndStoreLastTx(ctx, s.sequenceInProgress.Txs)
if res.Err != nil {
_, err := s.state.ProcessBatch(ctx, s.sequenceInProgress.Txs)
if err != nil {
s.sequenceInProgress.Txs = s.sequenceInProgress.Txs[:len(s.sequenceInProgress.Txs)-1]
log.Debugf("failed to process tx, hash: %s, err: %v", tx.Hash(), res.Err)
log.Debugf("failed to process tx, hash: %s, err: %v", tx.Hash(), err)
return
}

Expand All @@ -132,12 +138,12 @@ func waitTick(ctx context.Context, ticker *time.Ticker) {
}

func (s *Sequencer) isSynced(ctx context.Context) bool {
lastSyncedBatchNum, err := s.state.GetLastBatchNumber(ctx, "")
lastSyncedBatchNum, err := s.state.GetLastVirtualBatchNum(ctx)
if err != nil {
log.Errorf("failed to get last synced batch, err: %v", err)
return false
}
lastEthBatchNum, err := s.state.GetLastBatchNumberSeenOnEthereum(ctx, "")
lastEthBatchNum, err := s.state.GetLastBatchNumberSeenOnEthereum(ctx)
if err != nil {
log.Errorf("failed to get last eth batch, err: %v", err)
return false
Expand Down Expand Up @@ -228,8 +234,18 @@ func (s *Sequencer) getMostProfitablePendingTx(ctx context.Context) (*pool.Trans
return &tx[0], true
}

func (s *Sequencer) newSequence() types.Sequence {
return types.Sequence{}
func (s *Sequencer) newSequence(ctx context.Context) (types.Sequence, error) {
root, err := s.state.GetLatestGlobalExitRoot(ctx, nil)
if err != nil {
return types.Sequence{}, err
}

return types.Sequence{
GlobalExitRoot: root.GlobalExitRoot,
Timestamp: time.Now().Unix(),
ForceBatchesNum: 0,
Txs: nil,
}, nil
}

func isDataForEthTxTooBig(err error) bool {
Expand Down
Loading

0 comments on commit 94d765e

Please sign in to comment.