Skip to content

Commit

Permalink
latest proto (#3620)
Browse files Browse the repository at this point in the history
  • Loading branch information
ToniRamirezM committed Jun 4, 2024
1 parent 0f9086d commit bd5441a
Show file tree
Hide file tree
Showing 7 changed files with 449 additions and 140 deletions.
32 changes: 26 additions & 6 deletions proto/src/proto/datastream/v1/datastream.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ option go_package = "github.com/0xPolygonHermez/zkevm-node/state/datastream";

message BatchStart {
uint64 number = 1;
uint64 fork_id = 4;
uint64 chain_id = 5;
BatchType type = 2;
uint64 fork_id = 3;
uint64 chain_id = 4;
Debug debug = 5;
}

message BatchEnd {
uint64 number = 1;
bytes local_exit_root = 2;
bytes state_root = 3;
Debug debug = 4;
}

message L2Block {
Expand All @@ -28,14 +31,19 @@ message L2Block {
bytes state_root = 9;
bytes global_exit_root = 10;
bytes coinbase = 11;
uint64 block_gas_limit = 12;
bytes block_info_root = 13;
Debug debug = 14;
}

message Transaction {
uint64 l2block_number = 1;
bool is_valid = 2;
bytes encoded = 3;
uint32 effective_gas_price_percentage = 4;
bytes im_state_root = 5;
uint64 index = 2;
bool is_valid = 3;
bytes encoded = 4;
uint32 effective_gas_price_percentage = 5;
bytes im_state_root = 6;
Debug debug = 7;
}

message UpdateGER {
Expand All @@ -46,13 +54,18 @@ message UpdateGER {
uint64 fork_id = 5;
uint64 chain_id = 6;
bytes state_root = 7;
Debug debug = 8;
}

message BookMark {
BookmarkType type = 1;
uint64 value = 2;
}

message Debug {
string message = 1;
}

enum BookmarkType {
BOOKMARK_TYPE_UNSPECIFIED = 0;
BOOKMARK_TYPE_BATCH = 1;
Expand All @@ -67,3 +80,10 @@ enum EntryType {
ENTRY_TYPE_BATCH_END = 4;
ENTRY_TYPE_UPDATE_GER = 5;
}

enum BatchType {
BATCH_TYPE_UNSPECIFIED = 0;
BATCH_TYPE_REGULAR = 1;
BATCH_TYPE_FORCED = 2;
BATCH_TYPE_INJECTED = 3;
}
2 changes: 1 addition & 1 deletion sequencer/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (f *finalizer) insertSIPBatch(ctx context.Context, batchNumber uint64, stat
// Send batch bookmark to the datastream
f.DSSendBatchBookmark(batchNumber)
// Send batch start to the datastream
f.DSSendBatchStart(batchNumber)
f.DSSendBatchStart(batchNumber, false)

// Check if synchronizer is up-to-date
//TODO: review if this is needed
Expand Down
26 changes: 20 additions & 6 deletions sequencer/datastreamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ func (f *finalizer) DSSendL2Block(batchNumber uint64, blockResponse *state.Proce
ForkID: forkID,
BlockHash: blockResponse.BlockHash,
StateRoot: blockResponse.BlockHash, //From etrog, the blockhash is the block root
BlockInfoRoot: blockResponse.BlockInfoRoot,
}

l2Transactions := []state.DSL2Transaction{}

for _, txResponse := range blockResponse.TransactionResponses {
for i, txResponse := range blockResponse.TransactionResponses {
binaryTxData, err := txResponse.Tx.MarshalBinary()
if err != nil {
return err
Expand All @@ -37,12 +38,17 @@ func (f *finalizer) DSSendL2Block(batchNumber uint64, blockResponse *state.Proce
l2Transaction := state.DSL2Transaction{
L2BlockNumber: blockResponse.BlockNumber,
EffectiveGasPricePercentage: uint8(txResponse.EffectivePercentage),
Index: uint64(i),
IsValid: 1,
EncodedLength: uint32(len(binaryTxData)),
Encoded: binaryTxData,
StateRoot: txResponse.StateRoot,
}

if txResponse.Logs != nil && len(txResponse.Logs) > 0 {
l2Transaction.Index = uint64(txResponse.Logs[0].TxIndex)
}

l2Transactions = append(l2Transactions, l2Transaction)
}

Expand All @@ -67,15 +73,23 @@ func (f *finalizer) DSSendBatchBookmark(batchNumber uint64) {
}
}

func (f *finalizer) DSSendBatchStart(batchNumber uint64) {
func (f *finalizer) DSSendBatchStart(batchNumber uint64, isForced bool) {
forkID := f.stateIntf.GetForkIDByBatchNumber(batchNumber)

batchStart := datastream.BatchStart{
Number: batchNumber,
ForkId: forkID,
}

if isForced {
batchStart.Type = datastream.BatchType_BATCH_TYPE_FORCED
} else {
batchStart.Type = datastream.BatchType_BATCH_TYPE_REGULAR
}

if f.streamServer != nil {
// Send batch start to the streamer
f.dataToStream <- datastream.BatchStart{
Number: batchNumber,
ForkId: forkID,
}
f.dataToStream <- batchStart
}
}

Expand Down
28 changes: 22 additions & 6 deletions state/datastream.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,19 @@ type DSL2Block struct {
ChainID uint64
BlockHash common.Hash
StateRoot common.Hash
BlockGasLimit uint64
BlockInfoRoot common.Hash
}

// DSL2Transaction represents a data stream L2 transaction
type DSL2Transaction struct {
L2BlockNumber uint64 // Not included in the encoded data
ImStateRoot common.Hash // Not included in the encoded data
EffectiveGasPricePercentage uint8 // 1 byte
IsValid uint8 // 1 byte
StateRoot common.Hash // 32 bytes
EncodedLength uint32 // 4 bytes
L2BlockNumber uint64
ImStateRoot common.Hash
EffectiveGasPricePercentage uint8
IsValid uint8
Index uint64
StateRoot common.Hash
EncodedLength uint32
Encoded []byte
}

Expand Down Expand Up @@ -121,6 +124,7 @@ func GenerateDataStreamFile(ctx context.Context, streamServer *datastreamer.Stre

genesisBatchStart := &datastream.BatchStart{
Number: genesisL2Block.BatchNumber,
Type: datastream.BatchType_BATCH_TYPE_UNSPECIFIED,
ForkId: genesisL2Block.ForkID,
ChainId: chainID,
}
Expand Down Expand Up @@ -390,10 +394,19 @@ func GenerateDataStreamFile(ctx context.Context, streamServer *datastreamer.Stre

batchStart := &datastream.BatchStart{
Number: batch.BatchNumber,
Type: datastream.BatchType_BATCH_TYPE_REGULAR,
ForkId: batch.ForkID,
ChainId: chainID,
}

if batch.ForkID >= FORKID_ETROG && (batch.BatchNumber == 1 || (upgradeEtrogBatchNumber != 0 && batch.BatchNumber == upgradeEtrogBatchNumber)) {
batchStart.Type = datastream.BatchType_BATCH_TYPE_INJECTED
}

if batch.ForcedBatchNum != nil {
batchStart.Type = datastream.BatchType_BATCH_TYPE_FORCED
}

marshalledBatchStart, err := proto.Marshal(batchStart)
if err != nil {
return err
Expand Down Expand Up @@ -495,6 +508,8 @@ func GenerateDataStreamFile(ctx context.Context, streamServer *datastreamer.Stre
StateRoot: l2Block.StateRoot.Bytes(),
GlobalExitRoot: l2Block.GlobalExitRoot.Bytes(),
Coinbase: l2Block.Coinbase.Bytes(),
BlockInfoRoot: l2Block.BlockInfoRoot.Bytes(),
BlockGasLimit: l2Block.BlockGasLimit,
}

if l2Block.ForkID >= FORKID_ETROG {
Expand Down Expand Up @@ -560,6 +575,7 @@ func GenerateDataStreamFile(ctx context.Context, streamServer *datastreamer.Stre

transaction := &datastream.Transaction{
L2BlockNumber: tx.L2BlockNumber,
Index: tx.Index,
IsValid: tx.IsValid != 0,
Encoded: tx.Encoded,
EffectiveGasPricePercentage: uint32(tx.EffectiveGasPricePercentage),
Expand Down
Loading

0 comments on commit bd5441a

Please sign in to comment.