From 2d6148291f4423133c0cca271ec4c0e3314d5534 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Mon, 17 Mar 2025 22:13:11 +0800 Subject: [PATCH 1/2] cmd: fix error parsed from status The `codes.Unimplemented` is only returned when the RPCs are not built. When the wallet is in an unexpected state, `codes.Unknown` is returned instead, so we need to catch it properly to make sure we return the right error msg. --- cmd/commands/commands.go | 66 ++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 30 deletions(-) diff --git a/cmd/commands/commands.go b/cmd/commands/commands.go index f41925392..7969cd20a 100644 --- a/cmd/commands/commands.go +++ b/cmd/commands/commands.go @@ -251,41 +251,47 @@ func printModifiedProtoJSON(resp proto.Message) { // to command actions. func actionDecorator(f func(*cli.Context) error) func(*cli.Context) error { return func(c *cli.Context) error { - if err := f(c); err != nil { - s, ok := status.FromError(err) + err := f(c) - // If it's a command for the UnlockerService (like - // 'create' or 'unlock') but the wallet is already - // unlocked, then these methods aren't recognized any - // more because this service is shut down after - // successful unlock. That's why the code - // 'Unimplemented' means something different for these - // two commands. - if s.Code() == codes.Unimplemented && - (c.Command.Name == "create" || - c.Command.Name == "unlock" || - c.Command.Name == "changepassword" || - c.Command.Name == "createwatchonly") { + // Exit early if there's no error. + if err == nil { + return nil + } - return fmt.Errorf("Wallet is already unlocked") - } + // Try to parse the Status representatio from this error. + s, ok := status.FromError(err) - // lnd might be active, but not possible to contact - // using RPC if the wallet is encrypted. If we get - // error code Unimplemented, it means that lnd is - // running, but the RPC server is not active yet (only - // WalletUnlocker server active) and most likely this - // is because of an encrypted wallet. - if ok && s.Code() == codes.Unimplemented { - return fmt.Errorf("Wallet is encrypted. " + - "Please unlock using 'lncli unlock', " + - "or set password using 'lncli create'" + - " if this is the first time starting " + - "lnd.") - } + // If this cannot be represented by a Status, exit early. + if !ok { return err } - return nil + + // If it's a command for the UnlockerService (like 'create' or + // 'unlock') but the wallet is already unlocked, then these + // methods aren't recognized any more because this service is + // shut down after successful unlock. + if s.Code() == codes.Unknown && + (c.Command.Name == "create" || + c.Command.Name == "unlock" || + c.Command.Name == "changepassword" || + c.Command.Name == "createwatchonly") { + + return errors.New("wallet is already unlocked") + } + + // lnd might be active, but not possible to contact using RPC if + // the wallet is encrypted. If we get error code Unknown, it + // means that lnd is running, but the RPC server is not active + // yet (only WalletUnlocker server active) and most likely this + // is because of an encrypted wallet. + if s.Code() == codes.Unknown { + return errors.New("wallet is encrypted - please " + + "unlock using 'lncli unlock', or set " + + "password using 'lncli create' if this is " + + "the first time starting lnd") + } + + return s.Err() } } From e62aa7d43a76bd69d607e018001186f2518ff2b7 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Mon, 17 Mar 2025 22:25:17 +0800 Subject: [PATCH 2/2] docs: update release notes --- docs/release-notes/release-notes-0.19.0.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/release-notes/release-notes-0.19.0.md b/docs/release-notes/release-notes-0.19.0.md index 74eabc684..42a1a8e23 100644 --- a/docs/release-notes/release-notes-0.19.0.md +++ b/docs/release-notes/release-notes-0.19.0.md @@ -229,6 +229,12 @@ ## lncli Updates +* [Fixed](https://github.com/lightningnetwork/lnd/pull/9605) a case where + inaccurate error message is displayed. Previously, when the `lnd` is built + without with a given RPC service yet the `cli` does, running a command to + access the RPC server would give an error saying the wallet is encrypted. This + is now fixed to show specifically which RPC server is missing. + ## Code Health * [Add retry logic](https://github.com/lightningnetwork/lnd/pull/8381) for