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

Generate unique beacon per each fake EC tipset #752

Merged
merged 1 commit into from
Nov 21, 2024
Merged
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
37 changes: 15 additions & 22 deletions internal/consensus/fake_ec.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,27 +41,13 @@ type tipset struct {
tsk []byte
epoch int64
timestamp time.Time
beacon []byte
}

func (ts *tipset) Key() gpbft.TipSetKey {
return ts.tsk
}

func (ts *tipset) Epoch() int64 {
return ts.epoch
}
func (ts *tipset) Beacon() []byte {
h, err := blake2b.New256([]byte("beacon"))
if err != nil {
panic(err)
}
h.Write(ts.tsk)
return h.Sum(nil)
}

func (ts *tipset) Timestamp() time.Time {
return ts.timestamp
}
func (ts *tipset) Key() gpbft.TipSetKey { return ts.tsk }
func (ts *tipset) Epoch() int64 { return ts.epoch }
func (ts *tipset) Beacon() []byte { return ts.beacon }
func (ts *tipset) Timestamp() time.Time { return ts.timestamp }

func (ts *tipset) String() string {
res, _ := mbase.Encode(mbase.Base32, ts.tsk[:gpbft.CidMaxLen])
Expand Down Expand Up @@ -159,15 +145,22 @@ func (ec *FakeEC) genTipset(epoch int64) *tipset {
tsk = append(tsk, cidPrefixBytes...)
tsk = append(tsk, digest...)
}

h.Reset()
h.Write([]byte(fmt.Sprintf("beacon %d", epoch)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would make it dependant on seed but not critical and for sure better than it was.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The hasher h is keyed by seed already. That should do it right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, you are probably right. I forgot that we have a keyed hasher.

beacon := h.Sum(nil)

return &tipset{
tsk: tsk,
epoch: epoch,
timestamp: ec.ecStart.Add(time.Duration(epoch) * ec.ecPeriod),
beacon: beacon,
}
}

// GetTipsetByHeight should return a tipset or nil/empty byte array if it does not exists
func (ec *FakeEC) GetTipsetByEpoch(ctx context.Context, epoch int64) (ec.TipSet, error) {
// GetTipsetByEpoch returns the tipset at a given epoch. If the epoch does not
// yet exist, it returns an error.
func (ec *FakeEC) GetTipsetByEpoch(_ context.Context, epoch int64) (ec.TipSet, error) {
if ec.GetCurrentHead() < epoch {
return nil, fmt.Errorf("does not yet exist")
}
Expand Down Expand Up @@ -223,7 +216,7 @@ func (ec *FakeEC) GetHead(ctx context.Context) (ec.TipSet, error) {
return ec.GetTipsetByEpoch(ctx, ec.GetCurrentHead())
}

func (ec *FakeEC) GetPowerTable(ctx context.Context, tsk gpbft.TipSetKey) (gpbft.PowerEntries, error) {
func (ec *FakeEC) GetPowerTable(_ context.Context, tsk gpbft.TipSetKey) (gpbft.PowerEntries, error) {
targetEpoch := ec.epochFromTsk(tsk)
headEpoch := ec.GetCurrentHead()

Expand Down
Loading