forked from ava-labs/coreth
/
fork.go
54 lines (46 loc) · 1.54 KB
/
fork.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package utils
import (
"math/big"
"time"
)
func NewUint64(val uint64) *uint64 { return &val }
func TimeToNewUint64(time time.Time) *uint64 {
unix := uint64(time.Unix())
return NewUint64(unix)
}
func Uint64ToTime(val *uint64) time.Time {
timestamp := int64(*val)
return time.Unix(timestamp, 0)
}
// IsBlockForked returns whether a fork scheduled at block s is active at the given head block.
// Note: [s] and [head] can be either a block number or a block timestamp.
func IsBlockForked(s, head *big.Int) bool {
if s == nil || head == nil {
return false
}
return s.Cmp(head) <= 0
}
// IsTimestampForked returns whether a fork scheduled at timestamp s is active
// at the given head timestamp. Whilst this method is the same as isBlockForked,
// they are explicitly separate for clearer reading.
func IsTimestampForked(s *uint64, head uint64) bool {
if s == nil {
return false
}
return *s <= head
}
// IsForkTransition returns true if [fork] activates during the transition from
// [parent] to [current].
// Taking [parent] as a pointer allows for us to pass nil when checking forks
// that activate during genesis.
// Note: this works for both block number and timestamp activated forks.
func IsForkTransition(fork *uint64, parent *uint64, current uint64) bool {
var parentForked bool
if parent != nil {
parentForked = IsTimestampForked(fork, *parent)
}
currentForked := IsTimestampForked(fork, current)
return !parentForked && currentForked
}