diff --git a/bridges/ethMultiversX/bridgeExecutor_test.go b/bridges/ethMultiversX/bridgeExecutor_test.go index 56d2d0a0..7da6d87d 100644 --- a/bridges/ethMultiversX/bridgeExecutor_test.go +++ b/bridges/ethMultiversX/bridgeExecutor_test.go @@ -1476,7 +1476,7 @@ func TestGetBatchStatusesFromEthereum(t *testing.T) { t.Parallel() wasCalled := false - providedStatuses := []byte{clients.Executed, clients.Rejected} + providedStatuses := []byte{bridgeCommon.Executed, bridgeCommon.Rejected} args := createMockExecutorArgs() args.EthereumClient = &bridgeTests.EthereumClientStub{ GetTransactionsStatusesCalled: func(ctx context.Context, batchId uint64) ([]byte, error) { @@ -1554,7 +1554,7 @@ func TestWaitAndReturnFinalBatchStatuses(t *testing.T) { t.Run("GetBatchStatusesFromEthereum always returns success+statuses only after 4 checks", func(t *testing.T) { t.Parallel() - providedStatuses := []byte{clients.Executed, clients.Rejected} + providedStatuses := []byte{bridgeCommon.Executed, bridgeCommon.Rejected} args := createMockExecutorArgs() args.TimeForWaitOnEthereum = 10 * time.Second counter := 0 @@ -1585,7 +1585,7 @@ func TestWaitAndReturnFinalBatchStatuses(t *testing.T) { t.Run("GetBatchStatusesFromEthereum always returns success+statuses only after 4 checks, otherwise empty slice", func(t *testing.T) { t.Parallel() - providedStatuses := []byte{clients.Executed, clients.Rejected} + providedStatuses := []byte{bridgeCommon.Executed, bridgeCommon.Rejected} args := createMockExecutorArgs() args.TimeForWaitOnEthereum = 10 * time.Second counter := 0 @@ -1638,11 +1638,11 @@ func TestResolveNewDepositsStatuses(t *testing.T) { executor.batch = providedBatchForResolve.Clone() executor.ResolveNewDepositsStatuses(uint64(0)) - assert.Equal(t, []byte{clients.Rejected, clients.Rejected}, executor.batch.Statuses) + assert.Equal(t, []byte{bridgeCommon.Rejected, bridgeCommon.Rejected}, executor.batch.Statuses) executor.batch = providedBatchForResolve.Clone() executor.batch.ResolveNewDeposits(1) - assert.Equal(t, []byte{0, clients.Rejected}, executor.batch.Statuses) + assert.Equal(t, []byte{0, bridgeCommon.Rejected}, executor.batch.Statuses) }) t.Run("equal new deposits", func(t *testing.T) { t.Parallel() @@ -1662,7 +1662,7 @@ func TestResolveNewDepositsStatuses(t *testing.T) { executor.batch = providedBatchForResolve.Clone() executor.ResolveNewDepositsStatuses(uint64(3)) - assert.Equal(t, []byte{0, 0, clients.Rejected}, executor.batch.Statuses) + assert.Equal(t, []byte{0, 0, bridgeCommon.Rejected}, executor.batch.Statuses) }) } diff --git a/bridges/ethMultiversX/steps/multiversxToEth/step06ResolveSetStatus_test.go b/bridges/ethMultiversX/steps/multiversxToEth/step06ResolveSetStatus_test.go index 8b71aae7..8ea76468 100644 --- a/bridges/ethMultiversX/steps/multiversxToEth/step06ResolveSetStatus_test.go +++ b/bridges/ethMultiversX/steps/multiversxToEth/step06ResolveSetStatus_test.go @@ -4,7 +4,6 @@ import ( "context" "testing" - "github.com/multiversx/mx-bridge-eth-go/clients" "github.com/multiversx/mx-bridge-eth-go/common" "github.com/multiversx/mx-bridge-eth-go/core" bridgeTests "github.com/multiversx/mx-bridge-eth-go/testsCommon/bridge" @@ -102,7 +101,7 @@ func TestExecute_ResolveSetStatus(t *testing.T) { t.Parallel() bridgeStub := createStubExecutorResolveSetStatus() bridgeStub.WaitAndReturnFinalBatchStatusesCalled = func(ctx context.Context) []byte { - return []byte{clients.Executed, clients.Rejected} + return []byte{common.Executed, common.Rejected} } wasCalled := false diff --git a/clients/multiversx/client_test.go b/clients/multiversx/client_test.go index 36e0ac4c..751dde73 100644 --- a/clients/multiversx/client_test.go +++ b/clients/multiversx/client_test.go @@ -614,7 +614,7 @@ func TestClient_ProposeSetStatus(t *testing.T) { proposeSetStatusFuncName, hex.EncodeToString(big.NewInt(112233).Bytes()), } - expectedStatus := []byte{clients.Rejected, clients.Executed} + expectedStatus := []byte{common.Rejected, common.Executed} for _, stat := range expectedStatus { expectedArgs = append(expectedArgs, hex.EncodeToString([]byte{stat})) } @@ -990,18 +990,15 @@ func TestClient_PerformAction(t *testing.T) { } expectedDataField := strings.Join(dataStrings, "@") assert.Equal(t, expectedDataField, dataField) - depositsString := "" extraGas := uint64(0) for _, dt := range batch.Deposits { - depositsString = depositsString + depositToString(dt) if bytes.Equal(dt.Data, []byte{parsers.MissingDataProtocolMarker}) { continue } extraGas += (uint64(len(dt.Data))*2 + 1) * args.GasMapConfig.ScCallPerByte extraGas += args.GasMapConfig.ScCallPerformForEach } - dataStrings = append(dataStrings, depositsString) expectedGasLimit := c.gasMapConfig.PerformActionBase + uint64(len(batch.Statuses))*c.gasMapConfig.PerformActionForEach expectedGasLimit += extraGas diff --git a/clients/multiversx/mxClientDataGetter_test.go b/clients/multiversx/mxClientDataGetter_test.go index 2993af67..aaf074b8 100644 --- a/clients/multiversx/mxClientDataGetter_test.go +++ b/clients/multiversx/mxClientDataGetter_test.go @@ -95,7 +95,7 @@ func createMockBatch() *common.TransferBatch { DisplayableData: "00", }, }, - Statuses: []byte{clients.Rejected, clients.Executed}, + Statuses: []byte{common.Rejected, common.Executed}, } } diff --git a/common/batch.go b/common/batch.go index 0536bdee..eeeb817d 100644 --- a/common/batch.go +++ b/common/batch.go @@ -5,7 +5,6 @@ import ( "fmt" "math/big" - "github.com/multiversx/mx-bridge-eth-go/clients" logger "github.com/multiversx/mx-chain-logger-go" ) @@ -54,11 +53,11 @@ func (tb *TransferBatch) ResolveNewDeposits(newNumDeposits int) { } for i := newNumDeposits; i < oldLen; i++ { - tb.Statuses[i] = clients.Rejected + tb.Statuses[i] = Rejected } for newNumDeposits > len(tb.Statuses) { - tb.Statuses = append(tb.Statuses, clients.Rejected) + tb.Statuses = append(tb.Statuses, Rejected) } log.Warn("recovered num statuses", "len statuses", oldLen, "new num deposits", newNumDeposits) diff --git a/common/batch_test.go b/common/batch_test.go index f54c2bd9..421e6431 100644 --- a/common/batch_test.go +++ b/common/batch_test.go @@ -4,7 +4,6 @@ import ( "math/big" "testing" - "github.com/multiversx/mx-bridge-eth-go/clients" "github.com/stretchr/testify/assert" ) @@ -79,7 +78,7 @@ func TestTransferBatch_Clone(t *testing.T) { Data: []byte("tx data"), }, }, - Statuses: []byte{clients.Executed, clients.Rejected}, + Statuses: []byte{Executed, Rejected}, } cloned := tb.Clone() @@ -115,7 +114,7 @@ func TestTransferBatch_String(t *testing.T) { Amount: big.NewInt(5566), }, }, - Statuses: []byte{clients.Executed, clients.Rejected}, + Statuses: []byte{Executed, Rejected}, } expectedString := `Batch id 2243: @@ -145,11 +144,11 @@ func TestTransferBatch_ResolveNewDeposits(t *testing.T) { workingBatch := batch.Clone() workingBatch.ResolveNewDeposits(0) - assert.Equal(t, []byte{clients.Rejected, clients.Rejected}, workingBatch.Statuses) + assert.Equal(t, []byte{Rejected, Rejected}, workingBatch.Statuses) workingBatch = batch.Clone() workingBatch.ResolveNewDeposits(1) - assert.Equal(t, []byte{0, clients.Rejected}, workingBatch.Statuses) + assert.Equal(t, []byte{0, Rejected}, workingBatch.Statuses) }) t.Run("equal new deposits", func(t *testing.T) { t.Parallel() @@ -163,6 +162,6 @@ func TestTransferBatch_ResolveNewDeposits(t *testing.T) { workingBatch := batch.Clone() workingBatch.ResolveNewDeposits(3) - assert.Equal(t, []byte{0, 0, clients.Rejected}, workingBatch.Statuses) + assert.Equal(t, []byte{0, 0, Rejected}, workingBatch.Statuses) }) } diff --git a/clients/constants.go b/common/constants.go similarity index 90% rename from clients/constants.go rename to common/constants.go index d03f2b2f..aa7b9dd8 100644 --- a/clients/constants.go +++ b/common/constants.go @@ -1,4 +1,4 @@ -package clients +package common const ( // Executed is the Executed with success status value diff --git a/integrationTests/mock/multiversXContractStateMock.go b/integrationTests/mock/multiversXContractStateMock.go index d6d11b17..8c69bd34 100644 --- a/integrationTests/mock/multiversXContractStateMock.go +++ b/integrationTests/mock/multiversXContractStateMock.go @@ -1,6 +1,7 @@ package mock import ( + "encoding/binary" "encoding/hex" "fmt" "math/big" @@ -8,6 +9,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/multiversx/mx-bridge-eth-go/integrationTests" + "github.com/multiversx/mx-bridge-eth-go/parsers" "github.com/multiversx/mx-chain-core-go/core" "github.com/multiversx/mx-chain-core-go/data/transaction" "github.com/multiversx/mx-chain-core-go/data/vm" @@ -15,6 +17,13 @@ import ( "github.com/multiversx/mx-sdk-go/data" ) +const ( + lenToField = 32 + lenFromField = 20 + lenUint32 = 4 + lenUint64 = 8 +) + type multiversXProposedStatus struct { BatchId *big.Int Statuses []byte @@ -170,46 +179,11 @@ func (mock *multiversXContractStateMock) createProposedTransfer(dataSplit []stri BatchId: big.NewInt(0).SetBytes(buff), } - currentIndex := 2 - for currentIndex < len(dataSplit) { - from, errDecode := hex.DecodeString(dataSplit[currentIndex]) - if errDecode != nil { - panic(errDecode) - } - - to, errDecode := hex.DecodeString(dataSplit[currentIndex+1]) - if errDecode != nil { - panic(errDecode) - } - - amountBytes, errDecode := hex.DecodeString(dataSplit[currentIndex+3]) - if errDecode != nil { - panic(errDecode) - } - - nonceBytes, errDecode := hex.DecodeString(dataSplit[currentIndex+4]) - if errDecode != nil { - panic(errDecode) - } - - dataBytes, errDecode := hex.DecodeString(dataSplit[currentIndex+5]) - if errDecode != nil { - panic(errDecode) - } - - t := Transfer{ - From: from, - To: to, - Token: dataSplit[currentIndex+2], - Amount: big.NewInt(0).SetBytes(amountBytes), - Nonce: big.NewInt(0).SetBytes(nonceBytes), - Data: dataBytes, - } - - indexIncrementValue := 6 - + depositsBuff, _ := hex.DecodeString(dataSplit[2]) + for len(depositsBuff) > 0 { + var t Transfer + t, depositsBuff = mock.decodeDeposit(depositsBuff) transfer.Transfers = append(transfer.Transfers, t) - currentIndex += indexIncrementValue } hash, err := core.CalculateHash(integrationTests.TestMarshalizer, integrationTests.TestHasher, transfer) @@ -223,6 +197,49 @@ func (mock *multiversXContractStateMock) createProposedTransfer(dataSplit []stri return transfer, string(hash) } +func (mock *multiversXContractStateMock) decodeDeposit(buff []byte) (Transfer, []byte) { + from := buff[:lenFromField] + buff = buff[lenFromField:] + + to := buff[:lenToField] + buff = buff[lenToField:] + + lenTokenBytes := buff[:lenUint32] + buff = buff[lenUint32:] + lenToken := int(binary.BigEndian.Uint32(lenTokenBytes)) + token := buff[:lenToken] + buff = buff[lenToken:] + + lenAmountBytes := buff[:lenUint32] + buff = buff[lenUint32:] + lenAmount := int(binary.BigEndian.Uint32(lenAmountBytes)) + amount := big.NewInt(0).SetBytes(buff[:lenAmount]) + buff = buff[lenAmount:] + + nonceBytes := buff[:lenUint64] + buff = buff[lenUint64:] + nonce := binary.BigEndian.Uint64(nonceBytes) + + codec := &parsers.MultiversxCodec{} + callData, _ := codec.DecodeCallData(buff) + var reconstructedCallData []byte + if callData.Type == parsers.MissingDataProtocolMarker { + reconstructedCallData = []byte{parsers.MissingDataProtocolMarker} + } else { + reconstructedCallData = codec.EncodeCallData(callData) + } + buff = buff[len(reconstructedCallData):] + + return Transfer{ + Nonce: big.NewInt(0).SetUint64(nonce), + To: to, + From: from, + Token: hex.EncodeToString(token), + Amount: amount, + Data: reconstructedCallData, + }, buff +} + func (mock *multiversXContractStateMock) processVmRequests(vmRequest *data.VmValueRequest) (*data.VmValuesResponseData, error) { if vmRequest == nil { panic("vmRequest is nil") diff --git a/integrationTests/relayers/ethToMultiversX_test.go b/integrationTests/relayers/ethToMultiversX_test.go index 401e5e2a..d9bbaabe 100644 --- a/integrationTests/relayers/ethToMultiversX_test.go +++ b/integrationTests/relayers/ethToMultiversX_test.go @@ -12,8 +12,8 @@ import ( "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" - "github.com/multiversx/mx-bridge-eth-go/clients" "github.com/multiversx/mx-bridge-eth-go/clients/ethereum/contract" + bridgeCommon "github.com/multiversx/mx-bridge-eth-go/common" "github.com/multiversx/mx-bridge-eth-go/config" "github.com/multiversx/mx-bridge-eth-go/core" "github.com/multiversx/mx-bridge-eth-go/factory" @@ -35,8 +35,6 @@ type argsForSCCallsTest struct { } func TestRelayersShouldExecuteTransfersFromEthToMultiversX(t *testing.T) { - t.Skip("TODO: fix") // TODO(jls): fix - if testing.Short() { t.Skip("this is not a short test") } @@ -133,7 +131,7 @@ func testRelayersShouldExecuteTransfersFromEthToMultiversX(t *testing.T, withNat multiversXChainMock.SetLastExecutedEthBatchID(batchNonceOnEthereum) multiversXChainMock.SetLastExecutedEthTxId(txNonceOnEthereum) multiversXChainMock.GetStatusesAfterExecutionHandler = func() []byte { - return []byte{clients.Executed, clients.Rejected} + return []byte{bridgeCommon.Executed, bridgeCommon.Rejected} } multiversXChainMock.SetQuorum(numRelayers) @@ -193,8 +191,6 @@ func testRelayersShouldExecuteTransfersFromEthToMultiversX(t *testing.T, withNat } func TestRelayersShouldExecuteTransferFromEthToMultiversXHavingTxsWithSCcalls(t *testing.T) { - t.Skip("TODO: fix") // TODO(jls): fix - if testing.Short() { t.Skip("this is not a short test") } @@ -327,7 +323,7 @@ func testRelayersShouldExecuteTransferFromEthToMultiversXHavingTxsWithSCcalls(t multiversXChainMock.SetLastExecutedEthBatchID(batchNonceOnEthereum) multiversXChainMock.SetLastExecutedEthTxId(txNonceOnEthereum) multiversXChainMock.GetStatusesAfterExecutionHandler = func() []byte { - return []byte{clients.Executed, clients.Rejected, clients.Executed} + return []byte{bridgeCommon.Executed, bridgeCommon.Rejected, bridgeCommon.Executed} } multiversXChainMock.SetQuorum(numRelayers) diff --git a/integrationTests/relayers/multiversXToEth_test.go b/integrationTests/relayers/multiversXToEth_test.go index bc92d734..84afeb0d 100644 --- a/integrationTests/relayers/multiversXToEth_test.go +++ b/integrationTests/relayers/multiversXToEth_test.go @@ -12,7 +12,7 @@ import ( "time" "github.com/ethereum/go-ethereum/common" - "github.com/multiversx/mx-bridge-eth-go/clients" + bridgeCommon "github.com/multiversx/mx-bridge-eth-go/common" "github.com/multiversx/mx-bridge-eth-go/factory" "github.com/multiversx/mx-bridge-eth-go/integrationTests" "github.com/multiversx/mx-bridge-eth-go/integrationTests/mock" @@ -45,7 +45,7 @@ func TestRelayersShouldExecuteSimpleTransfersFromMultiversXToEth(t *testing.T) { numRelayers := 3 ethereumChainMock := mock.NewEthereumChainMock() ethereumChainMock.SetQuorum(numRelayers) - expectedStatuses := []byte{clients.Executed, clients.Rejected} + expectedStatuses := []byte{bridgeCommon.Executed, bridgeCommon.Rejected} ethereumChainMock.GetStatusesAfterExecutionHandler = func() ([]byte, bool) { if callIsFromBalanceValidator() { // statuses can not be final at this point as the batch was not executed yet @@ -142,7 +142,7 @@ func testRelayersShouldExecuteTransfersFromMultiversXToEthIfTransactionsAppearIn numRelayers := 3 ethereumChainMock := mock.NewEthereumChainMock() ethereumChainMock.SetQuorum(numRelayers) - expectedStatuses := []byte{clients.Executed, clients.Rejected} + expectedStatuses := []byte{bridgeCommon.Executed, bridgeCommon.Rejected} ethereumChainMock.GetStatusesAfterExecutionHandler = func() ([]byte, bool) { if callIsFromBalanceValidator() { // statuses can not be final at this point as the batch was not executed yet