AndrewKishino/sotez

contract.counter_in_the_past error from simulateOperation if reveal operation is injected

Closed this issue · 2 comments

Hi,

I'm trying to use this library for the first time. I created a client instance, I imported a key and then I ran a simulateOperation for a transfer, but it fails with an error proto.006-PsCARTHA.contract.counter_in_the_past. Why is that?

Welcome to Node.js v12.10.0.
Type ".help" for more information.
> sotez=require('sotez')
> c=new sotez.default('http://127.0.0.1:8732')
> await c.importKey('edsk******')
> c._debugMode=true
> await c.simulateOperation({operation:{kind:'transaction',fee:'2000',gas_limit:'10600',storage_limit:'0',amount:'1000',destination:'tz1S****'}})
Node call: /chains/main/blocks/head/metadata undefined
Node response: /chains/main/blocks/head/metadata {
  protocol: 'PsCARTHAGazKbHtnKfLzQg3kms52kSRpgnDY982a9oYsSXRLQEb',
  next_protocol: 'PsCARTHAGazKbHtnKfLzQg3kms52kSRpgnDY982a9oYsSXRLQEb',
  test_chain_status: { status: 'not_running' },
  max_operations_ttl: 60,
  max_operation_data_length: 16384,
  max_block_header_length: 238,
  max_operation_list_length: [
    { max_size: 32768, max_op: 32 },
    { max_size: 32768 },
    { max_size: 135168, max_op: 132 },
    { max_size: 524288 }
  ],
  baker: 'tz1eEnQhbwf6trb8Q8mPb2RaPkNk2rN7BKi8',
  level: {
    level: 944882,
    level_position: 944881,
    cycle: 230,
    cycle_position: 2801,
    voting_period: 28,
    voting_period_position: 27377,
    expected_commitment: false
  },
  voting_period_kind: 'proposal',
  nonce_hash: null,
  consumed_gas: '10207',
  deactivated: [],
  balance_updates: [
    {
      kind: 'contract',
      contract: 'tz1eEnQhbwf6trb8Q8mPb2RaPkNk2rN7BKi8',
      change: '-512000000'
    },
    {
      kind: 'freezer',
      category: 'deposits',
      delegate: 'tz1eEnQhbwf6trb8Q8mPb2RaPkNk2rN7BKi8',
      cycle: 230,
      change: '512000000'
    },
    {
      kind: 'freezer',
      category: 'rewards',
      delegate: 'tz1eEnQhbwf6trb8Q8mPb2RaPkNk2rN7BKi8',
      cycle: 230,
      change: '40000000'
    }
  ]
}
Node call: /chains/main/blocks/head/header undefined
Node response: /chains/main/blocks/head/header {
  protocol: 'PsCARTHAGazKbHtnKfLzQg3kms52kSRpgnDY982a9oYsSXRLQEb',
  chain_id: 'NetXdQprcVkpaWU',
  hash: 'BKr55UTFazCM1aatJSwvmjaevPtSfHk2B83eh1acdNBtd7bPgTV',
  level: 944882,
  proto: 6,
  predecessor: 'BL1RjncuVQ5AFb1YLsAnWfNFFNFPx8fsjEBgDFfH8mhk5PeMr6L',
  timestamp: '2020-05-09T12:45:35Z',
  validation_pass: 4,
  operations_hash: 'LLoZxNh5HLFPBbrVbWMtA5MwvEdYer58VhQ2t3ZxnyVrf3oTHTQPs',
  fitness: [ '01', '0000000000046af2' ],
  context: 'CoUtCqxCMDnR8trgAuhdh6XpSwHG1HPqknnuWnLTMGfTVJyKsCE8',
  priority: 0,
  proof_of_work_nonce: 'f0607f4bc7a40100',
  signature: 'sigsjgTwX3T8fuWCNyemVZVEVAwU8TPVjatjt8HyvsajTCEXU93hoj2PCSXM43abBy7Upau5PBbZbrpX2bGUEsiMjy8tW5ZD'
}
Node call: /chains/main/blocks/head/context/contracts/tz1e****/manager_key undefined
Node response: /chains/main/blocks/head/context/contracts/tz1e****/manager_key null
Node call: /chains/main/blocks/head/context/contracts/tz1e****/counter undefined
Node response: /chains/main/blocks/head/context/contracts/tz1e****/counter 4303594
Node call: /chains/main/blocks/head/helpers/scripts/run_operation {
  chain_id: 'NetXdQprcVkpaWU',
  operation: {
    branch: 'BKr55UTFazCM1aatJSwvmjaevPtSfHk2B83eh1acdNBtd7bPgTV',
    contents: [ [Object], [Object] ],
    signature: 'edsig****'
  }
}
Thrown: '[{"kind":"branch","id":"proto.006-PsCARTHA.contract.counter_in_the_past","contract":"tz1e****","expected":"4303596","found":"4303595"}]\n'

I tried many times and the result is always the same. What is wrong?

(The storage limit is intentionally zero but that shouldn't cause a counter issue, right?)

EDIT: I checked the mempool, there is no operation with counter 4303595.

EDIT2: After running require("util").inspect.defaultOptions.depth = null so that the full output is logged, the cause of the error becomes apparent:

Node call: /chains/main/blocks/head/helpers/scripts/run_operation {
  chain_id: 'NetXdQprcVkpaWU',
  operation: {
    branch: 'BKy****',
    contents: [
      {
        kind: 'reveal',
        fee: '1420',
        public_key: 'edpk****',
        source: 'tz1e****',
        gas_limit: '10600',
        storage_limit: '300',
        counter: '4303595'
      },
      {
        kind: 'transaction',
        fee: '2000',
        gas_limit: '10600',
        storage_limit: '0',
        amount: '1000',
        destination: 'tz1S****,
        source: 'tz1e****',
        counter: '4303595'
      }
    ],
    signature: 'edsig****'
  }
}

It appears that the auto-generated reveal operation has the same counter as the transaction! Of course this can't work. In that case, the transaction's counter needs to be increased!

EDIT3: It seems this happens only when simulating. It appears that the reason is that simulateOperation passes skipCounter: true which to me doesn't make a lot of sense because it will then of course generate operations with the same counter which will fail, even in simulation... I guess this is the root cause. If the intention was to not change any global state during simulating, then it would still have to locally count the counter up for the different operations that are run together...

I fixed the issue here: #61

Thanks for the contribution. This change has been included in version 0.6.3.