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

[email protected] tx.id() not equal to rpc getblock(blockhash, 1) tx id #176

Open
86chenjie opened this issue Sep 14, 2020 · 9 comments
Open

Comments

@86chenjie
Copy link

test code:

it('check_tx_id', async function () {
    // test 147056 / 00000000000007a678f2e1316e0f7f7acf2b967d998558aed8dadcd4430fd5f1
    const height = 147056
    const blockhash = await chain.getblockhash(height)
    const chain_block_hex = await chain.getblock(blockhash, 0)
    const chain_block = await chain.getblock(blockhash, 1)
    const chain_tx0_id = chain_block.tx[0]

    const block = bsv.Block.fromBuffer(Buffer.from(chain_block_hex, 'hex'))
    const block_tx0_id = block.txs[0].id()
    
    console.log(chain_tx0_id)
    console.log(block_tx0_id)
    expect(chain_tx0_id).to.equal(block_tx0_id);
  });

The two tx ids should be equal. Obviously block_tx0_id is not correct.

  2) bsv
       check_tx_id:

      AssertionError: expected '9fa47592276963ff2b8d44d0f547b03a41ac1e4e18e2a02b5445ade9b021071d' to equal '121f0f093c0e98bb499643bbe781a9746deaf4b9b123f958b2a962b3bb57d873'
      + expected - actual

      -9fa47592276963ff2b8d44d0f547b03a41ac1e4e18e2a02b5445ade9b021071d
      +121f0f093c0e98bb499643bbe781a9746deaf4b9b123f958b2a962b3bb57d873

      at Context.<anonymous> (test/chain.spec.js:59:29)
      at process._tickCallback (internal/process/next_tick.js:68:7)

image

@calvogenerico
Copy link
Contributor

I've been able to recreate this too. I used the tools included in the repo and files instead of an rpc connection:

it('parse a hex block generate the right txids', async () => {
    // const height = 147056
    // const blockHash = '00000000000007a678f2e1316e0f7f7acf2b967d998558aed8dadcd4430fd5f1'
    const blockHex = await new Promise(resolve => {
      fs.readFile('test/block.txt', 'utf-8', (err, data) => {
        if (err) {
          throw err
        }
        resolve(data)
      })
    })
    const blockDataFromRpc = await new Promise(resolve => {
      fs.readFile('test/block.json', 'utf-8', (err, data) => {
        if (err) {
          throw err
        }
        resolve(JSON.parse(data))
      })
    })
    const blockFromLibrary = Block.fromBuffer(Buffer.from(blockHex, 'hex'))

    const firstTxIdFromRpc = blockDataFromRpc.tx[0]

    const firstTxIdFromLibrary = blockFromLibrary.txs[0].id()

    const otherTxidsFromParsedBlock = blockFromLibrary.txs.map(tx => tx.id()).slice(1)
    const otherTxIdsFromRawBlock = blockDataFromRpc.tx.slice(1)

    should(otherTxidsFromParsedBlock).be.eql(otherTxIdsFromRawBlock)
    should(firstTxIdFromRpc).be.eql(firstTxIdFromLibrary)
  })

It fails exactly as in the report.

@calvogenerico
Copy link
Contributor

The issue happens parsing this script:
fc70035c7a81bc6fcc36947f7c1b2d63620560bec2aa336a676213bab74c9f03d46788100dca84c0f19a0f1c14ef0d67f3fc63c011ba4787510d55fde9554e554e

The first thing that I notice is that there is a weird behavior when I tried to encode it using different languages:

const weirdScript = bsv.Script.fromHex('fc70035c7a81bc6fcc36947f7c1b2d63620560bec2aa336a676213bab74c9f03d46788100dca84c0f19a0f1c14ef0d67f3fc63c011ba4787510d55fde9554e554e')

weirdScript.toString()
// => '0xfc OP_2OVER 3 0x5c7a81 0xbc OP_3DUP 0xcc 54 0x947f7c1b2d63620560bec2aa336a676213bab74c9f03d46788100dca84c0f19a0f1c14ef0d67f3fc63c011ba4787510d55fde9554e55 OP_PUSHDATA4 0 0x'
weirdScript.toAsmString()
// => 'fc OP_2OVER 5c7a81 bc OP_3DUP cc 947f7c1b2d63620560bec2aa336a676213bab74c9f03d46788100dca84c0f19a0f1c14ef0d67f3fc63c011ba4787510d55fde9554e55'

the asm version doesn't include the last push data. And the last push data is causing the issue.

@calvogenerico
Copy link
Contributor

Bsv legacy fails when trying to parse that as a script:

bsv.Script.fromHex('fc70035c7a81bc6fcc36947f7c1b2d63620560bec2aa336a676213bab74c9f03d46788100dca84c0f19a0f1c14ef0d67f3fc63c011ba4787510d55fde9554e554e')

Invalid script buffer: can't parse valid script from given buffer fc70035c7a81bc6fcc36947f7c1b2d63620560bec2aa336a676213bab74c9f03d46788100dca84c0f19a0f1c14ef0d67f3fc63c011ba4787510d55fde9554e554e
Uncaught Error
    at new NodeError (/home/migue/Projects/yours/money-button/node_modules/bsv/lib/errors/index.js:20:41)
    at Function.Script.fromBuffer (/home/migue/Projects/yours/money-button/node_modules/bsv/lib/script/script.js:100:15)
    at new Script (/home/migue/Projects/yours/money-button/node_modules/bsv/lib/script/script.js:33:19)
    at Function.Script.fromHex (/home/migue/Projects/yours/money-button/node_modules/bsv/lib/script/script.js:192:10)
    at repl:1:8
    at Script.runInThisContext (vm.js:120:20)
    at REPLServer.defaultEval (repl.js:431:29)
    at bound (domain.js:426:14)
    at REPLServer.runBound [as eval] (domain.js:439:12)
    at REPLServer.onLine (repl.js:759:10)

@86chenjie
Copy link
Author

@hojarasca thanks for the response. Looking forward the fix. This issue really block my progress. Thanks again.

@86chenjie
Copy link
Author

any progress?

@86chenjie
Copy link
Author

@ryanxcharles can you help to check? Thanks!

@86chenjie
Copy link
Author

86chenjie commented Oct 12, 2020

@hojarasca Your comment is correct. The root cause is the script parse process.
I checked the Script file. Personally i think the exception handling is not correct.
When the script is not valid, the script chunks should be like this: [{buf:'whole_script_buf'}]. So we can make sure the Script.toBuffer is the same with the buf in Script.fromBuffer(buf).
In this way, the fix should be very easy and fast.

@86chenjie
Copy link
Author

Workaround is here : 86chenjie@fe41812

@calvogenerico
Copy link
Contributor

This has been open for ever. I'm going to make time to fix this next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants