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

Vm delegate #243

Merged
merged 6 commits into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
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()
if err := st.useGas(intrinsicGas); err != nil {
return nil, 0, true, err, vmerr
}
Expand Down
9 changes: 9 additions & 0 deletions processor/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ func opGetAssetAmount(pc *uint64, evm *EVM, contract *Contract, memory *Memory,

ast, err := evm.AccountDB.GetAssetInfoByID(astID)
if err != nil || ast == nil {
stack.push(evm.interpreter.intPool.getZero())
stack.push(evm.interpreter.intPool.getZero())
return nil, nil
}
Expand All @@ -457,6 +458,7 @@ func opGetAssetAmount(pc *uint64, evm *EVM, contract *Contract, memory *Memory,
if uint64(datalen) > retSize.Uint64()*32 {
err = errors.New("out of space")
stack.push(evm.interpreter.intPool.getZero())
stack.push(evm.interpreter.intPool.getZero())
return nil, nil
}

Expand All @@ -465,8 +467,10 @@ func opGetAssetAmount(pc *uint64, evm *EVM, contract *Contract, memory *Memory,
amount, err := evm.AccountDB.GetAssetAmountByTime(astID, t)
if err != nil {
stack.push(evm.interpreter.intPool.getZero())
stack.push(evm.interpreter.intPool.getZero())
} else {
stack.push(amount)
stack.push(evm.interpreter.intPool.get().SetUint64(uint64(len(name))))
}
evm.interpreter.intPool.put(time, assetID)
return nil, nil
Expand Down Expand Up @@ -1444,6 +1448,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
2 changes: 1 addition & 1 deletion processor/vm/jump_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func NewByzantiumInstructionSet() [256]operation {
instructionSet[ASSETAMOUNT] = operation{
execute: opGetAssetAmount,
gasCost: gasGetAssetAmount,
validateStack: makeStackFunc(2, 1),
validateStack: makeStackFunc(2, 2),
valid: true,
returns: true,
}
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