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

Handle worker key changes correctly in runtime #4579

Merged
merged 1 commit into from
Oct 24, 2020
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions chain/gen/genesis/miners.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ func (fss *fakedSigSyscalls) VerifySignature(signature crypto.Signature, signer
}

func mkFakedSigSyscalls(base vm.SyscallBuilder) vm.SyscallBuilder {
return func(ctx context.Context, cstate *state.StateTree, cst cbor.IpldStore) runtime2.Syscalls {
return func(ctx context.Context, rt *vm.Runtime) runtime2.Syscalls {
return &fakedSigSyscalls{
base(ctx, cstate, cst),
base(ctx, rt),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions chain/stmgr/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func (sm *StateManager) Call(ctx context.Context, msg *types.Message, ts *types.
CircSupplyCalc: sm.GetVMCirculatingSupply,
NtwkVersion: sm.GetNtwkVersion,
BaseFee: types.NewInt(0),
LookbackState: LookbackStateGetterForTipset(sm, ts),
}

vmi, err := sm.newVM(ctx, vmopt)
Expand Down Expand Up @@ -178,6 +179,7 @@ func (sm *StateManager) CallWithGas(ctx context.Context, msg *types.Message, pri
CircSupplyCalc: sm.GetVMCirculatingSupply,
NtwkVersion: sm.GetNtwkVersion,
BaseFee: ts.Blocks()[0].ParentBaseFee,
LookbackState: LookbackStateGetterForTipset(sm, ts),
}
vmi, err := sm.newVM(ctx, vmopt)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions chain/stmgr/stmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ func (sm *StateManager) ApplyBlocks(ctx context.Context, parentEpoch abi.ChainEp
CircSupplyCalc: sm.GetVMCirculatingSupply,
NtwkVersion: sm.GetNtwkVersion,
BaseFee: baseFee,
LookbackState: LookbackStateGetterForTipset(sm, ts),
}

return sm.newVM(ctx, vmopt)
Expand Down
11 changes: 11 additions & 0 deletions chain/stmgr/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ func ComputeState(ctx context.Context, sm *StateManager, height abi.ChainEpoch,
CircSupplyCalc: sm.GetVMCirculatingSupply,
NtwkVersion: sm.GetNtwkVersion,
BaseFee: ts.Blocks()[0].ParentBaseFee,
LookbackState: LookbackStateGetterForTipset(sm, ts),
}
vmi, err := sm.newVM(ctx, vmopt)
if err != nil {
Expand All @@ -391,6 +392,16 @@ func ComputeState(ctx context.Context, sm *StateManager, height abi.ChainEpoch,
return root, trace, nil
}

func LookbackStateGetterForTipset(sm *StateManager, ts *types.TipSet) vm.LookbackStateGetter {
return func(ctx context.Context, round abi.ChainEpoch) (*state.StateTree, error) {
_, st, err := GetLookbackTipSetForRound(ctx, sm, ts, round)
if err != nil {
return nil, err
}
return sm.StateTree(st)
}
}

func GetLookbackTipSetForRound(ctx context.Context, sm *StateManager, ts *types.TipSet, round abi.ChainEpoch) (*types.TipSet, cid.Cid, error) {
var lbr abi.ChainEpoch
lb := policy.GetWinningPoStSectorSetLookback(sm.GetNtwkVersion(ctx, round))
Expand Down
47 changes: 29 additions & 18 deletions chain/vm/syscalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ func init() {

// Actual type is defined in chain/types/vmcontext.go because the VMContext interface is there

type SyscallBuilder func(ctx context.Context, cstate *state.StateTree, cst cbor.IpldStore) runtime2.Syscalls
type SyscallBuilder func(ctx context.Context, rt *Runtime) runtime2.Syscalls

func Syscalls(verifier ffiwrapper.Verifier) SyscallBuilder {
return func(ctx context.Context, cstate *state.StateTree, cst cbor.IpldStore) runtime2.Syscalls {
return func(ctx context.Context, rt *Runtime) runtime2.Syscalls {

return &syscallShim{
ctx: ctx,

cstate: cstate,
cst: cst,
actor: rt.Receiver(),
cstate: rt.state,
cst: rt.cst,
lbState: rt.vm.lbStateGet,

verifier: verifier,
}
Expand All @@ -52,6 +55,8 @@ func Syscalls(verifier ffiwrapper.Verifier) SyscallBuilder {
type syscallShim struct {
ctx context.Context

lbState LookbackStateGetter
actor address.Address
cstate *state.StateTree
cst cbor.IpldStore
verifier ffiwrapper.Verifier
Expand Down Expand Up @@ -184,35 +189,41 @@ func (ss *syscallShim) VerifyConsensusFault(a, b, extra []byte) (*runtime2.Conse
}

func (ss *syscallShim) VerifyBlockSig(blk *types.BlockHeader) error {

// get appropriate miner actor
act, err := ss.cstate.GetActor(blk.Miner)
waddr, err := ss.workerKeyAtLookback(blk.Height)
if err != nil {
return err
}

// use that to get the miner state
mas, err := miner.Load(adt.WrapStore(ss.ctx, ss.cst), act)
if err != nil {
if err := sigs.CheckBlockSignature(ss.ctx, blk, waddr); err != nil {
return err
}

info, err := mas.Info()
return nil
}

func (ss *syscallShim) workerKeyAtLookback(height abi.ChainEpoch) (address.Address, error) {
lbState, err := ss.lbState(ss.ctx, height)
if err != nil {
return err
return address.Undef, err
}
// get appropriate miner actor
act, err := lbState.GetActor(ss.actor)
if err != nil {
return address.Undef, err
}

// and use to get resolved workerKey
waddr, err := ResolveToKeyAddr(ss.cstate, ss.cst, info.Worker)
// use that to get the miner state
mas, err := miner.Load(adt.WrapStore(ss.ctx, ss.cst), act)
if err != nil {
return err
return address.Undef, err
}

if err := sigs.CheckBlockSignature(ss.ctx, blk, waddr); err != nil {
return err
info, err := mas.Info()
if err != nil {
return address.Undef, err
}

return nil
return ResolveToKeyAddr(ss.cstate, ss.cst, info.Worker)
}

func (ss *syscallShim) VerifyPoSt(proof proof2.WindowPoStVerifyInfo) error {
Expand Down
15 changes: 10 additions & 5 deletions chain/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ func (vm *VM) makeRuntime(ctx context.Context, msg *types.Message, parent *Runti
Blocks: &gasChargingBlocks{rt.chargeGasFunc(2), rt.pricelist, vm.cst.Blocks},
Atlas: vm.cst.Atlas,
}
rt.Syscalls = pricedSyscalls{
under: vm.Syscalls(ctx, vm.cstate, rt.cst),
chargeGas: rt.chargeGasFunc(1),
pl: rt.pricelist,
}

vmm := *msg
resF, ok := rt.ResolveAddress(msg.From)
Expand All @@ -156,6 +151,12 @@ func (vm *VM) makeRuntime(ctx context.Context, msg *types.Message, parent *Runti
rt.Message = &Message{msg: vmm}
}

rt.Syscalls = pricedSyscalls{
under: vm.Syscalls(ctx, rt),
chargeGas: rt.chargeGasFunc(1),
pl: rt.pricelist,
}

return rt
}

Expand All @@ -169,6 +170,7 @@ func (vm *UnsafeVM) MakeRuntime(ctx context.Context, msg *types.Message) *Runtim

type CircSupplyCalculator func(context.Context, abi.ChainEpoch, *state.StateTree) (abi.TokenAmount, error)
type NtwkVersionGetter func(context.Context, abi.ChainEpoch) network.Version
type LookbackStateGetter func(context.Context, abi.ChainEpoch) (*state.StateTree, error)

type VM struct {
cstate *state.StateTree
Expand All @@ -181,6 +183,7 @@ type VM struct {
circSupplyCalc CircSupplyCalculator
ntwkVersion NtwkVersionGetter
baseFee abi.TokenAmount
lbStateGet LookbackStateGetter

Syscalls SyscallBuilder
}
Expand All @@ -194,6 +197,7 @@ type VMOpts struct {
CircSupplyCalc CircSupplyCalculator
NtwkVersion NtwkVersionGetter // TODO: stebalien: In what cases do we actually need this? It seems like even when creating new networks we want to use the 'global'/build-default version getter
BaseFee abi.TokenAmount
LookbackState LookbackStateGetter
}

func NewVM(ctx context.Context, opts *VMOpts) (*VM, error) {
Expand All @@ -216,6 +220,7 @@ func NewVM(ctx context.Context, opts *VMOpts) (*VM, error) {
ntwkVersion: opts.NtwkVersion,
Syscalls: opts.Syscalls,
baseFee: opts.BaseFee,
lbStateGet: opts.LookbackState,
}, nil
}

Expand Down