diff --git a/cmd/run.go b/cmd/run.go index 748814c6ee..3bd3e01387 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -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: @@ -243,6 +240,10 @@ func start(cliCtx *cli.Context) error { } } + if c.Metrics.Enabled { + go startMetricsHttpServer(c.Metrics) + } + waitSignal(cancelFuncs) return nil diff --git a/go.mod b/go.mod index 9d48041dc4..b7f92b7110 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 5585d74562..7f43e03aad 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/sequencer/sequencer.go b/sequencer/sequencer.go index 94b56d93bb..7e56cdc2f6 100644 --- a/sequencer/sequencer.go +++ b/sequencer/sequencer.go @@ -4,7 +4,6 @@ import ( "context" "errors" "fmt" - "reflect" "time" "github.com/0xPolygonHermez/zkevm-data-streamer/datastreamer" @@ -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 { diff --git a/state/datastream.go b/state/datastream.go index 9c9c79680f..9aa6eda6bc 100644 --- a/state/datastream.go +++ b/state/datastream.go @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/tools/datastreamer/main.go b/tools/datastreamer/main.go index 826abf1ee5..b7534c6b58 100644 --- a/tools/datastreamer/main.go +++ b/tools/datastreamer/main.go @@ -5,7 +5,6 @@ import ( "encoding/binary" "fmt" "os" - "reflect" "time" "github.com/0xPolygonHermez/zkevm-data-streamer/datastreamer" @@ -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 @@ -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) } @@ -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")) } }