Skip to content

Commit

Permalink
Remove write lock option from the avm rpc API (#2156)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Oct 11, 2023
1 parent 2eb6e84 commit 9dbf82a
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 5 deletions.
77 changes: 73 additions & 4 deletions vms/avm/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ func (s *Service) GetBlock(_ *http.Request, args *api.GetBlockArgs, reply *api.G
zap.Stringer("encoding", args.Encoding),
)

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

if s.vm.chainManager == nil {
return errNotLinearized
}
Expand Down Expand Up @@ -115,6 +118,9 @@ func (s *Service) GetBlockByHeight(_ *http.Request, args *api.GetBlockByHeightAr
zap.Uint64("height", uint64(args.Height)),
)

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

if s.vm.chainManager == nil {
return errNotLinearized
}
Expand Down Expand Up @@ -166,6 +172,9 @@ func (s *Service) GetHeight(_ *http.Request, _ *struct{}, reply *api.GetHeightRe
zap.String("method", "getHeight"),
)

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

if s.vm.chainManager == nil {
return errNotLinearized
}
Expand Down Expand Up @@ -196,6 +205,10 @@ func (s *Service) IssueTx(_ *http.Request, args *api.FormattedTx, reply *api.JSO
if err != nil {
return fmt.Errorf("problem decoding transaction: %w", err)
}

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

txID, err := s.vm.IssueTx(txBytes)
if err != nil {
return err
Expand Down Expand Up @@ -263,6 +276,9 @@ func (s *Service) GetAddressTxs(_ *http.Request, args *GetAddressTxsArgs, reply
zap.Uint64("pageSize", pageSize),
)

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

// Read transactions from the indexer
reply.TxIDs, err = s.vm.addressTxsIndexer.Read(address[:], assetID, cursor, pageSize)
if err != nil {
Expand Down Expand Up @@ -296,6 +312,9 @@ func (s *Service) GetTxStatus(_ *http.Request, args *api.JSONTxID, reply *GetTxS
return errNilTxID
}

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

_, err := s.vm.state.GetTx(args.TxID)
switch err {
case nil:
Expand All @@ -320,6 +339,9 @@ func (s *Service) GetTx(_ *http.Request, args *api.GetTxArgs, reply *api.GetTxRe
return errNilTxID
}

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

tx, err := s.vm.state.GetTx(args.TxID)
if err != nil {
return err
Expand Down Expand Up @@ -399,6 +421,10 @@ func (s *Service) GetUTXOs(_ *http.Request, args *api.GetUTXOsArgs, reply *api.G
if limit <= 0 || int(maxPageSize) < limit {
limit = int(maxPageSize)
}

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

if sourceChain == s.vm.ctx.ChainID {
utxos, endAddr, endUTXOID, err = avax.GetPaginatedUTXOs(
s.vm.state,
Expand Down Expand Up @@ -471,6 +497,9 @@ func (s *Service) GetAssetDescription(_ *http.Request, args *GetAssetDescription
return err
}

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

tx, err := s.vm.state.GetTx(assetID)
if err != nil {
return err
Expand Down Expand Up @@ -524,8 +553,10 @@ func (s *Service) GetBalance(_ *http.Request, args *GetBalanceArgs, reply *GetBa
return err
}

addrSet := set.Set[ids.ShortID]{}
addrSet.Add(addr)
addrSet := set.Of(addr)

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

utxos, err := avax.GetAllUTXOs(s.vm.state, addrSet)
if err != nil {
Expand Down Expand Up @@ -592,8 +623,10 @@ func (s *Service) GetAllBalances(_ *http.Request, args *GetAllBalancesArgs, repl
if err != nil {
return fmt.Errorf("problem parsing address '%s': %w", args.Address, err)
}
addrSet := set.Set[ids.ShortID]{}
addrSet.Add(address)
addrSet := set.Of(address)

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

utxos, err := avax.GetAllUTXOs(s.vm.state, addrSet)
if err != nil {
Expand Down Expand Up @@ -687,6 +720,9 @@ func (s *Service) CreateAsset(_ *http.Request, args *CreateAssetArgs, reply *Ass
return err
}

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

// Get the UTXOs/keys for the from addresses
utxos, kc, err := s.vm.LoadUser(args.Username, args.Password, fromAddrs)
if err != nil {
Expand Down Expand Up @@ -842,6 +878,9 @@ func (s *Service) CreateNFTAsset(_ *http.Request, args *CreateNFTAssetArgs, repl
return err
}

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

// Get the UTXOs/keys for the from addresses
utxos, kc, err := s.vm.LoadUser(args.Username, args.Password, fromAddrs)
if err != nil {
Expand Down Expand Up @@ -938,6 +977,9 @@ func (s *Service) CreateAddress(_ *http.Request, args *api.UserPass, reply *api.
logging.UserString("username", args.Username),
)

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

user, err := keystore.NewUserFromKeystore(s.vm.ctx.Keystore, args.Username, args.Password)
if err != nil {
return err
Expand Down Expand Up @@ -967,6 +1009,9 @@ func (s *Service) ListAddresses(_ *http.Request, args *api.UserPass, response *a
logging.UserString("username", args.Username),
)

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

user, err := keystore.NewUserFromKeystore(s.vm.ctx.Keystore, args.Username, args.Password)
if err != nil {
return err
Expand Down Expand Up @@ -1019,6 +1064,9 @@ func (s *Service) ExportKey(_ *http.Request, args *ExportKeyArgs, reply *ExportK
return fmt.Errorf("problem parsing address %q: %w", args.Address, err)
}

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

user, err := keystore.NewUserFromKeystore(s.vm.ctx.Keystore, args.Username, args.Password)
if err != nil {
return err
Expand Down Expand Up @@ -1058,6 +1106,9 @@ func (s *Service) ImportKey(_ *http.Request, args *ImportKeyArgs, reply *api.JSO
return errMissingPrivateKey
}

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

user, err := keystore.NewUserFromKeystore(s.vm.ctx.Keystore, args.Username, args.Password)
if err != nil {
return err
Expand Down Expand Up @@ -1144,6 +1195,9 @@ func (s *Service) SendMultiple(_ *http.Request, args *SendMultipleArgs, reply *a
return err
}

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

// Load user's UTXOs/keys
utxos, kc, err := s.vm.LoadUser(args.Username, args.Password, fromAddrs)
if err != nil {
Expand Down Expand Up @@ -1302,6 +1356,9 @@ func (s *Service) Mint(_ *http.Request, args *MintArgs, reply *api.JSONTxIDChang
return err
}

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

// Get the UTXOs/keys for the from addresses
feeUTXOs, feeKc, err := s.vm.LoadUser(args.Username, args.Password, fromAddrs)
if err != nil {
Expand Down Expand Up @@ -1419,6 +1476,9 @@ func (s *Service) SendNFT(_ *http.Request, args *SendNFTArgs, reply *api.JSONTxI
return err
}

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

// Get the UTXOs/keys for the from addresses
utxos, kc, err := s.vm.LoadUser(args.Username, args.Password, fromAddrs)
if err != nil {
Expand Down Expand Up @@ -1535,6 +1595,9 @@ func (s *Service) MintNFT(_ *http.Request, args *MintNFTArgs, reply *api.JSONTxI
return err
}

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

// Get the UTXOs/keys for the from addresses
feeUTXOs, feeKc, err := s.vm.LoadUser(args.Username, args.Password, fromAddrs)
if err != nil {
Expand Down Expand Up @@ -1651,6 +1714,9 @@ func (s *Service) Import(_ *http.Request, args *ImportArgs, reply *api.JSONTxID)
return fmt.Errorf("problem parsing to address %q: %w", args.To, err)
}

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

utxos, kc, err := s.vm.LoadUser(args.Username, args.Password, nil)
if err != nil {
return err
Expand Down Expand Up @@ -1793,6 +1859,9 @@ func (s *Service) Export(_ *http.Request, args *ExportArgs, reply *api.JSONTxIDC
return err
}

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

// Get the UTXOs/keys for the from addresses
utxos, kc, err := s.vm.LoadUser(args.Username, args.Password, fromAddrs)
if err != nil {
Expand Down
Loading

0 comments on commit 9dbf82a

Please sign in to comment.