Skip to content

Commit

Permalink
- final fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
iulianpascalau committed Aug 4, 2024
1 parent a2c1e6c commit 2519e1c
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 72 deletions.
12 changes: 6 additions & 6 deletions bridges/ethMultiversX/bridgeExecutor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions clients/multiversx/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}))
}
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion clients/multiversx/mxClientDataGetter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func createMockBatch() *common.TransferBatch {
DisplayableData: "00",
},
},
Statuses: []byte{clients.Rejected, clients.Executed},
Statuses: []byte{common.Rejected, common.Executed},
}
}

Expand Down
5 changes: 2 additions & 3 deletions common/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"math/big"

"github.com/multiversx/mx-bridge-eth-go/clients"
logger "github.com/multiversx/mx-chain-logger-go"
)

Expand Down Expand Up @@ -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)
Expand Down
11 changes: 5 additions & 6 deletions common/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"math/big"
"testing"

"github.com/multiversx/mx-bridge-eth-go/clients"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand All @@ -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)
})
}
2 changes: 1 addition & 1 deletion clients/constants.go → common/constants.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package clients
package common

const (
// Executed is the Executed with success status value
Expand Down
95 changes: 56 additions & 39 deletions integrationTests/mock/multiversXContractStateMock.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
package mock

import (
"encoding/binary"
"encoding/hex"
"fmt"
"math/big"
"strings"

"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"
sdkCore "github.com/multiversx/mx-sdk-go/core"
"github.com/multiversx/mx-sdk-go/data"
)

const (
lenToField = 32
lenFromField = 20
lenUint32 = 4
lenUint64 = 8
)

type multiversXProposedStatus struct {
BatchId *big.Int
Statuses []byte
Expand Down Expand Up @@ -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)
Expand All @@ -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")
Expand Down
10 changes: 3 additions & 7 deletions integrationTests/relayers/ethToMultiversX_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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")
}
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions integrationTests/relayers/multiversXToEth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 2519e1c

Please sign in to comment.