Skip to content

Commit

Permalink
FIX: Bitmap + epoch bridge data tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusmihaic committed Jan 15, 2025
1 parent 44bbab0 commit 53f8aa7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
19 changes: 10 additions & 9 deletions server/txSender/dataFormatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package txSender

import (
"bytes"
"encoding/binary"
"encoding/hex"

"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/sovereign"
"github.com/multiversx/mx-chain-core-go/data/typeConverters"
"github.com/multiversx/mx-chain-core-go/data/typeConverters/uint64ByteSlice"
"github.com/multiversx/mx-chain-core-go/hashing"
)

Expand All @@ -18,8 +17,7 @@ const (
)

type dataFormatter struct {
hasher hashing.Hasher
uIntConverter typeConverters.Uint64ByteSliceConverter
hasher hashing.Hasher
}

// NewDataFormatter creates a sovereign bridge tx data formatter
Expand All @@ -29,8 +27,7 @@ func NewDataFormatter(hasher hashing.Hasher) (*dataFormatter, error) {
}

return &dataFormatter{
hasher: hasher,
uIntConverter: uint64ByteSlice.NewBigEndianConverter(),
hasher: hasher,
}, nil
}

Expand Down Expand Up @@ -69,20 +66,24 @@ func (df *dataFormatter) createRegisterBridgeOperationsData(bridgeData *sovereig
return nil
}

// TODO: Here, create a new uint32 converter or just use binary.Uin32 std library

registerBridgeOpData := []byte(registerBridgeOpsPrefix +
"@" + hex.EncodeToString(bridgeData.AggregatedSignature) +
"@" + hex.EncodeToString(bridgeData.Hash))
registerBridgeOpData = append(registerBridgeOpData, hashesHexEncodedArgs...)

bridgeDataArgs := []byte(
"@" + hex.EncodeToString(bridgeData.PubKeysBitmap) +
"@" + hex.EncodeToString(df.uIntConverter.ToByteSlice(uint64(bridgeData.Epoch))))
"@" + hex.EncodeToString(uint32ToBytes(bridgeData.Epoch)))

return append(registerBridgeOpData, bridgeDataArgs...)
}

func uint32ToBytes(value uint32) []byte {
buff := make([]byte, 4)
binary.BigEndian.PutUint32(buff, value)
return buff
}

func createBridgeOperationsData(hashOfHashes []byte, outGoingOperations []*sovereign.OutGoingOperation) [][]byte {
executeBridgeOpsTxData := make([][]byte, 0)
for _, operation := range outGoingOperations {
Expand Down
18 changes: 15 additions & 3 deletions server/txSender/dataFormatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data/sovereign"
"github.com/multiversx/mx-chain-sovereign-bridge-go/testscommon"
"github.com/stretchr/testify/require"

"github.com/multiversx/mx-chain-sovereign-bridge-go/testscommon"
)

func TestNewDataFormatter(t *testing.T) {
Expand Down Expand Up @@ -57,6 +58,9 @@ func TestDataFormatter_CreateTxsData(t *testing.T) {
bridgeDataOp2 := []byte("bridgeDataOp2")
bridgeDataOp3 := []byte("bridgeDataOp3")

pubKeysBitmap1 := []byte("pubKeysBitmap1")
pubKeysBitmap2 := []byte("pubKeysBitmap2")

bridgeOps := &sovereign.BridgeOperations{
Data: []*sovereign.BridgeOutGoingData{
{
Expand All @@ -73,6 +77,8 @@ func TestDataFormatter_CreateTxsData(t *testing.T) {
},
AggregatedSignature: aggregatedSig1,
LeaderSignature: leaderSig1,
PubKeysBitmap: pubKeysBitmap1,
Epoch: 1,
},
{
Hash: bridgeDataHash2,
Expand All @@ -84,6 +90,8 @@ func TestDataFormatter_CreateTxsData(t *testing.T) {
},
AggregatedSignature: aggregatedSig2,
LeaderSignature: leaderSig2,
PubKeysBitmap: pubKeysBitmap2,
Epoch: 2,
},
},
}
Expand Down Expand Up @@ -116,7 +124,9 @@ func TestDataFormatter_CreateTxsData(t *testing.T) {
"@" + hex.EncodeToString(aggregatedSig1) +
"@" + hex.EncodeToString(bridgeDataHash1) +
"@" + hex.EncodeToString(opHash1) +
"@" + hex.EncodeToString(opHash2))
"@" + hex.EncodeToString(opHash2) +
"@" + hex.EncodeToString(pubKeysBitmap1) +
"@" + "00000001")
execOp1 := []byte(executeBridgeOpsPrefix +
"@" + hex.EncodeToString(bridgeDataHash1) +
"@" + hex.EncodeToString(bridgeDataOp1))
Expand All @@ -128,7 +138,9 @@ func TestDataFormatter_CreateTxsData(t *testing.T) {
registerBridgeOpsPrefix +
"@" + hex.EncodeToString(aggregatedSig2) +
"@" + hex.EncodeToString(bridgeDataHash2) +
"@" + hex.EncodeToString(opHash3))
"@" + hex.EncodeToString(opHash3) +
"@" + hex.EncodeToString(pubKeysBitmap2) +
"@" + "00000002")
execOp3 := []byte(executeBridgeOpsPrefix +
"@" + hex.EncodeToString(bridgeDataHash2) +
"@" + hex.EncodeToString(bridgeDataOp3))
Expand Down

0 comments on commit 53f8aa7

Please sign in to comment.