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

Remove write lock option from the avm wallet API #2155

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion vms/avm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ func (vm *VM) CreateHandlers(context.Context) (map[string]*common.HTTPHandler, e

return map[string]*common.HTTPHandler{
"": {Handler: rpcServer},
"/wallet": {Handler: walletServer},
"/wallet": {LockOptions: common.NoLock, Handler: walletServer},
"/events": {LockOptions: common.NoLock, Handler: vm.pubsub},
}, err
}
Expand Down
7 changes: 7 additions & 0 deletions vms/avm/wallet_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ func (w *WalletService) IssueTx(_ *http.Request, args *api.FormattedTx, reply *a
if err != nil {
return fmt.Errorf("problem decoding transaction: %w", err)
}

w.vm.ctx.Lock.Lock()
defer w.vm.ctx.Lock.Unlock()

txID, err := w.issue(txBytes)
reply.TxID = txID
return err
Expand Down Expand Up @@ -179,6 +183,9 @@ func (w *WalletService) SendMultiple(_ *http.Request, args *SendMultipleArgs, re
return fmt.Errorf("couldn't parse 'From' addresses: %w", err)
}

w.vm.ctx.Lock.Lock()
defer w.vm.ctx.Lock.Unlock()

// Load user's UTXOs/keys
utxos, kc, err := w.vm.LoadUser(args.Username, args.Password, fromAddrs)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions vms/avm/wallet_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func TestWalletService_SendMultiple(t *testing.T) {
initialKeys: keys,
}},
})
env.vm.ctx.Lock.Unlock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd move it just before env.walletService.SendMultiple to signal unlocking is needed just to issue that call

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like it's weird to hold the lock longer than it is needed. Having the unlock here shows that locking isn't needed until we call buildAndAccept


defer func() {
require.NoError(env.vm.Shutdown(context.Background()))
env.vm.ctx.Lock.Unlock()
Expand Down Expand Up @@ -65,6 +67,8 @@ func TestWalletService_SendMultiple(t *testing.T) {
require.NoError(env.walletService.SendMultiple(nil, args, reply))
require.Equal(changeAddrStr, reply.ChangeAddr)

env.vm.ctx.Lock.Lock()

buildAndAccept(require, env.vm, env.issuer, reply.TxID)

_, err = env.vm.state.GetTx(reply.TxID)
Expand Down
Loading