2019-07-25 22:15:33 +00:00
|
|
|
package gen
|
2019-07-05 14:46:21 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-09-06 20:03:28 +00:00
|
|
|
|
2019-11-27 01:46:17 +00:00
|
|
|
bls "github.com/filecoin-project/filecoin-ffi"
|
2020-02-05 02:26:42 +00:00
|
|
|
amt "github.com/filecoin-project/go-amt-ipld/v2"
|
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"
|
2019-09-06 20:03:28 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
2020-02-04 22:19:05 +00:00
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
2019-09-17 01:56:37 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2019-08-27 00:46:39 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-07-08 15:14:36 +00:00
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
|
|
|
"github.com/filecoin-project/lotus/chain/stmgr"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/chain/vm"
|
|
|
|
"github.com/filecoin-project/lotus/chain/wallet"
|
2019-07-05 14:46:21 +00:00
|
|
|
)
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wallet, miner address.Address, parents *types.TipSet, ticket *types.Ticket, proof *types.EPostProof, msgs []*types.SignedMessage, height abi.ChainEpoch, timestamp uint64) (*types.FullBlock, error) {
|
2019-10-10 11:13:26 +00:00
|
|
|
st, recpts, err := sm.TipSetState(ctx, parents)
|
2019-07-05 14:46:21 +00:00
|
|
|
if err != nil {
|
2019-11-13 23:35:58 +00:00
|
|
|
return nil, xerrors.Errorf("failed to load tipset state: %w", err)
|
2019-07-05 14:46:21 +00:00
|
|
|
}
|
|
|
|
|
2019-10-23 17:39:14 +00:00
|
|
|
worker, err := stmgr.GetMinerWorkerRaw(ctx, sm, st, 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
|
|
|
|
2019-07-25 22:15:03 +00:00
|
|
|
next := &types.BlockHeader{
|
2019-09-27 23:55:15 +00:00
|
|
|
Miner: miner,
|
|
|
|
Parents: parents.Cids(),
|
2019-11-19 15:53:00 +00:00
|
|
|
Ticket: ticket,
|
2019-09-27 23:55:15 +00:00
|
|
|
Height: height,
|
|
|
|
Timestamp: timestamp,
|
2019-11-21 22:21:45 +00:00
|
|
|
EPostProof: *proof,
|
2019-09-27 23:55:15 +00:00
|
|
|
ParentStateRoot: st,
|
|
|
|
ParentMessageReceipts: recpts,
|
2019-07-05 14:46:21 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 20:40:47 +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
|
2019-07-11 02:36:43 +00:00
|
|
|
for _, msg := range msgs {
|
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)
|
2019-08-01 20:40:47 +00:00
|
|
|
blsMessages = append(blsMessages, &msg.Message)
|
2019-07-05 14:46:21 +00:00
|
|
|
|
2019-09-06 06:26:02 +00:00
|
|
|
c, err := sm.ChainStore().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-10-05 23:18:39 +00:00
|
|
|
c, err := sm.ChainStore().PutMessage(msg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
secpkMsgCids = append(secpkMsgCids, c)
|
2019-08-01 20:40:47 +00:00
|
|
|
secpkMessages = append(secpkMessages, msg)
|
2019-10-05 23:18:39 +00:00
|
|
|
|
2019-07-05 14:46:21 +00:00
|
|
|
}
|
2019-08-01 20:40:47 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 02:26:42 +00:00
|
|
|
bs := cbor.NewCborStore(sm.ChainStore().Blockstore())
|
|
|
|
blsmsgroot, err := amt.FromArray(ctx, bs, toIfArr(blsMsgCids))
|
2019-08-01 20:40:47 +00:00
|
|
|
if err != nil {
|
2019-09-17 01:56:37 +00:00
|
|
|
return nil, xerrors.Errorf("building bls amt: %w", err)
|
2019-08-01 20:40:47 +00:00
|
|
|
}
|
2020-02-05 02:26:42 +00:00
|
|
|
secpkmsgroot, err := amt.FromArray(ctx, bs, toIfArr(secpkMsgCids))
|
2019-08-01 20:40:47 +00:00
|
|
|
if err != nil {
|
2019-09-17 01:56:37 +00:00
|
|
|
return nil, xerrors.Errorf("building secpk amt: %w", err)
|
2019-08-01 20:40:47 +00:00
|
|
|
}
|
|
|
|
|
2020-02-05 02:26:42 +00:00
|
|
|
mmcid, err := bs.Put(ctx, &types.MsgMeta{
|
2019-08-01 20:40:47 +00:00
|
|
|
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
|
|
|
|
|
|
|
aggSig, err := aggregateSignatures(blsSigs)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
next.BLSAggregate = aggSig
|
2019-10-15 05:00:30 +00:00
|
|
|
pweight, err := sm.ChainStore().Weight(ctx, parents)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
next.ParentWeight = pweight
|
2019-07-05 14:46:21 +00:00
|
|
|
|
2020-02-04 22:19:05 +00:00
|
|
|
cst := cbor.NewCborStore(sm.ChainStore().Blockstore())
|
2019-10-03 19:45:14 +00:00
|
|
|
tree, err := state.LoadStateTree(cst, st)
|
2019-08-27 00:46:39 +00:00
|
|
|
if err != nil {
|
2019-10-03 19:45:14 +00:00
|
|
|
return nil, xerrors.Errorf("failed to load state tree: %w", err)
|
2019-08-27 00:46:39 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 19:45:14 +00:00
|
|
|
waddr, err := vm.ResolveToKeyAddr(tree, cst, worker)
|
2019-08-27 00:46:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to resolve miner address to key address: %w", err)
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-08-27 00:46:39 +00:00
|
|
|
sig, err := w.Sign(ctx, waddr, nosigbytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("failed to sign new block: %w", err)
|
|
|
|
}
|
|
|
|
|
2019-11-19 20:05:21 +00:00
|
|
|
next.BlockSig = sig
|
2019-08-27 00:46:39 +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
|
|
|
|
}
|
|
|
|
|
2020-03-21 22:25:00 +00:00
|
|
|
func aggregateSignatures(sigs []crypto.Signature) (*crypto.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)
|
2020-03-21 22:25:00 +00:00
|
|
|
return &crypto.Signature{
|
2020-02-12 23:52:36 +00:00
|
|
|
Type: crypto.SigTypeBLS,
|
2019-07-05 14:46:21 +00:00
|
|
|
Data: aggSig[:],
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-09-17 01:56:37 +00:00
|
|
|
func toIfArr(cids []cid.Cid) []cbg.CBORMarshaler {
|
|
|
|
out := make([]cbg.CBORMarshaler, 0, len(cids))
|
2019-07-05 14:46:21 +00:00
|
|
|
for _, c := range cids {
|
2019-09-17 01:56:37 +00:00
|
|
|
oc := cbg.CborCid(c)
|
|
|
|
out = append(out, &oc)
|
2019-07-05 14:46:21 +00:00
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|