Merge pull request #275 from filecoin-project/feat/cleanup-create-block

clean up create block
This commit is contained in:
Whyrusleeping 2019-10-03 14:05:03 -06:00 committed by GitHub
commit c0a3e675e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,8 +11,8 @@ import (
cbg "github.com/whyrusleeping/cbor-gen" cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors" "golang.org/x/xerrors"
"github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/state"
"github.com/filecoin-project/go-lotus/chain/stmgr" "github.com/filecoin-project/go-lotus/chain/stmgr"
"github.com/filecoin-project/go-lotus/chain/types" "github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/chain/vm" "github.com/filecoin-project/go-lotus/chain/vm"
@ -27,30 +27,10 @@ func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wal
height := parents.Height() + uint64(len(tickets)) height := parents.Height() + uint64(len(tickets))
r := vm.NewChainRand(sm.ChainStore(), parents.Cids(), parents.Height(), tickets)
vmi, err := vm.NewVM(st, height, r, miner, sm.ChainStore())
if err != nil {
return nil, err
}
owner, err := stmgr.GetMinerOwner(ctx, sm, st, miner)
if err != nil {
return nil, xerrors.Errorf("failed to get miner owner: %w", err)
}
worker, err := stmgr.GetMinerWorker(ctx, sm, st, miner) worker, err := stmgr.GetMinerWorker(ctx, sm, st, miner)
if err != nil { if err != nil {
return nil, xerrors.Errorf("failed to get miner worker: %w", err) return nil, xerrors.Errorf("failed to get miner worker: %w", err)
} }
networkBalance, err := vmi.ActorBalance(actors.NetworkAddress)
if err != nil {
return nil, xerrors.Errorf("failed to get network balance: %w", err)
}
// apply miner reward
if err := vmi.TransferFunds(actors.NetworkAddress, owner, vm.MiningReward(networkBalance)); err != nil {
return nil, err
}
next := &types.BlockHeader{ next := &types.BlockHeader{
Miner: miner, Miner: miner,
@ -113,19 +93,22 @@ func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wal
pweight := sm.ChainStore().Weight(parents) pweight := sm.ChainStore().Weight(parents)
next.ParentWeight = types.NewInt(pweight) next.ParentWeight = types.NewInt(pweight)
// TODO: set timestamp cst := hamt.CSTFromBstore(sm.ChainStore().Blockstore())
tree, err := state.LoadStateTree(cst, st)
if err != nil {
return nil, xerrors.Errorf("failed to load state tree: %w", err)
}
waddr, err := vm.ResolveToKeyAddr(tree, cst, worker)
if err != nil {
return nil, xerrors.Errorf("failed to resolve miner address to key address: %w", err)
}
nosigbytes, err := next.SigningBytes() nosigbytes, err := next.SigningBytes()
if err != nil { if err != nil {
return nil, xerrors.Errorf("failed to get signing bytes for block: %w", err) return nil, xerrors.Errorf("failed to get signing bytes for block: %w", err)
} }
cst := hamt.CSTFromBstore(sm.ChainStore().Blockstore())
waddr, err := vm.ResolveToKeyAddr(vmi.StateTree(), cst, worker)
if err != nil {
return nil, xerrors.Errorf("failed to resolve miner address to key address: %w", err)
}
sig, err := w.Sign(ctx, waddr, nosigbytes) sig, err := w.Sign(ctx, waddr, nosigbytes)
if err != nil { if err != nil {
return nil, xerrors.Errorf("failed to sign new block: %w", err) return nil, xerrors.Errorf("failed to sign new block: %w", err)