Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean up create block #275

Merged
merged 1 commit into from
Oct 3, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 10 additions & 27 deletions chain/gen/mining.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
cbg "github.com/whyrusleeping/cbor-gen"
"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/state"
"github.com/filecoin-project/go-lotus/chain/stmgr"
"github.com/filecoin-project/go-lotus/chain/types"
"github.com/filecoin-project/go-lotus/chain/vm"
Expand All @@ -27,30 +27,10 @@ func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wal

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)
if err != nil {
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{
Miner: miner,
Expand Down Expand Up @@ -113,19 +93,22 @@ func MinerCreateBlock(ctx context.Context, sm *stmgr.StateManager, w *wallet.Wal
pweight := sm.ChainStore().Weight(parents)
next.ParentWeight = types.NewInt(pweight)

// TODO: set timestamp

nosigbytes, err := next.SigningBytes()
cst := hamt.CSTFromBstore(sm.ChainStore().Blockstore())
tree, err := state.LoadStateTree(cst, st)
if err != nil {
return nil, xerrors.Errorf("failed to get signing bytes for block: %w", err)
return nil, xerrors.Errorf("failed to load state tree: %w", err)
}

cst := hamt.CSTFromBstore(sm.ChainStore().Blockstore())
waddr, err := vm.ResolveToKeyAddr(vmi.StateTree(), cst, worker)
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()
if err != nil {
return nil, xerrors.Errorf("failed to get signing bytes for block: %w", err)
}

sig, err := w.Sign(ctx, waddr, nosigbytes)
if err != nil {
return nil, xerrors.Errorf("failed to sign new block: %w", err)
Expand Down