|
| 1 | +// Adapted from livepeer/go-livepeer eth/watchers/timewatcher.go and |
| 2 | +// eth/blockwatch/block_watcher.go at 597f882cb9f4d42ad8430b0f592afd5929af4a6b: |
| 3 | +// Nico Vergauwen (b4605f705, 3aec3aa16), Yondon Fu (0acd88bbd), and |
| 4 | +// Rafał Leszko (4b6ede31f, 8c7ec0155). Immutable pinned observations and age |
| 5 | +// refusal also adapt Mike Zupper's BlueClawNetwork/livepeer-node eth/chainview.go |
| 6 | +// (2d95d40b8, 74f41b11f). Copyright (c) 2017 Livepeer; MIT notice in LICENSE. |
| 7 | +package eth |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "errors" |
| 12 | + "math/big" |
| 13 | + "sync" |
| 14 | + "time" |
| 15 | + |
| 16 | + "github.com/ethereum/go-ethereum/common" |
| 17 | + "github.com/ethereum/go-ethereum/common/hexutil" |
| 18 | +) |
| 19 | + |
| 20 | +var ErrObservationUnavailable = errors.New("chain observation unavailable") |
| 21 | + |
| 22 | +// PaymentObserver publishes complete canonical observations. Gaps and reorgs |
| 23 | +// rebuild state at the new hash instead of replaying uncertain event history. |
| 24 | +type PaymentObserver struct { |
| 25 | + chain PaymentChain |
| 26 | + interval, maxAge time.Duration |
| 27 | + now func() time.Time |
| 28 | + mu sync.RWMutex |
| 29 | + current ChainSnapshot |
| 30 | + header *miniHeader |
| 31 | + generation uint64 |
| 32 | + requireL1 bool |
| 33 | + invalid, stopped bool |
| 34 | + contact, progress time.Time |
| 35 | + l1Progress time.Time |
| 36 | + l1HighWater *big.Int |
| 37 | +} |
| 38 | + |
| 39 | +func NewPaymentObserver(chain PaymentChain, requireL1 bool) *PaymentObserver { |
| 40 | + chain.Observer = nil |
| 41 | + return &PaymentObserver{chain: chain, interval: 5 * time.Second, maxAge: 30 * time.Second, now: time.Now, requireL1: requireL1 || chain.Contracts.RPC.requireL1} |
| 42 | +} |
| 43 | + |
| 44 | +// Run has one service-owned caller. Every RPC inherits the service lifetime. |
| 45 | +func (o *PaymentObserver) Run(ctx context.Context) { |
| 46 | + defer func() { o.mu.Lock(); o.stopped = true; o.mu.Unlock() }() |
| 47 | + ticker := time.NewTicker(o.interval) |
| 48 | + defer ticker.Stop() |
| 49 | + for { |
| 50 | + read, cancel := context.WithTimeout(ctx, o.maxAge) |
| 51 | + _ = o.refresh(read) |
| 52 | + cancel() |
| 53 | + select { |
| 54 | + case <-ctx.Done(): |
| 55 | + return |
| 56 | + case <-ticker.C: |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// ObserverStatus separates successful contact from accepted state and progress. |
| 62 | +// A single provider cannot establish an absolute bound on protocol L1 lag. |
| 63 | +type ObserverStatus struct { |
| 64 | + ObservedAt, ContactAt, HeadProgressAt, L1ProgressAt time.Time |
| 65 | + Healthy bool |
| 66 | +} |
| 67 | + |
| 68 | +func (o *PaymentObserver) Status() ObserverStatus { |
| 69 | + o.mu.RLock() |
| 70 | + defer o.mu.RUnlock() |
| 71 | + return ObserverStatus{o.current.observedAt, o.contact, o.progress, o.l1Progress, o.validate(o.current) == nil} |
| 72 | +} |
| 73 | + |
| 74 | +func (o *PaymentObserver) Snapshot(ctx context.Context) (ChainSnapshot, error) { |
| 75 | + if err := ctx.Err(); err != nil { |
| 76 | + return ChainSnapshot{}, err |
| 77 | + } |
| 78 | + o.mu.RLock() |
| 79 | + defer o.mu.RUnlock() |
| 80 | + if err := o.validate(o.current); err != nil { |
| 81 | + return ChainSnapshot{}, err |
| 82 | + } |
| 83 | + s := o.current |
| 84 | + s.Block, s.Round, s.CurrentRound = new(big.Int).Set(s.Block), new(big.Int).Set(s.Round), new(big.Int).Set(s.CurrentRound) |
| 85 | + return s, nil |
| 86 | +} |
| 87 | + |
| 88 | +func (s ChainSnapshot) ObservedAt() time.Time { |
| 89 | + if s.observedAt.IsZero() { // Injected direct chains have no retained observation. |
| 90 | + return time.Now() |
| 91 | + } |
| 92 | + return s.observedAt |
| 93 | +} |
| 94 | + |
| 95 | +// Validate also rejects snapshots retained across an invalidation or shutdown. |
| 96 | +// Direct reads and injected test chains retain their existing behavior. |
| 97 | +func (s ChainSnapshot) Validate() error { |
| 98 | + if s.observer == nil { |
| 99 | + return nil |
| 100 | + } |
| 101 | + s.observer.mu.RLock() |
| 102 | + defer s.observer.mu.RUnlock() |
| 103 | + return s.observer.validate(s) |
| 104 | +} |
| 105 | + |
| 106 | +func (o *PaymentObserver) validate(s ChainSnapshot) error { |
| 107 | + if o.stopped || o.invalid || s.generation != o.generation || o.expired(s, o.l1Progress) { |
| 108 | + return ErrObservationUnavailable |
| 109 | + } |
| 110 | + return nil |
| 111 | +} |
| 112 | + |
| 113 | +func (o *PaymentObserver) expired(s ChainSnapshot, l1Progress time.Time) bool { |
| 114 | + now := o.now() |
| 115 | + return s.observedAt.IsZero() || now.Sub(s.observedAt) >= o.maxAge || now.Sub(s.blockTime) >= o.maxAge || now.Sub(l1Progress) >= o.maxAge |
| 116 | +} |
| 117 | + |
| 118 | +// Only Run calls refresh. Previous/current identity is the extension point for |
| 119 | +// complete hash-pinned logs or reset; no partial delta authorizes publication. |
| 120 | +func (o *PaymentObserver) refresh(ctx context.Context) error { |
| 121 | + at := o.now() |
| 122 | + head, err := o.chain.Contracts.RPC.header(ctx, "latest") |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + o.mu.Lock() |
| 127 | + o.contact = o.now() |
| 128 | + prior := o.header |
| 129 | + gap := false |
| 130 | + if head.Hash == (common.Hash{}) || head.Number == nil { |
| 131 | + o.mu.Unlock() |
| 132 | + return ErrObservationUnavailable |
| 133 | + } |
| 134 | + if prior != nil { |
| 135 | + distance := new(big.Int).Sub((*big.Int)(head.Number), (*big.Int)(prior.Number)) |
| 136 | + if distance.Sign() < 0 { |
| 137 | + o.invalid = true |
| 138 | + o.generation++ |
| 139 | + o.mu.Unlock() |
| 140 | + return ErrObservationUnavailable |
| 141 | + } |
| 142 | + gap = head.Hash != prior.Hash && distance.Cmp(big.NewInt(1)) > 0 |
| 143 | + if head.Hash != prior.Hash && !gap && (distance.Sign() == 0 || head.ParentHash != prior.Hash) { |
| 144 | + o.invalid = true |
| 145 | + o.generation++ |
| 146 | + } |
| 147 | + } |
| 148 | + // Usable identity conflicts invalidate even when other header metadata is |
| 149 | + // missing or stale. Such metadata must not restore a rejected observation. |
| 150 | + if head.Timestamp == nil || (o.requireL1 && head.L1BlockNumber == nil) { |
| 151 | + o.mu.Unlock() |
| 152 | + return ErrObservationUnavailable |
| 153 | + } |
| 154 | + blockTime := time.Unix(int64(*head.Timestamp), 0) |
| 155 | + if at.Sub(blockTime) >= o.maxAge || blockTime.After(at.Add(o.maxAge)) { |
| 156 | + o.mu.Unlock() |
| 157 | + return ErrObservationUnavailable |
| 158 | + } |
| 159 | + if prior != nil && head.Hash == prior.Hash { |
| 160 | + // A lagging backend may return N, N-1, then N again. Restore the |
| 161 | + // immutable observation without renewing any freshness deadline. |
| 162 | + o.current.generation, o.invalid = o.generation, false |
| 163 | + err := o.validate(o.current) |
| 164 | + o.mu.Unlock() |
| 165 | + return err |
| 166 | + } |
| 167 | + o.mu.Unlock() |
| 168 | + if gap { |
| 169 | + // A polling gap does not itself orphan the retained observation. Prove |
| 170 | + // its anchor before keeping it available during the pinned backfill. |
| 171 | + anchor, err := o.chain.Contracts.RPC.header(ctx, hexutil.EncodeBig((*big.Int)(prior.Number))) |
| 172 | + if err != nil || anchor.Hash != prior.Hash || anchor.Number == nil || (*big.Int)(anchor.Number).Cmp((*big.Int)(prior.Number)) != 0 { |
| 173 | + o.mu.Lock() |
| 174 | + o.invalid = true |
| 175 | + o.generation++ |
| 176 | + o.mu.Unlock() |
| 177 | + } |
| 178 | + } |
| 179 | + s, err := o.chain.snapshotAt(ctx, head, true) |
| 180 | + if err != nil { |
| 181 | + return err |
| 182 | + } |
| 183 | + if err := ctx.Err(); err != nil { |
| 184 | + return err |
| 185 | + } |
| 186 | + o.mu.Lock() |
| 187 | + defer o.mu.Unlock() |
| 188 | + progress, l1Progress, highWater := o.progress, o.l1Progress, o.l1HighWater |
| 189 | + if prior == nil || (*big.Int)(head.Number).Cmp((*big.Int)(prior.Number)) > 0 { |
| 190 | + progress = at |
| 191 | + } |
| 192 | + if highWater == nil || s.Block.Cmp(highWater) > 0 { |
| 193 | + l1Progress, highWater = at, s.Block |
| 194 | + } |
| 195 | + s.observer, s.generation, s.observedAt, s.blockTime = o, o.generation, at, blockTime |
| 196 | + if o.stopped || o.expired(s, l1Progress) { |
| 197 | + return ErrObservationUnavailable |
| 198 | + } |
| 199 | + o.current, o.header, o.invalid = s, head, false |
| 200 | + o.progress, o.l1Progress, o.l1HighWater = progress, l1Progress, highWater |
| 201 | + o.requireL1 = o.requireL1 || head.L1BlockNumber != nil |
| 202 | + return nil |
| 203 | +} |
0 commit comments