2019-07-25 22:15:33 +00:00
|
|
|
package gen
|
2019-07-05 14:46:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-07-19 11:56:18 +00:00
|
|
|
bls "github.com/filecoin-project/go-bls-sigs"
|
2019-07-05 14:46:21 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
hamt "github.com/ipfs/go-hamt-ipld"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
sharray "github.com/whyrusleeping/sharray"
|
2019-07-08 15:14:36 +00:00
|
|
|
|
2019-07-12 23:52:25 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/actors"
|
2019-07-08 15:14:36 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
2019-07-26 04:54:22 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/store"
|
2019-07-12 10:23:05 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
2019-07-26 04:54:22 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/vm"
|
2019-07-05 14:46:21 +00:00
|
|
|
)
|
|
|
|
|
2019-07-26 19:01:02 +00:00
|
|
|
func MinerCreateBlock(ctx context.Context, cs *store.ChainStore, miner address.Address, parents *types.TipSet, tickets []types.Ticket, proof types.ElectionProof, msgs []*types.SignedMessage) (*types.FullBlock, error) {
|
2019-07-11 02:36:43 +00:00
|
|
|
st, err := cs.TipSetState(parents.Cids())
|
2019-07-05 14:46:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to load tipset state")
|
|
|
|
}
|
|
|
|
|
2019-07-11 02:36:43 +00:00
|
|
|
height := parents.Height() + uint64(len(tickets))
|
2019-07-05 14:46:21 +00:00
|
|
|
|
2019-07-26 12:28:29 +00:00
|
|
|
vmi, err := vm.NewVM(st, height, miner, cs)
|
2019-07-11 02:36:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-05 14:46:21 +00:00
|
|
|
|
|
|
|
// apply miner reward
|
2019-07-26 12:28:29 +00:00
|
|
|
if err := vmi.TransferFunds(actors.NetworkAddress, miner, vm.MiningRewardForBlock(parents)); err != nil {
|
2019-07-05 14:46:21 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
next := &types.BlockHeader{
|
2019-07-11 02:36:43 +00:00
|
|
|
Miner: miner,
|
|
|
|
Parents: parents.Cids(),
|
|
|
|
Tickets: tickets,
|
2019-07-05 14:46:21 +00:00
|
|
|
Height: height,
|
|
|
|
}
|
|
|
|
|
2019-08-01 20:40:47 +00:00
|
|
|
var blsMessages []*types.Message
|
|
|
|
var secpkMessages []*types.SignedMessage
|
|
|
|
|
|
|
|
var blsMsgCids, secpkMsgCids []cid.Cid
|
2019-07-24 05:38:16 +00:00
|
|
|
var blsSigs []types.Signature
|
2019-07-11 02:36:43 +00:00
|
|
|
for _, msg := range msgs {
|
2019-07-05 14:46:21 +00:00
|
|
|
if msg.Signature.TypeCode() == 2 {
|
|
|
|
blsSigs = append(blsSigs, msg.Signature)
|
2019-08-01 20:40:47 +00:00
|
|
|
blsMessages = append(blsMessages, &msg.Message)
|
2019-07-05 14:46:21 +00:00
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
c, err := cs.PutMessage(&msg.Message)
|
2019-07-05 14:46:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-01 20:40:47 +00:00
|
|
|
blsMsgCids = append(blsMsgCids, c)
|
2019-07-05 14:46:21 +00:00
|
|
|
} else {
|
2019-08-01 20:40:47 +00:00
|
|
|
secpkMsgCids = append(secpkMsgCids, msg.Cid())
|
|
|
|
secpkMessages = append(secpkMessages, msg)
|
2019-07-05 14:46:21 +00:00
|
|
|
}
|
2019-08-01 20:40:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var receipts []interface{}
|
|
|
|
for _, msg := range blsMessages {
|
|
|
|
rec, err := vmi.ApplyMessage(ctx, msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "apply message failure")
|
|
|
|
}
|
|
|
|
|
|
|
|
receipts = append(receipts, rec)
|
|
|
|
}
|
|
|
|
for _, msg := range secpkMessages {
|
2019-07-26 12:28:29 +00:00
|
|
|
rec, err := vmi.ApplyMessage(ctx, &msg.Message)
|
2019-07-05 14:46:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "apply message failure")
|
|
|
|
}
|
|
|
|
|
2019-07-31 15:37:00 +00:00
|
|
|
receipts = append(receipts, rec.MessageReceipt)
|
2019-07-05 14:46:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-26 04:54:22 +00:00
|
|
|
cst := hamt.CSTFromBstore(cs.Blockstore())
|
2019-08-01 20:40:47 +00:00
|
|
|
blsmsgroot, err := sharray.Build(context.TODO(), 4, toIfArr(blsMsgCids), cst)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
secpkmsgroot, err := sharray.Build(context.TODO(), 4, toIfArr(secpkMsgCids), cst)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
mmcid, err := cst.Put(context.TODO(), &types.MsgMeta{
|
|
|
|
BlsMessages: blsmsgroot,
|
|
|
|
SecpkMessages: secpkmsgroot,
|
|
|
|
})
|
2019-07-05 14:46:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-01 20:40:47 +00:00
|
|
|
next.Messages = mmcid
|
2019-07-05 14:46:21 +00:00
|
|
|
|
|
|
|
rectroot, err := sharray.Build(context.TODO(), 4, receipts, cst)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
next.MessageReceipts = rectroot
|
|
|
|
|
2019-07-26 12:28:29 +00:00
|
|
|
stateRoot, err := vmi.Flush(context.TODO())
|
2019-07-05 14:46:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "flushing state tree failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
aggSig, err := aggregateSignatures(blsSigs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
next.BLSAggregate = aggSig
|
|
|
|
next.StateRoot = stateRoot
|
2019-07-11 02:36:43 +00:00
|
|
|
pweight := cs.Weight(parents)
|
2019-07-12 10:23:05 +00:00
|
|
|
next.ParentWeight = types.NewInt(pweight)
|
2019-07-05 14:46:21 +00:00
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
fullBlock := &types.FullBlock{
|
2019-08-01 20:40:47 +00:00
|
|
|
Header: next,
|
|
|
|
BlsMessages: blsMessages,
|
|
|
|
SecpkMessages: secpkMessages,
|
2019-07-05 14:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fullBlock, nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 05:38:16 +00:00
|
|
|
func aggregateSignatures(sigs []types.Signature) (types.Signature, error) {
|
2019-07-05 14:46:21 +00:00
|
|
|
var blsSigs []bls.Signature
|
|
|
|
for _, s := range sigs {
|
|
|
|
var bsig bls.Signature
|
|
|
|
copy(bsig[:], s.Data)
|
|
|
|
blsSigs = append(blsSigs, bsig)
|
|
|
|
}
|
|
|
|
|
|
|
|
aggSig := bls.Aggregate(blsSigs)
|
2019-07-24 05:38:16 +00:00
|
|
|
return types.Signature{
|
|
|
|
Type: types.KTBLS,
|
2019-07-05 14:46:21 +00:00
|
|
|
Data: aggSig[:],
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func toIfArr(cids []cid.Cid) []interface{} {
|
|
|
|
out := make([]interface{}, 0, len(cids))
|
|
|
|
for _, c := range cids {
|
|
|
|
out = append(out, c)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|