lotus/chain/consensus/filcns/mine.go

143 lines
3.7 KiB
Go
Raw Normal View History

2021-09-02 16:07:23 +00:00
package filcns
2019-07-05 14:46:21 +00:00
import (
"context"
2019-09-06 20:03:28 +00:00
2021-09-02 16:07:23 +00:00
"github.com/ipfs/go-cid"
2019-08-27 00:46:39 +00:00
"golang.org/x/xerrors"
2021-09-02 16:07:23 +00:00
"github.com/filecoin-project/go-state-types/crypto"
2021-09-02 16:45:18 +00:00
2020-04-09 00:24:10 +00:00
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain"
2021-09-02 16:45:18 +00:00
"github.com/filecoin-project/lotus/chain/consensus"
"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/types"
2019-07-05 14:46:21 +00:00
)
2021-09-02 16:07:23 +00:00
func (filec *FilecoinEC) CreateBlock(ctx context.Context, w api.Wallet, bt *api.BlockTemplate) (*types.FullBlock, error) {
2021-12-11 21:03:00 +00:00
pts, err := filec.sm.ChainStore().LoadTipSet(ctx, bt.Parents)
2020-04-09 00:24:10 +00:00
if err != nil {
return nil, xerrors.Errorf("failed to load parent tipset: %w", err)
}
2021-09-02 16:07:23 +00:00
st, recpts, err := filec.sm.TipSetState(ctx, pts)
2019-07-05 14:46:21 +00:00
if err != nil {
return nil, xerrors.Errorf("failed to load tipset state: %w", err)
2019-07-05 14:46:21 +00:00
}
2021-09-02 16:07:23 +00:00
_, lbst, err := stmgr.GetLookbackTipSetForRound(ctx, filec.sm, pts, bt.Epoch)
if err != nil {
return nil, xerrors.Errorf("getting lookback miner actor state: %w", err)
}
2021-09-02 16:07:23 +00:00
worker, err := stmgr.GetMinerWorkerRaw(ctx, filec.sm, lbst, bt.Miner)
2019-08-27 00:46:39 +00:00
if err != nil {
return nil, xerrors.Errorf("failed to get miner worker: %w", err)
}
2019-07-05 14:46:21 +00:00
next := &types.BlockHeader{
2020-04-09 00:24:10 +00:00
Miner: bt.Miner,
Parents: bt.Parents.Cids(),
Ticket: bt.Ticket,
ElectionProof: bt.Eproof,
2020-04-17 22:02:43 +00:00
BeaconEntries: bt.BeaconValues,
Height: bt.Epoch,
Timestamp: bt.Timestamp,
WinPoStProof: bt.WinningPoStProof,
2019-09-27 23:55:15 +00:00
ParentStateRoot: st,
ParentMessageReceipts: recpts,
2019-07-05 14:46:21 +00:00
}
var blsMessages []*types.Message
var secpkMessages []*types.SignedMessage
var blsMsgCids, secpkMsgCids []cid.Cid
2020-02-12 23:52:36 +00:00
var blsSigs []crypto.Signature
nv := filec.sm.GetNetworkVersion(ctx, bt.Epoch)
2020-04-09 00:24:10 +00:00
for _, msg := range bt.Messages {
2020-02-12 23:52:36 +00:00
if msg.Signature.Type == crypto.SigTypeBLS {
2019-07-05 14:46:21 +00:00
blsSigs = append(blsSigs, msg.Signature)
blsMessages = append(blsMessages, &msg.Message)
2019-07-05 14:46:21 +00:00
2021-12-17 09:42:09 +00:00
c, err := filec.sm.ChainStore().PutMessage(ctx, &msg.Message)
2019-07-05 14:46:21 +00:00
if err != nil {
return nil, err
}
blsMsgCids = append(blsMsgCids, c)
} else if chain.IsValidSecpkSigType(nv, msg.Signature.Type) {
2021-12-17 09:42:09 +00:00
c, err := filec.sm.ChainStore().PutMessage(ctx, msg)
if err != nil {
return nil, err
}
secpkMsgCids = append(secpkMsgCids, c)
secpkMessages = append(secpkMessages, msg)
} else {
return nil, xerrors.Errorf("unknown sig type: %d", msg.Signature.Type)
2019-07-05 14:46:21 +00:00
}
}
2021-09-02 16:07:23 +00:00
store := filec.sm.ChainStore().ActorStore(ctx)
blsmsgroot, err := consensus.ToMessagesArray(store, blsMsgCids)
if err != nil {
2019-09-17 01:56:37 +00:00
return nil, xerrors.Errorf("building bls amt: %w", err)
}
2021-09-02 16:07:23 +00:00
secpkmsgroot, err := consensus.ToMessagesArray(store, secpkMsgCids)
if err != nil {
2019-09-17 01:56:37 +00:00
return nil, xerrors.Errorf("building secpk amt: %w", err)
}
mmcid, err := store.Put(store.Context(), &types.MsgMeta{
BlsMessages: blsmsgroot,
SecpkMessages: secpkmsgroot,
})
2019-07-05 14:46:21 +00:00
if err != nil {
return nil, err
}
next.Messages = mmcid
2019-07-05 14:46:21 +00:00
2021-09-02 16:07:23 +00:00
aggSig, err := consensus.AggregateSignatures(blsSigs)
2019-07-05 14:46:21 +00:00
if err != nil {
return nil, err
}
next.BLSAggregate = aggSig
2021-09-02 16:07:23 +00:00
pweight, err := filec.sm.ChainStore().Weight(ctx, pts)
2019-10-15 05:00:30 +00:00
if err != nil {
return nil, err
}
next.ParentWeight = pweight
2019-07-05 14:46:21 +00:00
2021-09-02 16:07:23 +00:00
baseFee, err := filec.sm.ChainStore().ComputeBaseFee(ctx, pts)
if err != nil {
return nil, xerrors.Errorf("computing base fee: %w", err)
}
next.ParentBaseFee = baseFee
2019-10-03 19:45:14 +00:00
nosigbytes, err := next.SigningBytes()
if err != nil {
return nil, xerrors.Errorf("failed to get signing bytes for block: %w", err)
}
2020-10-14 00:44:42 +00:00
sig, err := w.WalletSign(ctx, worker, nosigbytes, api.MsgMeta{
2020-10-08 23:27:49 +00:00
Type: api.MTBlock,
})
2019-08-27 00:46:39 +00:00
if err != nil {
return nil, xerrors.Errorf("failed to sign new block: %w", err)
}
next.BlockSig = sig
2019-08-27 00:46:39 +00:00
fullBlock := &types.FullBlock{
Header: next,
BlsMessages: blsMessages,
SecpkMessages: secpkMessages,
2019-07-05 14:46:21 +00:00
}
return fullBlock, nil
}