lotus/chain/types/blockheader.go

216 lines
4.0 KiB
Go
Raw Normal View History

package types
import (
2019-08-21 17:15:28 +00:00
"bytes"
"math/big"
2019-08-15 02:30:21 +00:00
"github.com/minio/blake2b-simd"
2020-02-08 02:18:32 +00:00
"github.com/filecoin-project/specs-actors/actors/abi"
2020-02-12 23:52:36 +00:00
"github.com/filecoin-project/specs-actors/actors/crypto"
2020-01-07 16:23:12 +00:00
block "github.com/ipfs/go-block-format"
"github.com/ipfs/go-cid"
"github.com/multiformats/go-multihash"
2019-08-22 01:29:19 +00:00
xerrors "golang.org/x/xerrors"
"github.com/filecoin-project/go-address"
2020-01-07 16:23:12 +00:00
2019-10-28 18:22:40 +00:00
"github.com/filecoin-project/lotus/build"
)
2019-08-15 02:30:21 +00:00
type Ticket struct {
2019-10-09 04:38:59 +00:00
VRFProof []byte
}
type ElectionProof struct {
VRFProof []byte
2019-11-21 22:21:45 +00:00
}
type BeaconEntry struct {
2020-04-08 15:11:42 +00:00
Round uint64
Data []byte
2020-04-14 03:05:19 +00:00
}
func NewBeaconEntry(round uint64, data []byte) BeaconEntry {
2020-04-14 03:05:19 +00:00
return BeaconEntry{
Round: round,
Data: data,
2020-04-14 03:05:19 +00:00
}
}
type BlockHeader struct {
2020-01-30 21:30:21 +00:00
Miner address.Address // 0
2020-01-30 21:30:21 +00:00
Ticket *Ticket // 1
ElectionProof *ElectionProof // 2
BeaconEntries []BeaconEntry // 3
WinPoStProof []abi.PoStProof // 4
Parents []cid.Cid // 5
ParentWeight BigInt // 6
Height abi.ChainEpoch // 7
ParentStateRoot cid.Cid // 8
2019-09-27 23:55:15 +00:00
ParentMessageReceipts cid.Cid // 8
Messages cid.Cid // 10
BLSAggregate *crypto.Signature // 11
Timestamp uint64 // 12
2019-08-27 00:46:39 +00:00
BlockSig *crypto.Signature // 13
ForkSignaling uint64 // 14
}
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)
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
}
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 {
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
}
func (blk *BlockHeader) LastTicket() *Ticket {
return blk.Ticket
}
2019-09-11 20:10:29 +00:00
func (blk *BlockHeader) SigningBytes() ([]byte, error) {
blkcopy := *blk
blkcopy.BlockSig = nil
2019-09-11 20:10:29 +00:00
return blkcopy.Serialize()
}
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)
}
func CidArrsEqual(a, b []cid.Cid) bool {
if len(a) != len(b) {
return false
}
// order ignoring compare...
s := make(map[cid.Cid]bool)
for _, c := range a {
s[c] = true
}
for _, c := range b {
if !s[c] {
return false
}
}
return true
}
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
func IsTicketWinner(vrfTicket []byte, mypow BigInt, totpow BigInt) bool {
/*
Need to check that
(h(vrfout) + 1) / (max(h) + 1) <= e * myPower / totalPower
max(h) == 2^256-1
which in terms of integer math means:
(h(vrfout) + 1) * totalPower <= e * myPower * 2^256
2019-11-09 00:18:32 +00:00
in 2^256 space, it is equivalent to:
h(vrfout) * totalPower < e * myPower * 2^256
*/
h := blake2b.Sum256(vrfTicket)
lhs := BigFromBytes(h[:]).Int
lhs = lhs.Mul(lhs, totpow.Int)
2019-11-21 22:21:45 +00:00
// rhs = sectorSize * 2^256
// rhs = sectorSize << 256
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-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
}