diff --git a/chain/gen/mining.go b/chain/gen/mining.go index b5f7876e9..4825785c0 100644 --- a/chain/gen/mining.go +++ b/chain/gen/mining.go @@ -137,9 +137,9 @@ func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wal // TODO: set timestamp - nosigbytes, err := next.Serialize() + nosigbytes, err := next.SigningBytes() if err != nil { - return nil, xerrors.Errorf("failed to serialize block header with no signature: %w", err) + return nil, xerrors.Errorf("failed to get signing bytes for block: %w", err) } waddr, err := vm.ResolveToKeyAddr(vmi.StateTree(), cst, worker) diff --git a/chain/sync.go b/chain/sync.go index d2274dabb..298234d44 100644 --- a/chain/sync.go +++ b/chain/sync.go @@ -373,6 +373,15 @@ func (syncer *Syncer) validateTickets(ctx context.Context, mworker address.Addre return nil } +func checkBlockSignature(blk *types.BlockHeader, worker address.Address) error { + sigb, err := blk.SigningBytes() + if err != nil { + return xerrors.Errorf("failed to get block signing bytes: %w", err) + } + + return blk.BlockSig.Verify(worker, sigb) +} + // Should match up with 'Semantical Validation' in validation.md in the spec func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) error { h := b.Header @@ -404,6 +413,10 @@ func (syncer *Syncer) ValidateBlock(ctx context.Context, b *types.FullBlock) err return xerrors.Errorf("GetMinerWorker failed: %w", err) } + if err := checkBlockSignature(h, waddr); err != nil { + return xerrors.Errorf("check block signature failed: %w", err) + } + if err := syncer.validateTickets(ctx, waddr, h.Tickets, baseTs); err != nil { return xerrors.Errorf("validating block tickets failed: %w", err) } diff --git a/chain/types/blockheader.go b/chain/types/blockheader.go index f5c078e6b..81556a6e9 100644 --- a/chain/types/blockheader.go +++ b/chain/types/blockheader.go @@ -93,6 +93,13 @@ func (blk *BlockHeader) LastTicket() *Ticket { return blk.Tickets[len(blk.Tickets)-1] } +func (blk *BlockHeader) SigningBytes() ([]byte, error) { + blkcopy := *blk + blkcopy.BlockSig = Signature{} + + return blkcopy.Serialize() +} + type MsgMeta struct { BlsMessages cid.Cid SecpkMessages cid.Cid