Skip to content

Commit 1c466cc

Browse files
authored
Merge pull request #24 from iotexproject/fix-epoch
fix epoch num
2 parents 1178675 + 2f24493 commit 1c466cc

File tree

3 files changed

+300
-126
lines changed

3 files changed

+300
-126
lines changed

common/epoch.go

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,53 @@
11
package common
22

33
import (
4+
"fmt"
5+
6+
"github.com/iotexproject/iotex-core/v2/action/protocol/rolldpos"
47
"github.com/iotexproject/iotex-core/v2/blockchain/genesis"
58
)
69

710
var (
8-
genesisCfg = genesis.Default
11+
genesisCfg = genesis.Default
12+
rolldposProtocol *rolldpos.Protocol
913
)
1014

1115
func init() {
12-
//hardcode here https://raw.githubusercontent.com/iotexproject/iotex-bootstrap/v1.1.3/genesis_mainnet.yaml
13-
genesisCfg.Blockchain.NumSubEpochs = 15
16+
g := genesisCfg
17+
fmt.Printf("genesis %+v\n", g)
18+
rolldposProtocol = rolldpos.NewProtocol(
19+
g.NumCandidateDelegates,
20+
g.NumDelegates,
21+
g.NumSubEpochs,
22+
rolldpos.EnableDardanellesSubEpoch(g.DardanellesBlockHeight, g.DardanellesNumSubEpochs),
23+
rolldpos.EnableWakeSubEpoch(g.WakeBlockHeight, g.WakeNumSubEpochs),
24+
)
1425
}
1526

1627
// https://github.com/millken/iotex-core/blob/77950cec681d2e441a77b2b9a162ffa1c4ca4f55/action/protocol/rolldpos/epoch.go#L213
1728
// GetEpochNum returns the number of the epoch for a given height
1829
func GetEpochNum(height uint64) uint64 {
19-
if height == 0 {
20-
return 0
21-
}
22-
p := genesisCfg.Blockchain
23-
if height <= p.DardanellesBlockHeight {
24-
return (height-1)/p.NumDelegates/p.NumSubEpochs + 1
25-
}
26-
dardanellesEpoch := GetEpochNum(p.DardanellesBlockHeight)
27-
dardanellesEpochHeight := GetEpochHeight(dardanellesEpoch)
28-
return dardanellesEpoch + (height-dardanellesEpochHeight)/p.NumDelegates/p.DardanellesNumSubEpochs
30+
return rolldposProtocol.GetEpochNum(height)
2931
}
3032

3133
// NumSubEpochs returns the number of subEpochs given a block height
3234
func NumSubEpochs(height uint64) uint64 {
33-
p := genesisCfg.Blockchain
34-
if height < p.DardanellesBlockHeight {
35-
return p.NumSubEpochs
36-
}
37-
return p.DardanellesNumSubEpochs
35+
return rolldposProtocol.NumSubEpochs(height)
3836
}
3937

4038
// GetEpochHeight returns the start height of an epoch
4139
func GetEpochHeight(epochNum uint64) uint64 {
42-
if epochNum == 0 {
43-
return 0
44-
}
45-
p := genesisCfg.Blockchain
46-
dardanellesEpoch := GetEpochNum(p.DardanellesBlockHeight)
47-
if epochNum <= dardanellesEpoch {
48-
return (epochNum-1)*p.NumDelegates*p.NumSubEpochs + 1
49-
}
50-
dardanellesEpochHeight := GetEpochHeight(dardanellesEpoch)
51-
return dardanellesEpochHeight + (epochNum-dardanellesEpoch)*p.NumDelegates*p.DardanellesNumSubEpochs
40+
return rolldposProtocol.GetEpochHeight(epochNum)
5241
}
5342

5443
// GetEpochLastBlockHeight returns the last height of an epoch
5544
func GetEpochLastBlockHeight(epochNum uint64) uint64 {
56-
return GetEpochHeight(epochNum+1) - 1
45+
return rolldposProtocol.GetEpochLastBlockHeight(epochNum)
5746
}
5847

