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

Tudor/aggressive header compression #1502

Merged
merged 17 commits into from
Sep 12, 2023
18 changes: 18 additions & 0 deletions go/common/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ type RollupHeader struct {
LastBatchSeqNo uint64
}

// CalldataRollupHeader contains all information necessary to reconstruct the batches included in the rollup.
// This data structure is serialised, compressed, and encrypted, before being serialised again in the rollup.
type CalldataRollupHeader struct {
FirstBatchSequence *big.Int
FirstBatchHeight *big.Int
FirstParentHash L2BatchHash

StartTime uint64
BatchTimeDeltas []*big.Int // todo - minimize assuming a default of 1 sec and then store only exceptions

ReOrgs []*BatchHeader // sparse list of reorged headers - non null only for reorgs.
L1HeightDeltas []*big.Int // Contains the L1 height on the indexes where it changes. Todo - can be optimised with deltas
Copy link
Contributor

@StefanIliev545 StefanIliev545 Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field name and the description are confusing.


// todo - these fields are for debugging the compression. Should be removed.
BatchHashes []L2BatchHash
// BatchHeaders []*BatchHeader
}

// MarshalJSON custom marshals the RollupHeader into a json
func (r *RollupHeader) MarshalJSON() ([]byte, error) {
type Alias RollupHeader
Expand Down
8 changes: 4 additions & 4 deletions go/common/rollups.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (

// ExtRollup is an encrypted form of rollup used when passing the rollup around outside an enclave.
type ExtRollup struct {
Header *RollupHeader // the fields required by the management contract
BatchHeaders []byte // compressed batch headers
BatchPayloads []byte // The batches included in the rollup, in external/encrypted form.
hash atomic.Value
Header *RollupHeader // the fields required by the management contract
CalldataRollupHeader []byte // encrypted header useful for recreating the batches
BatchPayloads []byte // The transactions included in the rollup, in external/encrypted form.
hash atomic.Value
}

// Hash returns the keccak256 hash of the rollup's header.
Expand Down
8 changes: 4 additions & 4 deletions go/common/rpc/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func ToExtRollupMsg(rollup *common.ExtRollup) generated.ExtRollupMsg {
return generated.ExtRollupMsg{}
}

return generated.ExtRollupMsg{Header: ToRollupHeaderMsg(rollup.Header), BatchPayloads: rollup.BatchPayloads, BatchHeaders: rollup.BatchHeaders}
return generated.ExtRollupMsg{Header: ToRollupHeaderMsg(rollup.Header), BatchPayloads: rollup.BatchPayloads, BatchHeaders: rollup.CalldataRollupHeader}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should BatchHeaders name be changed ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's ok. It is the compressed batch headers

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think the name is fine! But it seems like the common.ExtRollup.BatchHeaders was changed to common.ExtRollup.CalldataRollupHeader so maybe the generated.ExtRollupMsg.BatchHeaders to generated.ExtRollupMsg.CalldataRollupHeader change was missed ?

}

func ToRollupHeaderMsg(header *common.RollupHeader) *generated.RollupHeaderMsg {
Expand Down Expand Up @@ -236,9 +236,9 @@ func FromExtRollupMsg(msg *generated.ExtRollupMsg) *common.ExtRollup {
}

return &common.ExtRollup{
Header: FromRollupHeaderMsg(msg.Header),
BatchPayloads: msg.BatchPayloads,
BatchHeaders: msg.BatchHeaders,
Header: FromRollupHeaderMsg(msg.Header),
BatchPayloads: msg.BatchPayloads,
CalldataRollupHeader: msg.BatchHeaders,
}
}

Expand Down
16 changes: 14 additions & 2 deletions go/enclave/components/batch_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,22 @@ func NewBatchExecutor(storage storage.Storage, cc *crosschain.Processors, genesi
}
}

func (executor *batchExecutor) ComputeBatchLight(BlockPtr common.L1BlockHash, ParentPtr common.L2BatchHash, Transactions common.L2Transactions, AtTime uint64, SequencerNo *big.Int) (*ComputedBatch, error) {
tudor-malene marked this conversation as resolved.
Show resolved Hide resolved
return executor.ComputeBatch(&BatchExecutionContext{
BlockPtr: BlockPtr,
ParentPtr: ParentPtr,
Transactions: Transactions,
AtTime: AtTime,
// Creator: executor,
ChainConfig: executor.chainConfig,
SequencerNo: SequencerNo,
})
}

func (executor *batchExecutor) ComputeBatch(context *BatchExecutionContext) (*ComputedBatch, error) {
defer executor.logger.Info("Batch context processed", log.DurationKey, measure.NewStopwatch())

// Block is loaded first since if its missing this batch might be based on l1 fork we dont know about
// and we want to filter out all fork batches based on not knowing the l1 block
// sanity check that the l1 block exists. We don't have to execute batches of forks.
block, err := executor.storage.FetchBlock(context.BlockPtr)
if errors.Is(err, errutil.ErrNotFound) {
return nil, errutil.ErrBlockForBatchNotFound
Expand All @@ -64,6 +75,7 @@ func (executor *batchExecutor) ComputeBatch(context *BatchExecutionContext) (*Co
// These variables will be used to create the new batch
parent, err := executor.storage.FetchBatch(context.ParentPtr)
if errors.Is(err, errutil.ErrNotFound) {
executor.logger.Error(fmt.Sprintf("can't find parent batch %s. Seq %d", context.ParentPtr, context.SequencerNo))
return nil, errutil.ErrAncestorBatchNotFound
}
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions go/enclave/components/batch_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ func (br *batchRegistry) BatchesAfter(batchSeqNo uint64, rollupLimiter limiters.
br.logger.Info("Added batch to rollup", log.BatchHashKey, batch.Hash(), log.BatchSeqNoKey, batch.SeqNo())
}

current := batches[0].SeqNo().Uint64()
for i, b := range batches {
tudor-malene marked this conversation as resolved.
Show resolved Hide resolved
if current+uint64(i) != b.SeqNo().Uint64() {
br.logger.Crit("invalid rollup")
}
}

return batches, nil
}

Expand Down
2 changes: 2 additions & 0 deletions go/enclave/components/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ type BatchExecutor interface {
// Should be safe to call in parallel
ComputeBatch(*BatchExecutionContext) (*ComputedBatch, error)

ComputeBatchLight(BlockPtr common.L1BlockHash, ParentPtr common.L2BatchHash, Transactions common.L2Transactions, AtTime uint64, SequencerNo *big.Int) (*ComputedBatch, error)

// ExecuteBatch - executes the transactions and xchain messages, returns the receipts, and updates the stateDB
ExecuteBatch(*core.Batch) (types.Receipts, error)

Expand Down
Loading
Loading