Skip to content

Commit

Permalink
Merge branch 'MX-15218-bridge-sender-service-refactor' into MX-15283-…
Browse files Browse the repository at this point in the history
…data-formatting-unconfirmed-operation

# Conflicts:
#	server/cmd/server/.env
#	server/txSender/dataFormatter.go
  • Loading branch information
mariusmihaic committed Mar 20, 2024
2 parents ff311e6 + 1507578 commit 6854422
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 36 deletions.
4 changes: 2 additions & 2 deletions server/cmd/server/.env
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ WALLET_PASSWORD=""
# MultiversX proxy (e.g.: https://testnet-gateway.multiversx.com)
MULTIVERSX_PROXY="https://testnet-gateway.multiversx.com"
# Multisig address on MultiversX to send transactions to
MULTISIG_SC_ADDRESS="erd1qqqqqqqqqqqqqpgqk2jcpfxm6frnctvp8mgjnuvz0eh5uc68ulmqmc7mf0"
MULTISIG_SC_ADDRESS="erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"
# ESDT Safe address on MultiversX
ESDT_SAFE_SC_ADDRESS="erd1qqqqqqqqqqqqqpgq3mp2xfh0qasrqymdszp292d4vxu0h849ulmqzq28ec"
ESDT_SAFE_SC_ADDRESS="erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"
# Max retries to wait in seconds for account nonce update after sending bridge txs
MAX_RETRIES_SECONDS_WAIT_NONCE=60
# Server certificate for tls secured connection with clients.
Expand Down
12 changes: 7 additions & 5 deletions server/txSender/dataFormatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,17 @@ func (df *dataFormatter) createRegisterBridgeOperationsData(bridgeData *sovereig
}

func createBridgeOperationsData(hashOfHashes []byte, outGoingOperations []*sovereign.OutGoingOperation) [][]byte {
ret := make([][]byte, 0)
executeBridgeOpsTxData := make([][]byte, 0)
for _, operation := range outGoingOperations {
currBridgeOp := []byte(executeBridgeOpsPrefix + "@" + hex.EncodeToString(hashOfHashes) + "@")
currBridgeOp = append(currBridgeOp, hex.EncodeToString(operation.Data)...)
bridgeOpTxData := []byte(
executeBridgeOpsPrefix +
"@" + hex.EncodeToString(hashOfHashes) +
"@" + hex.EncodeToString(operation.Data))

ret = append(ret, currBridgeOp)
executeBridgeOpsTxData = append(executeBridgeOpsTxData, bridgeOpTxData)
}

return ret
return executeBridgeOpsTxData
}

// IsInterfaceNil checks if the underlying pointer is nil
Expand Down
38 changes: 20 additions & 18 deletions server/txSender/dataFormatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package txSender

import (
"encoding/hex"
"fmt"
"testing"

"github.com/multiversx/mx-chain-core-go/core"
Expand Down Expand Up @@ -109,30 +108,33 @@ func TestDataFormatter_CreateTxsData(t *testing.T) {
df, _ := NewDataFormatter(hasher)

registerOp1 := []byte(
registerBridgeOpsPrefix + "@" +
hex.EncodeToString(bridgeDataHash1) + "@" +
fmt.Sprintf("%08x", len(opHash1)) + hex.EncodeToString(opHash1) +
fmt.Sprintf("%08x", len(opHash2)) + hex.EncodeToString(opHash2) + "@" +
hex.EncodeToString(aggregatedSig1))
execOp1 := []byte(executeBridgeOpsPrefix + "@" +
hex.EncodeToString(bridgeDataHash1) + "@" +
hex.EncodeToString([]byte("bridgeOp1")) +
hex.EncodeToString([]byte("bridgeOp2")))
registerBridgeOpsPrefix +
"@" + hex.EncodeToString(aggregatedSig1) +
"@" + hex.EncodeToString(bridgeDataHash1) +
"@" + hex.EncodeToString(opHash1) +
"@" + hex.EncodeToString(opHash2))
execOp1 := []byte(executeBridgeOpsPrefix +
"@" + hex.EncodeToString(bridgeDataHash1) +
"@" + hex.EncodeToString([]byte("bridgeOp1")))
execOp2 := []byte(executeBridgeOpsPrefix +
"@" + hex.EncodeToString(bridgeDataHash1) +
"@" + hex.EncodeToString([]byte("bridgeOp2")))

registerOp2 := []byte(
registerBridgeOpsPrefix + "@" +
hex.EncodeToString(bridgeDataHash2) + "@" +
fmt.Sprintf("%08x", len(opHash2)) + hex.EncodeToString(opHash3) + "@" +
hex.EncodeToString(aggregatedSig2))
execOp2 := []byte(executeBridgeOpsPrefix + "@" +
hex.EncodeToString(bridgeDataHash2) + "@" +
hex.EncodeToString([]byte("bridgeOp3")))
registerBridgeOpsPrefix +
"@" + hex.EncodeToString(aggregatedSig2) +
"@" + hex.EncodeToString(bridgeDataHash2) +
"@" + hex.EncodeToString(opHash3))
execOp3 := []byte(executeBridgeOpsPrefix +
"@" + hex.EncodeToString(bridgeDataHash2) +
"@" + hex.EncodeToString([]byte("bridgeOp3")))

expectedTxsData := [][]byte{
registerOp1,
execOp1,
registerOp2,
execOp2,
registerOp2,
execOp3,
}

txsData := df.CreateTxsData(bridgeOps)
Expand Down
12 changes: 5 additions & 7 deletions server/txSender/txSender.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package txSender

