Skip to content

Commit

Permalink
multi: unify utilizing NewBtcdClientWithConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedawnallah committed Jan 22, 2025
1 parent 9e5f16d commit 1d91cdd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
20 changes: 15 additions & 5 deletions btcwallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"runtime"
"sync"

"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcwallet/chain"
"github.com/btcsuite/btcwallet/rpc/legacyrpc"
"github.com/btcsuite/btcwallet/wallet"
Expand Down Expand Up @@ -262,15 +263,24 @@ func readCAFile() []byte {
}

// startChainRPC opens a RPC client connection to a btcd server for blockchain
// services. This function uses the RPC options from the global config and
// services. This function uses the RPC options from the global config and
// there is no recovery in case the server is not available or if there is an
// authentication error. Instead, all requests to the client will simply error.
func startChainRPC(certs []byte) (*chain.BtcdClient, error) {
log.Infof("Attempting RPC client connection to %v", cfg.RPCConnect)
rpcc, err := chain.NewBtcdClient(
activeNet.Params, cfg.RPCConnect, cfg.BtcdUsername, cfg.BtcdPassword,
certs, cfg.DisableClientTLS, 0,
)

chainCfg := &chain.BtcdConfig{
Conn: &rpcclient.ConnConfig{
Host: cfg.RPCConnect,
User: cfg.BtcdUsername,
Pass: cfg.BtcdPassword,
Certificates: certs,
DisableTLS: cfg.DisableClientTLS,
},
Chain: activeNet.Params,
ReconnectAttempts: 0,
}
rpcc, err := chain.NewBtcdClientWithConfig(chainCfg)
if err != nil {
return nil, err
}
Expand Down
37 changes: 23 additions & 14 deletions rpc/rpcserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
// Full documentation of the API implemented by this package is maintained in a
// language-agnostic document:
//
// https://github.com/btcsuite/btcwallet/blob/master/rpc/documentation/api.md
// https://github.com/btcsuite/btcwallet/blob/master/rpc/documentation/api.md
//
// Any API changes must be performed according to the steps listed here:
//
// https://github.com/btcsuite/btcwallet/blob/master/rpc/documentation/serverchanges.md
// https://github.com/btcsuite/btcwallet/blob/master/rpc/documentation/serverchanges.md
package rpcserver

import (
Expand Down Expand Up @@ -486,12 +486,12 @@ func (s *walletServer) SignTransaction(ctx context.Context, req *pb.SignTransact
}

// BUGS:
// - The transaction is not inspected to be relevant before publishing using
// sendrawtransaction, so connection errors to btcd could result in the tx
// never being added to the wallet database.
// - Once the above bug is fixed, wallet will require a way to purge invalid
// transactions from the database when they are rejected by the network, other
// than double spending them.
// - The transaction is not inspected to be relevant before publishing using
// sendrawtransaction, so connection errors to btcd could result in the tx
// never being added to the wallet database.
// - Once the above bug is fixed, wallet will require a way to purge invalid
// transactions from the database when they are rejected by the network, other
// than double spending them.
func (s *walletServer) PublishTransaction(ctx context.Context, req *pb.PublishTransactionRequest) (
*pb.PublishTransactionResponse, error) {

Expand Down Expand Up @@ -769,8 +769,9 @@ func (s *loaderServer) StartConsensusRpc(ctx context.Context, // nolint:golint
return nil, status.Errorf(codes.FailedPrecondition, "RPC client already created")
}

networkAddress, err := cfgutil.NormalizeAddress(req.NetworkAddress,
s.activeNet.RPCClientPort)
networkAddress, err := cfgutil.NormalizeAddress(
req.NetworkAddress, s.activeNet.RPCClientPort,

Check failure on line 773 in rpc/rpcserver/server.go

View workflow job for this annotation

GitHub Actions / Format, compilation and lint check

avoid direct access to proto field req.NetworkAddress, use req.GetNetworkAddress() instead (protogetter)
)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument,
"Network address is ill-formed: %v", err)
Expand All @@ -783,10 +784,18 @@ func (s *loaderServer) StartConsensusRpc(ctx context.Context, // nolint:golint
"wallet is loaded and already synchronizing")
}

rpcClient, err := chain.NewBtcdClient(
s.activeNet.Params, networkAddress, req.Username, string(req.Password),
req.Certificate, len(req.Certificate) == 0, 1,
)
chainCfg := &chain.BtcdConfig{
Conn: &rpcclient.ConnConfig{
Host: networkAddress,
User: req.Username,

Check failure on line 790 in rpc/rpcserver/server.go

View workflow job for this annotation

GitHub Actions / Format, compilation and lint check

avoid direct access to proto field req.Username, use req.GetUsername() instead (protogetter)
Pass: string(req.Password),

Check failure on line 791 in rpc/rpcserver/server.go

View workflow job for this annotation

GitHub Actions / Format, compilation and lint check

avoid direct access to proto field req.Password, use req.GetPassword() instead (protogetter)
Certificates: req.Certificate,

Check failure on line 792 in rpc/rpcserver/server.go

View workflow job for this annotation

GitHub Actions / Format, compilation and lint check

avoid direct access to proto field req.Certificate, use req.GetCertificate() instead (protogetter)
DisableTLS: len(req.Certificate) == 0,

Check failure on line 793 in rpc/rpcserver/server.go

View workflow job for this annotation

GitHub Actions / Format, compilation and lint check

avoid direct access to proto field req.Certificate, use req.GetCertificate() instead (protogetter)
},
Chain: s.activeNet.Params,
ReconnectAttempts: 1,
}
rpcClient, err := chain.NewBtcdClientWithConfig(chainCfg)
if err != nil {
return nil, translateError(err)
}
Expand Down

0 comments on commit 1d91cdd

Please sign in to comment.