package verify // The forked chain's consensus parameters that a swap verdict depends on. They are constants of // this venue, not of Bitcoin, and borrowing Bitcoin's values here would be wrong in both // directions: too generous a weight limit accepts a block the chain would reject, and Bitcoin's // coinbase maturity would let a funding output look spendable long after it is. const ( // XBTHeaderV2Height is the first block whose header carries the fork fields. XBTHeaderV2Height uint32 = 961640 // XBTRDTSWeight is the block weight limit while RDTS applies. Bitcoin's is 4,000,000; // using that here would accept blocks this chain refuses. XBTRDTSWeight int64 = 800_000 // XBTCoinbaseMaturity is how many blocks a coinbase output must age before it can be // spent on this chain. XBTCoinbaseMaturity int64 = 6_480 // XBTHeaderV2Len is the wire size of a version 2 header. XBTHeaderV2Len = 164 ) // Clock is the wall-clock estimate for a number of confirmations, built from observed inter-block // intervals rather than from an assumed ten minutes. type Clock struct { // Intervals are the observed gaps in seconds, newest first is fine, order does not matter. Intervals []int64 // Median is the median interval in seconds. Median int64 // Fastest and Slowest bound the sample. Fastest int64 Slowest int64 } // NewClock summarises a set of inter-block intervals. func NewClock(intervals []int64) Clock { out := Clock{Intervals: append([]int64{}, intervals...)} if len(out.Intervals) == 0 { return out } sorted := append([]int64{}, out.Intervals...) for i := 1; i < len(sorted); i++ { for j := i; j > 0 && sorted[j] < sorted[j-1]; j-- { sorted[j], sorted[j-1] = sorted[j-1], sorted[j] } } out.Median = sorted[len(sorted)/2] out.Fastest = sorted[0] out.Slowest = sorted[len(sorted)-1] return out } // Expected returns the expected elapsed seconds for depth confirmations, and the fast and slow // bounds of the sample. func (c Clock) Expected(depth int64) (expected, fast, slow int64) { if depth <= 0 || c.Median <= 0 { return 0, 0, 0 } return depth * c.Median, depth * c.Fastest, depth * c.Slowest } // ImmatureCoinbase reports whether a coinbase output at this depth is still locked by the fork's // maturity rule, which is long enough to outlast a swap's timeout. func ImmatureCoinbase(depth int64) bool { return depth < XBTCoinbaseMaturity }