0

According to this post: What are the keys used in the blockchain levelDB (ie what are the key:value pairs)?

One type of the keys in leveldb of bitcoin is formed in the following way

'b' + 32-byte block hash

I wrote the following code in go but I got "not found".

genesisBlockHash := "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
hashBytes, _ := hex.DecodeString(genesisBlockHash)
keyBytes := append([]byte("b"), hashBytes...)
data, err := db.Get(keyBytes, nil)

Where did I do wrong? How can I add 'b' to the hash?


Tried a simple case:

'R' -> 1-byte boolean ('1' if true): whether we're in the process of reindexing.

Go code

data, err := db.Get([]byte("R"), nil)

But I still got "not found". Why...


Tried file.

'f' + 4-byte file number -> file information record.

I could get some data. I think the following code should retrieve the "blk00000.dat" block file information, right?

readFile(db, []byte{0, 0, 0, 0})

func readFile(db *leveldb.DB, file []byte) {
    data, err := db.Get(append([]byte("f"), file...), nil)
    if err != nil {
        log.Println(err)
        os.Exit(1)
    }
    // read data
}

1 Answer 1

1

Where did I do wrong? How can I add 'b' to the hash?

The block hashes need to be byteswapped. Instead of 000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f, you should have 6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000.

But I still got "not found". Why...

This key-value pair is not always present.

I could get some data. I think the following code should retrieve the "blk00000.dat" block file information, right?

Yes.

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.