Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using block after nullrounds to find randomness #3619

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions chain/beacon/beacon.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type RandomBeacon interface {
Entry(context.Context, uint64) <-chan Response
VerifyEntry(types.BeaconEntry, types.BeaconEntry) error
MaxBeaconRoundForEpoch(abi.ChainEpoch, types.BeaconEntry) uint64
MinDrandEntryEpoch(filEpoch abi.ChainEpoch) abi.ChainEpoch
}

func ValidateBlockValues(b RandomBeacon, h *types.BlockHeader, prevEntry types.BeaconEntry) error {
Expand Down
24 changes: 23 additions & 1 deletion chain/beacon/drand/drand.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package drand
import (
"bytes"
"context"
"math"
"sync"
"time"

Expand Down Expand Up @@ -190,8 +191,29 @@ func (db *DrandBeacon) VerifyEntry(curr types.BeaconEntry, prev types.BeaconEntr
func (db *DrandBeacon) MaxBeaconRoundForEpoch(filEpoch abi.ChainEpoch, prevEntry types.BeaconEntry) uint64 {
// TODO: sometimes the genesis time for filecoin is zero and this goes negative
latestTs := ((uint64(filEpoch) * db.filRoundTime) + db.filGenTime) - db.filRoundTime
dround := (latestTs - db.drandGenTime) / uint64(db.interval.Seconds())
dround := (latestTs-db.drandGenTime)/uint64(db.interval.Seconds()) + 1
return dround
}

// MinDrandEntryEpoch returns the minimum filecoin epoch where the drand entry
// that is associated with `filEpoch` could have been inserted in the chain.
// Given filecoin and drand can have different frequency, a drand entry can be
// associated to one or multiple subsequent filecoin epochs. When searching the
// chain for the drand entry corresponding to filEpoch, we want to start
// searching from the minimum filecoin epoch where this drand entry should /
// could have been inserted and move onwards (for example if it falls on a null
// blok). This function returns the first such filecoin epoch.
// Note that if filecoin and drand frequency are the same, filEpoch and the
// returned epoch are the same.
func (db *DrandBeacon) MinDrandEntryEpoch(filEpoch abi.ChainEpoch) abi.ChainEpoch {
drandRound := db.MaxBeaconRoundForEpoch(filEpoch, types.BeaconEntry{})
drandTs := db.drandGenTime + (drandRound-1)*uint64(db.interval.Seconds())
// compute the minimum filecoin epoch associated with that timestamp
// We do the ceiling such that if we find on a decimal epoch number, we take
// the epoch number after this one - we don't include a drand entry at round
// 9.4, but at round 10 because we can't know it at the time of round 9
minEpoch := math.Ceil(float64((drandTs - db.filGenTime)) / float64(db.filRoundTime))
return abi.ChainEpoch(minEpoch)
}

var _ beacon.RandomBeacon = (*DrandBeacon)(nil)
26 changes: 26 additions & 0 deletions chain/beacon/drand/drand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package drand
import (
"os"
"testing"
"time"

dchain "github.com/drand/drand/chain"
hclient "github.com/drand/drand/client/http"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/specs-actors/actors/abi"
)

func TestPrintGroupInfo(t *testing.T) {
Expand All @@ -23,3 +26,26 @@ func TestPrintGroupInfo(t *testing.T) {
err = chain.ToJSON(os.Stdout)
assert.NoError(t, err)
}

func TestDrandBeaconMinDrandEntry(t *testing.T) {
db := DrandBeacon{
interval: 30 * time.Second,
drandGenTime: 666,
filGenTime: 1337,
filRoundTime: 30,
}

// test for equal frequency
in := abi.ChainEpoch(10)
res := db.MinDrandEntryEpoch(in)
require.Equal(t, in-1, res)

// test for different frequency
// since we give an odd epoch number, the drand entry should have been
// inserted in the even epoch number given filecoin is twice faster than
// drand
db.interval = 60 * time.Second
in = abi.ChainEpoch(11)
res = db.MinDrandEntryEpoch(in)
require.Equal(t, in-1, res)
}
4 changes: 4 additions & 0 deletions chain/beacon/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ func (mb *mockBeacon) MaxBeaconRoundForEpoch(epoch abi.ChainEpoch, prevEntry typ
return uint64(epoch)
}

func (mb *mockBeacon) MinDrandEntryEpoch(filEpoch abi.ChainEpoch) abi.ChainEpoch {
return filEpoch
}

var _ RandomBeacon = (*mockBeacon)(nil)
2 changes: 1 addition & 1 deletion chain/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ func (cs *ChainStore) GetBeaconRandomness(ctx context.Context, blks []cid.Cid, p
searchHeight = 0
}

randTs, err := cs.GetTipsetByHeight(ctx, searchHeight, ts, true)
randTs, err := cs.GetTipsetByHeight(ctx, searchHeight, ts, false)
if err != nil {
return nil, err
}
Expand Down