5948
// GetSubEpochNum returns the sub epoch number of a block height
6049
func GetSubEpochNum(height uint64) uint64 {
61-
p := genesisCfg.Blockchain
62-
return (height - GetEpochHeight(GetEpochNum(height))) / p.NumDelegates
50+
return rolldposProtocol.GetSubEpochNum(height)
6351
}
6452

6553
// FairbankEffectiveHeight returns the effective height of fairbank = 5166361

go.mod

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ require (
99
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
1010
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0
1111
github.com/imdario/mergo v0.3.12
12-
github.com/iotexproject/go-pkgs v0.1.13
12+
github.com/iotexproject/go-pkgs v0.1.15
1313
github.com/iotexproject/iotex-address v0.2.8
14-
github.com/iotexproject/iotex-antenna-go/v2 v2.5.1
15-
github.com/iotexproject/iotex-core/v2 v2.1.0
14+
github.com/iotexproject/iotex-antenna-go/v2 v2.6.4-0.20250203054316-e9b97f4e8885
15+
github.com/iotexproject/iotex-core/v2 v2.2.0
1616
github.com/iotexproject/iotex-proto v0.6.4
1717
github.com/mitchellh/go-homedir v1.1.0
1818
github.com/pkg/errors v0.9.1
19-
github.com/prometheus/client_golang v1.17.0
19+
github.com/prometheus/client_golang v1.20.5
2020
github.com/sethvargo/go-envconfig v0.4.0
2121
github.com/shopspring/decimal v1.2.0
2222
github.com/shurcooL/graphql v0.0.0-20200928012149-18c5c3165e3a
23-
github.com/stretchr/testify v1.9.0
23+
github.com/stretchr/testify v1.10.0
2424
github.com/ysugimoto/grpc-graphql-gateway v0.22.0
25-
google.golang.org/genproto v0.0.0-20230127162408-596548ed4efa
26-
google.golang.org/grpc v1.65.0
27-
google.golang.org/protobuf v1.34.2
25+
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4
26+
google.golang.org/grpc v1.67.3
27+
google.golang.org/protobuf v1.36.4
2828
gopkg.in/yaml.v2 v2.4.0
2929
gorm.io/driver/mysql v1.2.0
3030
gorm.io/driver/postgres v1.2.2
@@ -33,24 +33,33 @@ require (
3333
)
3434

3535
require (
36+
github.com/DataDog/zstd v1.4.5 // indirect
3637
github.com/beorn7/perks v1.0.1 // indirect
3738
github.com/bits-and-blooms/bitset v1.13.0 // indirect
3839
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
3940
github.com/cespare/xxhash/v2 v2.3.0 // indirect
41+
github.com/cockroachdb/errors v1.8.1 // indirect
42+
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f // indirect
43+
github.com/cockroachdb/pebble v0.0.0-20230928194634-aa077af62593 // indirect
44+
github.com/cockroachdb/redact v1.0.8 // indirect
45+
github.com/cockroachdb/sentry-go v0.6.1-cockroachdb.2 // indirect
46+
github.com/cockroachdb/tokenbucket v0.0.0-20230807174530-cc333fc44b06 // indirect
4047
github.com/consensys/bavard v0.1.13 // indirect
4148
github.com/consensys/gnark-crypto v0.12.1 // indirect
4249
github.com/crate-crypto/go-ipa v0.0.0-20240223125850-b1e8a79f509c // indirect
4350
github.com/crate-crypto/go-kzg-4844 v0.7.0 // indirect
4451
github.com/davecgh/go-spew v1.1.1 // indirect
4552
github.com/deckarep/golang-set/v2 v2.6.0 // indirect
46-
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
53+
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
4754
github.com/dustinxie/gmsm v1.4.0 // indirect
4855
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
4956
github.com/ethereum/go-ethereum v1.14.12 // indirect
5057
github.com/fsnotify/fsnotify v1.6.0 // indirect
5158
github.com/go-sql-driver/mysql v1.6.0 // indirect
59+
github.com/gogo/protobuf v1.3.2 // indirect
5260
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
53-
github.com/golang/protobuf v1.5.4 // indirect
61+
github.com/golang/mock v1.6.0 // indirect
62+
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
5463
github.com/google/uuid v1.6.0 // indirect
5564
github.com/holiman/uint256 v1.3.1 // indirect
5665
github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334 // indirect
@@ -65,32 +74,37 @@ require (
6574
github.com/jackc/pgx/v4 v4.13.0 // indirect
6675
github.com/jinzhu/inflection v1.0.0 // indirect
6776
github.com/jinzhu/now v1.1.2 // indirect
77+
github.com/klauspost/compress v1.17.11 // indirect
78+
github.com/kr/pretty v0.3.1 // indirect
79+
github.com/kr/text v0.2.0 // indirect
6880
github.com/magefile/mage v1.9.0 // indirect
6981
github.com/mattn/go-sqlite3 v1.14.9 // indirect
70-
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
7182
github.com/mmcloughlin/addchain v0.4.0 // indirect
83+
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
7284
github.com/pmezard/go-difflib v1.0.0 // indirect
73-
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
74-
github.com/prometheus/common v0.44.0 // indirect
75-
github.com/prometheus/procfs v0.11.1 // indirect
85+
github.com/prometheus/client_model v0.6.1 // indirect
86+
github.com/prometheus/common v0.62.0 // indirect
87+
github.com/prometheus/procfs v0.15.1 // indirect
88+
github.com/rogpeppe/go-internal v1.13.1 // indirect
7689
github.com/spf13/cobra v1.5.0 // indirect
7790
github.com/spf13/pflag v1.0.5 // indirect
7891
github.com/supranational/blst v0.3.13 // indirect
7992
github.com/uptrace/opentelemetry-go-extra/otelutil v0.2.2 // indirect
8093
github.com/uptrace/opentelemetry-go-extra/otelzap v0.2.2 // indirect
8194
go.elastic.co/ecszap v1.0.0 // indirect
82-
go.opentelemetry.io/otel v1.28.0 // indirect
83-
go.opentelemetry.io/otel/trace v1.28.0 // indirect
95+
go.etcd.io/bbolt v1.3.6 // indirect
96+
go.opentelemetry.io/otel v1.34.0 // indirect
97+
go.opentelemetry.io/otel/trace v1.34.0 // indirect
8498
go.uber.org/config v1.3.1 // indirect
8599
go.uber.org/multierr v1.11.0 // indirect
86-
go.uber.org/zap v1.26.0 // indirect
87-
golang.org/x/crypto v0.25.0 // indirect
88-
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
89-
golang.org/x/net v0.27.0 // indirect
90-
golang.org/x/sync v0.7.0 // indirect
91-
golang.org/x/sys v0.22.0 // indirect
92-
golang.org/x/term v0.22.0 // indirect
93-
golang.org/x/text v0.16.0 // indirect
100+
go.uber.org/zap v1.27.0 // indirect
101+
golang.org/x/crypto v0.35.0 // indirect
102+
golang.org/x/exp v0.0.0-20250128182459-e0ece0dbea4c // indirect
103+
golang.org/x/net v0.34.0 // indirect
104+
golang.org/x/sync v0.11.0 // indirect
105+
golang.org/x/sys v0.30.0 // indirect
106+
golang.org/x/term v0.29.0 // indirect
107+
golang.org/x/text v0.22.0 // indirect
94108
gopkg.in/yaml.v3 v3.0.1 // indirect
95109
rsc.io/tmplfunc v0.0.3 // indirect
96110
)

0 commit comments

Comments
 (0)