Skip to content

Commit

Permalink
grpc enabled flag
Browse files Browse the repository at this point in the history
  • Loading branch information
axenteoctavian committed Oct 24, 2024
1 parent 9cd7ced commit d7d54b8
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

type disabledGRPCServer struct {
}

// NewDisabledGRPCServer -
func NewDisabledGRPCServer() *disabledGRPCServer {
return &disabledGRPCServer{}
}

// Stop does nothing
func (dgs *disabledGRPCServer) Stop() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

type disabledMockServer struct {
}

// NewDisabledMockServer -
func NewDisabledMockServer() *disabledMockServer {
return &disabledMockServer{}
}

// ExtractRandomBridgeTopicsForConfirmation returns empty confirmed bridge op
func (dms *disabledMockServer) ExtractRandomBridgeTopicsForConfirmation() ([]*ConfirmedBridgeOp, error) {
return make([]*ConfirmedBridgeOp, 0), nil
}

// IsInterfaceNil -
func (dms *disabledMockServer) IsInterfaceNil() bool {
return dms == nil
}
14 changes: 14 additions & 0 deletions cmd/sovereignnode/systemTestDemo/mockNotifier/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,18 @@ var (
" log level.",
Value: "*:" + logger.LogTrace.String(),
}
grpcEnabled = cli.BoolFlag{
Name: "grpc-enabled",
Usage: "Boolean option for enabling GRPC server.",
}
sovereignBridgeCertificateFile = cli.StringFlag{
Name: "certificate",
Usage: "The path for sovereign outgoing bridge certificate file.",
Value: "~/MultiversX/testnet/node/config/certificate.crt",
}
sovereignBridgeCertificatePkFile = cli.StringFlag{
Name: "certificate-pk",
Usage: "The path for sovereign outgoing bridge private key certificate file.",
Value: "~/MultiversX/testnet/node/config/private_key.pem",
}
)
9 changes: 9 additions & 0 deletions cmd/sovereignnode/systemTestDemo/mockNotifier/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

type MockServer interface {
ExtractRandomBridgeTopicsForConfirmation() ([]*ConfirmedBridgeOp, error)
}

type GRPCServerMock interface {
Stop()
}
66 changes: 45 additions & 21 deletions cmd/sovereignnode/systemTestDemo/mockNotifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/big"
"net"
"os"
"strings"
"time"

