Skip to content

Commit bbddf37

Browse files
author
Naohiro Yoshida
committed
fix audit
Signed-off-by: Naohiro Yoshida <naohiro.yoshida@datachain.jp>
1 parent 6b4bcaf commit bbddf37

File tree

7 files changed

+410
-160
lines changed

7 files changed

+410
-160
lines changed

module/fork_spec.go

Lines changed: 78 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package module
33
import (
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,62 @@ const (
1618
Mainnet Network = "mainnet"
1719
)
1820

19-
var localLorentzHF isForkSpec_HeightOrTimestamp = &ForkSpec_Height{Height: 1}
21+
var localLatestHF isForkSpec_HeightOrTimestamp = &ForkSpec_Height{Height: 1}
2022

2123
func 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+
)
38+
39+
func getForkSpecParams() []*ForkSpec {
40+
return []*ForkSpec{
41+
// Pascal HF
42+
{
43+
AdditionalHeaderItemCount: 1,
44+
EpochLength: 200,
45+
MaxTurnLength: 9,
46+
GasLimitBoundDivider: 256,
47+
EnableHeaderMsec: false,
48+
},
49+
// Lorentz HF
50+
{
51+
AdditionalHeaderItemCount: 1,
52+
EpochLength: 500,
53+
MaxTurnLength: 64,
54+
GasLimitBoundDivider: 1024,
55+
EnableHeaderMsec: true,
56+
},
2957
}
3058
}
3159

3260
func GetForkParameters(network Network) []*ForkSpec {
61+
hardForks := getForkSpecParams()
3362
switch network {
3463
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-
}
64+
hardForks[indexPascalHF].HeightOrTimestamp = &ForkSpec_Height{Height: 0}
65+
hardForks[indexLorentzHF].HeightOrTimestamp = localLatestHF
66+
return hardForks
5367
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-
}
68+
hardForks[indexPascalHF].HeightOrTimestamp = &ForkSpec_Height{Height: 48576786}
69+
hardForks[indexLorentzHF].HeightOrTimestamp = &ForkSpec_Height{Height: 49791365}
70+
return hardForks
6971
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-
}
72+
hardForks[indexPascalHF].HeightOrTimestamp = &ForkSpec_Height{Height: 47618307}
73+
// https://bscscan.com/block/48773576
74+
// https://github.com/bnb-chain/bsc/releases/tag/v1.5.10
75+
hardForks[indexLorentzHF].HeightOrTimestamp = &ForkSpec_Height{Height: 48773576}
76+
return hardForks
8877
}
8978
return nil
9079
}
@@ -103,33 +92,45 @@ type BoundaryHeight struct {
10392
CurrentForkSpec ForkSpec
10493
}
10594

106-
func (b BoundaryHeight) GetBoundaryEpochs(prevForkSpec ForkSpec) (*BoundaryEpochs, error) {
95+
func (b BoundaryHeight) GetBoundaryEpochs(prevForkSpecs []*ForkSpec) (*BoundaryEpochs, error) {
96+
if len(prevForkSpecs) == 0 {
97+
return nil, errors.New("EmptyPreviousForkSpecs")
98+
}
99+
prevForkSpec := prevForkSpecs[0]
107100
boundaryHeight := b.Height
108101
prevLast := boundaryHeight - (boundaryHeight % prevForkSpec.EpochLength)
109-
index := uint64(0)
110102
currentFirst := uint64(0)
111-
for {
112-
candidate := boundaryHeight + index
113-
if candidate%b.CurrentForkSpec.EpochLength == 0 {
114-
currentFirst = candidate
115-
break
116-
}
117-
index++
103+
if boundaryHeight%b.CurrentForkSpec.EpochLength == 0 {
104+
currentFirst = boundaryHeight
105+
} else {
106+
currentFirst = boundaryHeight + (b.CurrentForkSpec.EpochLength - boundaryHeight%b.CurrentForkSpec.EpochLength)
118107
}
108+
119109
intermediates := make([]uint64, 0)
120-
// starts 0, 200, 400...epoch_length
110+
121111
if prevLast == 0 {
122-
const defaultEpochLength = 200
123-
for mid := prevLast + defaultEpochLength; mid < prevForkSpec.EpochLength; mid += defaultEpochLength {
124-
intermediates = append(intermediates, mid)
112+
epochLengthList := uniqMap(prevForkSpecs, func(item *ForkSpec, index int) uint64 {
113+
return item.EpochLength
114+
})
115+
slices.Reverse(epochLengthList)
116+
for i := 0; i < len(epochLengthList)-1; i++ {
117+
start, end := epochLengthList[i], epochLengthList[i+1]
118+
value := start
119+
for value < end {
120+
intermediates = append(intermediates, value)
121+
value += start
122+
}
125123
}
126124
}
127-
for mid := prevLast + prevForkSpec.EpochLength; mid < currentFirst; mid += prevForkSpec.EpochLength {
125+
126+
mid := prevLast + prevForkSpec.EpochLength
127+
for mid < currentFirst {
128128
intermediates = append(intermediates, mid)
129+
mid += prevForkSpec.EpochLength
129130
}
130131

131132
return &BoundaryEpochs{
132-
PreviousForkSpec: prevForkSpec,
133+
PreviousForkSpec: *prevForkSpec,
133134
CurrentForkSpec: b.CurrentForkSpec,
134135
BoundaryHeight: boundaryHeight,
135136
PrevLast: prevLast,
@@ -180,27 +181,27 @@ func (be BoundaryEpochs) PreviousEpochBlockNumber(currentEpochBlockNumber uint64
180181
return currentEpochBlockNumber - be.CurrentForkSpec.EpochLength
181182
}
182183

183-
func FindTargetForkSpec(forkSpecs []*ForkSpec, height uint64, timestamp uint64) (*ForkSpec, *ForkSpec, error) {
184+
func FindTargetForkSpec(forkSpecs []*ForkSpec, height uint64, timestamp uint64) (*ForkSpec, []*ForkSpec, error) {
184185
reversed := make([]*ForkSpec, len(forkSpecs))
185186
for i, spec := range forkSpecs {
186187
reversed[len(forkSpecs)-i-1] = spec
187188
}
188189

189-
getPrev := func(current *ForkSpec, i int) *ForkSpec {
190+
getPreviousForkSpecs := func(current *ForkSpec, i int) []*ForkSpec {
190191
if i == len(reversed)-1 {
191-
return current
192+
return []*ForkSpec{current}
192193
}
193-
return reversed[i+1]
194+
return reversed[i+1:]
194195
}
195196

196197
for i, spec := range reversed {
197198
if x, ok := spec.GetHeightOrTimestamp().(*ForkSpec_Height); ok {
198199
if x.Height <= height {
199-
return spec, getPrev(spec, i), nil
200+
return spec, getPreviousForkSpecs(spec, i), nil
200201
}
201202
} else {
202203
if spec.GetTimestamp() <= timestamp {
203-
return spec, getPrev(spec, i), nil
204+
return spec, getPreviousForkSpecs(spec, i), nil
204205
}
205206
}
206207
}

0 commit comments

Comments
 (0)