Skip to content

Commit 403887a

Browse files
committed
feat: implement FIP-0063
1 parent 9e03fca commit 403887a

13 files changed

+115
-96
lines changed

build/drand.go

+17
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
DrandDevnet
2828
DrandLocalnet
2929
DrandIncentinet
30+
DrandQuicknet
3031
)
3132

3233
var DrandConfigs = map[DrandEnum]dtypes.DrandConfig{
@@ -36,6 +37,7 @@ var DrandConfigs = map[DrandEnum]dtypes.DrandConfig{
3637
"https://api2.drand.sh",
3738
"https://api3.drand.sh",
3839
"https://drand.cloudflare.com",
40+
"https://api.drand.secureweb3.com:6875", // Storswift
3941
},
4042
Relays: []string{
4143
"/dnsaddr/api.drand.sh/",
@@ -44,6 +46,21 @@ var DrandConfigs = map[DrandEnum]dtypes.DrandConfig{
4446
},
4547
ChainInfoJSON: `{"public_key":"868f005eb8e6e4ca0a47c8a77ceaa5309a47978a7c71bc5cce96366b5d7a569937c529eeda66c7293784a9402801af31","period":30,"genesis_time":1595431050,"hash":"8990e7a9aaed2ffed73dbd7092123d6f289930540d7651336225dc172e51b2ce","groupHash":"176f93498eac9ca337150b46d21dd58673ea4e3581185f869672e59fa4cb390a"}`,
4648
},
49+
DrandQuicknet: {
50+
Servers: []string{
51+
"https://api.drand.sh",
52+
"https://api2.drand.sh",
53+
"https://api3.drand.sh",
54+
"https://drand.cloudflare.com",
55+
"https://api.drand.secureweb3.com:6875", // Storswift
56+
},
57+
Relays: []string{
58+
"/dnsaddr/api.drand.sh/",
59+
"/dnsaddr/api2.drand.sh/",
60+
"/dnsaddr/api3.drand.sh/",
61+
},
62+
ChainInfoJSON: `{"public_key":"83cf0f2896adee7eb8b5f01fcad3912212c437e0073e911fb90022d3e760183c8c4b450b6a0a6c3ac6a5776a2d1064510d1fec758c921cc22b0e17e63aaf4bcb5ed66304de9cf809bd274ca73bab4af5a6e9c76a4bc09e76eae8991ef5ece45a","period":3,"genesis_time":1692803367,"hash":"52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971","groupHash":"f477d5c89f21a17c863a7f937c6a6d15859414d2be09cd448d4279af331c5d3e","schemeID":"bls-unchained-g1-rfc9380","metadata":{"beaconID":"quicknet"}}`,
63+
},
4764
DrandTestnet: {
4865
Servers: []string{
4966
"https://pl-eu.testnet.drand.sh",

build/params_2k.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ var UpgradeThunderHeight = abi.ChainEpoch(-23)
6767

6868
var UpgradeWatermelonHeight = abi.ChainEpoch(-24)
6969

70-
var UpgradePineappleHeight = abi.ChainEpoch(200)
70+
var UpgradePineappleHeight = abi.ChainEpoch(20)
7171

7272
// This fix upgrade only ran on calibrationnet
7373
const UpgradeWatermelonFixHeight = -100
@@ -76,7 +76,8 @@ const UpgradeWatermelonFixHeight = -100
7676
const UpgradeWatermelonFix2Height = -101
7777

7878
var DrandSchedule = map[abi.ChainEpoch]DrandEnum{
79-
0: DrandMainnet,
79+
0: DrandMainnet,
80+
UpgradePineappleHeight + 10: DrandQuicknet,
8081
}
8182

8283
var SupportedProofTypes = []abi.RegisteredSealProof{

build/params_butterfly.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import (
1616
)
1717

1818
var DrandSchedule = map[abi.ChainEpoch]DrandEnum{
19-
0: DrandMainnet,
19+
0: DrandMainnet,
20+
UpgradePineappleHeight + 10: DrandQuicknet,
2021
}
2122

2223
const GenesisNetworkVersion = network.Version20

build/params_calibnet.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import (
1919
)
2020

2121
var DrandSchedule = map[abi.ChainEpoch]DrandEnum{
22-
0: DrandMainnet,
22+
0: DrandMainnet,
23+
UpgradePineappleHeight + 10: DrandQuicknet,
2324
}
2425

2526
const GenesisNetworkVersion = network.Version0

build/params_interop.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ const UpgradeWatermelonFixHeight = -1
6464
const UpgradeWatermelonFix2Height = -2
6565

6666
var DrandSchedule = map[abi.ChainEpoch]DrandEnum{
67-
0: DrandMainnet,
67+
0: DrandMainnet,
68+
UpgradePineappleHeight + 10: DrandQuicknet,
6869
}
6970

7071
var SupportedProofTypes = []abi.RegisteredSealProof{

build/params_mainnet.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import (
1616
)
1717

1818
var DrandSchedule = map[abi.ChainEpoch]DrandEnum{
19-
0: DrandIncentinet,
20-
UpgradeSmokeHeight: DrandMainnet,
19+
0: DrandIncentinet,
20+
UpgradeSmokeHeight: DrandMainnet,
21+
UpgradePineappleHeight + 10: DrandQuicknet,
2122
}
2223

2324
var NetworkBundle = "mainnet"

build/params_testground.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ var (
115115
UpgradePineappleHeight abi.ChainEpoch = -26
116116

117117
DrandSchedule = map[abi.ChainEpoch]DrandEnum{
118-
0: DrandMainnet,
118+
0: DrandMainnet,
119+
UpgradePineappleHeight + 10: DrandQuicknet,
119120
}
120121

121122
GenesisNetworkVersion = network.Version0

chain/beacon/beacon.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,21 @@ type BeaconPoint struct {
4343
// been posted on chain.
4444
type RandomBeacon interface {
4545
Entry(context.Context, uint64) <-chan Response
46-
VerifyEntry(types.BeaconEntry, types.BeaconEntry) error
46+
VerifyEntry(entry types.BeaconEntry, prevEntrySig []byte) error
4747
MaxBeaconRoundForEpoch(network.Version, abi.ChainEpoch) uint64
4848
}
4949

5050
func ValidateBlockValues(bSchedule Schedule, nv network.Version, h *types.BlockHeader, parentEpoch abi.ChainEpoch,
5151
prevEntry types.BeaconEntry) error {
52-
{
52+
// Before nv22 we had "chained" beacons, and so required two entries at a fork
53+
if nv < network.Version22 {
5354
parentBeacon := bSchedule.BeaconForEpoch(parentEpoch)
5455
currBeacon := bSchedule.BeaconForEpoch(h.Height)
5556
if parentBeacon != currBeacon {
5657
if len(h.BeaconEntries) != 2 {
5758
return xerrors.Errorf("expected two beacon entries at beacon fork, got %d", len(h.BeaconEntries))
5859
}
59-
err := currBeacon.VerifyEntry(h.BeaconEntries[1], h.BeaconEntries[0])
60+
err := currBeacon.VerifyEntry(h.BeaconEntries[1], h.BeaconEntries[0].Data)
6061
if err != nil {
6162
return xerrors.Errorf("beacon at fork point invalid: (%v, %v): %w",
6263
h.BeaconEntries[1], h.BeaconEntries[0], err)
@@ -65,9 +66,9 @@ func ValidateBlockValues(bSchedule Schedule, nv network.Version, h *types.BlockH
6566
}
6667
}
6768

68-
// TODO: fork logic
6969
b := bSchedule.BeaconForEpoch(h.Height)
7070
maxRound := b.MaxBeaconRoundForEpoch(nv, h.Height)
71+
// We don't expect to ever actually meet this condition
7172
if maxRound == prevEntry.Round {
7273
if len(h.BeaconEntries) != 0 {
7374
return xerrors.Errorf("expected not to have any beacon entries in this block, got %d", len(h.BeaconEntries))
@@ -79,7 +80,12 @@ func ValidateBlockValues(bSchedule Schedule, nv network.Version, h *types.BlockH
7980
return xerrors.Errorf("expected to have beacon entries in this block, but didn't find any")
8081
}
8182

82-
// Verify that the last beacon entry's round corresponds to the round we expect
83+
if nv < network.Version22 && prevEntry.Round == 0 {
84+
// This basically means that the drand entry of the first non-genesis tipset isn't verified IF we are starting on Drand mainnet (the "chained" drand)
85+
// Networks that start on drand quicknet, or other unchained randomness sources, will still verify it
86+
return nil
87+
}
88+
8389
last := h.BeaconEntries[len(h.BeaconEntries)-1]
8490
if last.Round != maxRound {
8591
return xerrors.Errorf("expected final beacon entry in block to be at round %d, got %d", maxRound, last.Round)
@@ -95,7 +101,7 @@ func ValidateBlockValues(bSchedule Schedule, nv network.Version, h *types.BlockH
95101

96102
// Verify the beacon entries themselves
97103
for i, e := range h.BeaconEntries {
98-
if err := b.VerifyEntry(e, prevEntry); err != nil {
104+
if err := b.VerifyEntry(e, prevEntry.Data); err != nil {
99105
return xerrors.Errorf("beacon entry %d (%d - %x (%d)) was invalid: %w", i, e.Round, e.Data, len(e.Data), err)
100106
}
101107
prevEntry = e
@@ -105,7 +111,8 @@ func ValidateBlockValues(bSchedule Schedule, nv network.Version, h *types.BlockH
105111
}
106112

107113
func BeaconEntriesForBlock(ctx context.Context, bSchedule Schedule, nv network.Version, epoch abi.ChainEpoch, parentEpoch abi.ChainEpoch, prev types.BeaconEntry) ([]types.BeaconEntry, error) {
108-
{
114+
// Before nv22 we had "chained" beacons, and so required two entries at a fork
115+
if nv < network.Version22 {
109116
parentBeacon := bSchedule.BeaconForEpoch(parentEpoch)
110117
currBeacon := bSchedule.BeaconForEpoch(epoch)
111118
if parentBeacon != currBeacon {
@@ -133,6 +140,7 @@ func BeaconEntriesForBlock(ctx context.Context, bSchedule Schedule, nv network.V
133140
start := build.Clock.Now()
134141

135142
maxRound := beacon.MaxBeaconRoundForEpoch(nv, epoch)
143+
// We don't expect this to ever be the case
136144
if maxRound == prev.Round {
137145
return nil, nil
138146
}

chain/beacon/drand/drand.go

+23-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
dchain "github.com/drand/drand/chain"
99
dclient "github.com/drand/drand/client"
1010
hclient "github.com/drand/drand/client/http"
11-
"github.com/drand/drand/common/scheme"
11+
dcrypto "github.com/drand/drand/crypto"
1212
dlog "github.com/drand/drand/log"
1313
gclient "github.com/drand/drand/lp2p/client"
1414
"github.com/drand/kyber"
@@ -47,6 +47,7 @@ type DrandBeacon struct {
4747
drandGenTime uint64
4848
filGenTime uint64
4949
filRoundTime uint64
50+
scheme *dcrypto.Scheme
5051

5152
localCache *lru.Cache[uint64, *types.BeaconEntry]
5253
}
@@ -68,6 +69,10 @@ func (l *logger) Named(s string) dlog.Logger {
6869
return &logger{l.SugaredLogger.Named(s)}
6970
}
7071

72+
func (l *logger) AddCallerSkip(skip int) dlog.Logger {
73+
return &logger{l.SugaredLogger.With(zap.AddCallerSkip(skip))}
74+
}
75+
7176
func NewDrandBeacon(genesisTs, interval uint64, ps *pubsub.PubSub, config dtypes.DrandConfig) (*DrandBeacon, error) {
7277
if genesisTs == 0 {
7378
panic("what are you doing this cant be zero")
@@ -116,6 +121,11 @@ func NewDrandBeacon(genesisTs, interval uint64, ps *pubsub.PubSub, config dtypes
116121
localCache: lc,
117122
}
118123

124+
sch, err := dcrypto.GetSchemeByIDWithDefault(drandChain.Scheme)
125+
if err != nil {
126+
return nil, err
127+
}
128+
db.scheme = sch
119129
db.pubkey = drandChain.PublicKey
120130
db.interval = drandChain.Period
121131
db.drandGenTime = uint64(drandChain.GenesisTime)
@@ -164,29 +174,27 @@ func (db *DrandBeacon) getCachedValue(round uint64) *types.BeaconEntry {
164174
return v
165175
}
166176

167-
func (db *DrandBeacon) VerifyEntry(curr types.BeaconEntry, prev types.BeaconEntry) error {
168-
if prev.Round == 0 {
169-
// TODO handle genesis better
170-
return nil
171-
}
172-
173-
if be := db.getCachedValue(curr.Round); be != nil {
174-
if !bytes.Equal(curr.Data, be.Data) {
177+
func (db *DrandBeacon) VerifyEntry(entry types.BeaconEntry, prevEntrySig []byte) error {
178+
if be := db.getCachedValue(entry.Round); be != nil {
179+
if !bytes.Equal(entry.Data, be.Data) {
175180
return xerrors.New("invalid beacon value, does not match cached good value")
176181
}
177182
// return no error if the value is in the cache already
178183
return nil
179184
}
180185
b := &dchain.Beacon{
181-
PreviousSig: prev.Data,
182-
Round: curr.Round,
183-
Signature: curr.Data,
186+
PreviousSig: prevEntrySig,
187+
Round: entry.Round,
188+
Signature: entry.Data,
184189
}
185-
err := dchain.NewVerifier(scheme.GetSchemeFromEnv()).VerifyBeacon(*b, db.pubkey)
186-
if err == nil {
187-
db.cacheValue(curr)
190+
191+
err := db.scheme.VerifyBeacon(b, db.pubkey)
192+
if err != nil {
193+
return xerrors.Errorf("failed to verify beacon: %w", err)
188194
}
189195

196+
db.cacheValue(entry)
197+
190198
return nil
191199
}
192200

chain/beacon/mock.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (mb *mockBeacon) Entry(ctx context.Context, index uint64) <-chan Response {
4747
return out
4848
}
4949

50-
func (mb *mockBeacon) VerifyEntry(from types.BeaconEntry, to types.BeaconEntry) error {
50+
func (mb *mockBeacon) VerifyEntry(from types.BeaconEntry, _prevEntrySig []byte) error {
5151
// TODO: cache this, especially for bls
5252
oe := mb.entryForIndex(from.Round)
5353
if !bytes.Equal(from.Data, oe.Data) {

chain/consensus/filcns/filecoin.go

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ func (filec *FilecoinEC) ValidateBlock(ctx context.Context, b *types.FullBlock)
129129
return xerrors.Errorf("failed to get lookback tipset for block: %w", err)
130130
}
131131

132+
// TODO: Optimization: See https://github.com/filecoin-project/lotus/issues/11597
132133
prevBeacon, err := filec.store.GetLatestBeaconEntry(ctx, baseTs)
133134
if err != nil {
134135
return xerrors.Errorf("failed to get latest beacon entry: %w", err)

go.mod

+11-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ retract v1.20.2 // Wrongfully cherry picked PR, use v1.20.2+ instead.
88

99
require (
1010
contrib.go.opencensus.io/exporter/prometheus v0.4.2
11-
github.com/BurntSushi/toml v1.2.1
11+
github.com/BurntSushi/toml v1.3.0
1212
github.com/DataDog/zstd v1.4.5
1313
github.com/GeertJohan/go.rice v1.0.3
1414
github.com/Gurpartap/async v0.0.0-20180927173644-4f7f499dd9ee
@@ -22,14 +22,14 @@ require (
2222
github.com/detailyang/go-fallocate v0.0.0-20180908115635-432fa640bd2e
2323
github.com/dgraph-io/badger/v2 v2.2007.4
2424
github.com/docker/go-units v0.5.0
25-
github.com/drand/drand v1.4.9
26-
github.com/drand/kyber v1.1.15
25+
github.com/drand/drand v1.5.7
26+
github.com/drand/kyber v1.2.0
2727
github.com/dustin/go-humanize v1.0.1
2828
github.com/elastic/go-elasticsearch/v7 v7.14.0
2929
github.com/elastic/go-sysinfo v1.7.0
3030
github.com/elastic/gosigar v0.14.2
3131
github.com/etclabscore/go-openrpc-reflect v0.0.36
32-
github.com/fatih/color v1.13.0
32+
github.com/fatih/color v1.15.0
3333
github.com/filecoin-project/dagstore v0.5.2
3434
github.com/filecoin-project/filecoin-ffi v0.30.4-0.20220519234331-bfd1f5f9fe38
3535
github.com/filecoin-project/go-address v1.1.0
@@ -195,9 +195,9 @@ require (
195195
github.com/davecgh/go-spew v1.1.1 // indirect
196196
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
197197
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
198-
github.com/dgraph-io/ristretto v0.1.0 // indirect
198+
github.com/dgraph-io/ristretto v0.1.1 // indirect
199199
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
200-
github.com/drand/kyber-bls12381 v0.2.3 // indirect
200+
github.com/drand/kyber-bls12381 v0.3.1 // indirect
201201
github.com/elastic/go-windows v1.0.0 // indirect
202202
github.com/etclabscore/go-jsonschema-walk v0.0.6 // indirect
203203
github.com/filecoin-project/go-amt-ipld/v2 v2.1.0 // indirect
@@ -208,10 +208,9 @@ require (
208208
github.com/filecoin-project/go-hamt-ipld/v3 v3.1.0 // indirect
209209
github.com/flynn/noise v1.0.0 // indirect
210210
github.com/francoispqt/gojay v1.2.13 // indirect
211-
github.com/fsnotify/fsnotify v1.6.0 // indirect
212211
github.com/gdamore/encoding v1.0.0 // indirect
213212
github.com/go-kit/log v0.2.1 // indirect
214-
github.com/go-logfmt/logfmt v0.5.1 // indirect
213+
github.com/go-logfmt/logfmt v0.6.0 // indirect
215214
github.com/go-logr/logr v1.2.4 // indirect
216215
github.com/go-logr/stdr v1.2.2 // indirect
217216
github.com/go-ole/go-ole v1.2.5 // indirect
@@ -221,7 +220,7 @@ require (
221220
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
222221
github.com/godbus/dbus/v5 v5.1.0 // indirect
223222
github.com/gogo/protobuf v1.3.2 // indirect
224-
github.com/golang/glog v1.1.0 // indirect
223+
github.com/golang/glog v1.1.1 // indirect
225224
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
226225
github.com/golang/protobuf v1.5.3 // indirect
227226
github.com/golang/snappy v0.0.4 // indirect
@@ -299,7 +298,7 @@ require (
299298
github.com/petar/GoLLRB v0.0.0-20210522233825-ae3b015fd3e9 // indirect
300299
github.com/pmezard/go-difflib v1.0.0 // indirect
301300
github.com/prometheus/client_model v0.4.0 // indirect
302-
github.com/prometheus/common v0.42.0 // indirect
301+
github.com/prometheus/common v0.44.0 // indirect
303302
github.com/prometheus/procfs v0.10.1 // indirect
304303
github.com/prometheus/statsd_exporter v0.22.7 // indirect
305304
github.com/quic-go/qpack v0.4.0 // indirect
@@ -310,7 +309,7 @@ require (
310309
github.com/rs/cors v1.7.0 // indirect
311310
github.com/russross/blackfriday/v2 v2.1.0 // indirect
312311
github.com/shirou/gopsutil v2.18.12+incompatible // indirect
313-
github.com/sirupsen/logrus v1.9.0 // indirect
312+
github.com/sirupsen/logrus v1.9.2 // indirect
314313
github.com/spaolacci/murmur3 v1.1.0 // indirect
315314
github.com/tidwall/gjson v1.14.4 // indirect
316315
github.com/twmb/murmur3 v1.1.6 // indirect
@@ -333,7 +332,7 @@ require (
333332
golang.org/x/mod v0.12.0 // indirect
334333
golang.org/x/text v0.14.0 // indirect
335334
gonum.org/v1/gonum v0.13.0 // indirect
336-
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
335+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
337336
google.golang.org/grpc v1.55.0 // indirect
338337
google.golang.org/protobuf v1.30.0 // indirect
339338
gopkg.in/yaml.v2 v2.4.0 // indirect

0 commit comments

Comments
 (0)