To query contracts or perform contract calculations, the local execution of smart contracts is performed on the local nodes of a blockchain without broadcasting or consensus. The parameters of locally executed transactions are identical to the transaction parameters that are involved in the consensus process.
The local execution of transactions is faster and more efficient than the execution of real transactions. It simulates the execution of a real smart contract, and the execution results will not participate in the consensus process or enter the storage.
Execute a common transaction locally
You can call the LocalTransaction
operatioin to execute a transaction locally without broadcasting or consensus.
Request parameters
The following parameters are encapsulated and passed in as an object.
Name | Required | Type | Description |
---|---|---|---|
apiName | true | string | The name of the operation called to execute the target transaction. |
block_number | true | number | The height of the block where the target transaction is executed. If a contract is called locally, the height of this block must be equal to or higher than the height of the block where the contract is deployed. |
Other parameters | true | To be determined. | Other parameters are passed in according to the specified apiName operation. For example, to call the CreateAccount operation, parameters need to be passed in according to the requirements of the CreateAccount operation. For more information, see the examples. |
Examples
Execute a local transaction to create an account.
chain.ctr.LocalTransaction({
apiName: 'CreateAccount',
block_number: 40,
from: 'Tester001',
to: 'Tester001' + Date.now(),
data: {
recover_key: '0xf5e50510a04a3f659a0e89f2063f79f8c1aed5ddaab6420ac47700020d9889dc14dae4dc9843c88d8222167095d9e6ce052e8a19cbc737c3f3cddf66409dbb0a',
auth_key: '0xf5e50510a04a3f659a0e89f2063f79f8c1aed5ddaab6420ac47700020d9889dc14dae4dc9843c88d8222167095d9e6ce052e8a19cbc737c3f3cddf66409dbb0a',
auth_weight: 100
}
}, (err, data) => {
console.log(data)
console.log(data.receipt.log_entry)
})
Execute a contract-related transaction locally.
You can execute transactions locally by adding configuration parameters to the parameter list of the contract-related operation method.
Request parameters to be added
Name | Required | Type | Description |
---|---|---|---|
apiName | true | string | The name of the operation called to execute the target transaction. |
block_number | true | number | The height of the block where the target transaction is executed. If a contract is called locally, the height of this block must be equal to or higher than the height of the block where the contract is deployed. |
Examples
Call a contract method locally.
myContract.new(bytecode, {
from: 'Tester001'
}, (err, contract, data) => {
console.log('contract deploy result:', data)
myContract.Issue('0xc60a9d48105950a0cca07a4c6320b98c303ad42d694a634529e8e1a0a16fcdb5', 100 , {
local: true,
block_number: data.block_number, // Note: If `data.block_number - 1` is used here, the call fails. The error code is 120.
from: 'Tester001'
}, (err, output, data) => {
console.log('contract call data:', data)
console.log('contract call output:', output)
})
})