Skip to content

Commit d41e672

Browse files
yoshidanNaohiro Yoshida
andauthored
Fix Audit (#63)
* fix audit Signed-off-by: Naohiro Yoshida <naohiro.yoshida@datachain.jp> * add elc version Signed-off-by: Naohiro Yoshida <naohiro.yoshida@datachain.jp> * Maxwell HF (#66) * Fix Maxwell Height (#67) --------- Signed-off-by: Naohiro Yoshida <naohiro.yoshida@datachain.jp> Co-authored-by: Naohiro Yoshida <naohiro.yoshida@datachain.jp>
1 parent 1346504 commit d41e672

File tree

14 files changed

+431
-165
lines changed

14 files changed

+431
-165
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ jobs:
5555
make chain
5656
make contracts
5757
# epoch is 200, 400, 500, 1000
58-
# must wait for 400 block (1.5sec * 400)
58+
# Since we want the previous epoch to be before HF we wait for 800 block (0.75sec * 800)
5959
sleep 600
6060
make relayer
6161
make test

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
## Supported Versions
66
- [yui-relayer v0.5.11](https://github.com/hyperledger-labs/yui-relayer/releases/tag/v0.5.11)
7-
- [ethereum-ibc-relay-chain v0.3.16](https://github.com/datachainlab/ethereum-ibc-relay-chain/releases/tag/v0.3.6)
7+
- [ethereum-ibc-relay-chain v0.3.17](https://github.com/datachainlab/ethereum-ibc-relay-chain/releases/tag/v0.3.7)
8+
- [parlia-elc v0.3.10](https://github.com/datachainlab/parlia-elc/releases/tag/v0.3.10)
89

910
## Setup Relayer
1011

e2e/chains/bsc/Dockerfile.bootstrap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ RUN apk add --d --no-cache npm nodejs bash alpine-sdk expect jq curl bash python
44
RUN curl -sSL https://install.python-poetry.org | python3 -
55

66
RUN git clone https://github.com/bnb-chain/bsc-genesis-contract -b develop /root/genesis \
7-
&& cd /root/genesis && git checkout 44ebc6c17a00bd24db3240141a78091528dcebbb && npm ci
7+
&& cd /root/genesis && git checkout bf3ac733f8aaf93ed88ca0ad2dcddd051166e4e1 && npm ci
88

99
RUN cd /root/genesis && /root/.local/bin/poetry install
1010
RUN cd /root/genesis && forge install --no-git --no-commit foundry-rs/forge-std@v1.7.3

e2e/chains/bsc/docker-compose.bsc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ services:
55
dockerfile: Dockerfile.bsc
66
args:
77
GIT_SOURCE: https://github.com/bnb-chain/bsc
8-
GIT_CHECKOUT_BRANCH: v1.5.10
8+
GIT_CHECKOUT_BRANCH: v1.5.13
99
image: bsc-geth:docker-local

e2e/chains/bsc/genesis/genesis-template.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"pascalTime": 0,
3737
"pragueTime": 0,
3838
"lorentzTime": 0,
39+
"maxwellTime": 0,
3940
"blobSchedule": {
4041
"cancun": {
4142
"target": 3,

e2e/chains/bsc/scripts/bootstrap.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ function generate_genesis() {
5656
echo "start generate process"
5757
/root/.local/bin/poetry run python3 scripts/generate.py dev
5858

59+
echo "move generate-dev.json to genesis.json"
60+
mv genesis-dev.json genesis.json
5961
}
6062

6163
function init_genesis_data() {

module/fork_spec.go

Lines changed: 90 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,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

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+
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

3269
func 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

Comments
 (0)