5

I see in the bitcoin wiki there is something called "script" which compiles down to some bytecode to be run by a very very basic virtual machine. Does this mean that each block is actually just a string/array of bytes when it comes in and is processed by a node? so a block in its "true" format would look something like [0x01, 0x04, 0x0f, 0xa1.... etc. ?

and then the node that recieves the block just puts the bytes into the interpreter to be run by the bitcoin VM?

which is correct? a block is pure bytecode or JSON? what would a bitcoincore node understand when receiving a new block?

1

2 Answers 2

4

A block consists of a header, and then a number of serialised transactions. The block header contains no script, it only contains data such as the merkle root of the transactions in the block (so the header commits to the list of transactions), nonce, version number, etc. Note that a block isn't encoded with JSON, it has its own serialisation format which you can find in most developer documentation (e.g. here)

The script is contained only inside transactions themselves, specifically to dictate the spending condition of an output. An output will have a scriptPubKey which holds the outputs script (usually of a specific form like P2PKH, P2SH) and the input when it's spent will contain a scriptSig which contains the spending script. In segregated witness transactions, the scriptSig will be empty and the script will instead be in the witness section of the transaction, but it's still the same principle.

Block verification takes a number of steps such as validation of the proof of work, validation that the timestamp is ok, that the coinbase transaction is ok, that all the other transactions are ok, etc etc.

0

I see in the bitcoin wiki there is something called "script" which compiles down to some bytecode to be run by a very very basic virtual machine.

Script is a stack-based language that is used to validate input and output scripts in transactions. Script is purposely designed to lack Turing-completeness (no loops) so that it is deterministic in nature. (No infinite loops, etc). An input/output is considered to be "valid" when the top stack item is true (or 0x01).

Does this mean that each block is actually just a string/array of bytes when it comes in and is processed by a node?

Bitcoin nodes validate transactions, Bitcoin miners compute blocks in accordance with the Proof-of-Work model defined in the Bitcoin protocol. That being said, yes, technically the raw block data is an array of hexadecimal bytes. When miners begin mining the block, they are only hashing the header of the block (first 80 bytes) which includes the following information:

nVersion|hashPrevBlock|hashMerkleRoot|Time|Bits|Nonce

They can get away with doing this because the hashMerkleRoot value is a reference to the hash of all the transactions in the block they are mining. These transactions have been validated by nodes.

In Bitcoin-core you can specify serialization as the second parameter in the getblock command. By default it will nicely display the data in a JSON like output, although it is really a long hexadecimal output.


Example:

getblock 00000000c937983704a73af28acdec37b049d214adbda81d7e2a3dd146f6ed09 0

Output: 010000007de867cc8adc5cc8fb6b898ca4462cf9fd66...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.