"github.com/multiversx/mx-chain-communication-go/websocket/data"
Expand Down Expand Up @@ -43,6 +44,9 @@ func main() {
"The blocks are sent with an arbitrary period between them."
app.Flags = []cli.Flag{
logLevel,
grpcEnabled,
sovereignBridgeCertificateFile,
sovereignBridgeCertificatePkFile,
}
app.Authors = []cli.Author{
{
Expand Down Expand Up @@ -71,14 +75,14 @@ func startMockNotifier(ctx *cli.Context) error {
return err
}

//mockedGRPCServer, grpcServerConn, err := createAndStartGRPCServer()
//if err != nil {
// log.Error("cannot create grpc server", "error", err)
// return err
//}
mockedGRPCServer, grpcServerConn, err := createAndStartGRPCServer(ctx)
if err != nil {
log.Error("cannot create grpc server", "error", err)
return err
}

defer func() {
//grpcServerConn.Stop()
grpcServerConn.Stop()
err = host.Close()
log.LogIfError(err)
}()
Expand All @@ -94,10 +98,10 @@ func startMockNotifier(ctx *cli.Context) error {
for {
headerV2 := createHeaderV2(nonce, prevHash, prevRandSeed)

//confirmedBridgeOps, err := mockedGRPCServer.ExtractRandomBridgeTopicsForConfirmation()
//log.LogIfError(err)
confirmedBridgeOps, err := mockedGRPCServer.ExtractRandomBridgeTopicsForConfirmation()
log.LogIfError(err)

outportBlock, err := createOutportBlock(headerV2, subscribedAddr, nil)
outportBlock, err := createOutportBlock(headerV2, subscribedAddr, confirmedBridgeOps)
if err != nil {
return err
}
Expand Down Expand Up @@ -148,15 +152,19 @@ func createWSHost() (factoryHost.FullDuplexHost, error) {
return factoryHost.CreateWebSocketHost(args)
}

func createAndStartGRPCServer() (*mockServer, *grpc.Server, error) {
func createAndStartGRPCServer(ctx *cli.Context) (MockServer, GRPCServerMock, error) {
if !ctx.Bool(grpcEnabled.Name) {
return NewDisabledMockServer(), NewDisabledGRPCServer(), nil
}

listener, err := net.Listen("tcp", grpcAddress)
if err != nil {
return nil, nil, err
}

tlsConfig, err := cert.LoadTLSServerConfig(cert.FileCfg{
CertFile: "certificate.crt",
PkFile: "private_key.pem",
CertFile: getAbsolutePath(ctx.GlobalString(sovereignBridgeCertificateFile.Name)),
PkFile: getAbsolutePath(ctx.GlobalString(sovereignBridgeCertificatePkFile.Name)),
})
if err != nil {
return nil, nil, err
Expand All @@ -182,6 +190,19 @@ func createAndStartGRPCServer() (*mockServer, *grpc.Server, error) {
return mockedServer, grpcServer, nil
}

func getAbsolutePath(path string) string {
if !strings.HasPrefix(path, "~") {
return path
}

homeDir, err := os.UserHomeDir()
if err != nil {
log.Error("Error getting home directory: " + err.Error())
return ""
}
return strings.Replace(path, "~", homeDir, 1)
}

func generateRandomHash() []byte {
randomBytes := make([]byte, hashSize)
_, _ = rand.Read(randomBytes)
Expand Down Expand Up @@ -214,10 +235,10 @@ func createOutportBlock(headerV2 *block.HeaderV2, subscribedAddr []byte, confirm
logs := make([]*outport.LogData, 0)
logs = append(logs, incomingLogs...)

//bridgeConfirmationLogs := createOutGoingBridgeOpsConfirmationLogs(confirmedBridgeOps, subscribedAddr)
//if len(bridgeConfirmationLogs) != 0 {
// logs = append(logs, bridgeConfirmationLogs...)
//}
bridgeConfirmationLogs := createOutGoingBridgeOpsConfirmationLogs(confirmedBridgeOps, subscribedAddr)
if len(bridgeConfirmationLogs) != 0 {
logs = append(logs, bridgeConfirmationLogs...)
}

return &outport.OutportBlock{
BlockData: blockData,
Expand Down Expand Up @@ -264,8 +285,8 @@ func createOutGoingBridgeOpsConfirmationLogs(confirmedBridgeOps []*ConfirmedBrid
Events: []*transaction.Event{
{
Address: subscribedAddr,
Identifier: []byte("executedBridgeOp"),
Topics: [][]byte{confirmedBridgeOp.HashOfHashes, confirmedBridgeOp.BridgeOpHash},
Identifier: []byte("execute"),
Topics: [][]byte{[]byte("executedBridgeOp"), confirmedBridgeOp.HashOfHashes, confirmedBridgeOp.BridgeOpHash},
},
},
},
Expand All @@ -291,9 +312,12 @@ func createTransferTopics(addr []byte, ct int64) ([][]byte, error) {
tokenMetaData, // meta data
}

topic := append([][]byte{addr}, transferNFT...)
topic = append(topic, transferESDT...)
return topic, nil
topics := make([][]byte, 0)
topics = append(topics, []byte("deposit"))
topics = append(topics, addr)
topics = append(topics, transferNFT...)
topics = append(topics, transferESDT...)
return topics, nil
}

func createESDTTokenData(
Expand Down

0 comments on commit d7d54b8

Please sign in to comment.