All Products
Search
Document Center

Blockchain as a Service:Local Service Interface

Last Updated:May 22, 2019

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.

  1. chain.ctr.LocalTransaction({
  2. apiName: 'CreateAccount',
  3. block_number: 40,
  4. from: 'Tester001',
  5. to: 'Tester001' + Date.now(),
  6. data: {
  7. recover_key: '0xf5e50510a04a3f659a0e89f2063f79f8c1aed5ddaab6420ac47700020d9889dc14dae4dc9843c88d8222167095d9e6ce052e8a19cbc737c3f3cddf66409dbb0a',
  8. auth_key: '0xf5e50510a04a3f659a0e89f2063f79f8c1aed5ddaab6420ac47700020d9889dc14dae4dc9843c88d8222167095d9e6ce052e8a19cbc737c3f3cddf66409dbb0a',
  9. auth_weight: 100
  10. }
  11. }, (err, data) => {
  12. console.log(data)
  13. console.log(data.receipt.log_entry)
  14. })

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.

  1. myContract.new(bytecode, {
  2. from: 'Tester001'
  3. }, (err, contract, data) => {
  4. console.log('contract deploy result:', data)
  5. myContract.Issue('0xc60a9d48105950a0cca07a4c6320b98c303ad42d694a634529e8e1a0a16fcdb5', 100 , {
  6. local: true,
  7. block_number: data.block_number, // Note: If `data.block_number - 1` is used here, the call fails. The error code is 120.
  8. from: 'Tester001'
  9. }, (err, output, data) => {
  10. console.log('contract call data:', data)
  11. console.log('contract call output:', output)
  12. })
  13. })