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

add entries decode #2699

Merged
merged 3 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ func start(cliCtx *cli.Context) error {
if c.Metrics.ProfilingEnabled {
go startProfilingHttpServer(c.Metrics)
}
if c.Metrics.Enabled {
go startMetricsHttpServer(c.Metrics)
}
for _, component := range components {
switch component {
case AGGREGATOR:
Expand Down Expand Up @@ -243,6 +240,10 @@ func start(cliCtx *cli.Context) error {
}
}

if c.Metrics.Enabled {
go startMetricsHttpServer(c.Metrics)
}

waitSignal(cancelFuncs)

return nil
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/0xPolygonHermez/zkevm-node
go 1.19

require (
github.com/0xPolygonHermez/zkevm-data-streamer v0.1.0
github.com/0xPolygonHermez/zkevm-data-streamer v0.1.1
github.com/didip/tollbooth/v6 v6.1.2
github.com/dop251/goja v0.0.0-20230806174421-c933cf95e127
github.com/ethereum/go-ethereum v1.13.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/0xPolygonHermez/zkevm-data-streamer v0.1.0 h1:fYsPST5nwgE+IAQMa53kk1jgGJcKwUAmtOd4gABm92I=
github.com/0xPolygonHermez/zkevm-data-streamer v0.1.0/go.mod h1:853hkExOKPpMqbyEGqF8cuobSlbVd2OLVEZoRFTGRzo=
github.com/0xPolygonHermez/zkevm-data-streamer v0.1.1 h1:MuaJWH06OnGPU+/tnAdfqOu088+N+XWfxZkQv4+S/Ss=
github.com/0xPolygonHermez/zkevm-data-streamer v0.1.1/go.mod h1:853hkExOKPpMqbyEGqF8cuobSlbVd2OLVEZoRFTGRzo=
github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 h1:w+iIsaOQNcT7OZ575w+acHgRric5iCyQh+xv+KJ4HB8=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
Expand Down
27 changes: 0 additions & 27 deletions sequencer/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"reflect"
"time"

"github.com/0xPolygonHermez/zkevm-data-streamer/datastreamer"
Expand Down Expand Up @@ -119,32 +118,6 @@ func (s *Sequencer) Start(ctx context.Context) {
log.Fatalf("failed to create stream server, err: %v", err)
}

// Set entities definition
entriesDefinition := map[datastreamer.EntryType]datastreamer.EntityDefinition{
state.EntryTypeL2BlockStart: {
Name: "L2BlockStart",
StreamType: state.StreamTypeSequencer,
Definition: reflect.TypeOf(state.DSL2BlockStart{}),
},
state.EntryTypeL2Tx: {
Name: "L2Transaction",
StreamType: state.StreamTypeSequencer,
Definition: reflect.TypeOf(state.DSL2Transaction{}),
},
state.EntryTypeL2BlockEnd: {
Name: "L2BlockEnd",
StreamType: state.StreamTypeSequencer,
Definition: reflect.TypeOf(state.DSL2BlockEnd{}),
},
state.EntryTypeUpdateGER: {
Name: "UpdateGER",
StreamType: state.StreamTypeSequencer,
Definition: reflect.TypeOf(state.DSUpdateGER{}),
},
}

streamServer.SetEntriesDef(entriesDefinition)

dbManager.streamServer = &streamServer
err = dbManager.streamServer.Start()
if err != nil {
Expand Down
46 changes: 46 additions & 0 deletions state/datastream.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ func (b DSL2BlockStart) Encode() []byte {
return bytes
}

// Decode decodes the DSL2BlockStart from a byte slice
func (b DSL2BlockStart) Decode(data []byte) DSL2BlockStart {
b.BatchNumber = binary.LittleEndian.Uint64(data[0:8])
b.L2BlockNumber = binary.LittleEndian.Uint64(data[8:16])
b.Timestamp = int64(binary.LittleEndian.Uint64(data[16:24]))
b.GlobalExitRoot = common.BytesToHash(data[24:56])
b.Coinbase = common.BytesToAddress(data[56:76])
b.ForkID = binary.LittleEndian.Uint16(data[76:78])
return b
}

// DSL2Transaction represents a data stream L2 transaction
type DSL2Transaction struct {
EffectiveGasPricePercentage uint8 // 1 byte
Expand All @@ -91,6 +102,15 @@ func (l DSL2Transaction) Encode() []byte {
return bytes
}

// Decode decodes the DSL2Transaction from a byte slice
func (l DSL2Transaction) Decode(data []byte) DSL2Transaction {
l.EffectiveGasPricePercentage = uint8(data[0])
l.IsValid = uint8(data[1])
l.EncodedLength = binary.LittleEndian.Uint32(data[2:6])
l.Encoded = data[6:]
return l
}

// DSL2BlockEnd represents a L2 block end
type DSL2BlockEnd struct {
L2BlockNumber uint64 // 8 bytes
Expand All @@ -107,6 +127,14 @@ func (b DSL2BlockEnd) Encode() []byte {
return bytes
}

// Decode decodes the DSL2BlockEnd from a byte slice
func (b DSL2BlockEnd) Decode(data []byte) DSL2BlockEnd {
b.L2BlockNumber = binary.LittleEndian.Uint64(data[0:8])
b.BlockHash = common.BytesToHash(data[8:40])
b.StateRoot = common.BytesToHash(data[40:72])
return b
}

// DSBookMark represents a data stream bookmark
type DSBookMark struct {
Type byte
Expand All @@ -121,6 +149,13 @@ func (b DSBookMark) Encode() []byte {
return bytes
}

// Decode decodes the DSBookMark from a byte slice
func (b DSBookMark) Decode(data []byte) DSBookMark {
b.Type = byte(data[0])
b.L2BlockNumber = binary.LittleEndian.Uint64(data[1:9])
return b
}

// DSUpdateGER represents a data stream GER update
type DSUpdateGER struct {
BatchNumber uint64 // 8 bytes
Expand All @@ -143,6 +178,17 @@ func (g DSUpdateGER) Encode() []byte {
return bytes
}

// Decode decodes the DSUpdateGER from a byte slice
func (g DSUpdateGER) Decode(data []byte) DSUpdateGER {
g.BatchNumber = binary.LittleEndian.Uint64(data[0:8])
g.Timestamp = int64(binary.LittleEndian.Uint64(data[8:16]))
g.GlobalExitRoot = common.BytesToHash(data[16:48])
g.Coinbase = common.BytesToAddress(data[48:68])
g.ForkID = binary.LittleEndian.Uint16(data[68:70])
g.StateRoot = common.BytesToHash(data[70:102])
return g
}

// DSState gathers the methods required to interact with the data stream state.
type DSState interface {
GetDSGenesisBlock(ctx context.Context, dbTx pgx.Tx) (*DSL2Block, error)
Expand Down
97 changes: 29 additions & 68 deletions tools/datastreamer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/binary"
"fmt"
"os"
"reflect"
"time"

"github.com/0xPolygonHermez/zkevm-data-streamer/datastreamer"
Expand Down Expand Up @@ -112,32 +111,6 @@ func initializeStreamServer(c *config.Config) (*datastreamer.StreamServer, error
return nil, err
}

// Set entities definition
entriesDefinition := map[datastreamer.EntryType]datastreamer.EntityDefinition{
state.EntryTypeL2BlockStart: {
Name: "L2BlockStart",
StreamType: state.StreamTypeSequencer,
Definition: reflect.TypeOf(state.DSL2BlockStart{}),
},
state.EntryTypeL2Tx: {
Name: "L2Transaction",
StreamType: state.StreamTypeSequencer,
Definition: reflect.TypeOf(state.DSL2Transaction{}),
},
state.EntryTypeL2BlockEnd: {
Name: "L2BlockEnd",
StreamType: state.StreamTypeSequencer,
Definition: reflect.TypeOf(state.DSL2BlockEnd{}),
},
state.EntryTypeUpdateGER: {
Name: "UpdateGER",
StreamType: state.StreamTypeSequencer,
Definition: reflect.TypeOf(state.DSUpdateGER{}),
},
}

streamServer.SetEntriesDef(entriesDefinition)

err = streamServer.Start()
if err != nil {
return nil, err
Expand Down Expand Up @@ -454,55 +427,47 @@ func decodeL2Block(cliCtx *cli.Context) error {
func printEntry(entry datastreamer.FileEntry, streamServer *datastreamer.StreamServer) {
switch entry.Type {
case state.EntryTypeBookMark:
bookmark := state.DSBookMark{}.Decode(entry.Data)
printColored(color.FgGreen, "Entry Type......: ")
printColored(color.FgHiYellow, "BookMark\n")
printColored(color.FgGreen, "Entry Number....: ")
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", entry.Number))
printColored(color.FgGreen, "L2 Block Number.: ")
l2BlockNumber := binary.LittleEndian.Uint64(entry.Data[1:9])
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", l2BlockNumber))
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", bookmark.L2BlockNumber))
case state.EntryTypeL2BlockStart:
blockStart := state.DSL2BlockStart{}.Decode(entry.Data)
printColored(color.FgGreen, "Entry Type......: ")
printColored(color.FgHiYellow, "L2 Block Start\n")
printColored(color.FgGreen, "Entry Number....: ")
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", entry.Number))
printColored(color.FgGreen, "Batch Number....: ")
batchNumber := binary.LittleEndian.Uint64(entry.Data[0:8])
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", batchNumber))
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", blockStart.BatchNumber))
printColored(color.FgGreen, "L2 Block Number.: ")
l2BlockNumber := binary.LittleEndian.Uint64(entry.Data[8:16])
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", l2BlockNumber))
timestamp := binary.LittleEndian.Uint64(entry.Data[16:24])
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", blockStart.L2BlockNumber))
printColored(color.FgGreen, "Timestamp.......: ")
printColored(color.FgHiWhite, fmt.Sprintf("%v (%d)\n", time.Unix(int64(timestamp), 0), timestamp))
globalExitRoot := "0x" + common.Bytes2Hex(entry.Data[24:56])
printColored(color.FgHiWhite, fmt.Sprintf("%v (%d)\n", time.Unix(int64(blockStart.Timestamp), 0), blockStart.Timestamp))
printColored(color.FgGreen, "Global Exit Root: ")
printColored(color.FgHiWhite, fmt.Sprintf("%s\n", globalExitRoot))
coinbase := "0x" + common.Bytes2Hex(entry.Data[56:76])
printColored(color.FgHiWhite, fmt.Sprintf("%s\n", blockStart.GlobalExitRoot))
printColored(color.FgGreen, "Coinbase........: ")
printColored(color.FgHiWhite, fmt.Sprintf("%s\n", coinbase))
forkID := binary.LittleEndian.Uint16(entry.Data[76:78])
printColored(color.FgHiWhite, fmt.Sprintf("%s\n", blockStart.Coinbase))
printColored(color.FgGreen, "Fork ID.........: ")
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", forkID))
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", blockStart.ForkID))
case state.EntryTypeL2Tx:
dsTx := state.DSL2Transaction{}.Decode(entry.Data)
printColored(color.FgGreen, "Entry Type......: ")
printColored(color.FgHiYellow, "L2 Transaction\n")
printColored(color.FgGreen, "Entry Number....: ")
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", entry.Number))
printColored(color.FgGreen, "Effec. Gas Price: ")
effectiveGasPricePercentage := entry.Data[0]
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", effectiveGasPricePercentage))
isValid := entry.Data[1] == 1
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", dsTx.EffectiveGasPricePercentage))
printColored(color.FgGreen, "Is Valid........: ")
printColored(color.FgHiWhite, fmt.Sprintf("%t\n", isValid))
encodeLength := binary.LittleEndian.Uint16(entry.Data[2:6])
printColored(color.FgGreen, "Encode Length...: ")
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", encodeLength))
encode := entry.Data[6:]
printColored(color.FgGreen, "Encode..........: ")
printColored(color.FgHiWhite, fmt.Sprintf("%s\n", "0x"+common.Bytes2Hex(encode)))

tx, err := state.DecodeTx(common.Bytes2Hex(encode))
printColored(color.FgHiWhite, fmt.Sprintf("%t\n", dsTx.IsValid == 1))
printColored(color.FgGreen, "Encoded Length..: ")
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", dsTx.EncodedLength))
printColored(color.FgGreen, "Encoded.........: ")
printColored(color.FgHiWhite, fmt.Sprintf("%s\n", "0x"+common.Bytes2Hex(dsTx.Encoded)))

tx, err := state.DecodeTx(common.Bytes2Hex(dsTx.Encoded))
if err != nil {
log.Fatal(err)
}
Expand All @@ -518,39 +483,35 @@ func printEntry(entry datastreamer.FileEntry, streamServer *datastreamer.StreamS
printColored(color.FgGreen, "Nonce...........: ")
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", nonce))
case state.EntryTypeL2BlockEnd:
blockEnd := state.DSL2BlockEnd{}.Decode(entry.Data)
printColored(color.FgGreen, "Entry Type......: ")
printColored(color.FgHiYellow, "L2 Block End\n")
printColored(color.FgGreen, "Entry Number....: ")
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", entry.Number))
printColored(color.FgGreen, "L2 Block Number.: ")
l2BlockNumber := binary.LittleEndian.Uint64(entry.Data[0:8])
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", l2BlockNumber))
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", blockEnd.L2BlockNumber))
printColored(color.FgGreen, "L2 Block Hash...: ")
printColored(color.FgHiWhite, fmt.Sprint("0x"+common.Bytes2Hex(entry.Data[8:40])+"\n"))
printColored(color.FgHiWhite, fmt.Sprint("0x"+blockEnd.BlockHash.Hex()+"\n"))
printColored(color.FgGreen, "State Root......: ")
printColored(color.FgHiWhite, fmt.Sprint("0x"+common.Bytes2Hex(entry.Data[40:72])+"\n"))
printColored(color.FgHiWhite, fmt.Sprint("0x"+blockEnd.StateRoot.Hex()+"\n"))
case state.EntryTypeUpdateGER:
updateGer := state.DSUpdateGER{}.Decode(entry.Data)
printColored(color.FgGreen, "Entry Type......: ")
printColored(color.FgHiYellow, "Update GER\n")
printColored(color.FgGreen, "Entry Number....: ")
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", entry.Number))
printColored(color.FgGreen, "Batch Number....: ")
batchNumber := binary.LittleEndian.Uint64(entry.Data[0:8])
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", batchNumber))
timestamp := binary.LittleEndian.Uint64(entry.Data[8:16])
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", updateGer.BatchNumber))
printColored(color.FgGreen, "Timestamp.......: ")
printColored(color.FgHiWhite, fmt.Sprintf("%v (%d)\n", time.Unix(int64(timestamp), 0), timestamp))
globalExitRoot := "0x" + common.Bytes2Hex(entry.Data[16:48])
printColored(color.FgHiWhite, fmt.Sprintf("%v (%d)\n", time.Unix(int64(updateGer.Timestamp), 0), updateGer.Timestamp))
printColored(color.FgGreen, "Global Exit Root: ")
printColored(color.FgHiWhite, fmt.Sprintf("%s\n", globalExitRoot))
coinbase := "0x" + common.Bytes2Hex(entry.Data[48:68])
printColored(color.FgHiWhite, fmt.Sprintf("%s\n", updateGer.GlobalExitRoot))
printColored(color.FgGreen, "Coinbase........: ")
printColored(color.FgHiWhite, fmt.Sprintf("%s\n", coinbase))
forkID := binary.LittleEndian.Uint16(entry.Data[68:70])
printColored(color.FgHiWhite, fmt.Sprintf("%s\n", updateGer.Coinbase))
printColored(color.FgGreen, "Fork ID.........: ")
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", forkID))
printColored(color.FgHiWhite, fmt.Sprintf("%d\n", updateGer.ForkID))
printColored(color.FgGreen, "State Root......: ")
printColored(color.FgHiWhite, fmt.Sprint("0x"+common.Bytes2Hex(entry.Data[70:102])+"\n"))
printColored(color.FgHiWhite, fmt.Sprint("0x"+updateGer.StateRoot.Hex()+"\n"))
}
}

Expand Down
Loading