2019-07-25 22:15:03 +00:00
|
|
|
package types
|
|
|
|
|
|
|
|
import (
|
2019-08-15 02:30:21 +00:00
|
|
|
"fmt"
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
block "github.com/ipfs/go-block-format"
|
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
"github.com/multiformats/go-multihash"
|
|
|
|
"github.com/polydawn/refmt/obj/atlas"
|
|
|
|
|
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cbor.RegisterCborType(atlas.BuildEntry(BlockHeader{}).UseTag(43).Transform().
|
|
|
|
TransformMarshal(atlas.MakeMarshalTransformFunc(
|
|
|
|
func(blk BlockHeader) ([]interface{}, error) {
|
|
|
|
if blk.Tickets == nil {
|
2019-08-15 02:30:21 +00:00
|
|
|
blk.Tickets = []*Ticket{}
|
2019-07-25 22:15:03 +00:00
|
|
|
}
|
|
|
|
if blk.Parents == nil {
|
|
|
|
blk.Parents = []cid.Cid{}
|
|
|
|
}
|
2019-08-15 02:30:21 +00:00
|
|
|
|
|
|
|
var tickarrs [][][]byte // oh boy
|
|
|
|
for _, t := range blk.Tickets {
|
|
|
|
tickarrs = append(tickarrs, [][]byte{
|
|
|
|
t.VRFProof, t.VDFResult, t.VDFProof,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
return []interface{}{
|
|
|
|
blk.Miner.Bytes(),
|
2019-08-15 02:30:21 +00:00
|
|
|
tickarrs,
|
2019-07-25 22:15:03 +00:00
|
|
|
blk.ElectionProof,
|
|
|
|
blk.Parents,
|
|
|
|
blk.ParentWeight,
|
|
|
|
blk.Height,
|
|
|
|
blk.StateRoot,
|
|
|
|
blk.Messages,
|
|
|
|
blk.MessageReceipts,
|
|
|
|
}, nil
|
|
|
|
})).
|
|
|
|
TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(
|
|
|
|
func(arr []interface{}) (BlockHeader, error) {
|
|
|
|
miner, err := address.NewFromBytes(arr[0].([]byte))
|
|
|
|
if err != nil {
|
|
|
|
return BlockHeader{}, err
|
|
|
|
}
|
|
|
|
|
2019-08-15 02:30:21 +00:00
|
|
|
tickets := []*Ticket{}
|
2019-07-25 22:15:03 +00:00
|
|
|
ticketarr, _ := arr[1].([]interface{})
|
|
|
|
for _, t := range ticketarr {
|
2019-08-15 02:30:21 +00:00
|
|
|
ticklist, ok := t.([]interface{})
|
|
|
|
if !ok {
|
|
|
|
return BlockHeader{}, fmt.Errorf("tickets were incorrectly formatted (type = %T)", t)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ticklist) != 3 {
|
|
|
|
return BlockHeader{}, fmt.Errorf("ticket should be a three item array of Byte arrays (got len = %d)", len(ticklist))
|
|
|
|
}
|
|
|
|
|
|
|
|
tickets = append(tickets, &Ticket{
|
|
|
|
VRFProof: ticklist[0].([]byte),
|
|
|
|
VDFResult: ticklist[1].([]byte),
|
|
|
|
VDFProof: ticklist[2].([]byte),
|
|
|
|
})
|
2019-07-25 22:15:03 +00:00
|
|
|
}
|
|
|
|
electionProof, _ := arr[2].([]byte)
|
|
|
|
|
|
|
|
parents := []cid.Cid{}
|
|
|
|
parentsArr, _ := arr[3].([]interface{})
|
|
|
|
for _, p := range parentsArr {
|
|
|
|
parents = append(parents, p.(cid.Cid))
|
|
|
|
}
|
|
|
|
parentWeight := arr[4].(BigInt)
|
|
|
|
height := arr[5].(uint64)
|
|
|
|
stateRoot := arr[6].(cid.Cid)
|
|
|
|
|
|
|
|
msgscid := arr[7].(cid.Cid)
|
|
|
|
recscid := arr[8].(cid.Cid)
|
|
|
|
|
|
|
|
return BlockHeader{
|
|
|
|
Miner: miner,
|
|
|
|
Tickets: tickets,
|
|
|
|
ElectionProof: electionProof,
|
|
|
|
Parents: parents,
|
|
|
|
ParentWeight: parentWeight,
|
|
|
|
Height: height,
|
|
|
|
StateRoot: stateRoot,
|
|
|
|
Messages: msgscid,
|
|
|
|
MessageReceipts: recscid,
|
|
|
|
}, nil
|
|
|
|
})).
|
|
|
|
Complete())
|
2019-08-01 20:40:47 +00:00
|
|
|
cbor.RegisterCborType(MsgMeta{})
|
2019-08-15 02:30:21 +00:00
|
|
|
cbor.RegisterCborType(Ticket{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type Ticket struct {
|
|
|
|
VRFProof []byte
|
|
|
|
VDFResult []byte
|
|
|
|
VDFProof []byte
|
2019-07-25 22:15:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ElectionProof []byte
|
|
|
|
|
|
|
|
type BlockHeader struct {
|
|
|
|
Miner address.Address
|
|
|
|
|
2019-08-15 02:30:21 +00:00
|
|
|
Tickets []*Ticket
|
2019-07-25 22:15:03 +00:00
|
|
|
|
|
|
|
ElectionProof []byte
|
|
|
|
|
|
|
|
Parents []cid.Cid
|
|
|
|
|
|
|
|
ParentWeight BigInt
|
|
|
|
|
|
|
|
Height uint64
|
|
|
|
|
|
|
|
StateRoot cid.Cid
|
|
|
|
|
|
|
|
Messages cid.Cid
|
|
|
|
|
|
|
|
BLSAggregate Signature
|
|
|
|
|
|
|
|
MessageReceipts cid.Cid
|
|
|
|
}
|
|
|
|
|
2019-08-01 20:40:47 +00:00
|
|
|
type MsgMeta struct {
|
|
|
|
BlsMessages cid.Cid
|
|
|
|
SecpkMessages cid.Cid
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
pref := cid.NewPrefixV1(0x1f, 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 {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb.Cid()
|
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeBlock(b []byte) (*BlockHeader, error) {
|
|
|
|
var blk BlockHeader
|
|
|
|
if err := cbor.DecodeInto(b, &blk); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &blk, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (blk *BlockHeader) Serialize() ([]byte, error) {
|
|
|
|
return cbor.DumpObject(blk)
|
|
|
|
}
|