drand wip

,
Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2020-04-06 14:47:14 +02:00 committed by Jeromy
parent 7075eaba26
commit afdb1db529
12 changed files with 121 additions and 55 deletions

View File

@ -73,7 +73,7 @@ type FullNode interface {
// miner
MinerGetBaseInfo(context.Context, address.Address, types.TipSetKey) (*MiningBaseInfo, error)
MinerCreateBlock(context.Context, address.Address, types.TipSetKey, *types.Ticket, *types.EPostProof, []*types.BeaconEntry, []*types.SignedMessage, abi.ChainEpoch, uint64) (*types.BlockMsg, error)
MinerCreateBlock(context.Context, address.Address, types.TipSetKey, *types.Ticket, *types.EPostProof, []types.BeaconEntry, []*types.SignedMessage, abi.ChainEpoch, uint64) (*types.BlockMsg, error)
// // UX ?

View File

@ -88,8 +88,8 @@ type FullNodeStruct struct {
MpoolGetNonce func(context.Context, address.Address) (uint64, error) `perm:"read"`
MpoolSub func(context.Context) (<-chan api.MpoolUpdate, error) `perm:"read"`
MinerGetBaseInfo func(context.Context, address.Address, types.TipSetKey) (*api.MiningBaseInfo, error) `perm:"read"`
MinerCreateBlock func(context.Context, address.Address, types.TipSetKey, *types.Ticket, *types.EPostProof, []*types.BeaconEntry, []*types.SignedMessage, abi.ChainEpoch, uint64) (*types.BlockMsg, error) `perm:"write"`
MinerGetBaseInfo func(context.Context, address.Address, types.TipSetKey) (*api.MiningBaseInfo, error) `perm:"read"`
MinerCreateBlock func(context.Context, address.Address, types.TipSetKey, *types.Ticket, *types.EPostProof, []types.BeaconEntry, []*types.SignedMessage, abi.ChainEpoch, uint64) (*types.BlockMsg, error) `perm:"write"`
WalletNew func(context.Context, crypto.SigType) (address.Address, error) `perm:"write"`
WalletHas func(context.Context, address.Address) (bool, error) `perm:"write"`
@ -330,7 +330,7 @@ func (c *FullNodeStruct) MinerGetBaseInfo(ctx context.Context, maddr address.Add
return c.Internal.MinerGetBaseInfo(ctx, maddr, tsk)
}
func (c *FullNodeStruct) MinerCreateBlock(ctx context.Context, addr address.Address, base types.TipSetKey, ticket *types.Ticket, eproof *types.EPostProof, bvals []*types.BeaconEntry, msgs []*types.SignedMessage, height abi.ChainEpoch, ts uint64) (*types.BlockMsg, error) {
func (c *FullNodeStruct) MinerCreateBlock(ctx context.Context, addr address.Address, base types.TipSetKey, ticket *types.Ticket, eproof *types.EPostProof, bvals []types.BeaconEntry, msgs []*types.SignedMessage, height abi.ChainEpoch, ts uint64) (*types.BlockMsg, error) {
return c.Internal.MinerCreateBlock(ctx, addr, base, ticket, eproof, bvals, msgs, height, ts)
}

View File

@ -69,7 +69,7 @@ const MaxSealLookback = SealRandomnessLookbackLimit + 2000 // TODO: Get from spe
// Mining
// Epochs
const EcRandomnessLookback = 1
const EcRandomnessLookback = Finality
// /////
// Devnet settings

View File

@ -8,7 +8,6 @@ import (
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/specs-actors/actors/abi"
"github.com/filecoin-project/specs-actors/actors/crypto"
logging "github.com/ipfs/go-log"
"golang.org/x/xerrors"
@ -18,30 +17,28 @@ import (
var log = logging.Logger("beacon")
type Response struct {
Entry *types.BeaconEntry
Entry types.BeaconEntry
Err error
}
type DrandBeacon interface {
RoundTime() time.Duration
LastEntry() (*types.BeaconEntry, error)
LastEntry() (types.BeaconEntry, error)
Entry(context.Context, uint64) <-chan Response
VerifyEntry(*types.BeaconEntry) (bool, error)
VerifyEntry(types.BeaconEntry) (bool, error)
BeaconIndexesForEpoch(abi.ChainEpoch, int) []uint64
IsEntryForEpoch(e types.BeaconEntry, epoch abi.ChainEpoch, nulls int) (bool, error)
}
func ValidateBlockValues(b DrandBeacon, h *types.BlockHeader, nulls int) error {
indexes := b.BeaconIndexesForEpoch(h.Height, nulls)
if len(h.BeaconEntries) != len(indexes) {
return xerrors.Errorf("incorrect number of beacon entries, exp:%d got:%d", len(indexes), len(h.BeaconEntries))
}
for i, ix := range indexes {
if h.BeaconEntries[i].Index != ix {
return xerrors.Errorf("beacon entry at [%d] had wrong index, exp:%d got:%d", i, ix, h.BeaconEntries[i].Index)
for i, be := range h.BeaconEntries {
if ok, err := b.IsEntryForEpoch(be, h.Height, nulls); err != nil {
return xerrors.Errorf("failed to check if beacon belongs: %w")
} else if !ok {
return xerrors.Errorf("beacon does not belong in this block: %d", i)
}
if ok, err := b.VerifyEntry(h.BeaconEntries[i]); err != nil {
if ok, err := b.VerifyEntry(be); err != nil {
return xerrors.Errorf("failed to verify beacon entry %d: %w", i, err)
} else if !ok {
return xerrors.Errorf("beacon entry %d was invalid", i)
@ -51,10 +48,10 @@ func ValidateBlockValues(b DrandBeacon, h *types.BlockHeader, nulls int) error {
return nil
}
func BeaconEntriesForBlock(ctx context.Context, beacon DrandBeacon, round abi.ChainEpoch, nulls int) ([]*types.BeaconEntry, error) {
func BeaconEntriesForBlock(ctx context.Context, beacon DrandBeacon, round abi.ChainEpoch, nulls int) ([]types.BeaconEntry, error) {
start := time.Now()
var out []*types.BeaconEntry
var out []types.BeaconEntry
for _, ei := range beacon.BeaconIndexesForEpoch(round, nulls) {
rch := beacon.Entry(ctx, ei)
select {
@ -87,17 +84,17 @@ func (mb *mockBeacon) RoundTime() time.Duration {
return mb.interval
}
func (mb *mockBeacon) LastEntry() (*types.BeaconEntry, error) {
func (mb *mockBeacon) LastEntry() (types.BeaconEntry, error) {
panic("NYI")
}
func (mb *mockBeacon) entryForIndex(index uint64) *types.BeaconEntry {
func (mb *mockBeacon) entryForIndex(index uint64) types.BeaconEntry {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, index)
rval := blake2b.Sum256(buf)
return &types.BeaconEntry{
Index: index,
Signature: crypto.Signature{Type: crypto.SigTypeBLS, Data: rval[:]},
return types.BeaconEntry{
Index: index,
Data: rval[:],
}
}
@ -108,18 +105,23 @@ func (mb *mockBeacon) Entry(ctx context.Context, index uint64) <-chan Response {
return out
}
func (mb *mockBeacon) VerifyEntry(e *types.BeaconEntry) (bool, error) {
func (mb *mockBeacon) VerifyEntry(e types.BeaconEntry) (bool, error) {
// TODO: cache this, especially for bls
oe := mb.entryForIndex(e.Index)
return bytes.Equal(e.Signature.Data, oe.Signature.Data), nil
return bytes.Equal(e.Data, oe.Data), nil
}
func (mb *mockBeacon) IsEntryForEpoch(e types.BeaconEntry, epoch abi.ChainEpoch, nulls int) (bool, error) {
return int64(e.Index) <= int64(epoch) && int64(epoch)-int64(nulls) >= int64(e.Index), nil
}
func (mb *mockBeacon) BeaconIndexesForEpoch(epoch abi.ChainEpoch, nulls int) []uint64 {
var out []uint64
for i := nulls; i > 0; i-- {
out = append(out, uint64(epoch))
for i := 0; i < nulls; i++ {
out = append(out, uint64(epoch)-uint64(i))
}
out = append(out, uint64(epoch))
return []uint64{uint64(epoch)}
return out
}
var _ DrandBeacon = (*mockBeacon)(nil)

View File

@ -381,7 +381,7 @@ func (cg *ChainGen) NextTipSetFromMiners(base *types.TipSet, miners []address.Ad
}, nil
}
func (cg *ChainGen) makeBlock(parents *types.TipSet, m address.Address, eproof *types.EPostProof, ticket *types.Ticket, bvals []*types.BeaconEntry, height abi.ChainEpoch, msgs []*types.SignedMessage) (*types.FullBlock, error) {
func (cg *ChainGen) makeBlock(parents *types.TipSet, m address.Address, eproof *types.EPostProof, ticket *types.Ticket, bvals []types.BeaconEntry, height abi.ChainEpoch, msgs []*types.SignedMessage) (*types.FullBlock, error) {
var ts uint64
if cg.Timestamper != nil {

View File

@ -20,7 +20,9 @@ import (
"github.com/filecoin-project/lotus/chain/wallet"
)
func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wallet, miner address.Address, parents *types.TipSet, ticket *types.Ticket, proof *types.EPostProof, bvals []*types.BeaconEntry, msgs []*types.SignedMessage, height abi.ChainEpoch, timestamp uint64) (*types.FullBlock, error) {
func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wallet, miner address.Address,
parents *types.TipSet, ticket *types.Ticket, proof *types.EPostProof, bvals []types.BeaconEntry,
msgs []*types.SignedMessage, height abi.ChainEpoch, timestamp uint64) (*types.FullBlock, error) {
st, recpts, err := sm.TipSetState(ctx, parents)
if err != nil {
return nil, xerrors.Errorf("failed to load tipset state: %w", err)

View File

@ -892,7 +892,7 @@ func (cs *ChainStore) TryFillTipSet(ts *types.TipSet) (*FullTipSet, error) {
return NewFullTipSet(out), nil
}
func drawRandomness(t *types.Ticket, pers crypto.DomainSeparationTag, round int64, entropy []byte) ([]byte, error) {
func drawRandomness(t *types.Ticket, pers crypto.DomainSeparationTag, round int64, drandRng []byte, entropy []byte) ([]byte, error) {
h := blake2b.New256()
if err := binary.Write(h, binary.BigEndian, int64(pers)); err != nil {
return nil, xerrors.Errorf("deriving randomness: %w", err)
@ -902,6 +902,7 @@ func drawRandomness(t *types.Ticket, pers crypto.DomainSeparationTag, round int6
if err := binary.Write(h, binary.BigEndian, round); err != nil {
return nil, xerrors.Errorf("deriving randomness: %w", err)
}
h.Write(drandRng)
h.Write(entropy)
return h.Sum(nil), nil
@ -917,6 +918,16 @@ func (cs *ChainStore) GetRandomness(ctx context.Context, blks []cid.Cid, pers cr
log.Infof("getRand %v %d %d %x -> %x", blks, pers, round, entropy, out)
}()
*/
var drandRng []byte
if pers == crypto.DomainSeparationTag_ElectionPoStChallengeSeed {
// special case, use lates beacon
nts, err := cs.LoadTipSet(types.NewTipSetKey(blks...))
if err != nil {
return nil, err
}
drandRng = nts.Blocks()[0].BeaconEntries[0].Data
}
for {
nts, err := cs.LoadTipSet(types.NewTipSetKey(blks...))
@ -929,7 +940,10 @@ func (cs *ChainStore) GetRandomness(ctx context.Context, blks []cid.Cid, pers cr
// if at (or just past -- for null epochs) appropriate epoch
// or at genesis (works for negative epochs)
if int64(nts.Height()) <= round || mtb.Height == 0 {
return drawRandomness(nts.MinTicketBlock().Ticket, pers, round, entropy)
if len(drandRng) == 0 {
drandRng = nts.Blocks()[0].BeaconEntries[0].Data
}
return drawRandomness(nts.MinTicketBlock().Ticket, pers, round, drandRng, entropy)
}
blks = mtb.Parents

View File

@ -957,15 +957,47 @@ func (syncer *Syncer) collectHeaders(ctx context.Context, from *types.TipSet, to
trace.Int64Attribute("toHeight", int64(to.Height())),
)
markBad := func(fmts string, args ...interface{}) {
for _, b := range from.Cids() {
syncer.bad.Add(b, fmt.Sprintf(fmts, args...))
}
}
for _, pcid := range from.Parents().Cids() {
if reason, ok := syncer.bad.Has(pcid); ok {
for _, b := range from.Cids() {
syncer.bad.Add(b, fmt.Sprintf("linked to %s", pcid))
}
markBad("linked to %s", pcid)
return nil, xerrors.Errorf("chain linked to block marked previously as bad (%s, %s) (reason: %s)", from.Cids(), pcid, reason)
}
}
{
targetBE := from.Blocks()[0].BeaconEntries
if len(targetBE) != 0 {
cur := targetBE[0].Index
for _, e := range targetBE[1:] {
if cur <= e.Index {
markBad("wrong order of beacon entires")
return nil, xerrors.Errorf("wrong order of beacon entires")
}
}
}
for _, bh := range from.Blocks()[1:] {
if len(targetBE) != len(bh.BeaconEntries) {
markBad("different number of beacon entires")
return nil, xerrors.Errorf("tipset contained different number for beacon entires")
}
for i, be := range bh.BeaconEntries {
if targetBE[i].Index != be.Index || !bytes.Equal(targetBE[i].Data, be.Data) {
markBad("different beacon eintires in epoch")
return nil, xerrors.Errorf("tipset contained different number for beacon entires")
}
}
}
}
blockSet := []*types.TipSet{from}
at := from.Parents()

View File

@ -35,8 +35,8 @@ type EPostProof struct {
}
type BeaconEntry struct {
Index uint64
Signature crypto.Signature
Index uint64
Data []byte
}
type BlockHeader struct {
@ -46,7 +46,7 @@ type BlockHeader struct {
EPostProof EPostProof // 2
BeaconEntries []*BeaconEntry
BeaconEntries []BeaconEntry
Parents []cid.Cid // 3

View File

@ -40,7 +40,7 @@ func (t *BlockHeader) MarshalCBOR(w io.Writer) error {
return err
}
// t.BeaconEntries ([]*types.BeaconEntry) (slice)
// t.BeaconEntries ([]types.BeaconEntry) (slice)
if len(t.BeaconEntries) > cbg.MaxLength {
return xerrors.Errorf("Slice value in field t.BeaconEntries was too long")
}
@ -181,7 +181,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error {
}
}
// t.BeaconEntries ([]*types.BeaconEntry) (slice)
// t.BeaconEntries ([]types.BeaconEntry) (slice)
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
@ -196,7 +196,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error {
return fmt.Errorf("expected cbor array")
}
if extra > 0 {
t.BeaconEntries = make([]*BeaconEntry, extra)
t.BeaconEntries = make([]BeaconEntry, extra)
}
for i := 0; i < int(extra); i++ {
@ -205,7 +205,7 @@ func (t *BlockHeader) UnmarshalCBOR(r io.Reader) error {
return err
}
t.BeaconEntries[i] = &v
t.BeaconEntries[i] = v
}
// t.Parents ([]cid.Cid) (slice)
@ -1512,8 +1512,15 @@ func (t *BeaconEntry) MarshalCBOR(w io.Writer) error {
return err
}
// t.Signature (crypto.Signature) (struct)
if err := t.Signature.MarshalCBOR(w); err != nil {
// t.Data ([]uint8) (slice)
if len(t.Data) > cbg.ByteArrayMaxLen {
return xerrors.Errorf("Byte array in field t.Data was too long")
}
if _, err := w.Write(cbg.CborEncodeMajorType(cbg.MajByteString, uint64(len(t.Data)))); err != nil {
return err
}
if _, err := w.Write(t.Data); err != nil {
return err
}
return nil
@ -1548,14 +1555,22 @@ func (t *BeaconEntry) UnmarshalCBOR(r io.Reader) error {
t.Index = uint64(extra)
}
// t.Signature (crypto.Signature) (struct)
// t.Data ([]uint8) (slice)
{
if err := t.Signature.UnmarshalCBOR(br); err != nil {
return xerrors.Errorf("unmarshaling t.Signature: %w", err)
}
maj, extra, err = cbg.CborReadHeader(br)
if err != nil {
return err
}
if extra > cbg.ByteArrayMaxLen {
return fmt.Errorf("t.Data: byte array too large (%d)", extra)
}
if maj != cbg.MajByteString {
return fmt.Errorf("expected byte array")
}
t.Data = make([]byte, extra)
if _, err := io.ReadFull(br, t.Data); err != nil {
return err
}
return nil
}

View File

@ -378,7 +378,8 @@ func (m *Miner) computeTicket(ctx context.Context, addr address.Address, base *M
}, nil
}
func (m *Miner) createBlock(base *MiningBase, addr address.Address, ticket *types.Ticket, proof *types.EPostProof, bvals []*types.BeaconEntry, pending []*types.SignedMessage) (*types.BlockMsg, error) {
func (m *Miner) createBlock(base *MiningBase, addr address.Address, ticket *types.Ticket,
proof *types.EPostProof, bvals []types.BeaconEntry, pending []*types.SignedMessage) (*types.BlockMsg, error) {
msgs, err := SelectMessages(context.TODO(), m.api.StateGetActor, base.ts, pending)
if err != nil {
return nil, xerrors.Errorf("message filtering failed: %w", err)

View File

@ -269,7 +269,7 @@ func (a *StateAPI) MinerGetBaseInfo(ctx context.Context, maddr address.Address,
return stmgr.MinerGetBaseInfo(ctx, a.StateManager, tsk, maddr)
}
func (a *StateAPI) MinerCreateBlock(ctx context.Context, addr address.Address, parentsTSK types.TipSetKey, ticket *types.Ticket, proof *types.EPostProof, bvals []*types.BeaconEntry, msgs []*types.SignedMessage, height abi.ChainEpoch, ts uint64) (*types.BlockMsg, error) {
func (a *StateAPI) MinerCreateBlock(ctx context.Context, addr address.Address, parentsTSK types.TipSetKey, ticket *types.Ticket, proof *types.EPostProof, bvals []types.BeaconEntry, msgs []*types.SignedMessage, height abi.ChainEpoch, ts uint64) (*types.BlockMsg, error) {
parents, err := a.Chain.GetTipSetFromKey(parentsTSK)
if err != nil {
return nil, xerrors.Errorf("loading tipset %s: %w", parentsTSK, err)