some cleanup of vm transfer logic to make juan happier

This commit is contained in:
whyrusleeping 2019-10-11 18:13:04 +09:00
parent decbc3ef6c
commit a56e786e36
2 changed files with 28 additions and 13 deletions

View File

@ -110,11 +110,10 @@ func (sm *StateManager) computeTipSetState(ctx context.Context, blks []*types.Bl
return cid.Undef, cid.Undef, xerrors.Errorf("failed to get miner owner actor") return cid.Undef, cid.Undef, xerrors.Errorf("failed to get miner owner actor")
} }
if err := vm.DeductFunds(netact, reward); err != nil { if err := vm.Transfer(netact, act, reward); err != nil {
return cid.Undef, cid.Undef, xerrors.Errorf("failed to deduct funds from network actor: %w", err) return cid.Undef, cid.Undef, xerrors.Errorf("failed to deduct funds from network actor: %w", err)
} }
vm.DepositFunds(act, reward)
} }
// TODO: can't use method from chainstore because it doesnt let us know who the block miners were // TODO: can't use method from chainstore because it doesnt let us know who the block miners were

View File

@ -396,10 +396,9 @@ func (vm *VM) send(ctx context.Context, msg *types.Message, parent *VMContext,
return nil, aerrors.Wrap(aerr, "sending funds"), nil return nil, aerrors.Wrap(aerr, "sending funds"), nil
} }
if err := DeductFunds(fromActor, msg.Value); err != nil { if err := Transfer(fromActor, toActor, msg.Value); err != nil {
return nil, aerrors.Absorb(err, 1, "failed to deduct funds"), nil return nil, aerrors.Absorb(err, 1, "failed to transfer funds"), nil
} }
DepositFunds(toActor, msg.Value)
} }
if msg.Method != 0 { if msg.Method != 0 {
@ -463,8 +462,10 @@ func (vm *VM) ApplyMessage(ctx context.Context, msg *types.Message) (*ApplyRet,
if fromActor.Balance.LessThan(totalCost) { if fromActor.Balance.LessThan(totalCost) {
return nil, xerrors.Errorf("not enough funds (%s < %s)", fromActor.Balance, totalCost) return nil, xerrors.Errorf("not enough funds (%s < %s)", fromActor.Balance, totalCost)
} }
if err := DeductFunds(fromActor, gascost); err != nil {
return nil, xerrors.Errorf("failed to deduct funds: %w", err) gasHolder := &types.Actor{Balance: types.NewInt(0)}
if err := Transfer(fromActor, gasHolder, gascost); err != nil {
return nil, xerrors.Errorf("failed to withdraw gas funds: %w", err)
} }
if msg.Nonce != fromActor.Nonce { if msg.Nonce != fromActor.Nonce {
@ -494,7 +495,9 @@ func (vm *VM) ApplyMessage(ctx context.Context, msg *types.Message) (*ApplyRet,
// refund unused gas // refund unused gas
gasUsed = vmctx.GasUsed() gasUsed = vmctx.GasUsed()
refund := types.BigMul(types.BigSub(msg.GasLimit, gasUsed), msg.GasPrice) refund := types.BigMul(types.BigSub(msg.GasLimit, gasUsed), msg.GasPrice)
DepositFunds(fromActor, refund) if err := Transfer(gasHolder, fromActor, refund); err != nil {
return nil, xerrors.Errorf("failed to refund gas")
}
} }
miner, err := st.GetActor(vm.blockMiner) miner, err := st.GetActor(vm.blockMiner)
@ -503,7 +506,13 @@ func (vm *VM) ApplyMessage(ctx context.Context, msg *types.Message) (*ApplyRet,
} }
gasReward := types.BigMul(msg.GasPrice, gasUsed) gasReward := types.BigMul(msg.GasPrice, gasUsed)
DepositFunds(miner, gasReward) if err := Transfer(gasHolder, miner, gasReward); err != nil {
return nil, xerrors.Errorf("failed to give miner gas reward: %w", err)
}
if types.BigCmp(types.NewInt(0), gasHolder.Balance) != 0 {
return nil, xerrors.Errorf("gas handling math is wrong")
}
return &ApplyRet{ return &ApplyRet{
MessageReceipt: types.MessageReceipt{ MessageReceipt: types.MessageReceipt{
@ -597,10 +606,9 @@ func (vm *VM) TransferFunds(from, to address.Address, amt types.BigInt) error {
return err return err
} }
if err := DeductFunds(fromAct, amt); err != nil { if err := Transfer(fromAct, toAct, amt); err != nil {
return xerrors.Errorf("failed to deduct funds: %w", err) return xerrors.Errorf("failed to deduct funds: %w", err)
} }
DepositFunds(toAct, amt)
return nil return nil
} }
@ -628,7 +636,15 @@ func (vm *VM) Invoke(act *types.Actor, vmctx *VMContext, method uint64, params [
return ret, nil return ret, nil
} }
func DeductFunds(act *types.Actor, amt types.BigInt) error { func Transfer(from, to *types.Actor, amt types.BigInt) error {
if err := deductFunds(from, amt); err != nil {
return err
}
depositFunds(to, amt)
return nil
}
func deductFunds(act *types.Actor, amt types.BigInt) error {
if act.Balance.LessThan(amt) { if act.Balance.LessThan(amt) {
return fmt.Errorf("not enough funds") return fmt.Errorf("not enough funds")
} }
@ -637,7 +653,7 @@ func DeductFunds(act *types.Actor, amt types.BigInt) error {
return nil return nil
} }
func DepositFunds(act *types.Actor, amt types.BigInt) { func depositFunds(act *types.Actor, amt types.BigInt) {
act.Balance = types.BigAdd(act.Balance, amt) act.Balance = types.BigAdd(act.Balance, amt)
} }