2019-07-25 22:15:03 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2019-08-21 17:15:28 +00:00
|
|
|
"bytes"
|
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"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/minio/blake2b-simd"
|
2022-06-15 10:06:22 +00:00
|
|
|
"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"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
"github.com/filecoin-project/go-state-types/crypto"
|
|
|
|
"github.com/filecoin-project/go-state-types/proof"
|
2020-01-07 16:23:12 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-11 21:35:06 +00:00
|
|
|
func (t *Ticket) Quality() float64 {
|
|
|
|
ticketHash := blake2b.Sum256(t.VRFProof)
|
|
|
|
ticketNum := BigFromBytes(ticketHash[:]).Int
|
|
|
|
ticketDenu := big.NewInt(1)
|
|
|
|
ticketDenu.Lsh(ticketDenu, 256)
|
|
|
|
tv, _ := new(big.Rat).SetFrac(ticketNum, ticketDenu).Float64()
|
|
|
|
tq := 1 - tv
|
|
|
|
return tq
|
|
|
|
}
|
|
|
|
|
2020-03-25 23:16:17 +00:00
|
|
|
type BeaconEntry struct {
|
2020-04-08 15:11:42 +00:00
|
|
|
Round uint64
|
2020-04-06 12:47:14 +00:00
|
|
|
Data []byte
|
2020-04-14 03:05:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 18:39:37 +00:00
|
|
|
func NewBeaconEntry(round uint64, data []byte) BeaconEntry {
|
2020-04-14 03:05:19 +00:00
|
|
|
return BeaconEntry{
|
2020-04-30 18:39:37 +00:00
|
|
|
Round: round,
|
|
|
|
Data: data,
|
2020-04-14 03:05:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
type BlockHeader struct {
|
2021-09-02 16:07:23 +00:00
|
|
|
Miner address.Address // 0 unique per block/miner
|
|
|
|
|
2022-04-20 21:34:28 +00:00
|
|
|
Ticket *Ticket // 1 unique per block/miner: should be a valid VRF
|
|
|
|
ElectionProof *ElectionProof // 2 unique per block/miner: should be a valid VRF
|
|
|
|
BeaconEntries []BeaconEntry // 3 identical for all blocks in same tipset
|
|
|
|
WinPoStProof []proof.PoStProof // 4 unique per block/miner
|
|
|
|
Parents []cid.Cid // 5 identical for all blocks in same tipset
|
|
|
|
ParentWeight BigInt // 6 identical for all blocks in same tipset
|
|
|
|
Height abi.ChainEpoch // 7 identical for all blocks in same tipset
|
|
|
|
ParentStateRoot cid.Cid // 8 identical for all blocks in same tipset
|
|
|
|
ParentMessageReceipts cid.Cid // 9 identical for all blocks in same tipset
|
|
|
|
Messages cid.Cid // 10 unique per block
|
|
|
|
BLSAggregate *crypto.Signature // 11 unique per block: aggrregate of BLS messages from above
|
|
|
|
Timestamp uint64 // 12 identical for all blocks in same tipset / hard-tied to the value of Height above
|
|
|
|
BlockSig *crypto.Signature // 13 unique per block/miner: miner signature
|
|
|
|
ForkSignaling uint64 // 14 currently unused/undefined
|
|
|
|
ParentBaseFee abi.TokenAmount // 15 identical for all blocks in same tipset: the base fee after executing parent tipset
|
2021-01-27 19:52:18 +00:00
|
|
|
|
|
|
|
validated bool // internal, true if the signature has been validated
|
2019-07-25 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (blk *BlockHeader) ToStorageBlock() (block.Block, error) {
|
|
|
|
data, err := blk.Serialize()
|
2019-07-25 22:15:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-23 02:05:11 +00:00
|
|
|
c, err := abi.CidBuilder.Sum(data)
|
2019-07-25 22:15:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return block.NewBlockWithCid(data, c)
|
|
|
|
}
|
|
|
|
|
2020-06-02 14:29:39 +00:00
|
|
|
func (blk *BlockHeader) Cid() cid.Cid {
|
|
|
|
sb, err := blk.ToStorageBlock()
|
2019-07-25 22:15:03 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2020-05-14 16:41:10 +00:00
|
|
|
func (blk *BlockHeader) SetValidated() {
|
|
|
|
blk.validated = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (blk *BlockHeader) IsValidated() bool {
|
|
|
|
return blk.validated
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-07-23 02:05:11 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := mm.MarshalCBOR(&buf); err != nil {
|
2019-08-22 01:29:19 +00:00
|
|
|
return nil, xerrors.Errorf("failed to marshal MsgMeta: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-07-23 02:05:11 +00:00
|
|
|
c, err := abi.CidBuilder.Sum(buf.Bytes())
|
2019-08-22 01:29:19 +00:00
|
|
|
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
|
2020-08-06 06:24:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func CidArrsSubset(a, b []cid.Cid) bool {
|
|
|
|
// order ignoring compare...
|
|
|
|
s := make(map[cid.Cid]bool)
|
|
|
|
for _, c := range b {
|
|
|
|
s[c] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range a {
|
|
|
|
if !s[c] {
|
|
|
|
return false
|
2019-09-03 04:36:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2019-09-06 06:26:02 +00:00
|
|
|
|
2020-04-10 15:14:43 +00:00
|
|
|
func CidArrsContains(a []cid.Cid, b cid.Cid) bool {
|
|
|
|
for _, elem := range a {
|
|
|
|
if elem.Equals(b) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-10-28 18:22:40 +00:00
|
|
|
var blocksPerEpoch = NewInt(build.BlocksPerEpoch)
|
|
|
|
|
2019-12-02 22:49:41 +00:00
|
|
|
const sha256bits = 256
|
|
|
|
|
2020-04-08 19:06:41 +00:00
|
|
|
func IsTicketWinner(vrfTicket []byte, mypow BigInt, totpow BigInt) bool {
|
2019-09-06 06:26:02 +00:00
|
|
|
/*
|
|
|
|
Need to check that
|
2020-04-07 18:23:16 +00:00
|
|
|
(h(vrfout) + 1) / (max(h) + 1) <= e * myPower / totalPower
|
2019-10-28 19:01:10 +00:00
|
|
|
max(h) == 2^256-1
|
|
|
|
which in terms of integer math means:
|
2020-04-07 18:23:16 +00:00
|
|
|
(h(vrfout) + 1) * totalPower <= e * myPower * 2^256
|
2019-11-09 00:18:32 +00:00
|
|
|
in 2^256 space, it is equivalent to:
|
2020-04-07 18:23:16 +00:00
|
|
|
h(vrfout) * totalPower < e * myPower * 2^256
|
2019-12-09 14:15:25 +00:00
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
*/
|
|
|
|
|
2020-05-02 02:43:36 +00:00
|
|
|
h := blake2b.Sum256(vrfTicket)
|
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-09-06 06:26:02 +00:00
|
|
|
|
2019-11-21 22:21:45 +00:00
|
|
|
// rhs = sectorSize * 2^256
|
|
|
|
// rhs = sectorSize << 256
|
2020-04-07 18:23:16 +00:00
|
|
|
rhs := new(big.Int).Lsh(mypow.Int, sha256bits)
|
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
|
|
|
|
|
|
|
func (t *Ticket) Equals(ot *Ticket) bool {
|
2020-02-28 17:21:22 +00:00
|
|
|
return bytes.Equal(t.VRFProof, ot.VRFProof)
|
2019-10-01 18:47:42 +00:00
|
|
|
}
|