From b9ad66471d25793ff8bccce1935916604760face Mon Sep 17 00:00:00 2001 From: Elle Mouton Date: Thu, 17 Apr 2025 13:17:33 +0200 Subject: [PATCH] multi+refactor: remove unnecessary type alias Remove the `NewPrivacyMapDB` type alias. It is not needed. --- firewall/privacy_mapper.go | 10 +++++----- firewall/privacy_mapper_test.go | 12 ++++++------ firewall/rule_enforcer.go | 8 ++++---- firewalldb/privacy_mapper.go | 6 ------ session_rpcserver.go | 10 +++++----- terminal.go | 6 +++--- 6 files changed, 23 insertions(+), 29 deletions(-) diff --git a/firewall/privacy_mapper.go b/firewall/privacy_mapper.go index cbd8a8da..fed4ba53 100644 --- a/firewall/privacy_mapper.go +++ b/firewall/privacy_mapper.go @@ -60,19 +60,19 @@ var _ mid.RequestInterceptor = (*PrivacyMapper)(nil) // PrivacyMapper is a RequestInterceptor that maps any pseudo names in certain // requests to their real values and vice versa for responses. type PrivacyMapper struct { - newDB firewalldb.NewPrivacyMapDB + db firewalldb.PrivacyMapper randIntn func(int) (int, error) sessionDB firewalldb.SessionDB } // NewPrivacyMapper returns a new instance of PrivacyMapper. The randIntn // function is used to draw randomness for request field obfuscation. -func NewPrivacyMapper(newDB firewalldb.NewPrivacyMapDB, +func NewPrivacyMapper(newDB firewalldb.PrivacyMapper, randIntn func(int) (int, error), sessionDB firewalldb.SessionDB) *PrivacyMapper { return &PrivacyMapper{ - newDB: newDB, + db: newDB, randIntn: randIntn, sessionDB: sessionDB, } @@ -195,7 +195,7 @@ func (p *PrivacyMapper) checkAndReplaceIncomingRequest(ctx context.Context, return nil, err } - db := p.newDB(session.GroupID) + db := p.db.PrivacyDB(session.GroupID) // If we don't have a handler for the URI, we don't allow the request // to go through. @@ -225,7 +225,7 @@ func (p *PrivacyMapper) replaceOutgoingResponse(ctx context.Context, uri string, return nil, err } - db := p.newDB(session.GroupID) + db := p.db.PrivacyDB(session.GroupID) // If we don't have a handler for the URI, we don't allow the response // to go to avoid accidental leaks. diff --git a/firewall/privacy_mapper_test.go b/firewall/privacy_mapper_test.go index 1998d128..9dcc814b 100644 --- a/firewall/privacy_mapper_test.go +++ b/firewall/privacy_mapper_test.go @@ -902,7 +902,7 @@ func TestPrivacyMapper(t *testing.T) { // randIntn is used for deterministic testing. randIntn := func(n int) (int, error) { return 100, nil } - p := NewPrivacyMapper(db.NewSessionDB, randIntn, pd) + p := NewPrivacyMapper(db, randIntn, pd) rawMsg, err := proto.Marshal(test.msg) require.NoError(t, err) @@ -978,7 +978,7 @@ func TestPrivacyMapper(t *testing.T) { rawMsg, err := proto.Marshal(msg) require.NoError(t, err) - p := NewPrivacyMapper(db.NewSessionDB, CryptoRandIntn, pd) + p := NewPrivacyMapper(db, CryptoRandIntn, pd) require.NoError(t, err) // We test the independent outgoing amount (incoming amount @@ -1071,7 +1071,7 @@ func newMockDB(t *testing.T, preloadRealToPseudo map[string]string, sessID session.ID) mockDB { db := mockDB{privDB: make(map[string]*mockPrivacyMapDB)} - sessDB := db.NewSessionDB(sessID) + sessDB := db.PrivacyDB(sessID) _ = sessDB.Update(context.Background(), func(ctx context.Context, tx firewalldb.PrivacyMapTx) error { @@ -1085,14 +1085,14 @@ func newMockDB(t *testing.T, preloadRealToPseudo map[string]string, return db } -func (m mockDB) NewSessionDB(sessionID session.ID) firewalldb.PrivacyMapDB { - db, ok := m.privDB[string(sessionID[:])] +func (m mockDB) PrivacyDB(groupID session.ID) firewalldb.PrivacyMapDB { + db, ok := m.privDB[string(groupID[:])] if ok { return db } newDB := newMockPrivacyMapDB() - m.privDB[string(sessionID[:])] = newDB + m.privDB[string(groupID[:])] = newDB return newDB } diff --git a/firewall/rule_enforcer.go b/firewall/rule_enforcer.go index 7914965e..472143f0 100644 --- a/firewall/rule_enforcer.go +++ b/firewall/rule_enforcer.go @@ -33,7 +33,7 @@ type RuleEnforcer struct { actionsDB firewalldb.ActionReadDBGetter sessionDB firewalldb.SessionDB markActionErrored func(reqID uint64, reason string) error - newPrivMap firewalldb.NewPrivacyMapDB + privMapDB firewalldb.PrivacyMapper permsMgr *perms.Manager getFeaturePerms featurePerms @@ -64,7 +64,7 @@ func NewRuleEnforcer(ruleDB firewalldb.RulesDB, lndClient lndclient.LightningClient, lndConnID string, ruleMgrs rules.ManagerSet, markActionErrored func(reqID uint64, reason string) error, - privMap firewalldb.NewPrivacyMapDB) *RuleEnforcer { + privMap firewalldb.PrivacyMapper) *RuleEnforcer { return &RuleEnforcer{ ruleDB: ruleDB, @@ -76,7 +76,7 @@ func NewRuleEnforcer(ruleDB firewalldb.RulesDB, lndClient: lndClient, ruleMgrs: ruleMgrs, markActionErrored: markActionErrored, - newPrivMap: privMap, + privMapDB: privMap, sessionDB: sessionIDIndex, lndConnID: lndConnID, } @@ -392,7 +392,7 @@ func (r *RuleEnforcer) initRule(ctx context.Context, reqID uint64, name string, } if privacy { - privMap := r.newPrivMap(session.GroupID) + privMap := r.privMapDB.PrivacyDB(session.GroupID) ruleValues, err = ruleValues.PseudoToReal( ctx, privMap, session.PrivacyFlags, diff --git a/firewalldb/privacy_mapper.go b/firewalldb/privacy_mapper.go index 8d3642c3..cde91bfe 100644 --- a/firewalldb/privacy_mapper.go +++ b/firewalldb/privacy_mapper.go @@ -11,8 +11,6 @@ import ( "strconv" "strings" "sync" - - "github.com/lightninglabs/lightning-terminal/session" ) var ( @@ -29,10 +27,6 @@ var ( "value already exists") ) -// NewPrivacyMapDB is a function type that takes a group ID and uses it to -// construct a new PrivacyMapDB. -type NewPrivacyMapDB func(groupID session.ID) PrivacyMapDB - // PrivacyMapDB provides an Update and View method that will allow the caller // to perform atomic read and write transactions defined by PrivacyMapTx on the // underlying DB. diff --git a/session_rpcserver.go b/session_rpcserver.go index b2070094..ffb8e6b4 100644 --- a/session_rpcserver.go +++ b/session_rpcserver.go @@ -66,7 +66,7 @@ type sessionRpcServerConfig struct { actionsDB *firewalldb.BoltDB autopilot autopilotserver.Autopilot ruleMgrs rules.ManagerSet - privMap firewalldb.NewPrivacyMapDB + privMap firewalldb.PrivacyMapper } // newSessionRPCServer creates a new sessionRpcServer using the passed config. @@ -628,7 +628,7 @@ func (s *sessionRpcServer) PrivacyMapConversion(ctx context.Context, } var res string - privMap := s.cfg.privMap(groupID) + privMap := s.cfg.privMap.PrivacyDB(groupID) err = privMap.View(ctx, func(ctx context.Context, tx firewalldb.PrivacyMapTx) error { @@ -900,7 +900,7 @@ func (s *sessionRpcServer) AddAutopilotSession(ctx context.Context, linkedGroupID = &groupID linkedGroupSession = groupSess - privDB := s.cfg.privMap(groupID) + privDB := s.cfg.privMap.PrivacyDB(groupID) err = privDB.View(ctx, func(ctx context.Context, tx firewalldb.PrivacyMapTx) error { @@ -1225,7 +1225,7 @@ func (s *sessionRpcServer) AddAutopilotSession(ctx context.Context, } // Register all the privacy map pairs for this session ID. - privDB := s.cfg.privMap(sess.GroupID) + privDB := s.cfg.privMap.PrivacyDB(sess.GroupID) err = privDB.Update(ctx, func(ctx context.Context, tx firewalldb.PrivacyMapTx) error { @@ -1487,7 +1487,7 @@ func (s *sessionRpcServer) marshalRPCSession(ctx context.Context, } if sess.WithPrivacyMapper { - db := s.cfg.privMap( + db := s.cfg.privMap.PrivacyDB( sess.GroupID, ) val, err = val.PseudoToReal( diff --git a/terminal.go b/terminal.go index 3acad5b4..9b35e0a6 100644 --- a/terminal.go +++ b/terminal.go @@ -534,7 +534,7 @@ func (g *LightningTerminal) start(ctx context.Context) error { actionsDB: g.stores.firewallBolt, autopilot: g.autopilotClient, ruleMgrs: g.ruleMgrs, - privMap: g.stores.firewall.PrivacyDB, + privMap: g.stores.firewall, }) if err != nil { return fmt.Errorf("could not create new session rpc "+ @@ -1100,7 +1100,7 @@ func (g *LightningTerminal) startInternalSubServers(ctx context.Context, } privacyMapper := firewall.NewPrivacyMapper( - g.stores.firewall.PrivacyDB, firewall.CryptoRandIntn, + g.stores.firewall, firewall.CryptoRandIntn, g.stores.sessions, ) @@ -1123,7 +1123,7 @@ func (g *LightningTerminal) startInternalSubServers(ctx context.Context, reqID, firewalldb.ActionStateError, reason, ) - }, g.stores.firewall.PrivacyDB, + }, g.stores.firewall, ) mw = append(mw, ruleEnforcer)