@@ -40,13 +40,19 @@ type ManagerConfig struct {
40
40
// ChainParams is the chain configuration(mainnet, testnet...) this
41
41
// manager uses.
42
42
ChainParams * chaincfg.Params
43
+
44
+ // ChainNotifier is the chain notifier that is used to listen for new
45
+ // blocks.
46
+ ChainNotifier lndclient.ChainNotifierClient
43
47
}
44
48
45
49
// Manager manages the address state machines.
46
50
type Manager struct {
47
51
cfg * ManagerConfig
48
52
49
53
sync.Mutex
54
+
55
+ currentHeight int32
50
56
}
51
57
52
58
// NewManager creates a new address manager.
@@ -58,8 +64,26 @@ func NewManager(cfg *ManagerConfig) *Manager {
58
64
59
65
// Run runs the address manager.
60
66
func (m * Manager ) Run (ctx context.Context ) error {
61
- <- ctx .Done ()
62
- return nil
67
+ newBlockChan , newBlockErrChan , err :=
68
+ m .cfg .ChainNotifier .RegisterBlockEpochNtfn (ctx )
69
+
70
+ if err != nil {
71
+ return err
72
+ }
73
+
74
+ for {
75
+ select {
76
+ case currentHeight := <- newBlockChan :
77
+ m .currentHeight = currentHeight
78
+
79
+ case err = <- newBlockErrChan :
80
+ return err
81
+
82
+ case <- ctx .Done ():
83
+ // Signal subroutines that the manager is exiting.
84
+ return ctx .Err ()
85
+ }
86
+ }
63
87
}
64
88
65
89
// NewAddress starts a new address creation flow.
@@ -86,6 +110,11 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
86
110
}
87
111
m .Unlock ()
88
112
113
+ // Ensure that we have that we have a sane current block height.
114
+ if m .currentHeight == 0 {
115
+ return nil , fmt .Errorf ("current block height is unknown" )
116
+ }
117
+
89
118
// We are fetching a new L402 token from the server. There is one static
90
119
// address per L402 token allowed.
91
120
err = m .cfg .FetchL402 (ctx )
@@ -147,6 +176,7 @@ func (m *Manager) NewAddress(ctx context.Context) (*btcutil.AddressTaproot,
147
176
ProtocolVersion : version .AddressProtocolVersion (
148
177
protocolVersion ,
149
178
),
179
+ InitiationHeight : m .currentHeight ,
150
180
}
151
181
err = m .cfg .Store .CreateStaticAddress (ctx , addrParams )
152
182
if err != nil {
0 commit comments