import (
"context"
"encoding/hex"
"strings"

"github.com/multiversx/mx-chain-core-go/core/check"
Expand Down Expand Up @@ -98,9 +97,8 @@ func (ts *txSender) createAndSendTxs(ctx context.Context, data *sovereign.Bridge
for _, txData := range txsData {
var tx *coreTx.FrontendTransaction

//switch
switch {
case strings.HasPrefix(string(txData), "register"):
case strings.HasPrefix(string(txData), registerBridgeOpsPrefix):
tx = &coreTx.FrontendTransaction{
Value: "0",
Receiver: ts.scMultisigAddress,
Expand All @@ -111,7 +109,7 @@ func (ts *txSender) createAndSendTxs(ctx context.Context, data *sovereign.Bridge
ChainID: ts.netConfigs.ChainID,
Version: ts.netConfigs.MinTransactionVersion,
}
case strings.HasPrefix(string(txData), "execute"):
case strings.HasPrefix(string(txData), executeBridgeOpsPrefix):
tx = &coreTx.FrontendTransaction{
Value: "0",
Receiver: ts.scEsdtSafeAddress,
Expand All @@ -123,16 +121,16 @@ func (ts *txSender) createAndSendTxs(ctx context.Context, data *sovereign.Bridge
Version: ts.netConfigs.MinTransactionVersion,
}
default:
log.Debug("invalid tx data received", "data", hex.EncodeToString(tx.Data))
log.Error("invalid tx data received",
"data", string(tx.Data))
}

err := ts.txNonceHandler.ApplyNonceAndGasPrice(ctx, ts.wallet.GetAddressHandler(), tx)
log.Debug("transaction", "nonce", tx.Nonce)
if err != nil {
return nil, err
}

log.Debug("transaction", "nonce", tx.Nonce)

err = ts.txInteractor.ApplyUserSignature(ts.wallet, tx)
if err != nil {
return nil, err
Expand Down
18 changes: 14 additions & 4 deletions server/txSender/txSender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func TestNewTxSender(t *testing.T) {
t.Parallel()

t.Run("nil wallet", func(t *testing.T) {
t.Parallel()

args := createArgs()
args.Wallet = nil

Expand All @@ -38,6 +40,8 @@ func TestNewTxSender(t *testing.T) {
require.Equal(t, errNilWallet, err)
})
t.Run("nil proxy", func(t *testing.T) {
t.Parallel()

args := createArgs()
args.Proxy = nil

Expand All @@ -46,6 +50,8 @@ func TestNewTxSender(t *testing.T) {
require.Equal(t, errNilProxy, err)
})
t.Run("nil tx interactor", func(t *testing.T) {
t.Parallel()

args := createArgs()
args.TxInteractor = nil

Expand All @@ -54,6 +60,8 @@ func TestNewTxSender(t *testing.T) {
require.Equal(t, errNilTxInteractor, err)
})
t.Run("nil data formatter", func(t *testing.T) {
t.Parallel()

args := createArgs()
args.DataFormatter = nil

Expand All @@ -62,6 +70,8 @@ func TestNewTxSender(t *testing.T) {
require.Equal(t, errNilDataFormatter, err)
})
t.Run("should work", func(t *testing.T) {
t.Parallel()

args := createArgs()

ts, err := NewTxSender(args)
Expand All @@ -77,7 +87,7 @@ func TestTxSender_SendTxs(t *testing.T) {
expectedNonce := 0
expectedDataIdx := 0
expectedTxHashes := []string{"txHash1", "txHash2", "txHash3"}
expectedTxsData := [][]byte{[]byte("txData1"), []byte("txData2"), []byte("txData3")}
expectedTxsData := [][]byte{[]byte(registerBridgeOpsPrefix + "txData1"), []byte(executeBridgeOpsPrefix + "txData2"), []byte(executeBridgeOpsPrefix + "txData3")}
expectedSigs := []string{"sig1", "sig2", "sig3"}
expectedBridgeData := &sovereign.BridgeOperations{
Data: []*sovereign.BridgeOutGoingData{
Expand Down Expand Up @@ -116,7 +126,7 @@ func TestTxSender_SendTxs(t *testing.T) {
require.Equal(t, &transaction.FrontendTransaction{
Nonce: 0,
Value: "0",
Receiver: args.SCEsdtSafeAddress,
Receiver: tx.Receiver,
Sender: args.Wallet.GetBech32(),
GasPrice: expectedNetworkConfig.MinGasPrice,
GasLimit: 50_000_000,
Expand All @@ -138,7 +148,7 @@ func TestTxSender_SendTxs(t *testing.T) {
require.Equal(t, &transaction.FrontendTransaction{
Nonce: uint64(expectedNonce),
Value: "0",
Receiver: args.SCEsdtSafeAddress,
Receiver: tx.Receiver,
Sender: args.Wallet.GetBech32(),
GasPrice: expectedNetworkConfig.MinGasPrice,
GasLimit: 50_000_000,
Expand Down Expand Up @@ -175,7 +185,7 @@ func TestTxSender_SendTxsConcurrently(t *testing.T) {

args.DataFormatter = &testscommon.DataFormatterMock{
CreateTxsDataCalled: func(data *sovereign.BridgeOperations) [][]byte {
return [][]byte{[]byte("txData")}
return [][]byte{[]byte(executeBridgeOpsPrefix + "txData")}
},
}
args.TxNonceHandler = &testscommon.TxNonceSenderHandlerMock{
Expand Down

0 comments on commit 6854422

Please sign in to comment.