mirror of
https://github.com/lightninglabs/lightning-terminal.git
synced 2026-08-13 12:33:36 +02:00
This commit updates: - LND - pool - loop - taproot-assets - faraday - lighting-node-connect - aperture - lndclient - btclog We start using the new btclog v2 library and the associated breaking changes in the lnd/build package. With this commit, we deprecate the `--remote.lit-maxlogfiles` and `--remote.lit-maxlogfilesize` options and introduce new logging options under the `--remote.lit-logging` namespace. Finally, the LND update introduced a new `MaxBlocksMinedPerTest` variable in the `lntest` package that we now need to override in order for our itests to pass.
98 lines
2.8 KiB
Go
98 lines
2.8 KiB
Go
package accounts
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/btcsuite/btclog/v2"
|
|
)
|
|
|
|
// ContextKey is the type that we use to identify account specific values in the
|
|
// request context. We wrap the string inside a struct because of this comment
|
|
// in the context API: "The provided key must be comparable and should not be of
|
|
// type string or any other built-in type to avoid collisions between packages
|
|
// using context."
|
|
type ContextKey struct {
|
|
Name string
|
|
}
|
|
|
|
var (
|
|
// KeyAccount is the key under which we store the account in the request
|
|
// context.
|
|
KeyAccount = ContextKey{"account"}
|
|
|
|
// KeyRequestID is the key under which we store the middleware request
|
|
// ID.
|
|
KeyRequestID = ContextKey{"request_id"}
|
|
)
|
|
|
|
// FromContext tries to extract a value from the given context.
|
|
func FromContext(ctx context.Context, key ContextKey) interface{} {
|
|
return ctx.Value(key)
|
|
}
|
|
|
|
// AddAccountToContext adds the given value to the context for easy retrieval
|
|
// later on.
|
|
func AddAccountToContext(ctx context.Context,
|
|
value *OffChainBalanceAccount) context.Context {
|
|
|
|
return context.WithValue(ctx, KeyAccount, value)
|
|
}
|
|
|
|
// AccountFromContext attempts to extract an account from the given context.
|
|
func AccountFromContext(ctx context.Context) (*OffChainBalanceAccount, error) {
|
|
val := FromContext(ctx, KeyAccount)
|
|
if val == nil {
|
|
return nil, fmt.Errorf("no account found in context")
|
|
}
|
|
|
|
acct, ok := val.(*OffChainBalanceAccount)
|
|
if !ok {
|
|
return nil, fmt.Errorf("invalid account value in context")
|
|
}
|
|
|
|
return acct, nil
|
|
}
|
|
|
|
// AddRequestIDToContext adds the given request ID to the context for easy
|
|
// retrieval later on.
|
|
func AddRequestIDToContext(ctx context.Context, value uint64) context.Context {
|
|
return context.WithValue(ctx, KeyRequestID, value)
|
|
}
|
|
|
|
// RequestIDFromContext attempts to extract a request ID from the given context.
|
|
func RequestIDFromContext(ctx context.Context) (uint64, error) {
|
|
val := FromContext(ctx, KeyRequestID)
|
|
if val == nil {
|
|
return 0, fmt.Errorf("no request ID found in context")
|
|
}
|
|
|
|
reqID, ok := val.(uint64)
|
|
if !ok {
|
|
return 0, fmt.Errorf("invalid request ID value in context")
|
|
}
|
|
|
|
return reqID, nil
|
|
}
|
|
|
|
// requestScopedValuesFromCtx is a helper function that can be used to extract
|
|
// an account and requestID from the given context. It also creates a new
|
|
// prefixed logger that can be used by account request and response handlers.
|
|
// Each log line will be prefixed by the account ID and the request ID.
|
|
func requestScopedValuesFromCtx(ctx context.Context) (btclog.Logger,
|
|
*OffChainBalanceAccount, uint64, error) {
|
|
|
|
acc, err := AccountFromContext(ctx)
|
|
if err != nil {
|
|
return nil, nil, 0, err
|
|
}
|
|
|
|
reqID, err := RequestIDFromContext(ctx)
|
|
if err != nil {
|
|
return nil, nil, 0, err
|
|
}
|
|
|
|
prefix := fmt.Sprintf("[account: %s, request: %d]", acc.ID, reqID)
|
|
|
|
return log.WithPrefix(prefix), acc, reqID, nil
|
|
}
|