Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relayer: Sending of XRPL originated toke from the coreum back to the XRPL implementation #46

Merged
merged 7 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions contract/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub enum ExecuteMsg {
SaveEvidence {
evidence: Evidence,
},
#[serde(rename = "send_to_xrpl")]
SendToXRPL {
recipient: String,
},
Expand Down
371 changes: 322 additions & 49 deletions integration-tests/coreum/contract_client_test.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion integration-tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ require (
github.com/cosmos/cosmos-sdk v0.47.5
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/gogoproto v1.4.10
github.com/cosmos/gogoproto v1.4.10 // indirect
github.com/cosmos/iavl v0.20.0 // indirect
github.com/cosmos/ibc-go/v7 v7.2.0 // indirect
github.com/cosmos/ics23/go v0.10.0 // indirect
Expand Down
40 changes: 38 additions & 2 deletions integration-tests/processes/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,15 +252,18 @@ func (r *RunnerEnv) AllocateTickets(
require.Len(t, availableTickets, int(numberOfTicketsToAllocate))
}

// RegisterXRPLTokenAndAwaitTrustSet registers XRPL currency and awaits for the trust set ot be set.
func (r *RunnerEnv) RegisterXRPLTokenAndAwaitTrustSet(
// RegisterXRPLOriginatedToken registers XRPL currency and awaits for the trust set ot be set.
func (r *RunnerEnv) RegisterXRPLOriginatedToken(
ctx context.Context,
t *testing.T,
issuer rippledata.Account,
currency rippledata.Currency,
sendingPrecision int32,
maxHoldingAmount sdkmath.Int,
) coreum.XRPLToken {
r.Chains.Coreum.FundAccountWithOptions(ctx, t, r.ContractOwner, coreumintegration.BalancesOptions{
Amount: r.Chains.Coreum.QueryAssetFTParams(ctx, t).IssueFee.Amount,
})
_, err := r.ContractClient.RegisterXRPLToken(ctx, r.ContractOwner, issuer.String(), xrpl.ConvertCurrencyToString(currency), sendingPrecision, maxHoldingAmount)
require.NoError(t, err)
// await for the trust set
Expand Down Expand Up @@ -325,6 +328,39 @@ func (r *RunnerEnv) SendXRPLPartialPaymentTx(
require.NoError(t, r.Chains.XRPL.AutoFillSignAndSubmitTx(ctx, t, &xrpPaymentTx, senderAcc))
}

func (r *RunnerEnv) EnableXRPLAccountRippling(ctx context.Context, t *testing.T, account rippledata.Account) {
// enable rippling on this account's trust lines.
accountSetTx := rippledata.AccountSet{
SetFlag: lo.ToPtr(uint32(rippledata.TxDefaultRipple)),
TxBase: rippledata.TxBase{
TransactionType: rippledata.ACCOUNT_SET,
},
}
require.NoError(t, r.Chains.XRPL.AutoFillSignAndSubmitTx(ctx, t, &accountSetTx, account))
}

func (r *RunnerEnv) SendXRPLMaxTrustSetTx(
ctx context.Context,
t *testing.T,
account rippledata.Account,
issuer rippledata.Account,
currency rippledata.Currency,
) {
value, err := rippledata.NewValue("1000000000000", false)
require.NoError(t, err)
trustSetTx := rippledata.TrustSet{
LimitAmount: rippledata.Amount{
Value: value,
Currency: currency,
Issuer: issuer,
},
TxBase: rippledata.TxBase{
TransactionType: rippledata.TRUST_SET,
},
}
require.NoError(t, r.Chains.XRPL.AutoFillSignAndSubmitTx(ctx, t, &trustSetTx, account))
}

func genCoreumRelayers(
ctx context.Context,
t *testing.T,
Expand Down
205 changes: 205 additions & 0 deletions integration-tests/processes/send_from_coreum_to_xrpl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
//go:build integrationtests
// +build integrationtests

package processes_test

import (
"encoding/hex"
"strings"
"testing"
"time"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
rippledata "github.com/rubblelabs/ripple/data"
"github.com/stretchr/testify/require"

coreumintegration "github.com/CoreumFoundation/coreum/v3/testutil/integration"
integrationtests "github.com/CoreumFoundation/coreumbridge-xrpl/integration-tests"
"github.com/CoreumFoundation/coreumbridge-xrpl/relayer/coreum"
"github.com/CoreumFoundation/coreumbridge-xrpl/relayer/xrpl"
)

func TestRegisterXRPLOriginatedTokensSendFromXRPLToCoreumAndBack(t *testing.T) {
t.Parallel()

ctx, chains := integrationtests.NewTestingContext(t)

envCfg := DefaultRunnerEnvConfig()
runnerEnv := NewRunnerEnv(ctx, t, envCfg, chains)
runnerEnv.StartAllRunnerProcesses(ctx, t)
runnerEnv.AllocateTickets(ctx, t, uint32(200))

coreumSender := chains.Coreum.GenAccount()
chains.Coreum.FundAccountWithOptions(ctx, t, coreumSender, coreumintegration.BalancesOptions{
Amount: sdkmath.NewIntFromUint64(1_000_000),
})
t.Logf("Coreum sender: %s", coreumSender.String())
xrplRecipientAddress := chains.XRPL.GenAccount(ctx, t, 0)
t.Logf("XRPL recipient: %s", xrplRecipientAddress.String())

registeredXRPLCurrency, err := rippledata.NewCurrency("RCP")
require.NoError(t, err)

xrplIssuerAddress := chains.XRPL.GenAccount(ctx, t, 1)
// enable to be able to send to any address
runnerEnv.EnableXRPLAccountRippling(ctx, t, xrplIssuerAddress)
registeredXRPLToken := runnerEnv.RegisterXRPLOriginatedToken(ctx, t, xrplIssuerAddress, registeredXRPLCurrency, int32(6), integrationtests.ConvertStringWithDecimalsToSDKInt(t, "1", 30))

valueToSendFromXRPLtoCoreum, err := rippledata.NewValue("1e10", false)
require.NoError(t, err)
amountToSendFromXRPLtoCoreum := rippledata.Amount{
Value: valueToSendFromXRPLtoCoreum,
Currency: registeredXRPLCurrency,
Issuer: xrplIssuerAddress,
}
memo, err := xrpl.EncodeCoreumRecipientToMemo(coreumSender)
require.NoError(t, err)

runnerEnv.SendXRPLPaymentTx(ctx, t, xrplIssuerAddress, runnerEnv.bridgeXRPLAddress, amountToSendFromXRPLtoCoreum, memo)
runnerEnv.AwaitCoreumBalance(ctx, t, chains.Coreum, coreumSender, sdk.NewCoin(registeredXRPLToken.CoreumDenom, integrationtests.ConvertStringWithDecimalsToSDKInt(t, valueToSendFromXRPLtoCoreum.String(), XRPLTokenDecimals)))

// send the full amount in 4 transactions to XRPL
amountToSend := integrationtests.ConvertStringWithDecimalsToSDKInt(t, valueToSendFromXRPLtoCoreum.String(), XRPLTokenDecimals).QuoRaw(4)

// send 2 transactions without the trust set to be reverted
// TODO(dzmitryhil) update assertion once we add the final tx revert/recovery
_, err = runnerEnv.ContractClient.SendToXRPL(ctx, coreumSender, xrplRecipientAddress.String(), sdk.NewCoin(registeredXRPLToken.CoreumDenom, amountToSend))
require.NoError(t, err)
_, err = runnerEnv.ContractClient.SendToXRPL(ctx, coreumSender, xrplRecipientAddress.String(), sdk.NewCoin(registeredXRPLToken.CoreumDenom, amountToSend))
require.NoError(t, err)
runnerEnv.AwaitNoPendingOperations(ctx, t)

// send TrustSet to be able to receive coins
runnerEnv.SendXRPLMaxTrustSetTx(ctx, t, xrplRecipientAddress, xrplIssuerAddress, registeredXRPLCurrency)

_, err = runnerEnv.ContractClient.SendToXRPL(ctx, coreumSender, xrplRecipientAddress.String(), sdk.NewCoin(registeredXRPLToken.CoreumDenom, amountToSend))
require.NoError(t, err)
_, err = runnerEnv.ContractClient.SendToXRPL(ctx, coreumSender, xrplRecipientAddress.String(), sdk.NewCoin(registeredXRPLToken.CoreumDenom, amountToSend))
require.NoError(t, err)
runnerEnv.AwaitNoPendingOperations(ctx, t)

balance := runnerEnv.Chains.XRPL.GetAccountBalance(ctx, t, xrplRecipientAddress, xrplIssuerAddress, registeredXRPLCurrency)
require.Equal(t, "5000000000", balance.Value.String())
}

func TestSendFromXRPLToCoreumWithMaliciousRelayer(t *testing.T) {
t.Parallel()

ctx, chains := integrationtests.NewTestingContext(t)

envCfg := DefaultRunnerEnvConfig()
envCfg.MaliciousRelayerNumber = 1
runnerEnv := NewRunnerEnv(ctx, t, envCfg, chains)
runnerEnv.StartAllRunnerProcesses(ctx, t)
runnerEnv.AllocateTickets(ctx, t, uint32(200))

coreumSender := chains.Coreum.GenAccount()
chains.Coreum.FundAccountWithOptions(ctx, t, coreumSender, coreumintegration.BalancesOptions{
Amount: sdkmath.NewIntFromUint64(1_000_000),
})
t.Logf("Coreum sender: %s", coreumSender.String())
xrplRecipientAddress := chains.XRPL.GenAccount(ctx, t, 0)
t.Logf("XRPL recipient: %s", xrplRecipientAddress.String())

currencyHexSymbol := hex.EncodeToString([]byte(strings.Repeat("X", 20)))
registeredXRPLCurrency, err := rippledata.NewCurrency(currencyHexSymbol)
require.NoError(t, err)

xrplIssuerAddress := chains.XRPL.GenAccount(ctx, t, 1)
// enable to be able to send to any address
runnerEnv.EnableXRPLAccountRippling(ctx, t, xrplIssuerAddress)
registeredXRPLToken := runnerEnv.RegisterXRPLOriginatedToken(ctx, t, xrplIssuerAddress, registeredXRPLCurrency, int32(6), integrationtests.ConvertStringWithDecimalsToSDKInt(t, "1", 30))

valueToSendFromXRPLtoCoreum, err := rippledata.NewValue("1e10", false)
require.NoError(t, err)
amountToSendFromXRPLtoCoreum := rippledata.Amount{
Value: valueToSendFromXRPLtoCoreum,
Currency: registeredXRPLCurrency,
Issuer: xrplIssuerAddress,
}
memo, err := xrpl.EncodeCoreumRecipientToMemo(coreumSender)
require.NoError(t, err)

runnerEnv.SendXRPLPaymentTx(ctx, t, xrplIssuerAddress, runnerEnv.bridgeXRPLAddress, amountToSendFromXRPLtoCoreum, memo)
runnerEnv.AwaitCoreumBalance(ctx, t, chains.Coreum, coreumSender, sdk.NewCoin(registeredXRPLToken.CoreumDenom, integrationtests.ConvertStringWithDecimalsToSDKInt(t, valueToSendFromXRPLtoCoreum.String(), XRPLTokenDecimals)))

// send TrustSet to be able to receive coins
runnerEnv.SendXRPLMaxTrustSetTx(ctx, t, xrplRecipientAddress, xrplIssuerAddress, registeredXRPLCurrency)

amountToSend := integrationtests.ConvertStringWithDecimalsToSDKInt(t, valueToSendFromXRPLtoCoreum.String(), XRPLTokenDecimals).QuoRaw(4)
_, err = runnerEnv.ContractClient.SendToXRPL(ctx, coreumSender, xrplRecipientAddress.String(), sdk.NewCoin(registeredXRPLToken.CoreumDenom, amountToSend))
require.NoError(t, err)
runnerEnv.AwaitNoPendingOperations(ctx, t)

balance := runnerEnv.Chains.XRPL.GetAccountBalance(ctx, t, xrplRecipientAddress, xrplIssuerAddress, registeredXRPLCurrency)
require.Equal(t, "2500000000", balance.Value.String())
}

func TestSendFromXRPLToCoreumWithTicketsReallocation(t *testing.T) {
t.Parallel()

ctx, chains := integrationtests.NewTestingContext(t)

envCfg := DefaultRunnerEnvConfig()
envCfg.UsedTicketSequenceThreshold = 3
runnerEnv := NewRunnerEnv(ctx, t, envCfg, chains)
runnerEnv.StartAllRunnerProcesses(ctx, t)
runnerEnv.AllocateTickets(ctx, t, uint32(5))
sendingCount := 10

coreumSender := chains.Coreum.GenAccount()
chains.Coreum.FundAccountWithOptions(ctx, t, coreumSender, coreumintegration.BalancesOptions{
Amount: sdkmath.NewIntFromUint64(1_000_000),
})
t.Logf("Coreum sender: %s", coreumSender.String())
xrplRecipientAddress := chains.XRPL.GenAccount(ctx, t, 0)
t.Logf("XRPL recipient: %s", xrplRecipientAddress.String())

currencyHexSymbol := hex.EncodeToString([]byte(strings.Repeat("X", 20)))
registeredXRPLCurrency, err := rippledata.NewCurrency(currencyHexSymbol)
require.NoError(t, err)

xrplIssuerAddress := chains.XRPL.GenAccount(ctx, t, 1)
// enable to be able to send to any address
runnerEnv.EnableXRPLAccountRippling(ctx, t, xrplIssuerAddress)
registeredXRPLToken := runnerEnv.RegisterXRPLOriginatedToken(ctx, t, xrplIssuerAddress, registeredXRPLCurrency, int32(6), integrationtests.ConvertStringWithDecimalsToSDKInt(t, "1", 30))

valueToSendFromXRPLtoCoreum, err := rippledata.NewValue("1e10", false)
require.NoError(t, err)
amountToSendFromXRPLtoCoreum := rippledata.Amount{
Value: valueToSendFromXRPLtoCoreum,
Currency: registeredXRPLCurrency,
Issuer: xrplIssuerAddress,
}
memo, err := xrpl.EncodeCoreumRecipientToMemo(coreumSender)
require.NoError(t, err)

runnerEnv.SendXRPLPaymentTx(ctx, t, xrplIssuerAddress, runnerEnv.bridgeXRPLAddress, amountToSendFromXRPLtoCoreum, memo)
runnerEnv.AwaitCoreumBalance(ctx, t, chains.Coreum, coreumSender, sdk.NewCoin(registeredXRPLToken.CoreumDenom, integrationtests.ConvertStringWithDecimalsToSDKInt(t, valueToSendFromXRPLtoCoreum.String(), XRPLTokenDecimals)))

// send TrustSet to be able to receive coins
runnerEnv.SendXRPLMaxTrustSetTx(ctx, t, xrplRecipientAddress, xrplIssuerAddress, registeredXRPLCurrency)

totalSent := sdkmath.ZeroInt()
amountToSend := integrationtests.ConvertStringWithDecimalsToSDKInt(t, "10", XRPLTokenDecimals)
for i := 0; i < sendingCount; i++ {
for {
_, err = runnerEnv.ContractClient.SendToXRPL(ctx, coreumSender, xrplRecipientAddress.String(), sdk.NewCoin(registeredXRPLToken.CoreumDenom, amountToSend))
if err == nil {
break
}
if coreum.IsLastTicketReservedError(err) || coreum.IsNoAvailableTicketsError(err) {
t.Logf("No tickets left, waiting for new tickets...")
<-time.After(500 * time.Millisecond)
continue
}
require.NoError(t, err)
}
totalSent = totalSent.Add(amountToSend)
}
runnerEnv.AwaitNoPendingOperations(ctx, t)

balance := runnerEnv.Chains.XRPL.GetAccountBalance(ctx, t, xrplRecipientAddress, xrplIssuerAddress, registeredXRPLCurrency)
require.Equal(t, totalSent.Quo(sdkmath.NewIntWithDecimal(1, XRPLTokenDecimals)).String(), balance.Value.String())
}
Loading
Loading