Skip to content

Commit

Permalink
feat: implement FIP-0063
Browse files Browse the repository at this point in the history
  • Loading branch information
arajasek committed Jan 25, 2024
1 parent a967a36 commit 06c5ad6
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 96 deletions.
17 changes: 17 additions & 0 deletions build/drand.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
DrandDevnet
DrandLocalnet
DrandIncentinet
DrandQuicknet
)

var DrandConfigs = map[DrandEnum]dtypes.DrandConfig{
Expand All @@ -36,6 +37,7 @@ var DrandConfigs = map[DrandEnum]dtypes.DrandConfig{
"https://api2.drand.sh",
"https://api3.drand.sh",
"https://drand.cloudflare.com",
"https://api.drand.secureweb3.com:6875", // Storswift
},
Relays: []string{
"/dnsaddr/api.drand.sh/",
Expand All @@ -44,6 +46,21 @@ var DrandConfigs = map[DrandEnum]dtypes.DrandConfig{
},
ChainInfoJSON: `{"public_key":"868f005eb8e6e4ca0a47c8a77ceaa5309a47978a7c71bc5cce96366b5d7a569937c529eeda66c7293784a9402801af31","period":30,"genesis_time":1595431050,"hash":"8990e7a9aaed2ffed73dbd7092123d6f289930540d7651336225dc172e51b2ce","groupHash":"176f93498eac9ca337150b46d21dd58673ea4e3581185f869672e59fa4cb390a"}`,
},
DrandQuicknet: {
Servers: []string{
"https://api.drand.sh",
"https://api2.drand.sh",
"https://api3.drand.sh",
"https://drand.cloudflare.com",
"https://api.drand.secureweb3.com:6875", // Storswift
},
Relays: []string{
"/dnsaddr/api.drand.sh/",
"/dnsaddr/api2.drand.sh/",
"/dnsaddr/api3.drand.sh/",
},
ChainInfoJSON: `{"public_key":"83cf0f2896adee7eb8b5f01fcad3912212c437e0073e911fb90022d3e760183c8c4b450b6a0a6c3ac6a5776a2d1064510d1fec758c921cc22b0e17e63aaf4bcb5ed66304de9cf809bd274ca73bab4af5a6e9c76a4bc09e76eae8991ef5ece45a","period":3,"genesis_time":1692803367,"hash":"52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971","groupHash":"f477d5c89f21a17c863a7f937c6a6d15859414d2be09cd448d4279af331c5d3e","schemeID":"bls-unchained-g1-rfc9380","metadata":{"beaconID":"quicknet"}}`,
},
DrandTestnet: {
Servers: []string{
"https://pl-eu.testnet.drand.sh",
Expand Down
5 changes: 3 additions & 2 deletions build/params_2k.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ var UpgradeThunderHeight = abi.ChainEpoch(-23)

var UpgradeWatermelonHeight = abi.ChainEpoch(-24)

var UpgradePineappleHeight = abi.ChainEpoch(200)
var UpgradePineappleHeight = abi.ChainEpoch(20)

// This fix upgrade only ran on calibrationnet
const UpgradeWatermelonFixHeight = -100
Expand All @@ -76,7 +76,8 @@ const UpgradeWatermelonFixHeight = -100
const UpgradeWatermelonFix2Height = -101

var DrandSchedule = map[abi.ChainEpoch]DrandEnum{
0: DrandMainnet,
0: DrandMainnet,
UpgradePineappleHeight + 10: DrandQuicknet,
}

var SupportedProofTypes = []abi.RegisteredSealProof{
Expand Down
3 changes: 2 additions & 1 deletion build/params_butterfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
)

var DrandSchedule = map[abi.ChainEpoch]DrandEnum{
0: DrandMainnet,
0: DrandMainnet,
UpgradePineappleHeight + 10: DrandQuicknet,
}

const GenesisNetworkVersion = network.Version20
Expand Down
3 changes: 2 additions & 1 deletion build/params_calibnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
)

var DrandSchedule = map[abi.ChainEpoch]DrandEnum{
0: DrandMainnet,
0: DrandMainnet,
UpgradePineappleHeight + 10: DrandQuicknet,
}

const GenesisNetworkVersion = network.Version0
Expand Down
3 changes: 2 additions & 1 deletion build/params_interop.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ const UpgradeWatermelonFixHeight = -1
const UpgradeWatermelonFix2Height = -2

var DrandSchedule = map[abi.ChainEpoch]DrandEnum{
0: DrandMainnet,
0: DrandMainnet,
UpgradePineappleHeight + 10: DrandQuicknet,
}

var SupportedProofTypes = []abi.RegisteredSealProof{
Expand Down
5 changes: 3 additions & 2 deletions build/params_mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (
)

var DrandSchedule = map[abi.ChainEpoch]DrandEnum{
0: DrandIncentinet,
UpgradeSmokeHeight: DrandMainnet,
0: DrandIncentinet,
UpgradeSmokeHeight: DrandMainnet,
UpgradePineappleHeight + 10: DrandQuicknet,
}

var NetworkBundle = "mainnet"
Expand Down
3 changes: 2 additions & 1 deletion build/params_testground.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ var (
UpgradePineappleHeight abi.ChainEpoch = -26

DrandSchedule = map[abi.ChainEpoch]DrandEnum{
0: DrandMainnet,
0: DrandMainnet,
UpgradePineappleHeight + 10: DrandQuicknet,
}

GenesisNetworkVersion = network.Version0
Expand Down
22 changes: 15 additions & 7 deletions chain/beacon/beacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,21 @@ type BeaconPoint struct {
// been posted on chain.
type RandomBeacon interface {
Entry(context.Context, uint64) <-chan Response
VerifyEntry(types.BeaconEntry, types.BeaconEntry) error
VerifyEntry(entry types.BeaconEntry, prevEntrySig []byte) error
MaxBeaconRoundForEpoch(network.Version, abi.ChainEpoch) uint64
}

func ValidateBlockValues(bSchedule Schedule, nv network.Version, h *types.BlockHeader, parentEpoch abi.ChainEpoch,
prevEntry types.BeaconEntry) error {
{
// Before nv22 we had "chained" beacons, and so required two entries at a fork
if nv < network.Version22 {
parentBeacon := bSchedule.BeaconForEpoch(parentEpoch)
currBeacon := bSchedule.BeaconForEpoch(h.Height)
if parentBeacon != currBeacon {
if len(h.BeaconEntries) != 2 {
return xerrors.Errorf("expected two beacon entries at beacon fork, got %d", len(h.BeaconEntries))
}
err := currBeacon.VerifyEntry(h.BeaconEntries[1], h.BeaconEntries[0])
err := currBeacon.VerifyEntry(h.BeaconEntries[1], h.BeaconEntries[0].Data)
if err != nil {
return xerrors.Errorf("beacon at fork point invalid: (%v, %v): %w",
h.BeaconEntries[1], h.BeaconEntries[0], err)
Expand All @@ -65,9 +66,9 @@ func ValidateBlockValues(bSchedule Schedule, nv network.Version, h *types.BlockH
}
}

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

// Verify that the last beacon entry's round corresponds to the round we expect
if nv < network.Version22 && prevEntry.Round == 0 {
// 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)
// Networks that start on drand quicknet, or other unchained randomness sources, will still verify it
return nil
}

last := h.BeaconEntries[len(h.BeaconEntries)-1]
if last.Round != maxRound {
return xerrors.Errorf("expected final beacon entry in block to be at round %d, got %d", maxRound, last.Round)
Expand All @@ -95,7 +101,7 @@ func ValidateBlockValues(bSchedule Schedule, nv network.Version, h *types.BlockH

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

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

maxRound := beacon.MaxBeaconRoundForEpoch(nv, epoch)
// We don't expect this to ever be the case
if maxRound == prev.Round {
return nil, nil
}
Expand Down
38 changes: 23 additions & 15 deletions chain/beacon/drand/drand.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
dchain "github.com/drand/drand/chain"
dclient "github.com/drand/drand/client"
hclient "github.com/drand/drand/client/http"
"github.com/drand/drand/common/scheme"
dcrypto "github.com/drand/drand/crypto"
dlog "github.com/drand/drand/log"
gclient "github.com/drand/drand/lp2p/client"
"github.com/drand/kyber"
Expand Down Expand Up @@ -47,6 +47,7 @@ type DrandBeacon struct {
drandGenTime uint64
filGenTime uint64
filRoundTime uint64
scheme *dcrypto.Scheme

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

func (l *logger) AddCallerSkip(skip int) dlog.Logger {
return &logger{l.SugaredLogger.With(zap.AddCallerSkip(skip))}
}

func NewDrandBeacon(genesisTs, interval uint64, ps *pubsub.PubSub, config dtypes.DrandConfig) (*DrandBeacon, error) {
if genesisTs == 0 {
panic("what are you doing this cant be zero")
Expand Down Expand Up @@ -116,6 +121,11 @@ func NewDrandBeacon(genesisTs, interval uint64, ps *pubsub.PubSub, config dtypes
localCache: lc,
}

sch, err := dcrypto.GetSchemeByIDWithDefault(drandChain.Scheme)
if err != nil {
return nil, err
}
db.scheme = sch
db.pubkey = drandChain.PublicKey
db.interval = drandChain.Period
db.drandGenTime = uint64(drandChain.GenesisTime)
Expand Down Expand Up @@ -164,29 +174,27 @@ func (db *DrandBeacon) getCachedValue(round uint64) *types.BeaconEntry {
return v
}

func (db *DrandBeacon) VerifyEntry(curr types.BeaconEntry, prev types.BeaconEntry) error {
if prev.Round == 0 {
// TODO handle genesis better
return nil
}

if be := db.getCachedValue(curr.Round); be != nil {
if !bytes.Equal(curr.Data, be.Data) {
func (db *DrandBeacon) VerifyEntry(entry types.BeaconEntry, prevEntrySig []byte) error {
if be := db.getCachedValue(entry.Round); be != nil {
if !bytes.Equal(entry.Data, be.Data) {
return xerrors.New("invalid beacon value, does not match cached good value")
}
// return no error if the value is in the cache already
return nil
}
b := &dchain.Beacon{
PreviousSig: prev.Data,
Round: curr.Round,
Signature: curr.Data,
PreviousSig: prevEntrySig,
Round: entry.Round,
Signature: entry.Data,
}
err := dchain.NewVerifier(scheme.GetSchemeFromEnv()).VerifyBeacon(*b, db.pubkey)
if err == nil {
db.cacheValue(curr)

err := db.scheme.VerifyBeacon(b, db.pubkey)
if err != nil {
return xerrors.Errorf("failed to verify beacon: %w", err)
}

db.cacheValue(entry)

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion chain/beacon/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (mb *mockBeacon) Entry(ctx context.Context, index uint64) <-chan Response {
return out
}

func (mb *mockBeacon) VerifyEntry(from types.BeaconEntry, to types.BeaconEntry) error {
func (mb *mockBeacon) VerifyEntry(from types.BeaconEntry, _prevEntrySig []byte) error {
// TODO: cache this, especially for bls
oe := mb.entryForIndex(from.Round)
if !bytes.Equal(from.Data, oe.Data) {
Expand Down
1 change: 1 addition & 0 deletions chain/consensus/filcns/filecoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func (filec *FilecoinEC) ValidateBlock(ctx context.Context, b *types.FullBlock)
return xerrors.Errorf("failed to get lookback tipset for block: %w", err)
}

// TODO: Optimization: See https://github.com/filecoin-project/lotus/issues/11597
prevBeacon, err := filec.store.GetLatestBeaconEntry(ctx, baseTs)
if err != nil {
return xerrors.Errorf("failed to get latest beacon entry: %w", err)
Expand Down
23 changes: 11 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ retract v1.20.2 // Wrongfully cherry picked PR, use v1.20.2+ instead.

require (
contrib.go.opencensus.io/exporter/prometheus v0.4.2
github.com/BurntSushi/toml v1.2.1
github.com/BurntSushi/toml v1.3.0
github.com/DataDog/zstd v1.4.5
github.com/GeertJohan/go.rice v1.0.3
github.com/Gurpartap/async v0.0.0-20180927173644-4f7f499dd9ee
Expand All @@ -22,14 +22,14 @@ require (
github.com/detailyang/go-fallocate v0.0.0-20180908115635-432fa640bd2e
github.com/dgraph-io/badger/v2 v2.2007.4
github.com/docker/go-units v0.5.0
github.com/drand/drand v1.4.9
github.com/drand/kyber v1.1.15
github.com/drand/drand v1.5.7
github.com/drand/kyber v1.2.0
github.com/dustin/go-humanize v1.0.1
github.com/elastic/go-elasticsearch/v7 v7.14.0
github.com/elastic/go-sysinfo v1.7.0
github.com/elastic/gosigar v0.14.2
github.com/etclabscore/go-openrpc-reflect v0.0.36
github.com/fatih/color v1.13.0
github.com/fatih/color v1.15.0
github.com/filecoin-project/dagstore v0.5.2
github.com/filecoin-project/filecoin-ffi v0.30.4-0.20220519234331-bfd1f5f9fe38
github.com/filecoin-project/go-address v1.1.0
Expand Down Expand Up @@ -195,9 +195,9 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davidlazar/go-crypto v0.0.0-20200604182044-b73af7476f6c // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dgraph-io/ristretto v0.1.0 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/drand/kyber-bls12381 v0.2.3 // indirect
github.com/drand/kyber-bls12381 v0.3.1 // indirect
github.com/elastic/go-windows v1.0.0 // indirect
github.com/etclabscore/go-jsonschema-walk v0.0.6 // indirect
github.com/filecoin-project/go-amt-ipld/v2 v2.1.0 // indirect
Expand All @@ -209,10 +209,9 @@ require (
github.com/filecoin-project/go-hamt-ipld/v3 v3.1.0 // indirect
github.com/flynn/noise v1.0.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.5 // indirect
Expand All @@ -222,7 +221,7 @@ require (
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/glog v1.1.1 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
Expand Down Expand Up @@ -300,7 +299,7 @@ require (
github.com/petar/GoLLRB v0.0.0-20210522233825-ae3b015fd3e9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/prometheus/statsd_exporter v0.22.7 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
Expand All @@ -311,7 +310,7 @@ require (
github.com/rs/cors v1.7.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shirou/gopsutil v2.18.12+incompatible // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/sirupsen/logrus v1.9.2 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/twmb/murmur3 v1.1.6 // indirect
Expand All @@ -334,7 +333,7 @@ require (
golang.org/x/mod v0.12.0 // indirect
golang.org/x/text v0.14.0 // indirect
gonum.org/v1/gonum v0.13.0 // indirect
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.55.0 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading

0 comments on commit 06c5ad6

Please sign in to comment.