From 265d76a393bde7503bdfebf85364cb0b03e5185a Mon Sep 17 00:00:00 2001 From: Ori Newman Date: Wed, 27 Dec 2023 14:14:50 +0200 Subject: [PATCH] small fixes --- cmd/kaspawallet/daemon/server/balance.go | 13 ++++++++----- .../daemon/server/create_unsigned_transaction.go | 7 +------ cmd/kaspawallet/daemon/server/server.go | 12 ++++++++++++ cmd/kaspawallet/daemon/server/split_transaction.go | 2 +- cmd/kaspawallet/daemon/server/sync.go | 4 ++-- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/cmd/kaspawallet/daemon/server/balance.go b/cmd/kaspawallet/daemon/server/balance.go index 429764d37f..de1b040066 100644 --- a/cmd/kaspawallet/daemon/server/balance.go +++ b/cmd/kaspawallet/daemon/server/balance.go @@ -2,6 +2,7 @@ package server import ( "context" + "github.com/pkg/errors" "github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/pb" "github.com/kaspanet/kaspad/cmd/kaspawallet/libkaspawallet" @@ -14,13 +15,15 @@ func (s *server) GetBalance(_ context.Context, _ *pb.GetBalanceRequest) (*pb.Get s.lock.RLock() defer s.lock.RUnlock() + if !s.isSynced() { + return nil, errors.Errorf("wallet daemon is not synced yet, %s", s.formatSyncStateReport()) + } + dagInfo, err := s.rpcClient.GetBlockDAGInfo() if err != nil { return nil, err } daaScore := dagInfo.VirtualDAAScore - maturity := s.params.BlockCoinbaseMaturity - balancesMap := make(balancesMapType, 0) for _, entry := range s.utxosSortedByAmount { amount := entry.UTXOEntry.Amount() @@ -30,7 +33,7 @@ func (s *server) GetBalance(_ context.Context, _ *pb.GetBalanceRequest) (*pb.Get balances = new(balancesType) balancesMap[address] = balances } - if isUTXOSpendable(entry, daaScore, maturity) { + if s.isUTXOSpendable(entry, daaScore) { balances.available += amount } else { balances.pending += amount @@ -64,9 +67,9 @@ func (s *server) GetBalance(_ context.Context, _ *pb.GetBalanceRequest) (*pb.Get }, nil } -func isUTXOSpendable(entry *walletUTXO, virtualDAAScore uint64, coinbaseMaturity uint64) bool { +func (s *server) isUTXOSpendable(entry *walletUTXO, virtualDAAScore uint64) bool { if !entry.UTXOEntry.IsCoinbase() { return true } - return entry.UTXOEntry.BlockDAAScore()+coinbaseMaturity < virtualDAAScore + return entry.UTXOEntry.BlockDAAScore()+s.coinbaseMaturity < virtualDAAScore } diff --git a/cmd/kaspawallet/daemon/server/create_unsigned_transaction.go b/cmd/kaspawallet/daemon/server/create_unsigned_transaction.go index 3f9247a4ab..950b1d1e61 100644 --- a/cmd/kaspawallet/daemon/server/create_unsigned_transaction.go +++ b/cmd/kaspawallet/daemon/server/create_unsigned_transaction.go @@ -98,14 +98,9 @@ func (s *server) selectUTXOs(spendAmount uint64, isSendAll bool, feePerInput uin return nil, 0, 0, err } - coinbaseMaturity := s.params.BlockCoinbaseMaturity - if dagInfo.NetworkName == "kaspa-testnet-11" { - coinbaseMaturity = 1000 - } - for _, utxo := range s.utxosSortedByAmount { if (fromAddresses != nil && !walletAddressesContain(fromAddresses, utxo.address)) || - !isUTXOSpendable(utxo, dagInfo.VirtualDAAScore, coinbaseMaturity) { + !s.isUTXOSpendable(utxo, dagInfo.VirtualDAAScore) { continue } diff --git a/cmd/kaspawallet/daemon/server/server.go b/cmd/kaspawallet/daemon/server/server.go index f912512e0a..1b43dac8cf 100644 --- a/cmd/kaspawallet/daemon/server/server.go +++ b/cmd/kaspawallet/daemon/server/server.go @@ -33,6 +33,7 @@ type server struct { rpcClient *rpcclient.RPCClient // RPC client for ongoing user requests backgroundRPCClient *rpcclient.RPCClient // RPC client dedicated for address and UTXO background fetching params *dagconfig.Params + coinbaseMaturity uint64 // Is different from default if we use testnet-11 lock sync.RWMutex utxosSortedByAmount []*walletUTXO @@ -94,10 +95,21 @@ func Start(params *dagconfig.Params, listen, rpcServer string, keysFilePath stri return err } + dagInfo, err := rpcClient.GetBlockDAGInfo() + if err != nil { + return nil + } + + coinbaseMaturity := params.BlockCoinbaseMaturity + if dagInfo.NetworkName == "kaspa-testnet-11" { + coinbaseMaturity = 1000 + } + serverInstance := &server{ rpcClient: rpcClient, backgroundRPCClient: backgroundRPCClient, params: params, + coinbaseMaturity: coinbaseMaturity, utxosSortedByAmount: []*walletUTXO{}, nextSyncStartIndex: 0, keysFile: keysFile, diff --git a/cmd/kaspawallet/daemon/server/split_transaction.go b/cmd/kaspawallet/daemon/server/split_transaction.go index b2154c9007..2f71fb9c2a 100644 --- a/cmd/kaspawallet/daemon/server/split_transaction.go +++ b/cmd/kaspawallet/daemon/server/split_transaction.go @@ -264,7 +264,7 @@ func (s *server) moreUTXOsForMergeTransaction(alreadySelectedUTXOs []*libkaspawa if _, ok := alreadySelectedUTXOsMap[*utxo.Outpoint]; ok { continue } - if !isUTXOSpendable(utxo, dagInfo.VirtualDAAScore, s.params.BlockCoinbaseMaturity) { + if !s.isUTXOSpendable(utxo, dagInfo.VirtualDAAScore) { continue } additionalUTXOs = append(additionalUTXOs, &libkaspawallet.UTXO{ diff --git a/cmd/kaspawallet/daemon/server/sync.go b/cmd/kaspawallet/daemon/server/sync.go index 1e64de6245..5ba1d3a480 100644 --- a/cmd/kaspawallet/daemon/server/sync.go +++ b/cmd/kaspawallet/daemon/server/sync.go @@ -285,9 +285,9 @@ func (s *server) updateUTXOSet(entries []*appmessage.UTXOsByAddressesEntry, memp func (s *server) refreshUTXOs() error { refreshStart := time.Now() - s.lock.Lock() + + // No need to lock for reading since the only writer of this set is on `syncLoop` on the same goroutine. addresses := s.addressSet.strings() - s.lock.Unlock() // It's important to check the mempool before calling `GetUTXOsByAddresses`: // If we would do it the other way around an output can be spent in the mempool // and not in consensus, and between the calls its spending transaction will be