blockchain – How does Bitcoin learn from/write to LevelDB

0
65


Within the particular instance you requested, it does so by querying the LevelDB situated in ./bitcoin/chainstate (there may be the place the UTXO set is saved).

UTXOs are recognized by its txid (Little Endian illustration) leaded by a c.

Instance

Transaction

246c5a81b6ad0dfc0dbc0b2ff5bde65ee1913f75a47d409b8ff8074a27ec1000

is recognized within the LevelDB by:

c0010ec274a07f88f9b407da4753f91e15ee6bdf52f0bbc0dfc0dadb6815a6c24

or what’s the identical:

630010ec274a07f88f9b407da4753f91e15ee6bdf52f0bbc0dfc0dadb6815a6c24

because the hex illustration of the lowercase ASCII character ‘c’ is 63. Discover that once you question the database you shouldn’t do it utilizing the string illustration of the worth, however the byte array one.

Every entry within the LevelDB comprises two values, the identifier (defined avobe and used to question the info), and the worth.

The worth of every entry (the UTXO) is encoded within the LevelDB following the construction that follows (extracted from this remark from the supply code):

  • Serialized format
    • VARINT(nVersion)
    • VARINT(nCode)
    • unspentness bitvector, for vout[2] and additional; least important byte first
    • the non-spent CTxOuts (through CTxOutCompressor)
    • VARINT(nHeight) * The nCode worth consists of:
      • bit 1: IsCoinBase()
      • bit 2: vout[0] just isn’t spent
      • bit 4: vout[1] just isn’t spent
      • The upper bits encode N, the variety of non-zero bytes within the following bitvector.
      • In case each bit 2 and bit 4 are unset, they encode N-1, as there have to be at the very least one non-spent output).

You will discover a greater formated rationalization and examples within the supply code from the supplied hyperlink.

Discover that, since chainstate database used to set off anti-virus software program, as you possibly can examine on this problem and on this query, the primary strains of the chainstate comprises the obfuscation key, a 64-bit worth recognized by 0e00obfuscation_key that ought to be XORed with every knowledge worth from the database. To take action, the secret is concatenated with itself till it reaches the size of the obfuscated worth.

Instance

Lets o_k = '27c78118b7316105' be our obfuscation key, and

{"key": "63000002f414665fb03389dd19776732bf90883bcb399d23323747596e98dd1801", "worth": "26c326d7353661dc7005d274976f458691f24f0f05d141335f4ad5927e41"}

be one entry from the database. As you possibly can see, worth doesn’t observe the above launched format, since it’s obfuscated. worth is 60 characters lengthy, so if we prolong the obsfuscation key because it reaches the identical size, we get:

27c78118b731610527c78118b731610527c78118b731610527c78118b731

Now, if we carry out the XOR between the worth and the important thing:

26c326d7353661dc7005d274976f458691f24f0f05d141335f4ad5927e41 XOR 27c78118b731610527c78118b731610527c78118b731610527c78118b731  

We get hold of:

0104a7cf820700d957c2536c205e2483b635ce17b2e02036788d548ac970

That matches with the earlier acknowledged format, and it is certainly the UTXO worth recognized by 63000002f414665fb03389dd19776732bf90883bcb399d23323747596e98dd1801.

LEAVE A REPLY

Please enter your comment!
Please enter your name here