Skip to content

Commit

Permalink
added base components structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ssd04 committed Mar 14, 2024
1 parent d6afbdf commit 61a89af
Show file tree
Hide file tree
Showing 10 changed files with 328 additions and 86 deletions.
33 changes: 30 additions & 3 deletions factory/wsConnectorFactory.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/multiversx/mx-chain-core-go/marshal"
"github.com/multiversx/mx-chain-core-go/marshal/factory"
logger "github.com/multiversx/mx-chain-logger-go"
"github.com/multiversx/mx-chain-storage-go/storageUnit"

"github.com/multiversx/mx-chain-ws-connector-template-go/config"
"github.com/multiversx/mx-chain-ws-connector-template-go/process"
Expand All @@ -30,15 +31,41 @@ func CreateWSConnector(cfg config.WebSocketConfig) (process.WSConnector, error)
return nil, err
}

dataProcessor, err := process.NewFirehoseDataProcessor(
firehosePublisher, err := process.NewFirehosePublisher(
os.Stdout, // DO NOT CHANGE
blockContainer,
&marshal.GogoProtoMarshalizer{}, // DO NOT CHANGE
)
if err != nil {
return nil, err
}

cacheConfig := storageUnit.CacheConfig{
Type: storageUnit.SizeLRUCache,
SizeInBytes: 209715200, // 200MB
Capacity: 100,
}

cacher, err := storageUnit.NewCache(cacheConfig)
if err != nil {
return nil, err
}

blocksPool, err := process.NewBlocksPool(cacher)
if err != nil {
return nil, err
}

protoMarshaller := &marshal.GogoProtoMarshalizer{}

dataAggregator, err := process.NewDataAggregator(blockContainer, blocksPool, protoMarshaller)
if err != nil {
return nil, err
}

dataProcessor, err := process.NewDataProcessor(firehosePublisher, protoMarshaller, blocksPool, dataAggregator)
if err != nil {
return nil, err
}

wsHost, err := createWsHost(marshaller, cfg)
if err != nil {
return nil, err
Expand Down
13 changes: 9 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ module github.com/multiversx/mx-chain-ws-connector-template-go
go 1.20

require (
github.com/multiversx/mx-chain-communication-go v1.0.12
github.com/multiversx/mx-chain-core-go v1.2.18
github.com/multiversx/mx-chain-logger-go v1.0.13
github.com/multiversx/mx-chain-communication-go v1.0.13-0.20240126121117-627adccf10ad
github.com/multiversx/mx-chain-core-go v1.2.19-0.20240222081523-011c96ab2548
github.com/multiversx/mx-chain-go v1.7.6
github.com/multiversx/mx-chain-logger-go v1.0.14-0.20240129144507-d00e967c890c
github.com/stretchr/testify v1.8.4
github.com/urfave/cli v1.22.14
)
Expand All @@ -18,11 +19,15 @@ require (
github.com/golang/protobuf v1.5.3 // indirect
github.com/gorilla/mux v1.8.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/hashicorp/golang-lru v0.6.0 // indirect
github.com/mr-tron/base58 v1.2.0 // indirect
github.com/multiversx/concurrent-map v0.1.4 // indirect
github.com/multiversx/mx-chain-storage-go v1.0.15-0.20240129144933-b1c0d642d7f8 // indirect
github.com/pelletier/go-toml v1.9.3 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/crypto v0.10.0 // indirect
golang.org/x/sys v0.10.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
31 changes: 21 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,32 @@ github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/golang-lru v0.6.0 h1:uL2shRDx7RTrOrTCUZEGP/wJUFiUI8QT6E7z5o8jga4=
github.com/hashicorp/golang-lru v0.6.0/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/multiversx/mx-chain-communication-go v1.0.12 h1:67WOaf87gpwouydD1AAOHw5LMGZh7NfITrp/KqFY3Tw=
github.com/multiversx/mx-chain-communication-go v1.0.12/go.mod h1:+oaUowpq+SqrEmAsMPGwhz44g7L81loWb6AiNQU9Ms4=
github.com/multiversx/mx-chain-core-go v1.2.18 h1:fnub2eFL7XYOLrKKVZAPPsaM1TWEnaK5qqY3FLUv168=
github.com/multiversx/mx-chain-core-go v1.2.18/go.mod h1:BILOGHUOIG5dNNX8cgkzCNfDaVtoYrJRYcPnpxRMH84=
github.com/multiversx/mx-chain-crypto-go v1.2.8 h1:wOgVlUaO5X4L8iEbFjcQcL8SZvv6WZ7LqH73BiRPhxU=
github.com/multiversx/mx-chain-logger-go v1.0.13 h1:eru/TETo0MkO4ZTnXsQDKf4PBRpAXmqjT02klNT/JnY=
github.com/multiversx/mx-chain-logger-go v1.0.13/go.mod h1:MZJhTAtZTJxT+yK2EHc4ZW3YOHUc1UdjCD0iahRNBZk=
github.com/multiversx/concurrent-map v0.1.4 h1:hdnbM8VE4b0KYJaGY5yJS2aNIW9TFFsUYwbO0993uPI=
github.com/multiversx/concurrent-map v0.1.4/go.mod h1:8cWFRJDOrWHOTNSqgYCUvwT7c7eFQ4U2vKMOp4A/9+o=
github.com/multiversx/mx-chain-communication-go v1.0.13-0.20240126121117-627adccf10ad h1:izxTyKCxvT7z2mhXCWAZibSxwRVgLmq/kDovs4Nx/6Y=
github.com/multiversx/mx-chain-communication-go v1.0.13-0.20240126121117-627adccf10ad/go.mod h1:n4E8BWIV0g3AcNGe1gf+vcjUC8A2QCJ4ARQSbiUDGrI=
github.com/multiversx/mx-chain-core-go v1.2.19-0.20240222081523-011c96ab2548 h1:WQoVgQG9YWiYM5Q3MmnbnxeoQkfHr63iFJZScFYsMxk=
github.com/multiversx/mx-chain-core-go v1.2.19-0.20240222081523-011c96ab2548/go.mod h1:B5zU4MFyJezmEzCsAHE9YNULmGCm2zbPHvl9hazNxmE=
github.com/multiversx/mx-chain-crypto-go v1.2.10-0.20231206065052-38843c1f1479 h1:beVIhs5ysylwNplQ/bZ0h5DoDlqKNWgpWE/NMHHNmAw=
github.com/multiversx/mx-chain-go v1.7.6 h1:OOXIVBJjtlppXbTKPoDmmnfCHtCyrUe8ju2S6if52SU=
github.com/multiversx/mx-chain-go v1.7.6/go.mod h1:QW56aus7apttxjK9o1msJwdrlfE2gHUfrP9CXRaJPO0=
github.com/multiversx/mx-chain-logger-go v1.0.14-0.20240129144507-d00e967c890c h1:QIUOn8FgNRa5cir4BCWHZi/Qcr6Gg0eGNhns4+jy6+k=
github.com/multiversx/mx-chain-logger-go v1.0.14-0.20240129144507-d00e967c890c/go.mod h1:fH/fR/GEBsDjPkBoZDVJMoYo2HhlA7++DP6QfITJ1N8=
github.com/multiversx/mx-chain-storage-go v1.0.15-0.20240129144933-b1c0d642d7f8 h1:/EYv/HGX0OKbeNFt667J0yZRtuJiZH0lEK8YtobuH/c=
github.com/multiversx/mx-chain-storage-go v1.0.15-0.20240129144933-b1c0d642d7f8/go.mod h1:zl1A6teNe39T8yhdZlkX3ckm5aLYrMIJJZ6Ord1E71M=
github.com/pelletier/go-toml v1.9.3 h1:zeC5b1GviRUyKYd6OJPvBU/mcVDVoL1OhT17FCt5dSQ=
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -41,14 +50,16 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d h1:vfofYNRScrDdvS342BElfbETmL1Aiz3i2t0zfRj16Hs=
github.com/urfave/cli v1.22.14 h1:ebbhrRiGK2i4naQJr+1Xj92HXZCrK7MsyTS/ob3HnAk=
github.com/urfave/cli v1.22.14/go.mod h1:X0eDS6pD6Exaclxm99NJ3FiCDRED7vIHpx2mDOHLvkA=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.9.0 h1:LF6fAI+IutBocDJ2OT0Q1g8plpYljMZ4+lty+dsqw3g=
golang.org/x/crypto v0.10.0 h1:LKqV2xt9+kDzSTfOhx4FrkEBcMrAgHSYgzywV9zcGmM=
golang.org/x/crypto v0.10.0/go.mod h1:o4eNf7Ede1fv+hwOwZsTHl9EsPFO6q6ZvYR8vYfY45I=
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
Expand All @@ -61,8 +72,8 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down
39 changes: 39 additions & 0 deletions process/blocksPool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package process

import (
"fmt"

"github.com/multiversx/mx-chain-core-go/data/outport"
"github.com/multiversx/mx-chain-go/storage"
)

type blocksPool struct {
cacher storage.Cacher
}

func NewBlocksPool(cacher storage.Cacher) (*blocksPool, error) {
return &blocksPool{cacher: cacher}, nil
}

func (bp *blocksPool) PutBlock(hash []byte, outportBlock *outport.OutportBlock) error {
_ = bp.cacher.Put(hash, outportBlock, 0)
return nil
}

func (bp *blocksPool) GetBlock(hash []byte) (*outport.OutportBlock, error) {
data, ok := bp.cacher.Get(hash)
if !ok {
return nil, fmt.Errorf("failed to get data from pool")
}

outportBlock, ok := data.(*outport.OutportBlock)
if !ok {
return nil, ErrWrongTypeAssertion
}

return outportBlock, nil
}

func (bp *blocksPool) IsInterfaceNil() bool {
return bp == nil
}
63 changes: 63 additions & 0 deletions process/dataAggregator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package process

import (
"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"
"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"
)

type dataAggregator struct {
blockCreator BlockContainerHandler
blocksPool BlocksPool
marshaller marshal.Marshalizer
}

func NewDataAggregator(
blockCreator BlockContainerHandler,
blocksPool BlocksPool,
marshaller marshal.Marshalizer,
) (*dataAggregator, error) {
if check.IfNil(blockCreator) {
return nil, errNilBlockCreator
}
if check.IfNil(blocksPool) {
return nil, errNilBlocksPool
}
if check.IfNil(marshaller) {
return nil, errNilMarshaller
}

return &dataAggregator{
blockCreator: blockCreator,
blocksPool: blocksPool,
marshaller: marshaller,
}, nil
}

func (da *dataAggregator) ProcessHyperBlock(outportBlock *outport.OutportBlock) (data.HeaderHandler, []byte, error) {
blockCreator, err := da.blockCreator.Get(core.HeaderType(outportBlock.BlockData.HeaderType))
if err != nil {
return nil, nil, err
}

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

// dummy marshalled data
marshalledData, err := da.marshaller.Marshal(outportBlock)
if err != nil {
return nil, nil, err
}

return header, marshalledData, nil
}

// IsInterfaceNil returns true if there is no value under interface
func (da *dataAggregator) IsInterfaceNil() bool {
return da == nil
}
116 changes: 116 additions & 0 deletions process/dataProcessor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package process

import (
"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/outport"
"github.com/multiversx/mx-chain-core-go/marshal"
)

type dataProcessor struct {
marshaller marshal.Marshalizer
operationHandlers map[string]func(marshalledData []byte) error
publisher Publisher
blocksPool BlocksPool
dataAggregator DataAggregator
}

// NewDataProcessor creates a data processor able to receive data from a ws outport driver and handle blocks
func NewDataProcessor(
publisher Publisher,
marshaller marshal.Marshalizer,
blocksPool BlocksPool,
dataAggregator DataAggregator,
) (DataProcessor, error) {
if publisher == nil {
return nil, errNilWriter
}
if check.IfNil(blocksPool) {
return nil, errNilBlocksPool
}
if check.IfNil(marshaller) {
return nil, errNilMarshaller
}
if check.IfNil(dataAggregator) {
return nil, errNilDataAggregator
}

dp := &dataProcessor{
marshaller: marshaller,
publisher: publisher,
blocksPool: blocksPool,
dataAggregator: dataAggregator,
}

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

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 {
operationHandler, found := dp.operationHandlers[topic]
if !found {
return nil
}

return operationHandler(payload)
}

func (dp *dataProcessor) saveBlock(marshalledData []byte) error {
outportBlock := &outport.OutportBlock{}
err := dp.marshaller.Unmarshal(outportBlock, marshalledData)
if err != nil {
return err
}

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

if outportBlock.ShardID == core.MetachainShardId {
return dp.handleMetaOutportBlock(outportBlock)
}

return dp.handleShardOutportBlock(outportBlock)
}

func (dp *dataProcessor) handleMetaOutportBlock(outportBlock *outport.OutportBlock) error {
header, marshalledData, err := dp.dataAggregator.ProcessHyperBlock(outportBlock)
if err != nil {
return err
}

// test
headerHash := header.GetRootHash()

err = dp.publisher.PublishHyperBlock(header, headerHash, marshalledData)
if err != nil {
return err
}

return nil
}

func (dp *dataProcessor) handleShardOutportBlock(outportBlock *outport.OutportBlock) error {
blockHash := outportBlock.BlockData.HeaderHash

err := dp.blocksPool.PutBlock(blockHash, outportBlock)
if err != nil {
return err
}

return nil
}

// Close will close the internal writer
func (dp *dataProcessor) Close() error {
return dp.publisher.Close()
}

// IsInterfaceNil checks if the underlying pointer is nil
func (dp *dataProcessor) IsInterfaceNil() bool {
return dp == nil
}
9 changes: 9 additions & 0 deletions process/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,12 @@ var errNilOutportBlockData = errors.New("nil outport block data")
var errNilWriter = errors.New("nil writer provided")

var errNilBlockCreator = errors.New("nil block creator provided")

var errNilPublisher = errors.New("nil publisher provided")

var errNilBlocksPool = errors.New("nil blocks pool provided")

var errNilDataAggregator = errors.New("nil data aggregator provided")

// ErrWrongTypeAssertion signals that a type assertion faled
var ErrWrongTypeAssertion = errors.New("type assertion failed")
Loading

0 comments on commit 61a89af

Please sign in to comment.