@@ -3,8 +3,10 @@ package module
33import (
44 "context"
55 "fmt"
6+ "github.com/cockroachdb/errors"
67 "github.com/hyperledger-labs/yui-relayer/log"
78 "os"
9+ "slices"
810 "strconv"
911)
1012
@@ -16,75 +18,74 @@ const (
1618 Mainnet Network = "mainnet"
1719)
1820
19- var localLorentzHF isForkSpec_HeightOrTimestamp = & ForkSpec_Height {Height : 1 }
21+ var localLatestHF isForkSpec_HeightOrTimestamp = & ForkSpec_Height {Height : 2 }
2022
2123func init () {
22- localLorentzHFTimestamp := os .Getenv ("LOCAL_LORENTZ_HF_TIMESTAMP " )
23- if localLorentzHFTimestamp != "" {
24- result , err := strconv .Atoi (localLorentzHFTimestamp )
24+ localLatestHFTimestamp := os .Getenv ("LOCAL_LATEST_HF_TIMESTAMP " )
25+ if localLatestHFTimestamp != "" {
26+ result , err := strconv .Atoi (localLatestHFTimestamp )
2527 if err != nil {
2628 panic (err )
2729 }
28- localLorentzHF = & ForkSpec_Timestamp {Timestamp : uint64 (result )}
30+ localLatestHF = & ForkSpec_Timestamp {Timestamp : uint64 (result )}
31+ }
32+ }
33+
34+ const (
35+ indexPascalHF = 0
36+ indexLorentzHF = 1
37+ indexMaxwellHF = 2
38+ )
39+
40+ func getForkSpecParams () []* ForkSpec {
41+ return []* ForkSpec {
42+ // Pascal HF
43+ {
44+ AdditionalHeaderItemCount : 1 ,
45+ EpochLength : 200 ,
46+ MaxTurnLength : 9 ,
47+ GasLimitBoundDivider : 256 ,
48+ EnableHeaderMsec : false ,
49+ },
50+ // Lorentz HF
51+ {
52+ AdditionalHeaderItemCount : 1 ,
53+ EpochLength : 500 ,
54+ MaxTurnLength : 64 ,
55+ GasLimitBoundDivider : 1024 ,
56+ EnableHeaderMsec : true ,
57+ },
58+ // Maxwell HF
59+ {
60+ AdditionalHeaderItemCount : 1 ,
61+ EpochLength : 1000 ,
62+ MaxTurnLength : 64 ,
63+ GasLimitBoundDivider : 1024 ,
64+ EnableHeaderMsec : true ,
65+ },
2966 }
3067}
3168
3269func GetForkParameters (network Network ) []* ForkSpec {
70+ hardForks := getForkSpecParams ()
3371 switch network {
3472 case Localnet :
35- return []* ForkSpec {
36- // Pascal HF
37- {
38- // Must Set Milli timestamp
39- HeightOrTimestamp : & ForkSpec_Height {Height : 0 },
40- AdditionalHeaderItemCount : 1 ,
41- EpochLength : 200 ,
42- MaxTurnLength : 9 ,
43- },
44- // Lorentz HF
45- {
46- // Must Set Milli timestamp
47- HeightOrTimestamp : localLorentzHF ,
48- AdditionalHeaderItemCount : 1 ,
49- EpochLength : 500 ,
50- MaxTurnLength : 64 ,
51- },
52- }
73+ hardForks [indexPascalHF ].HeightOrTimestamp = & ForkSpec_Height {Height : 0 }
74+ hardForks [indexLorentzHF ].HeightOrTimestamp = & ForkSpec_Height {Height : 1 }
75+ hardForks [indexMaxwellHF ].HeightOrTimestamp = localLatestHF
76+ return hardForks
5377 case Testnet :
54- return []* ForkSpec {
55- {
56- // https://forum.bnbchain.org/t/bnb-chain-upgrades-testnet/934
57- HeightOrTimestamp : & ForkSpec_Height {Height : 48576786 },
58- AdditionalHeaderItemCount : 1 ,
59- EpochLength : 200 ,
60- MaxTurnLength : 9 ,
61- },
62- {
63- HeightOrTimestamp : & ForkSpec_Height {Height : 49791365 },
64- AdditionalHeaderItemCount : 1 ,
65- EpochLength : 500 ,
66- MaxTurnLength : 64 ,
67- },
68- }
78+ hardForks [indexPascalHF ].HeightOrTimestamp = & ForkSpec_Height {Height : 48576786 }
79+ hardForks [indexLorentzHF ].HeightOrTimestamp = & ForkSpec_Height {Height : 49791365 }
80+ hardForks [indexMaxwellHF ].HeightOrTimestamp = & ForkSpec_Height {Height : 52552978 }
81+ return hardForks
6982 case Mainnet :
70- return []* ForkSpec {
71- {
72- // https://bscscan.com/block/47618307
73- // https://github.com/bnb-chain/bsc/releases/tag/v1.5.7
74- HeightOrTimestamp : & ForkSpec_Height {Height : 47618307 },
75- AdditionalHeaderItemCount : 1 ,
76- EpochLength : 200 ,
77- MaxTurnLength : 9 ,
78- },
79- // https://bscscan.com/block/48773576
80- // https://github.com/bnb-chain/bsc/releases/tag/v1.5.10
81- {
82- HeightOrTimestamp : & ForkSpec_Height {Height : 48773576 },
83- AdditionalHeaderItemCount : 1 ,
84- EpochLength : 500 ,
85- MaxTurnLength : 64 ,
86- },
87- }
83+ hardForks [indexPascalHF ].HeightOrTimestamp = & ForkSpec_Height {Height : 47618307 }
84+ // https://bscscan.com/block/48773576
85+ // https://github.com/bnb-chain/bsc/releases/tag/v1.5.10
86+ hardForks [indexLorentzHF ].HeightOrTimestamp = & ForkSpec_Height {Height : 48773576 }
87+ //TODO Maxwell
88+ return hardForks
8889 }
8990 return nil
9091}
@@ -103,33 +104,45 @@ type BoundaryHeight struct {
103104 CurrentForkSpec ForkSpec
104105}
105106
106- func (b BoundaryHeight ) GetBoundaryEpochs (prevForkSpec ForkSpec ) (* BoundaryEpochs , error ) {
107+ func (b BoundaryHeight ) GetBoundaryEpochs (prevForkSpecs []* ForkSpec ) (* BoundaryEpochs , error ) {
108+ if len (prevForkSpecs ) == 0 {
109+ return nil , errors .New ("EmptyPreviousForkSpecs" )
110+ }
111+ prevForkSpec := prevForkSpecs [0 ]
107112 boundaryHeight := b .Height
108113 prevLast := boundaryHeight - (boundaryHeight % prevForkSpec .EpochLength )
109- index := uint64 (0 )
110114 currentFirst := uint64 (0 )
111- for {
112- candidate := boundaryHeight + index
113- if candidate % b .CurrentForkSpec .EpochLength == 0 {
114- currentFirst = candidate
115- break
116- }
117- index ++
115+ if boundaryHeight % b .CurrentForkSpec .EpochLength == 0 {
116+ currentFirst = boundaryHeight
117+ } else {
118+ currentFirst = boundaryHeight + (b .CurrentForkSpec .EpochLength - boundaryHeight % b .CurrentForkSpec .EpochLength )
118119 }
120+
119121 intermediates := make ([]uint64 , 0 )
120- // starts 0, 200, 400...epoch_length
122+
121123 if prevLast == 0 {
122- const defaultEpochLength = 200
123- for mid := prevLast + defaultEpochLength ; mid < prevForkSpec .EpochLength ; mid += defaultEpochLength {
124- intermediates = append (intermediates , mid )
124+ epochLengthList := uniqMap (prevForkSpecs , func (item * ForkSpec , index int ) uint64 {
125+ return item .EpochLength
126+ })
127+ slices .Reverse (epochLengthList )
128+ for i := 0 ; i < len (epochLengthList )- 1 ; i ++ {
129+ start , end := epochLengthList [i ], epochLengthList [i + 1 ]
130+ value := start
131+ for value < end {
132+ intermediates = append (intermediates , value )
133+ value += start
134+ }
125135 }
126136 }
127- for mid := prevLast + prevForkSpec .EpochLength ; mid < currentFirst ; mid += prevForkSpec .EpochLength {
137+
138+ mid := prevLast + prevForkSpec .EpochLength
139+ for mid < currentFirst {
128140 intermediates = append (intermediates , mid )
141+ mid += prevForkSpec .EpochLength
129142 }
130143
131144 return & BoundaryEpochs {
132- PreviousForkSpec : prevForkSpec ,
145+ PreviousForkSpec : * prevForkSpec ,
133146 CurrentForkSpec : b .CurrentForkSpec ,
134147 BoundaryHeight : boundaryHeight ,
135148 PrevLast : prevLast ,
@@ -180,27 +193,27 @@ func (be BoundaryEpochs) PreviousEpochBlockNumber(currentEpochBlockNumber uint64
180193 return currentEpochBlockNumber - be .CurrentForkSpec .EpochLength
181194}
182195
183- func FindTargetForkSpec (forkSpecs []* ForkSpec , height uint64 , timestamp uint64 ) (* ForkSpec , * ForkSpec , error ) {
196+ func FindTargetForkSpec (forkSpecs []* ForkSpec , height uint64 , timestamp uint64 ) (* ForkSpec , [] * ForkSpec , error ) {
184197 reversed := make ([]* ForkSpec , len (forkSpecs ))
185198 for i , spec := range forkSpecs {
186199 reversed [len (forkSpecs )- i - 1 ] = spec
187200 }
188201
189- getPrev := func (current * ForkSpec , i int ) * ForkSpec {
202+ getPreviousForkSpecs := func (current * ForkSpec , i int ) [] * ForkSpec {
190203 if i == len (reversed )- 1 {
191- return current
204+ return [] * ForkSpec { current }
192205 }
193- return reversed [i + 1 ]
206+ return reversed [i + 1 : ]
194207 }
195208
196209 for i , spec := range reversed {
197210 if x , ok := spec .GetHeightOrTimestamp ().(* ForkSpec_Height ); ok {
198211 if x .Height <= height {
199- return spec , getPrev (spec , i ), nil
212+ return spec , getPreviousForkSpecs (spec , i ), nil
200213 }
201214 } else {
202215 if spec .GetTimestamp () <= timestamp {
203- return spec , getPrev (spec , i ), nil
216+ return spec , getPreviousForkSpecs (spec , i ), nil
204217 }
205218 }
206219 }
0 commit comments