Skip to content

Commit ecf658c

Browse files
committed
comments
1 parent 2e1c446 commit ecf658c

File tree

6 files changed

+148
-116
lines changed

6 files changed

+148
-116
lines changed

x/confidentialtransfers/keeper/keeper.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ type Keeper interface {
2020
InitGenesis(sdk.Context, *types.GenesisState)
2121
ExportGenesis(sdk.Context) *types.GenesisState
2222

23-
GetAccount(ctx sdk.Context, address sdk.AccAddress, denom string) (types.Account, bool)
24-
SetAccount(ctx sdk.Context, address sdk.AccAddress, denom string, account types.Account)
25-
DeleteAccount(ctx sdk.Context, address sdk.AccAddress, denom string)
23+
GetAccount(ctx sdk.Context, addrString string, denom string) (types.Account, bool)
24+
SetAccount(ctx sdk.Context, addrString string, denom string, account types.Account) error
25+
DeleteAccount(ctx sdk.Context, addrString string, denom string) error
2626

2727
GetParams(ctx sdk.Context) types.Params
2828
SetParams(ctx sdk.Context, params types.Params)
@@ -71,8 +71,13 @@ func NewKeeper(
7171
}
7272
}
7373

74-
func (k BaseKeeper) GetAccount(ctx sdk.Context, address sdk.AccAddress, denom string) (types.Account, bool) {
74+
func (k BaseKeeper) GetAccount(ctx sdk.Context, addrString, denom string) (types.Account, bool) {
7575
store := ctx.KVStore(k.storeKey)
76+
address, err := sdk.AccAddressFromBech32(addrString)
77+
if err != nil {
78+
return types.Account{}, false
79+
}
80+
7681
key := types.GetAccountKey(address, denom)
7782
if !store.Has(key) {
7883
return types.Account{}, false
@@ -88,18 +93,30 @@ func (k BaseKeeper) GetAccount(ctx sdk.Context, address sdk.AccAddress, denom st
8893
return *account, true
8994
}
9095

91-
func (k BaseKeeper) SetAccount(ctx sdk.Context, address sdk.AccAddress, denom string, account types.Account) {
96+
func (k BaseKeeper) SetAccount(ctx sdk.Context, addrString, denom string, account types.Account) error {
9297
store := ctx.KVStore(k.storeKey)
98+
address, err := sdk.AccAddressFromBech32(addrString)
99+
if err != nil {
100+
return err
101+
}
102+
93103
key := types.GetAccountKey(address, denom)
94104
ctAccount := types.NewCtAccount(&account)
95105
bz := k.cdc.MustMarshal(ctAccount) // Marshal the Account object into bytes
96106
store.Set(key, bz) // Store the serialized account under the key
107+
return nil
97108
}
98109

99-
func (k BaseKeeper) DeleteAccount(ctx sdk.Context, address sdk.AccAddress, denom string) {
110+
func (k BaseKeeper) DeleteAccount(ctx sdk.Context, addrString, denom string) error {
111+
address, err := sdk.AccAddressFromBech32(addrString)
112+
if err != nil {
113+
return err
114+
}
115+
100116
store := ctx.KVStore(k.storeKey)
101117
key := types.GetAccountKey(address, denom)
102118
store.Delete(key) // Store the serialized account under the key
119+
return nil
103120
}
104121

105122
// Logger returns a logger for the x/confidentialtransfers module

x/confidentialtransfers/keeper/msg_server.go

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
sdk "github.com/cosmos/cosmos-sdk/types"
66
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
77
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/types"
8+
"github.com/sei-protocol/sei-chain/x/confidentialtransfers/utils"
89
"github.com/sei-protocol/sei-cryptography/pkg/encryption/elgamal"
910
"github.com/sei-protocol/sei-cryptography/pkg/zkproofs"
1011
"math"
@@ -25,21 +26,16 @@ var _ types.MsgServer = msgServer{}
2526
func (m msgServer) InitializeAccount(goCtx context.Context, req *types.MsgInitializeAccount) (*types.MsgInitializeAccountResponse, error) {
2627
ctx := sdk.UnwrapSDKContext(goCtx)
2728

28-
// Convert the instruction to proto. This also validates the request.
29+
// Convert the instruction from proto. This also validates the request.
2930
instruction, err := req.FromProto()
3031
if err != nil {
3132
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid msg")
3233
}
3334

3435
// Check if the account already exists
35-
address, err := sdk.AccAddressFromBech32(instruction.FromAddress)
36-
if err != nil {
37-
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid address")
38-
}
39-
40-
_, exists := m.Keeper.GetAccount(ctx, address, instruction.Denom)
36+
_, exists := m.Keeper.GetAccount(ctx, req.FromAddress, instruction.Denom)
4137
if exists {
42-
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "account already exists")
38+
return nil, sdkerrors.Wrap(sdkerrors.ErrNotFound, "account already exists")
4339
}
4440

4541
// Validate the public key
@@ -77,7 +73,10 @@ func (m msgServer) InitializeAccount(goCtx context.Context, req *types.MsgInitia
7773
}
7874

7975
// Store the account
80-
m.Keeper.SetAccount(ctx, address, req.Denom, account)
76+
err = m.Keeper.SetAccount(ctx, req.FromAddress, req.Denom, account)
77+
if err != nil {
78+
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "error setting account")
79+
}
8180

8281
ctx.EventManager().EmitEvents(sdk.Events{
8382
sdk.NewEvent(
@@ -105,9 +104,9 @@ func (m msgServer) Deposit(goCtx context.Context, req *types.MsgDeposit) (*types
105104
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid address")
106105
}
107106

108-
account, exists := m.Keeper.GetAccount(ctx, address, req.Denom)
107+
account, exists := m.Keeper.GetAccount(ctx, req.FromAddress, req.Denom)
109108
if !exists {
110-
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "account does not exist")
109+
return nil, sdkerrors.Wrap(sdkerrors.ErrNotFound, "account does not exist")
111110
}
112111

113112
// The maximum transfer amount is 2^48
@@ -131,10 +130,10 @@ func (m msgServer) Deposit(goCtx context.Context, req *types.MsgDeposit) (*types
131130

132131
// Split the deposit amount into lo and hi bits.
133132
// Extract the bottom 16 bits (rightmost 16 bits)
134-
bottom16 := uint16(req.Amount & 0xFFFF)
135-
136-
// Extract the next 32 bits (from bit 16 to bit 47) (Everything else is ignored since the max is 48 bits)
137-
next32 := uint32((req.Amount >> 16) & 0xFFFFFFFF)
133+
bottom16, next32, err := utils.SplitTransferBalance(req.Amount)
134+
if err != nil {
135+
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "error splitting transfer balance")
136+
}
138137

139138
// Compute the new balances
140139
teg := elgamal.NewTwistedElgamal()
@@ -154,7 +153,10 @@ func (m msgServer) Deposit(goCtx context.Context, req *types.MsgDeposit) (*types
154153
account.PendingBalanceCreditCounter += 1
155154

156155
// Save the changes to the account state
157-
m.Keeper.SetAccount(ctx, address, req.Denom, account)
156+
err = m.Keeper.SetAccount(ctx, req.FromAddress, req.Denom, account)
157+
if err != nil {
158+
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "error setting account")
159+
}
158160

159161
// Emit any required events
160162
ctx.EventManager().EmitEvents(sdk.Events{
@@ -179,9 +181,9 @@ func (m msgServer) Withdraw(goCtx context.Context, req *types.MsgWithdraw) (*typ
179181
}
180182

181183
// Get the user's account
182-
account, exists := m.Keeper.GetAccount(ctx, address, req.Denom)
184+
account, exists := m.Keeper.GetAccount(ctx, req.FromAddress, req.Denom)
183185
if !exists {
184-
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "account does not exist")
186+
return nil, sdkerrors.Wrap(sdkerrors.ErrNotFound, "account does not exist")
185187
}
186188

187189
// Convert the struct to a usable form. This also validates the request.
@@ -191,7 +193,8 @@ func (m msgServer) Withdraw(goCtx context.Context, req *types.MsgWithdraw) (*typ
191193
}
192194

193195
// Verify that the account has sufficient funds (Remaining balance after making the transfer is greater than or equal to zero.)
194-
// This range proof verification is performed on the RemainingBalanceCommitment sent by the user. An additional check is required to ensure that this matches the remaining balance calculated by the server.
196+
// This range proof verification is performed on the RemainingBalanceCommitment sent by the user.
197+
// An additional check is required to ensure that this matches the remaining balance calculated by the server.
195198
verified, _ := zkproofs.VerifyRangeProof(instruction.Proofs.RemainingBalanceRangeProof, instruction.RemainingBalanceCommitment, 64)
196199
if !verified {
197200
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "range proof verification failed")
@@ -204,7 +207,10 @@ func (m msgServer) Withdraw(goCtx context.Context, req *types.MsgWithdraw) (*typ
204207
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "error subtracting amount")
205208
}
206209

207-
verified = zkproofs.VerifyCiphertextCommitmentEquality(instruction.Proofs.RemainingBalanceEqualityProof, &account.PublicKey, remainingBalanceCalculated, &instruction.RemainingBalanceCommitment.C)
210+
verified = zkproofs.VerifyCiphertextCommitmentEquality(
211+
instruction.Proofs.RemainingBalanceEqualityProof,
212+
&account.PublicKey, remainingBalanceCalculated,
213+
&instruction.RemainingBalanceCommitment.C)
208214
if !verified {
209215
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "ciphertext commitment equality verification failed")
210216
}
@@ -214,7 +220,10 @@ func (m msgServer) Withdraw(goCtx context.Context, req *types.MsgWithdraw) (*typ
214220
account.AvailableBalance = remainingBalanceCalculated
215221

216222
// Save the account state
217-
m.Keeper.SetAccount(ctx, address, req.Denom, account)
223+
err = m.Keeper.SetAccount(ctx, req.FromAddress, req.Denom, account)
224+
if err != nil {
225+
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "error setting account")
226+
}
218227

219228
// Return the tokens to the sender
220229
coins := sdk.NewCoins(sdk.NewCoin(instruction.Denom, sdk.NewIntFromUint64(instruction.Amount)))
@@ -239,14 +248,9 @@ func (m msgServer) ApplyPendingBalance(goCtx context.Context, req *types.MsgAppl
239248
ctx := sdk.UnwrapSDKContext(goCtx)
240249

241250
// Check if the account exists
242-
address, err := sdk.AccAddressFromBech32(req.Address)
243-
if err != nil {
244-
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid address")
245-
}
246-
247-
account, exists := m.Keeper.GetAccount(ctx, address, req.Denom)
251+
account, exists := m.Keeper.GetAccount(ctx, req.Address, req.Denom)
248252
if !exists {
249-
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "account does not exist")
253+
return nil, sdkerrors.Wrap(sdkerrors.ErrNotFound, "account does not exist")
250254
}
251255

252256
if account.PendingBalanceCreditCounter == 0 {
@@ -277,7 +281,7 @@ func (m msgServer) ApplyPendingBalance(goCtx context.Context, req *types.MsgAppl
277281
account.PendingBalanceCreditCounter = 0
278282

279283
// Save the changes to the account state
280-
m.Keeper.SetAccount(ctx, address, req.Denom, account)
284+
m.Keeper.SetAccount(ctx, req.Address, req.Denom, account)
281285

282286
// Emit any required events
283287
ctx.EventManager().EmitEvents(sdk.Events{
@@ -295,14 +299,9 @@ func (m msgServer) CloseAccount(goCtx context.Context, req *types.MsgCloseAccoun
295299
ctx := sdk.UnwrapSDKContext(goCtx)
296300

297301
// Check if the account exists
298-
address, err := sdk.AccAddressFromBech32(req.Address)
299-
if err != nil {
300-
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid address")
301-
}
302-
303-
account, exists := m.Keeper.GetAccount(ctx, address, req.Denom)
302+
account, exists := m.Keeper.GetAccount(ctx, req.Address, req.Denom)
304303
if !exists {
305-
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "account does not exist")
304+
return nil, sdkerrors.Wrap(sdkerrors.ErrNotFound, "account does not exist")
306305
}
307306

308307
instruction, err := req.FromProto()
@@ -329,7 +328,10 @@ func (m msgServer) CloseAccount(goCtx context.Context, req *types.MsgCloseAccoun
329328
}
330329

331330
// Delete the account
332-
m.Keeper.DeleteAccount(ctx, address, req.Denom)
331+
err = m.Keeper.DeleteAccount(ctx, req.Address, req.Denom)
332+
if err != nil {
333+
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "error deleting account")
334+
}
333335

334336
// Emit any required events
335337
ctx.EventManager().EmitEvents(sdk.Events{
@@ -352,24 +354,14 @@ func (m msgServer) Transfer(goCtx context.Context, req *types.MsgTransfer) (*typ
352354
}
353355

354356
// Check that sender and recipient accounts exist.
355-
senderAddress, err := sdk.AccAddressFromBech32(req.FromAddress)
356-
if err != nil {
357-
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid sender address")
358-
}
359-
360-
recipientAddress, err := sdk.AccAddressFromBech32(req.ToAddress)
361-
if err != nil {
362-
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid recipient address")
363-
}
364-
365-
senderAccount, exists := m.Keeper.GetAccount(ctx, senderAddress, req.Denom)
357+
senderAccount, exists := m.Keeper.GetAccount(ctx, req.FromAddress, req.Denom)
366358
if !exists {
367-
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "sender account does not exist")
359+
return nil, sdkerrors.Wrap(sdkerrors.ErrNotFound, "sender account does not exist")
368360
}
369361

370-
recipientAccount, exists := m.Keeper.GetAccount(ctx, recipientAddress, req.Denom)
362+
recipientAccount, exists := m.Keeper.GetAccount(ctx, req.ToAddress, req.Denom)
371363
if !exists {
372-
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "recipient account does not exist")
364+
return nil, sdkerrors.Wrap(sdkerrors.ErrNotFound, "recipient account does not exist")
373365
}
374366

375367
// Check that account does not have the maximum limit of pending transactions.
@@ -392,14 +384,10 @@ func (m msgServer) Transfer(goCtx context.Context, req *types.MsgTransfer) (*typ
392384

393385
// Validate proofs for each auditor
394386
for _, auditorParams := range instruction.Auditors {
395-
auditorAddress, err := sdk.AccAddressFromBech32(auditorParams.Address)
396-
if err != nil {
397-
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "invalid auditor address")
398-
}
399387

400-
auditorAccount, exists := m.Keeper.GetAccount(ctx, auditorAddress, instruction.Denom)
388+
auditorAccount, exists := m.Keeper.GetAccount(ctx, auditorParams.Address, instruction.Denom)
401389
if !exists {
402-
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "auditor account does not exist")
390+
return nil, sdkerrors.Wrap(sdkerrors.ErrNotFound, "auditor account does not exist")
403391
}
404392

405393
err = types.VerifyAuditorProof(
@@ -434,8 +422,15 @@ func (m msgServer) Transfer(goCtx context.Context, req *types.MsgTransfer) (*typ
434422
senderAccount.AvailableBalance = newSenderBalanceCiphertext
435423

436424
// Save the account states
437-
m.Keeper.SetAccount(ctx, senderAddress, req.Denom, senderAccount)
438-
m.Keeper.SetAccount(ctx, recipientAddress, req.Denom, recipientAccount)
425+
err = m.Keeper.SetAccount(ctx, req.FromAddress, req.Denom, senderAccount)
426+
if err != nil {
427+
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "error setting sender account")
428+
}
429+
430+
err = m.Keeper.SetAccount(ctx, req.ToAddress, req.Denom, recipientAccount)
431+
if err != nil {
432+
return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "error setting recipient account")
433+
}
439434

440435
// Emit any required events
441436
ctx.EventManager().EmitEvents(sdk.Events{

0 commit comments

Comments
 (0)