@@ -35,6 +35,10 @@ type PaymentObserver struct {
3535 l1Progress time.Time
3636 l1HighWater * big.Int
3737 recipient common.Address
38+ lifecycle context.Context
39+ payers map [payerKey ]* payerEntry
40+ payerReads sync.WaitGroup
41+ activeReads int
3842}
3943
4044func NewPaymentObserver (chain PaymentChain , requireL1 bool , recipient common.Address ) * PaymentObserver {
@@ -44,7 +48,17 @@ func NewPaymentObserver(chain PaymentChain, requireL1 bool, recipient common.Add
4448
4549// Run has one service-owned caller. Every RPC inherits the service lifetime.
4650func (o * PaymentObserver ) Run (ctx context.Context ) {
47- defer func () { o .mu .Lock (); o .stopped = true ; o .mu .Unlock () }()
51+ o .mu .Lock ()
52+ lifecycle , cancelReads := context .WithCancel (ctx )
53+ o .lifecycle = lifecycle
54+ o .mu .Unlock ()
55+ defer func () {
56+ cancelReads ()
57+ o .mu .Lock ()
58+ o .stopped = true
59+ o .mu .Unlock ()
60+ o .payerReads .Wait ()
61+ }()
4862 ticker := time .NewTicker (o .interval )
4963 defer ticker .Stop ()
5064 for {
@@ -64,13 +78,20 @@ func (o *PaymentObserver) Run(ctx context.Context) {
6478type ObserverStatus struct {
6579 ObservedAt , ContactAt , HeadProgressAt , L1ProgressAt time.Time
6680 Healthy , RecipientActive bool
81+ PayerEntries , PayerReads , PayerFailures int
6782}
6883
6984func (o * PaymentObserver ) Status () ObserverStatus {
7085 o .mu .RLock ()
7186 defer o .mu .RUnlock ()
7287 healthy := o .validate (o .current ) == nil
73- return ObserverStatus {o .current .observedAt , o .contact , o .progress , o .l1Progress , healthy , healthy && o .recipient != (common.Address {}) && o .current .recipientErr == nil && o .current .recipientActive }
88+ status := ObserverStatus {ObservedAt : o .current .observedAt , ContactAt : o .contact , HeadProgressAt : o .progress , L1ProgressAt : o .l1Progress , Healthy : healthy , RecipientActive : healthy && o .recipient != (common.Address {}) && o .current .recipientErr == nil && o .current .recipientActive , PayerEntries : len (o .payers ), PayerReads : o .activeReads }
89+ for _ , entry := range o .payers {
90+ if ! entry .valid && entry .failure != nil {
91+ status .PayerFailures ++
92+ }
93+ }
94+ return status
7495}
7596
7697func (o * PaymentObserver ) Snapshot (ctx context.Context ) (ChainSnapshot , error ) {
@@ -82,9 +103,14 @@ func (o *PaymentObserver) Snapshot(ctx context.Context) (ChainSnapshot, error) {
82103 if err := o .validate (o .current ); err != nil {
83104 return ChainSnapshot {}, err
84105 }
85- s := o .current
86- s .Block , s .Round , s .CurrentRound = new (big.Int ).Set (s .Block ), new (big.Int ).Set (s .Round ), new (big.Int ).Set (s .CurrentRound )
87- return s , nil
106+ return o .current .clone (), nil
107+ }
108+
109+ func (s ChainSnapshot ) clone () ChainSnapshot {
110+ for _ , value := range []* * big.Int {& s .Block , & s .Number , & s .Round , & s .CurrentRound } {
111+ * value = new (big.Int ).Set (* value )
112+ }
113+ return s
88114}
89115
90116func (s ChainSnapshot ) ObservedAt () time.Time {
@@ -106,7 +132,7 @@ func (s ChainSnapshot) Validate() error {
106132}
107133
108134func (o * PaymentObserver ) validate (s ChainSnapshot ) error {
109- if o .stopped || o .invalid || s .generation != o .generation || o .expired (s , o .l1Progress ) {
135+ if o .stopped || o .invalid || ( o . lifecycle != nil && o . lifecycle . Err () != nil ) || s .generation != o .generation || o .expired (s , o .l1Progress ) {
110136 return ErrObservationUnavailable
111137 }
112138 return nil
@@ -117,8 +143,7 @@ func (o *PaymentObserver) expired(s ChainSnapshot, l1Progress time.Time) bool {
117143 return s .observedAt .IsZero () || now .Sub (s .observedAt ) >= o .maxAge || now .Sub (s .blockTime ) >= o .maxAge || now .Sub (l1Progress ) >= o .maxAge
118144}
119145
120- // Only Run calls refresh. Previous/current identity is the extension point for
121- // complete hash-pinned logs or reset; no partial delta authorizes publication.
146+ // Only Run calls refresh.
122147func (o * PaymentObserver ) refresh (ctx context.Context ) error {
123148 at := o .now ()
124149 head , err := o .chain .Contracts .RPC .header (ctx , "latest" )
@@ -128,7 +153,7 @@ func (o *PaymentObserver) refresh(ctx context.Context) error {
128153 o .mu .Lock ()
129154 o .contact = o .now ()
130155 prior := o .header
131- gap := false
156+ gap , continuous := false , false
132157 if head .Hash == (common.Hash {}) || head .Number == nil {
133158 o .mu .Unlock ()
134159 return ErrObservationUnavailable
@@ -142,6 +167,7 @@ func (o *PaymentObserver) refresh(ctx context.Context) error {
142167 return ErrObservationUnavailable
143168 }
144169 gap = head .Hash != prior .Hash && distance .Cmp (big .NewInt (1 )) > 0
170+ continuous = distance .Cmp (big .NewInt (1 )) == 0 && head .ParentHash == prior .Hash
145171 if head .Hash != prior .Hash && ! gap && (distance .Sign () == 0 || head .ParentHash != prior .Hash ) {
146172 o .invalid = true
147173 o .generation ++
@@ -171,7 +197,8 @@ func (o *PaymentObserver) refresh(ctx context.Context) error {
171197 // A polling gap does not itself orphan the retained observation. Prove
172198 // its anchor before keeping it available during the pinned backfill.
173199 anchor , err := o .chain .Contracts .RPC .header (ctx , hexutil .EncodeBig ((* big .Int )(prior .Number )))
174- if err != nil || anchor .Hash != prior .Hash || anchor .Number == nil || (* big .Int )(anchor .Number ).Cmp ((* big .Int )(prior .Number )) != 0 {
200+ continuous = err == nil && anchor .Hash == prior .Hash && anchor .Number != nil && (* big .Int )(anchor .Number ).Cmp ((* big .Int )(prior .Number )) == 0
201+ if ! continuous {
175202 o .mu .Lock ()
176203 o .invalid = true
177204 o .generation ++
@@ -193,6 +220,19 @@ func (o *PaymentObserver) refresh(ctx context.Context) error {
193220 if err := ctx .Err (); err != nil {
194221 return err
195222 }
223+ changed , reset := o .payerChanges (ctx , prior , head , s , continuous )
224+ if gap && reset {
225+ o .invalidate ()
226+ }
227+ // Hash-pinned logs can outlive their canonical block, even on a reset.
228+ canonical , err := o .chain .Contracts .RPC .header (ctx , hexutil .EncodeBig ((* big .Int )(head .Number )))
229+ if err != nil || canonical .Hash != head .Hash {
230+ o .invalidate ()
231+ return ErrObservationUnavailable
232+ }
233+ if err := ctx .Err (); err != nil {
234+ return err
235+ }
196236 o .mu .Lock ()
197237 defer o .mu .Unlock ()
198238 progress , l1Progress , highWater := o .progress , o .l1Progress , o .l1HighWater
@@ -206,6 +246,12 @@ func (o *PaymentObserver) refresh(ctx context.Context) error {
206246 if o .stopped || o .expired (s , l1Progress ) {
207247 return ErrObservationUnavailable
208248 }
249+ for key , entry := range o .payers {
250+ if _ , affected := changed [key .payer ]; reset || affected {
251+ entry .version ++
252+ entry .valid , entry .failure = false , nil
253+ }
254+ }
209255 o .current , o .header , o .invalid = s , head , false
210256 o .progress , o .l1Progress , o .l1HighWater = progress , l1Progress , highWater
211257 o .requireL1 = o .requireL1 || head .L1BlockNumber != nil
0 commit comments