Skip to content

Instantly share code, notes, and snippets.

@moofone
Last active November 14, 2019 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moofone/67f2b2fc2dcf0c5596d0f55d8ee7d950 to your computer and use it in GitHub Desktop.
Save moofone/67f2b2fc2dcf0c5596d0f55d8ee7d950 to your computer and use it in GitHub Desktop.
ian
GET_BLOCK_TEMPLATE
{"bytes_limit":"0x91c08","cellbase":{"cycles":null,"data":{"cell_deps":[],"header_deps":[],"inputs":[{"previous_output":{"index":"0xffffffff","tx_hash":"0x0000000000000000000000000000000000000000000000000000000000000000"},"since":"0x51b5"}],"outputs":[{"capacity":"0x3dfc9dcd68","lock":{"args":"0xb2e61ff569acf041b3c2c17724e2379c581eeac3","code_hash":"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8","hash_type":"type"},"type":null}],"outputs_data":["0x"],"version":"0x0","witnesses":["0x590000000c00000055000000490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8011400000048b12c67274f56be27ccf4920402e16cd401c71c00000000"]},"hash":"0x47ad3afa5e8cb070929b92c9bbec5a76076bea8ce40494bef9a28233faf05691"},"compact_target":"0x1d722992","current_time":"0x16e67721478","cycles_limit":"0x2540be400","dao":"0x6180438021b5630054f3c3099883260012d1aa0131ef05000036d94c26860b00","epoch":"0x2eb012a00001f","number":"0x51b5","parent_hash":"0x097048451c099f846f2c4d19b79360cd9fe54b415496b454e008748bed493e8b","proposals":[],"raw":"0x000000009229721d8d0072676e010000b5510000000000001f00002a01eb0200097048451c099f846f2c4d19b79360cd9fe54b415496b454e008748bed493e8b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006180438021b5630054f3c3099883260012d1aa0131ef05000036d94c26860b00","transactions":[],"uncles":[],"uncles_count_limit":"0x2","version":"0x0","work_id":"0x1"}
POW
extraNonce2:0000000000000002a9df0189
nonceBuf:000000010000000000000002a9df0189
(not used currently.. tried this) nonceBigNum (LE,auto) base10:182113964086912058668270381438167154688
(not used currently.. tried this) nonceBigNum (LE,auto) baase16:8901dfa9020000000000000001000000
HSPACE:010000000000000000000000000000000000000000000000000000000000000000
blockCompactDiff:494021010
blockTgt:3077811784929926005267193015430431727813141032940143402072250542718976
blockDiff: 37621562
headerHash:26b720beb675ea36c1042993cdd51f40c5ff1479588e583eafd984119e26bc69
headerHashWithNonce:26b720beb675ea36c1042993cdd51f40c5ff1479588e583eafd984119e26bc69000000010000000000000002a9df0189
powHash(eaglesonog):000000009d2125a3f0dd2afa7c8d4fd08dec2f429bd00a99f472d44cbcdad873
powBigNum:9d2125a3f0dd2afa7c8d4fd08dec2f429bd00a99f472d44cbcdad873
shareDiff:6997487827
RAW HEADER
000000009229721d8d0072676e010000b5510000000000001f00002a01eb0200097048451c099f846f2c4d19b79360cd9fe54b415496b454e008748bed493e8b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006180438021b5630054f3c3099883260012d1aa0131ef05000036d94c26860b00
SUBMIT_BLOCK
arg[0]:0x1
args[1]:{"header":{"compact_target":"0x1d722992","dao":"0x6180438021b5630054f3c3099883260012d1aa0131ef05000036d94c26860b00","epoch":"0x2eb012a00001f","nonce":"0x10000000000000002a9df0189","number":"0x51b5","parent_hash":"0x097048451c099f846f2c4d19b79360cd9fe54b415496b454e008748bed493e8b","proposals_hash":"0x0000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x16e6772008d","transactions_root":"0x0000000000000000000000000000000000000000000000000000000000000000","uncles_hash":"0x0000000000000000000000000000000000000000000000000000000000000000","version":"0x0"},"proposals":[],"transactions":[{"cell_deps":[],"header_deps":[],"inputs":[{"previous_output":{"index":"0xffffffff","tx_hash":"0x0000000000000000000000000000000000000000000000000000000000000000"},"since":"0x51b5"}],"outputs":[{"capacity":"0x3dfc9dcd68","lock":{"args":"0xb2e61ff569acf041b3c2c17724e2379c581eeac3","code_hash":"0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8","hash_type":"type"},"type":null}],"outputs_data":["0x"],"version":"0x0","witnesses":["0x590000000c00000055000000490000001000000030000000310000009bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8011400000048b12c67274f56be27ccf4920402e16cd401c71c00000000"]}],"uncles":[]}
ERROR [1573693559859] (21491 on ice-devlab): ckb submit_block error 0 when submitting block with submit_block {"code":-3,"message":"Header(Pow(InvalidNonce))"}
@doitian
Copy link

