2020-04-08 22:12:36 +00:00
|
|
|
package beacon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/binary"
|
|
|
|
"time"
|
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/minio/blake2b-simd"
|
|
|
|
"golang.org/x/xerrors"
|
2021-09-27 01:23:41 +00:00
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/network"
|
|
|
|
|
2020-04-08 22:12:36 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Mock beacon assumes that filecoin rounds are 1:1 mapped with the beacon rounds
|
|
|
|
type mockBeacon struct {
|
|
|
|
interval time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMockBeacon(interval time.Duration) RandomBeacon {
|
|
|
|
mb := &mockBeacon{interval: interval}
|
|
|
|
|
|
|
|
return mb
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mb *mockBeacon) RoundTime() time.Duration {
|
|
|
|
return mb.interval
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mb *mockBeacon) entryForIndex(index uint64) types.BeaconEntry {
|
|
|
|
buf := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint64(buf, index)
|
|
|
|
rval := blake2b.Sum256(buf)
|
|
|
|
return types.BeaconEntry{
|
|
|
|
Round: index,
|
|
|
|
Data: rval[:],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mb *mockBeacon) Entry(ctx context.Context, index uint64) <-chan Response {
|
|
|
|
e := mb.entryForIndex(index)
|
|
|
|
out := make(chan Response, 1)
|
|
|
|
out <- Response{Entry: e}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2024-01-25 00:40:46 +00:00
|
|
|
func (mb *mockBeacon) VerifyEntry(from types.BeaconEntry, _prevEntrySig []byte) error {
|
2020-04-08 22:12:36 +00:00
|
|
|
// TODO: cache this, especially for bls
|
|
|
|
oe := mb.entryForIndex(from.Round)
|
|
|
|
if !bytes.Equal(from.Data, oe.Data) {
|
|
|
|
return xerrors.Errorf("mock beacon entry was invalid!")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-27 01:23:41 +00:00
|
|
|
func (mb *mockBeacon) MaxBeaconRoundForEpoch(nv network.Version, epoch abi.ChainEpoch) uint64 {
|
2021-09-18 17:57:04 +00:00
|
|
|
// offset for better testing
|
|
|
|
return uint64(epoch + 100)
|
2020-04-08 22:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ RandomBeacon = (*mockBeacon)(nil)
|