Skip to content

Commit

Permalink
feat: add validation for quote hash in RefundUserPegOut
Browse files Browse the repository at this point in the history
 - add tests
  • Loading branch information
AndresQuijano committed Nov 22, 2024
1 parent 1a6b5b9 commit def51c3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cmd/utils/refund_user_pegout/refund_user_pegout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func TestReadRefundUserPegOutScriptInput(t *testing.T) {
}

func TestParseRefundUserPegOutScriptInput(t *testing.T) {
// parse is a no-op function used as a placeholder in tests since the actual parsing
// functionality is not relevant for these test cases
parse := func() {}

t.Run("should validate required fields", func(t *testing.T) {
Expand Down
9 changes: 9 additions & 0 deletions internal/adapters/dataproviders/rootstock/lbc.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,15 @@ func (lbc *liquidityBridgeContractImpl) UpdateProvider(name, url string) (string
}

func (lbc *liquidityBridgeContractImpl) RefundUserPegOut(quoteHash string) (string, error) {
// Validate the hash format
hashBytesSlice, err := hex.DecodeString(quoteHash)
if err != nil {
return "", fmt.Errorf("invalid quote hash format: %w", err)
}
if len(hashBytesSlice) != 32 {
return "", errors.New("quote hash must be 32 bytes long")
}

opts := &bind.TransactOpts{
From: lbc.signer.Address(),
Signer: lbc.signer.Sign,
Expand Down
58 changes: 55 additions & 3 deletions internal/adapters/dataproviders/rootstock/lbc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"context"
"encoding/hex"
"errors"
"math/big"
"strings"
"testing"
"time"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
geth "github.com/ethereum/go-ethereum/core/types"
Expand All @@ -19,9 +24,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"math/big"
"testing"
"time"
)

const (
Expand Down Expand Up @@ -918,6 +920,56 @@ func TestLiquidityBridgeContractImpl_RegisterPegin_ErrorHandling(t *testing.T) {
})
}

func TestLiquidityBridgeContractImpl_RefundUserPegOut(t *testing.T) {
lbcMock := &mocks.LbcAdapterMock{}
signer := &mocks.TransactionSignerMock{}
client := &mocks.RpcClientBindingMock{}
lbc := rootstock.NewLiquidityBridgeContractImpl(
rootstock.NewRskClient(client),
test.AnyAddress,
lbcMock,
signer,
rootstock.RetryParams{},
)

t.Run("should fail with invalid hash format", func(t *testing.T) {
result, err := lbc.RefundUserPegOut("invalid hash")
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid quote hash format")
assert.Empty(t, result)
})

t.Run("should fail with invalid hash length", func(t *testing.T) {
result, err := lbc.RefundUserPegOut("ab")
require.Error(t, err)
assert.Contains(t, err.Error(), "quote hash must be 32 bytes long")
assert.Empty(t, result)
})

t.Run("should fail if transaction fails", func(t *testing.T) {
validHash := strings.Repeat("aa", 32)
tx := prepareTxMocks(client, signer, false)
lbcMock.On("RefundUserPegOut", mock.Anything, mock.Anything).Return(tx, assert.AnError).Once()

result, err := lbc.RefundUserPegOut(validHash)
require.Error(t, err)
assert.Contains(t, err.Error(), "refund user peg out error")
assert.Empty(t, result)
})

t.Run("should succeed", func(t *testing.T) {
validHash := strings.Repeat("aa", 32)
tx := prepareTxMocks(client, signer, true)
lbcMock.On("RefundUserPegOut", mock.Anything, mock.Anything).Return(tx, nil).Once()

result, err := lbc.RefundUserPegOut(validHash)
require.NoError(t, err)
assert.Equal(t, tx.Hash().String(), result)

lbcMock.AssertExpectations(t)
})
}

// nolint:funlen
func TestLiquidityBridgeContractImpl_RefundPegout(t *testing.T) {
var gasLimit uint64 = 500
Expand Down

0 comments on commit def51c3

Please sign in to comment.