1 package verify
2 3 // The forked chain's consensus parameters that a swap verdict depends on. They are constants of
4 // this venue, not of Bitcoin, and borrowing Bitcoin's values here would be wrong in both
5 // directions: too generous a weight limit accepts a block the chain would reject, and Bitcoin's
6 // coinbase maturity would let a funding output look spendable long after it is.
7 const (
8 // XBTHeaderV2Height is the first block whose header carries the fork fields.
9 XBTHeaderV2Height uint32 = 961640
10 11 // XBTRDTSWeight is the block weight limit while RDTS applies. Bitcoin's is 4,000,000;
12 // using that here would accept blocks this chain refuses.
13 XBTRDTSWeight int64 = 800_000
14 15 // XBTCoinbaseMaturity is how many blocks a coinbase output must age before it can be
16 // spent on this chain.
17 XBTCoinbaseMaturity int64 = 6_480
18 19 // XBTHeaderV2Len is the wire size of a version 2 header.
20 XBTHeaderV2Len = 164
21 )
22 23 // Clock is the wall-clock estimate for a number of confirmations, built from observed inter-block
24 // intervals rather than from an assumed ten minutes.
25 type Clock struct {
26 // Intervals are the observed gaps in seconds, newest first is fine, order does not matter.
27 Intervals []int64
28 // Median is the median interval in seconds.
29 Median int64
30 // Fastest and Slowest bound the sample.
31 Fastest int64
32 Slowest int64
33 }
34 35 // NewClock summarises a set of inter-block intervals.
36 func NewClock(intervals []int64) Clock {
37 out := Clock{Intervals: append([]int64{}, intervals...)}
38 39 if len(out.Intervals) == 0 {
40 return out
41 }
42 43 sorted := append([]int64{}, out.Intervals...)
44 45 for i := 1; i < len(sorted); i++ {
46 for j := i; j > 0 && sorted[j] < sorted[j-1]; j-- {
47 sorted[j], sorted[j-1] = sorted[j-1], sorted[j]
48 }
49 }
50 51 out.Median = sorted[len(sorted)/2]
52 out.Fastest = sorted[0]
53 out.Slowest = sorted[len(sorted)-1]
54 55 return out
56 }
57 58 // Expected returns the expected elapsed seconds for depth confirmations, and the fast and slow
59 // bounds of the sample.
60 func (c Clock) Expected(depth int64) (expected, fast, slow int64) {
61 if depth <= 0 || c.Median <= 0 {
62 return 0, 0, 0
63 }
64 65 return depth * c.Median, depth * c.Fastest, depth * c.Slowest
66 }
67 68 // ImmatureCoinbase reports whether a coinbase output at this depth is still locked by the fork's
69 // maturity rule, which is long enough to outlast a swap's timeout.
70 func ImmatureCoinbase(depth int64) bool {
71 return depth < XBTCoinbaseMaturity
72 }
73