Skip to content

Commit

Permalink
Vm delegate (#243)
Browse files Browse the repository at this point in the history
* assetamount push amount and assetName length into stack

* assetamount push amount and assetName length into stack

* cost more gas when receipt first receive assetID

* cost more gas when receipt first receive assetID
  • Loading branch information
JacobDenver007 committed May 9, 2019
1 parent 0518a87 commit 1957dea
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions processor/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func (st *StateTransition) TransitionDb() (ret []byte, usedGas uint64, failed bo
if err != nil {
return nil, 0, true, err, vmerr
}
intrinsicGas += st.evm.CheckReceipt(st.action)
if err := st.useGas(intrinsicGas); err != nil {
return nil, 0, true, err, vmerr
}
Expand Down
5 changes: 5 additions & 0 deletions processor/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,11 @@ func opCallEx(pc *uint64, evm *EVM, contract *Contract, memory *Memory, stack *S

action := types.NewAction(types.CallContract, contract.Name(), toName, 0, assetID, 0, value, nil, nil)

if !contract.UseGas(evm.CheckReceipt(action)) {
stack.push(evm.interpreter.intPool.getZero())
return nil, nil
}

err = evm.AccountDB.TransferAsset(action.Sender(), action.Recipient(), action.AssetID(), action.Value())
//distribute gas
var assetName common.Name
Expand Down
30 changes: 30 additions & 0 deletions processor/vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,24 @@ func (evm *EVM) Cancel() {
atomic.StoreInt32(&evm.abort, 1)
}

func (evm *EVM) CheckReceipt(action *types.Action) uint64 {
toAcct, err := evm.AccountDB.GetAccountByName(action.Recipient())
if err != nil {
return 0
}
if toAcct == nil {
return 0
}
if toAcct.IsDestroyed() {
return 0
}
_, err = toAcct.GetBalanceByID(action.AssetID())
if err == accountmanager.ErrAccountAssetNotExist {
return params.CallValueTransferGas
}
return 0
}

func (evm *EVM) distributeContractGas(runGas uint64, contractName common.Name, callerName common.Name) {
if runGas > 0 && len(contractName.String()) > 0 {
contratFounderRatio := evm.chainConfig.ChargeCfg.ContractRatio
Expand Down Expand Up @@ -246,6 +264,12 @@ func (evm *EVM) Call(caller ContractRef, action *types.Action, gas uint64) (ret
snapshot = evm.StateDB.Snapshot()
)

receiptGas := evm.CheckReceipt(action)
if gas < receiptGas {
return nil, gas, ErrInsufficientBalance
} else {
gas -= receiptGas
}
if err := evm.AccountDB.TransferAsset(action.Sender(), action.Recipient(), action.AssetID(), action.Value()); err != nil {
return nil, gas, err
}
Expand Down Expand Up @@ -504,6 +528,12 @@ func (evm *EVM) Create(caller ContractRef, action *types.Action, gas uint64) (re
return nil, 0, ErrContractCodeCollision
}

receiptGas := evm.CheckReceipt(action)
if gas < receiptGas {
return nil, gas, ErrInsufficientBalance
} else {
gas -= receiptGas
}
if err := evm.AccountDB.TransferAsset(action.Sender(), action.Recipient(), evm.AssetID, action.Value()); err != nil {
evm.StateDB.RevertToSnapshot(snapshot)
return nil, gas, err
Expand Down

0 comments on commit 1957dea

Please sign in to comment.