From 825043832cb235b13422c4c788cc67c8e56cff79 Mon Sep 17 00:00:00 2001 From: Thiago Figueiredo Date: Sat, 2 May 2020 14:38:37 -0300 Subject: [PATCH 1/2] Retry connection with dcrwallet --- dcrtimed/backend/filesystem/filesystem.go | 1 - dcrtimed/dcrtimed.go | 4 +++ dcrtimed/dcrtimewallet/dcrtimewallet.go | 38 +++++++++++++++++++++-- dcrtimed/dcrtimewallet/log.go | 10 ++++-- dcrtimed/log.go | 6 ++-- 5 files changed, 51 insertions(+), 8 deletions(-) diff --git a/dcrtimed/backend/filesystem/filesystem.go b/dcrtimed/backend/filesystem/filesystem.go index ba64671c..f4c08d2f 100644 --- a/dcrtimed/backend/filesystem/filesystem.go +++ b/dcrtimed/backend/filesystem/filesystem.go @@ -831,7 +831,6 @@ func New(root, cert, host string, enableCollections bool, passphrase []byte) (*F fs.enableCollections = enableCollections // Runtime bits - dcrtimewallet.UseLogger(log) fs.wallet, err = dcrtimewallet.New(cert, host, passphrase) if err != nil { return nil, err diff --git a/dcrtimed/dcrtimed.go b/dcrtimed/dcrtimed.go index 04c78388..65b79473 100644 --- a/dcrtimed/dcrtimed.go +++ b/dcrtimed/dcrtimed.go @@ -26,6 +26,7 @@ import ( v2 "github.com/decred/dcrtime/api/v2" "github.com/decred/dcrtime/dcrtimed/backend" "github.com/decred/dcrtime/dcrtimed/backend/filesystem" + "github.com/decred/dcrtime/dcrtimed/dcrtimewallet" "github.com/decred/dcrtime/util" "github.com/gorilla/handlers" "github.com/gorilla/mux" @@ -1269,6 +1270,9 @@ func _main() error { log.Infof("Network : %v", activeNetParams.Params.Name) log.Infof("Home dir: %v", loadedCfg.HomeDir) + // Sets subsystem loggers + dcrtimewallet.UseLogger(walletLog) + // Create the data directory in case it does not exist. err = os.MkdirAll(loadedCfg.DataDir, 0700) if err != nil { diff --git a/dcrtimed/dcrtimewallet/dcrtimewallet.go b/dcrtimed/dcrtimewallet/dcrtimewallet.go index 69209055..46a9438b 100644 --- a/dcrtimed/dcrtimewallet/dcrtimewallet.go +++ b/dcrtimed/dcrtimewallet/dcrtimewallet.go @@ -8,6 +8,7 @@ import ( "context" "crypto/sha256" "fmt" + "time" "google.golang.org/grpc" "google.golang.org/grpc/credentials" @@ -102,7 +103,7 @@ func (d *DcrtimeWallet) Lookup(tx chainhash.Hash) (*Result, error) { }, nil } -// Construct creates aand submits an anchored tx with the provided merkle root. +// Construct creates and submits an anchored tx with the provided merkle root. func (d *DcrtimeWallet) Construct(merkleRoot [sha256.Size]byte) (*chainhash.Hash, error) { // Generate script that contains OP_RETURN followed by the merkle root. script, err := txscript.NewScriptBuilder().AddOp(txscript.OP_RETURN). @@ -188,6 +189,37 @@ func (d *DcrtimeWallet) Close() { d.conn.Close() } +// getWalletGrpcConnection tries to estabilish a wallet connection. If it fails, +// it keeps retrying to connect until max retry attempts is reached. +func getWalletGrpcConnection(creds credentials.TransportCredentials, host string) (*grpc.ClientConn, error) { + var ( + maxRetries = 100 + duration = time.Duration(5 * time.Second) + conn *grpc.ClientConn + err error + done bool + ) + + for retries := 0; !done; retries++ { + if retries == maxRetries { + return nil, fmt.Errorf("Max retries exceeded") + } + conn, err = grpc.Dial(host, grpc.WithBlock(), + grpc.WithTransportCredentials(creds), + grpc.WithTimeout(duration)) + + if err != nil { + log.Warnf("Cannot estabilish a dcrwallet connection: %v", err) + log.Warnf("Retrying... attempt: %v", retries) + continue + } + + done = true + } + + return conn, nil +} + // New returns a DcrtimeWallet context. func New(cert, host string, passphrase []byte) (*DcrtimeWallet, error) { d := &DcrtimeWallet{ @@ -203,11 +235,11 @@ func New(cert, host string, passphrase []byte) (*DcrtimeWallet, error) { } log.Infof("Wallet: %v", host) - d.conn, err = grpc.Dial(host, grpc.WithBlock(), - grpc.WithTransportCredentials(creds)) + d.conn, err = getWalletGrpcConnection(creds, host) if err != nil { return nil, err } + d.wallet = pb.NewWalletServiceClient(d.conn) return d, nil diff --git a/dcrtimed/dcrtimewallet/log.go b/dcrtimed/dcrtimewallet/log.go index 0fb2f542..38eff424 100644 --- a/dcrtimed/dcrtimewallet/log.go +++ b/dcrtimed/dcrtimewallet/log.go @@ -28,12 +28,18 @@ import ( // requests it. var log = slog.Disabled -// UseLogger sets the logger to use for the gRPC server. -func UseLogger(l slog.Logger) { +// UseGrpcLogger sets the logger to use for the gRPC server. +func UseGrpcLogger(l slog.Logger) { grpclog.SetLogger(logger{l}) log = l } +// UseLogger sets the subsystem logger for this package, without +// gRPC logging. +func UseLogger(l slog.Logger) { + log = l +} + // logger uses a slog.Logger to implement the grpclog.Logger interface. type logger struct { slog.Logger diff --git a/dcrtimed/log.go b/dcrtimed/log.go index 115005be..119a3776 100644 --- a/dcrtimed/log.go +++ b/dcrtimed/log.go @@ -40,14 +40,16 @@ var ( // application shutdown. logRotator *rotator.Rotator - log = backendLog.Logger("DCRT") - fsbeLog = backendLog.Logger("FSBE") + log = backendLog.Logger("DCRT") + fsbeLog = backendLog.Logger("FSBE") + walletLog = backendLog.Logger("DCRW") ) // subsystemLoggers maps each subsystem identifier to its associated logger. var subsystemLoggers = map[string]slog.Logger{ "DCRT": log, "FSBE": fsbeLog, + "DCRW": walletLog, } // initLogRotator initializes the logging rotater to write logs to logFile and From 33ba5f5bfbd00c081366e07088a009b23a69bed1 Mon Sep 17 00:00:00 2001 From: thi4go Date: Mon, 4 May 2020 11:30:24 -0300 Subject: [PATCH 2/2] remove redundant conversion --- dcrtimed/dcrtimewallet/dcrtimewallet.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dcrtimed/dcrtimewallet/dcrtimewallet.go b/dcrtimed/dcrtimewallet/dcrtimewallet.go index 46a9438b..ee7202dd 100644 --- a/dcrtimed/dcrtimewallet/dcrtimewallet.go +++ b/dcrtimed/dcrtimewallet/dcrtimewallet.go @@ -194,7 +194,7 @@ func (d *DcrtimeWallet) Close() { func getWalletGrpcConnection(creds credentials.TransportCredentials, host string) (*grpc.ClientConn, error) { var ( maxRetries = 100 - duration = time.Duration(5 * time.Second) + duration = 5 * time.Second conn *grpc.ClientConn err error done bool