Skip to content

Commit

Permalink
fix merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
ssd04 committed Mar 19, 2024
2 parents 0b011cf + aa1ff1d commit e7362e7
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 1 deletion.
128 changes: 128 additions & 0 deletions process/firehoseDataProcessor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package process

import (
"encoding/base64"
"encoding/hex"
"fmt"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/core/check"
"github.com/multiversx/mx-chain-core-go/data/block"
"github.com/multiversx/mx-chain-core-go/data/outport"
"github.com/multiversx/mx-chain-core-go/marshal"
logger "github.com/multiversx/mx-chain-logger-go"
)

var log = logger.GetOrCreate("firehose")

Check failure on line 16 in process/firehoseDataProcessor.go

View workflow job for this annotation

GitHub Actions / build

previous declaration

Check failure on line 16 in process/firehoseDataProcessor.go

View workflow job for this annotation

GitHub Actions / build

previous declaration

Check failure on line 16 in process/firehoseDataProcessor.go

View workflow job for this annotation

GitHub Actions / golangci linter

other declaration of log (typecheck)

const (
firehosePrefix = "FIRE"

Check failure on line 19 in process/firehoseDataProcessor.go

View workflow job for this annotation

GitHub Actions / golangci linter

other declaration of firehosePrefix (typecheck)
blockPrefix = "BLOCK"

Check failure on line 20 in process/firehoseDataProcessor.go

View workflow job for this annotation

GitHub Actions / golangci linter

other declaration of blockPrefix (typecheck)
initPrefix = "INIT"

Check failure on line 21 in process/firehoseDataProcessor.go

View workflow job for this annotation

GitHub Actions / golangci linter

other declaration of initPrefix (typecheck)

protocolReaderVersion = "1.0"
protoMessageType = "type.googleapis.com/proto.OutportBlock"
)

type dataProcessor struct {

Check failure on line 27 in process/firehoseDataProcessor.go

View workflow job for this annotation

GitHub Actions / build

dataProcessor redeclared in this block

Check failure on line 27 in process/firehoseDataProcessor.go

View workflow job for this annotation

GitHub Actions / build

dataProcessor redeclared in this block

Check failure on line 27 in process/firehoseDataProcessor.go

View workflow job for this annotation

GitHub Actions / golangci linter

`dataProcessor` redeclared in this block (typecheck)
marshaller marshal.Marshalizer
operationHandlers map[string]func(marshalledData []byte) error
writer Writer
blockCreator BlockContainerHandler
}

// NewFirehoseDataProcessor creates a data processor able to receive data from a ws outport driver and print saved blocks
func NewFirehoseDataProcessor(
writer Writer,
blockCreator BlockContainerHandler,
marshaller marshal.Marshalizer,
) (DataProcessor, error) {
if writer == nil {
return nil, errNilWriter
}
if check.IfNil(blockCreator) {
return nil, errNilBlockCreator
}
if check.IfNil(marshaller) {
return nil, errNilMarshaller
}

dp := &dataProcessor{
marshaller: marshaller,
writer: writer,
blockCreator: blockCreator,
}

dp.operationHandlers = map[string]func(marshalledData []byte) error{
outport.TopicSaveBlock: dp.saveBlock,
}

_, _ = fmt.Fprintf(dp.writer, "%s %s %s %s\n", firehosePrefix, initPrefix, protocolReaderVersion, protoMessageType)

return dp, nil
}

// ProcessPayload will process the received payload only for TopicSaveBlock, otherwise ignores it.
func (dp *dataProcessor) ProcessPayload(payload []byte, topic string, _ uint32) error {

Check failure on line 66 in process/firehoseDataProcessor.go

View workflow job for this annotation

GitHub Actions / build

(*dataProcessor).ProcessPayload redeclared in this block

Check failure on line 66 in process/firehoseDataProcessor.go

View workflow job for this annotation

GitHub Actions / build

(*dataProcessor).ProcessPayload redeclared in this block
operationHandler, found := dp.operationHandlers[topic]
if !found {
return nil
}

return operationHandler(payload)
}

func (dp *dataProcessor) saveBlock(marshalledData []byte) error {

Check failure on line 75 in process/firehoseDataProcessor.go

View workflow job for this annotation

GitHub Actions / build

(*dataProcessor).saveBlock redeclared in this block

Check failure on line 75 in process/firehoseDataProcessor.go

View workflow job for this annotation

GitHub Actions / build

(*dataProcessor).saveBlock redeclared in this block
outportBlock := &outport.OutportBlock{}
err := dp.marshaller.Unmarshal(outportBlock, marshalledData)
if err != nil {
return err
}

if outportBlock == nil || outportBlock.BlockData == nil {
return errNilOutportBlockData
}

blockCreator, err := dp.blockCreator.Get(core.HeaderType(outportBlock.BlockData.HeaderType))
if err != nil {
return err
}

header, err := block.GetHeaderFromBytes(dp.marshaller, blockCreator, outportBlock.BlockData.HeaderBytes)
if err != nil {
return err
}

log.Info("saving block", "nonce", header.GetNonce(), "hash", outportBlock.BlockData.HeaderHash)

blockNum := header.GetNonce()
parentNum := blockNum - 1
if blockNum == 0 {
parentNum = 0
}
encodedMarshalledData := base64.StdEncoding.EncodeToString(marshalledData)

_, err = fmt.Fprintf(dp.writer, "%s %s %d %s %d %s %d %d %s\n",
firehosePrefix,
blockPrefix,
blockNum,
hex.EncodeToString(outportBlock.BlockData.HeaderHash),
parentNum,
hex.EncodeToString(header.GetPrevHash()),
outportBlock.HighestFinalBlockNonce,
header.GetTimeStamp(),
encodedMarshalledData,
)

return err
}

// Close will close the internal writer
func (dp *dataProcessor) Close() error {

Check failure on line 121 in process/firehoseDataProcessor.go

View workflow job for this annotation

GitHub Actions / build

(*dataProcessor).Close redeclared in this block

Check failure on line 121 in process/firehoseDataProcessor.go

View workflow job for this annotation

GitHub Actions / build

(*dataProcessor).Close redeclared in this block
return dp.writer.Close()
}

// IsInterfaceNil checks if the underlying pointer is nil
func (dp *dataProcessor) IsInterfaceNil() bool {
return dp == nil
}
2 changes: 1 addition & 1 deletion process/firehosePublisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
initPrefix = "INIT"

Check failure on line 21 in process/firehosePublisher.go

View workflow job for this annotation

GitHub Actions / golangci linter

`initPrefix` redeclared in this block (typecheck)

protocolReaderVersion = "1.0"
protoMessageType = "type.googleapis.com/bstream.pb.sf.bstream.v1.OutportBlock"
protoMessageType = "type.googleapis.com/proto.OutportBlock"
)

type firehosePublisher struct {
Expand Down

0 comments on commit e7362e7

Please sign in to comment.