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

Add other confidential tx methods to seid #1933

Merged
merged 9 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add util tests
  • Loading branch information
mj850 committed Nov 18, 2024
commit 4a411f0ef5b0b1e5ebf7f77be4e600e2526c46fa
2 changes: 1 addition & 1 deletion x/confidentialtransfers/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ func (suite *KeeperTestSuite) TestMsgServer_DepositOversizedDeposit() {
depositStruct := types.MsgDeposit{
FromAddress: testAddr.String(),
Denom: DefaultTestDenom,
Amount: (2 << 48) + 1,
Amount: (1 << 48) + 1,
}

// Test depositing an amount larger than 48 bits.
Expand Down
5 changes: 1 addition & 4 deletions x/confidentialtransfers/types/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,7 @@ func NewMsgApplyPendingBalance(
}

// Get the pending balance by combining the lo and hi bits
pendingBalance, err := utils.CombineTransferAmount(uint16(loBalance), uint32(hiBalance))
if err != nil {
return nil, err
}
pendingBalance := utils.CombineTransferAmount(uint16(loBalance), uint32(hiBalance))

// Sum the balances to get the new available balance
newDecryptedAvailableBalance := currentBalance + pendingBalance
Expand Down
13 changes: 8 additions & 5 deletions x/confidentialtransfers/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package utils

import "errors"

// Splits some amount (maximum of 48 bit) into two parts: the bottom 16 bits and the next 32 bits
// SplitTransferBalance splits some amount (maximum of 48 bit) into two parts: the bottom 16 bits and the next 32 bits
func SplitTransferBalance(amount uint64) (uint16, uint32, error) {

if amount > uint64((2<<48)-1) {
// The maximum transfer amount is 48 bits.
maxAmount := uint64((1 << 48) - 1)

if amount > maxAmount {
return 0, 0, errors.New("amount is too large")
}

Expand All @@ -18,10 +21,10 @@ func SplitTransferBalance(amount uint64) (uint16, uint32, error) {
return bottom16, next32, nil
}

// Combines the bottom 32 bits and the next 48 bits into a single 64-bit number.
func CombineTransferAmount(bottom16 uint16, hi uint32) (uint64, error) {
// CombineTransferAmount combines the bottom 32 bits and the next 48 bits into a single 64-bit number.
func CombineTransferAmount(bottom16 uint16, hi uint32) uint64 {
// Combine the bottom 32 bits and the next 48 bits
combined := (uint64(hi) << 16) | uint64(bottom16)

return combined, nil
return combined
}
154 changes: 154 additions & 0 deletions x/confidentialtransfers/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package utils

import (
"math"
"testing"

"github.com/stretchr/testify/require"
)

// Test the SplitTransferBalance method
func TestUtils_SplitTransferBalance(t *testing.T) {
type args struct {
totalAmount uint64
expectedLo uint16
expectedHi uint32
}

testCases := []struct {
name string
args args
wantErr bool
}{
{
name: "Happy Path",
args: args{
totalAmount: 0x0000ABCD12345678,
expectedLo: 0x5678,
expectedHi: 0xABCD1234,
},
wantErr: false,
},
{
name: "Zero expected hi",
args: args{
totalAmount: 0x0000000000005678,
expectedLo: 0x5678,
expectedHi: 0x00000000,
},
wantErr: false,
},
{
name: "Zero expected lo",
args: args{
totalAmount: 0x0000123456780000,
expectedLo: 0x0000,
expectedHi: 0x12345678,
},
wantErr: false,
},
{
name: "All Zeros",
args: args{
totalAmount: 0,
expectedLo: 0,
expectedHi: 0,
},
wantErr: false,
},
{
name: "Max Amounts",
args: args{
totalAmount: 1<<48 - 1,
expectedLo: math.MaxUint16,
expectedHi: math.MaxUint32,
},
wantErr: false,
},
{
name: "Transfer amount exceeds 48 bits",
args: args{
totalAmount: 0x0001000000000000,
expectedLo: 0,
expectedHi: 0,
},
wantErr: true,
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
amountLo, amountHi, err := SplitTransferBalance(tt.args.totalAmount)
if tt.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, tt.args.expectedLo, amountLo)
require.Equal(t, tt.args.expectedHi, amountHi)
}
})
}
}

// Test the CombineTransferAmount method
func TestUtils_CombineTransferAmount(t *testing.T) {
type args struct {
expectedTotal uint64
loBits uint16
hiBits uint32
}

testCases := []struct {
name string
args args
}{
{
name: "Happy Path",
args: args{
expectedTotal: 0x0000ABCD12345678,
loBits: 0x5678,
hiBits: 0xABCD1234,
},
},
{
name: "All Zeroes",
args: args{
expectedTotal: 0x0000000000000000,
loBits: 0x0000,
hiBits: 0x00000000,
},
},
{
name: "No Lo Bits",
args: args{
expectedTotal: 0x0000123456780000,
loBits: 0x0000,
hiBits: 0x12345678,
},
},
{
name: "No Hi Bits",
args: args{
expectedTotal: 0x0000000000001234,
loBits: 0x1234,
hiBits: 0x00000000,
},
},
{
name: "Max Amounts",
args: args{
expectedTotal: 1<<48 - 1,
loBits: math.MaxUint16,
hiBits: math.MaxUint32,
},
},
}

for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
totalAmount := CombineTransferAmount(tt.args.loBits, tt.args.hiBits)

require.Equal(t, tt.args.expectedTotal, totalAmount)
})
}
}
Loading