forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make simulated call use same path as call_context
and so handle revert correctly Signed-off-by: Silas Davis <[email protected]>
- Loading branch information
Silas Davis
committed
Aug 16, 2018
1 parent
97d9215
commit e93c9eb
Showing
8 changed files
with
96 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,59 @@ | ||
package execution | ||
|
||
import ( | ||
"fmt" | ||
"runtime/debug" | ||
|
||
"github.com/hyperledger/burrow/acm" | ||
"github.com/hyperledger/burrow/acm/state" | ||
"github.com/hyperledger/burrow/bcm" | ||
"github.com/hyperledger/burrow/binary" | ||
"github.com/hyperledger/burrow/crypto" | ||
"github.com/hyperledger/burrow/execution/contexts" | ||
"github.com/hyperledger/burrow/execution/evm" | ||
"github.com/hyperledger/burrow/execution/exec" | ||
"github.com/hyperledger/burrow/logging" | ||
"github.com/hyperledger/burrow/txs" | ||
"github.com/hyperledger/burrow/txs/payload" | ||
) | ||
|
||
// Run a contract's code on an isolated and unpersisted state | ||
// Cannot be used to create new contracts | ||
func CallSim(reader state.Reader, tip bcm.BlockchainInfo, fromAddress, address crypto.Address, data []byte, | ||
logger *logging.Logger) (*exec.TxExecution, error) { | ||
|
||
if evm.IsRegisteredNativeContract(address.Word256()) { | ||
return nil, fmt.Errorf("attempt to call native contract at address "+ | ||
"%X, but native contracts can not be called directly. Use a deployed "+ | ||
"contract that calls the native function instead", address) | ||
cache := state.NewCache(reader) | ||
exe := contexts.CallContext{ | ||
RunCall: true, | ||
StateWriter: cache, | ||
Tip: tip, | ||
Logger: logger, | ||
} | ||
// This was being run against CheckTx cache, need to understand the reasoning | ||
callee, err := state.GetMutableAccount(reader, address) | ||
|
||
txe := exec.NewTxExecution(txs.Enclose(tip.ChainID(), &payload.CallTx{ | ||
Input: &payload.TxInput{ | ||
Address: fromAddress, | ||
}, | ||
Address: &address, | ||
Data: data, | ||
GasLimit: contexts.GasLimit, | ||
})) | ||
err := exe.Execute(txe) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if callee == nil { | ||
return nil, fmt.Errorf("account %s does not exist", address) | ||
} | ||
return CallCodeSim(reader, tip, fromAddress, address, callee.Code(), data, logger) | ||
return txe, nil | ||
} | ||
|
||
// Run the given code on an isolated and unpersisted state | ||
// Cannot be used to create new contracts. | ||
func CallCodeSim(reader state.Reader, tip bcm.BlockchainInfo, fromAddress, address crypto.Address, code, data []byte, | ||
logger *logging.Logger) (_ *exec.TxExecution, err error) { | ||
// This was being run against CheckTx cache, need to understand the reasoning | ||
caller := acm.ConcreteAccount{Address: fromAddress}.MutableAccount() | ||
callee := acm.ConcreteAccount{Address: address}.MutableAccount() | ||
|
||
txCache := state.NewCache(reader) | ||
|
||
params := vmParams(tip) | ||
vmach := evm.NewVM(params, caller.Address(), nil, logger.WithScope("CallCode")) | ||
logger *logging.Logger) (*exec.TxExecution, error) { | ||
|
||
txe := &exec.TxExecution{} | ||
vmach.SetEventSink(txe) | ||
gas := params.GasLimit | ||
defer func() { | ||
if r := recover(); r != nil { | ||
err = fmt.Errorf("panic from VM in simulated call: %v\n%s", r, debug.Stack()) | ||
} | ||
}() | ||
// Attach code to target account (overwriting target) | ||
cache := state.NewCache(reader) | ||
err := cache.UpdateAccount(acm.ConcreteAccount{ | ||
Address: address, | ||
Code: code, | ||
}.MutableAccount()) | ||
|
||
ret, err := vmach.Call(txCache, caller, callee, code, data, 0, &gas) | ||
if err != nil { | ||
return nil, err | ||
} | ||
txe.Return(ret, params.GasLimit-gas) | ||
return txe, nil | ||
} | ||
|
||
func vmParams(tip bcm.BlockchainInfo) evm.Params { | ||
return evm.Params{ | ||
BlockHeight: tip.LastBlockHeight(), | ||
BlockHash: binary.LeftPadWord256(tip.LastBlockHash()), | ||
BlockTime: tip.LastBlockTime().Unix(), | ||
GasLimit: contexts.GasLimit, | ||
} | ||
return CallSim(cache, tip, fromAddress, address, data, logger) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.