doitian commented Nov 14, 2019

There are two errors in your raw header:

lines in this order: version, compact_target, timestamp, number, epoch, parent_hash, transactions_root, proposals_hash, uncles_hash, dao
mine
00000000
9229721d
781472676e010000
b551000000000000
1f00002a01eb0200
097048451c099f846f2c4d19b79360cd9fe54b415496b454e008748bed493e8b
78fdfacab36063f7863901aa6de85631254fe3741c387e5b0330c610014f1da4
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
6180438021b5630054f3c3099883260012d1aa0131ef05000036d94c26860b00

yours
00000000
9229721d
8d0072676e010000
b551000000000000
1f00002a01eb0200
097048451c099f846f2c4d19b79360cd9fe54b415496b454e008748bed493e8b
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000
6180438021b5630054f3c3099883260012d1aa0131ef05000036d94c26860b00

First, the timestamp does not match the timestamp you send in submit_block. It also does not match the current_time in the get_block_template response. I guess you generate the raw header and block template using different timestamp.

In your get_block_template response, the timestamp is 0x16e67721478, which is 781472676e010000 in the raw header, but in your raw header, it is 8d0072676e010000, which is 0x16e6772008bd.

Second, the transactions_root is wrong. When creating the block from block template, the cellbase has to put into the transactions as the first transaction. It is 78fdfacab36063f7863901aa6de85631254fe3741c387e5b0330c610014f1da4 in my raw header.

@doitian
Copy link

doitian commented Nov 14, 2019

Sample project to verify the result (in Rust) https://github.com/doitian/submit-block-demo

@doitian
Copy link

doitian commented Nov 14, 2019

In your submit_block requests, there are three differences

6c6
<     "timestamp": "0x16e6772008d",
---
>     "timestamp": "0x16e67721478",
9c9
<     "transactions_root": "0x0000000000000000000000000000000000000000000000000000000000000000",
---
>     "transactions_root": "0x78fdfacab36063f7863901aa6de85631254fe3741c387e5b0330c610014f1da4",
13c13
<     "nonce": "0x10000000000000002a9df0189"
---
>     "nonce": "0x8901dfa9020000000000000001000000"

I have mentioned the first two in raw header. The reason of the last one is the invalid format of nonce.

@moofone
Copy link
Author

moofone commented Nov 14, 2019

Great analysis - thank you.

Let's start with an easy one: How much impact does timestamp have on this validation? you are correct in that where I get the raw header and get_block_template call are different sources. if this really matters, what is the source of truth for time?

@doitian
Copy link

doitian commented Nov 14, 2019

It's OK to just use the one returned from block template. The miner also can use the latest time, but ensure use the same time in both raw header and the submit block request.

There's two rules related to timestamp:

  • A block timestamp has to be larger than the median of the timestamp in the previous 37 blocks.
  • CKB node will hold a block if the block timestamp is more than 15 seconds in the future.

The time returned by get_block_template have already ensure the timestamp can pass the two rules.

@moofone
Copy link
Author

moofone commented Nov 14, 2019

Can you also tell me the proper source of transactions_root?

@doitian
Copy link

doitian commented Nov 14, 2019

We define blake256 as blake2b with personal ckb-default-hash and output size 256 bits.

|| denotes bytes concatenation.

transactions_root = blake256( transaction hashes CBMT root || transaction witness hashes CBMT root)
  • transaction hashes CBMT is the root of a CBMT which nodes are the hashes of all the transactions in the block, and the merge function is blake256(left || right)
  • transaction witness hashes CBMT root is also a CBMT, but the nodes are the witness hashes of all the transactions.

A transaction hash is blake256(serialized RawTransaction) and transaction witness hash is blake256(serialized Transaction).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment