2019-07-25 22:15:03 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2019-08-21 17:15:28 +00:00
|
|
|
"bytes"
|
2019-10-13 09:08:34 +00:00
|
|
|
"context"
|
2019-12-19 21:06:25 +00:00
|
|
|
"github.com/filecoin-project/go-sectorbuilder"
|
2019-09-06 06:26:02 +00:00
|
|
|
"math/big"
|
2019-08-15 02:30:21 +00:00
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
block "github.com/ipfs/go-block-format"
|
|
|
|
"github.com/ipfs/go-cid"
|
2019-10-28 19:01:10 +00:00
|
|
|
"github.com/minio/sha256-simd"
|
2019-07-25 22:15:03 +00:00
|
|
|
"github.com/multiformats/go-multihash"
|
2019-10-13 09:08:34 +00:00
|
|
|
"go.opencensus.io/trace"
|
2019-08-22 01:29:19 +00:00
|
|
|
xerrors "golang.org/x/xerrors"
|
2019-07-25 22:15:03 +00:00
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2019-10-28 18:22:40 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2019-07-25 22:15:03 +00:00
|
|
|
)
|
|
|
|
|
2019-08-15 02:30:21 +00:00
|
|
|
type Ticket struct {
|
2019-10-09 04:38:59 +00:00
|
|
|
VRFProof []byte
|
2019-07-25 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 22:21:45 +00:00
|
|
|
type EPostTicket struct {
|
|
|
|
Partial []byte
|
|
|
|
SectorID uint64
|
|
|
|
ChallengeIndex uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type EPostProof struct {
|
2019-11-28 12:46:56 +00:00
|
|
|
Proof []byte
|
|
|
|
PostRand []byte
|
|
|
|
Candidates []EPostTicket
|
2019-11-21 22:21:45 +00:00
|
|
|
}
|
2019-07-25 22:15:03 +00:00
|
|
|
|
|
|
|
type BlockHeader struct {
|
|
|
|
Miner address.Address
|
|
|
|
|
2019-11-19 15:53:00 +00:00
|
|
|
Ticket *Ticket
|
2019-07-25 22:15:03 +00:00
|
|
|
|
2019-11-21 22:21:45 +00:00
|
|
|
EPostProof EPostProof
|
2019-07-25 22:15:03 +00:00
|
|
|
|
|
|
|
Parents []cid.Cid
|
|
|
|
|
|
|
|
ParentWeight BigInt
|
|
|
|
|
|
|
|
Height uint64
|
|
|
|
|
2019-09-27 23:55:15 +00:00
|
|
|
ParentStateRoot cid.Cid
|
|
|
|
|
|
|
|
ParentMessageReceipts cid.Cid
|
2019-07-25 22:15:03 +00:00
|
|
|
|
|
|
|
Messages cid.Cid
|
|
|
|
|
|
|
|
BLSAggregate Signature
|
|
|
|
|
2019-08-27 00:46:39 +00:00
|
|
|
Timestamp uint64
|
|
|
|
|
2019-11-19 20:05:21 +00:00
|
|
|
BlockSig *Signature
|
2019-07-25 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BlockHeader) ToStorageBlock() (block.Block, error) {
|
|
|
|
data, err := b.Serialize()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-21 17:15:28 +00:00
|
|
|
pref := cid.NewPrefixV1(cid.DagCBOR, multihash.BLAKE2B_MIN+31)
|
2019-07-25 22:15:03 +00:00
|
|
|
c, err := pref.Sum(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return block.NewBlockWithCid(data, c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BlockHeader) Cid() cid.Cid {
|
|
|
|
sb, err := b.ToStorageBlock()
|
|
|
|
if err != nil {
|
2019-11-16 23:41:14 +00:00
|
|
|
panic(err) // Not sure i'm entirely comfortable with this one, needs to be checked
|
2019-07-25 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return sb.Cid()
|
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeBlock(b []byte) (*BlockHeader, error) {
|
|
|
|
var blk BlockHeader
|
2019-08-21 17:15:28 +00:00
|
|
|
if err := blk.UnmarshalCBOR(bytes.NewReader(b)); err != nil {
|
2019-07-25 22:15:03 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &blk, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (blk *BlockHeader) Serialize() ([]byte, error) {
|
2019-08-21 17:15:28 +00:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := blk.MarshalCBOR(buf); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
func (blk *BlockHeader) LastTicket() *Ticket {
|
2019-11-19 15:53:00 +00:00
|
|
|
return blk.Ticket
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
|
|
|
|
2019-09-11 20:10:29 +00:00
|
|
|
func (blk *BlockHeader) SigningBytes() ([]byte, error) {
|
|
|
|
blkcopy := *blk
|
2019-11-19 20:05:21 +00:00
|
|
|
blkcopy.BlockSig = nil
|
2019-09-11 20:10:29 +00:00
|
|
|
|
|
|
|
return blkcopy.Serialize()
|
|
|
|
}
|
|
|
|
|
2019-10-13 09:08:34 +00:00
|
|
|
func (blk *BlockHeader) CheckBlockSignature(ctx context.Context, worker address.Address) error {
|
2019-12-05 05:14:19 +00:00
|
|
|
_, span := trace.StartSpan(ctx, "checkBlockSignature")
|
2019-10-13 09:08:34 +00:00
|
|
|
defer span.End()
|
|
|
|
|
2019-09-12 04:12:35 +00:00
|
|
|
sigb, err := blk.SigningBytes()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to get block signing bytes: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return blk.BlockSig.Verify(worker, sigb)
|
|
|
|
}
|
|
|
|
|
2019-08-22 01:29:19 +00:00
|
|
|
type MsgMeta struct {
|
|
|
|
BlsMessages cid.Cid
|
|
|
|
SecpkMessages cid.Cid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *MsgMeta) Cid() cid.Cid {
|
|
|
|
b, err := mm.ToStorageBlock()
|
|
|
|
if err != nil {
|
2019-11-16 23:41:14 +00:00
|
|
|
panic(err) // also maybe sketchy
|
2019-08-22 01:29:19 +00:00
|
|
|
}
|
|
|
|
return b.Cid()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mm *MsgMeta) ToStorageBlock() (block.Block, error) {
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
if err := mm.MarshalCBOR(buf); err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to marshal MsgMeta: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
pref := cid.NewPrefixV1(cid.DagCBOR, multihash.BLAKE2B_MIN+31)
|
|
|
|
c, err := pref.Sum(buf.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return block.NewBlockWithCid(buf.Bytes(), c)
|
|
|
|
}
|
2019-09-03 04:36:07 +00:00
|
|
|
|
|
|
|
func CidArrsEqual(a, b []cid.Cid) bool {
|
|
|
|
if len(a) != len(b) {
|
|
|
|
return false
|
|
|
|
}
|
2019-09-08 20:14:01 +00:00
|
|
|
|
|
|
|
// order ignoring compare...
|
|
|
|
s := make(map[cid.Cid]bool)
|
|
|
|
for _, c := range a {
|
|
|
|
s[c] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range b {
|
|
|
|
if !s[c] {
|
2019-09-03 04:36:07 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2019-09-06 06:26:02 +00:00
|
|
|
|
2019-10-28 18:22:40 +00:00
|
|
|
var blocksPerEpoch = NewInt(build.BlocksPerEpoch)
|
|
|
|
|
2019-12-02 22:49:41 +00:00
|
|
|
const sha256bits = 256
|
|
|
|
|
2019-12-09 14:15:25 +00:00
|
|
|
func IsTicketWinner(partialTicket []byte, ssizeI uint64, snum uint64, totpow BigInt) bool {
|
2019-11-21 22:21:45 +00:00
|
|
|
ssize := NewInt(ssizeI)
|
2019-12-17 22:23:43 +00:00
|
|
|
ssampled := ElectionPostChallengeCount(snum, 0) // TODO: faults in epost?
|
2019-09-06 06:26:02 +00:00
|
|
|
/*
|
|
|
|
Need to check that
|
2019-11-21 22:21:45 +00:00
|
|
|
(h(vrfout) + 1) / (max(h) + 1) <= e * sectorSize / totalPower
|
2019-10-28 19:01:10 +00:00
|
|
|
max(h) == 2^256-1
|
|
|
|
which in terms of integer math means:
|
2019-11-21 22:21:45 +00:00
|
|
|
(h(vrfout) + 1) * totalPower <= e * sectorSize * 2^256
|
2019-11-09 00:18:32 +00:00
|
|
|
in 2^256 space, it is equivalent to:
|
2019-11-21 22:21:45 +00:00
|
|
|
h(vrfout) * totalPower < e * sectorSize * 2^256
|
2019-12-09 14:15:25 +00:00
|
|
|
|
|
|
|
Because of SectorChallengeRatioDiv sampling for proofs
|
|
|
|
we need to scale this appropriately.
|
|
|
|
|
|
|
|
Let c = ceil(numSectors/SectorChallengeRatioDiv)
|
|
|
|
(c is the number of tickets a miner requests)
|
|
|
|
Accordingly we check
|
|
|
|
(h(vrfout) + 1) / 2^256 <= e * sectorSize / totalPower * snum / c
|
|
|
|
or
|
|
|
|
h(vrfout) * totalPower * c < e * sectorSize * 2^256 * snum
|
2019-09-06 06:26:02 +00:00
|
|
|
*/
|
|
|
|
|
2019-11-21 22:21:45 +00:00
|
|
|
h := sha256.Sum256(partialTicket)
|
2019-09-06 06:26:02 +00:00
|
|
|
|
2019-10-28 19:01:10 +00:00
|
|
|
lhs := BigFromBytes(h[:]).Int
|
|
|
|
lhs = lhs.Mul(lhs, totpow.Int)
|
2019-12-09 14:15:25 +00:00
|
|
|
lhs = lhs.Mul(lhs, new(big.Int).SetUint64(ssampled))
|
2019-09-06 06:26:02 +00:00
|
|
|
|
2019-11-21 22:21:45 +00:00
|
|
|
// rhs = sectorSize * 2^256
|
|
|
|
// rhs = sectorSize << 256
|
2019-12-02 22:49:41 +00:00
|
|
|
rhs := new(big.Int).Lsh(ssize.Int, sha256bits)
|
2019-12-09 14:15:25 +00:00
|
|
|
rhs = rhs.Mul(rhs, new(big.Int).SetUint64(snum))
|
2019-10-28 20:28:01 +00:00
|
|
|
rhs = rhs.Mul(rhs, blocksPerEpoch.Int)
|
2019-11-09 00:18:32 +00:00
|
|
|
|
2019-11-21 22:21:45 +00:00
|
|
|
// h(vrfout) * totalPower < e * sectorSize * 2^256?
|
2019-12-02 23:18:27 +00:00
|
|
|
return lhs.Cmp(rhs) < 0
|
2019-09-06 06:26:02 +00:00
|
|
|
}
|
2019-10-01 18:47:42 +00:00
|
|
|
|
2019-12-17 22:23:43 +00:00
|
|
|
func ElectionPostChallengeCount(sectors uint64, faults int) uint64 {
|
2019-12-19 21:06:25 +00:00
|
|
|
return sectorbuilder.ElectionPostChallengeCount(sectors, faults)
|
2019-12-09 14:15:25 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 18:47:42 +00:00
|
|
|
func (t *Ticket) Equals(ot *Ticket) bool {
|
2019-10-09 04:38:59 +00:00
|
|
|
return bytes.Equal(t.VRFProof, ot.VRFProof)
|
2019-10-01 18:47:42 +00:00
|
|
|
}
|