Wednesday, 2 August 2017

Bitcoin core tutorial & code walk through (Part 4) - gettransaction

1) Today i will talk about some basic concepts in cryptography and its applications in Bitcoin.

Hashing
- deterministic
- arbitrary length input to fixed length output
- could be many to 1 mapping (collision)
- difficult to reverse (one way mapping)
- hashed message authentication code (HMAC), integrity and authenticity of the message

Encryption
- 1 to 1 mapping
- reversible

private key --> public key --> bitcoin address
      ECC encryption         hashing
            K = G*k
where
G: generator function
k: private key
K: public key

Bitcoin network node with 4 functions
- wallet, miner, blockchain, network routing

Merkle root: binary hash tree used in a block, it is a summary of transactions in a block

Fork: a change in consensus rules

Colored coins:
- use bitcoin transaction to record extrinsic assets
- bitcoin is intrinsic to the blockchain
- avoid using a colored coin related key in a regular bitcoin wallet,
- colored coins should not be sent to address managed by regular wallets

Coinbase
- coinbase transaction is first transaction in each block
- coinbase in genesis block cannot be spent, and that coinbase transaction cannot be displayed

P2PKH
- pay to public key hash
- hide the public key until users are ready to spend their coins
- P2PK is pay to public key
- the first bitcoin transaction is P2PK

2) In this section, i will show you how to get the details of bitcoin transaction

$ ./src/bitcoin-cli -regtest getblock    60fba3ca57ca71dfcc3361abf3b2b3c199e6f4a0b1c1d4e7d8ac12cb9a7de986
{
  "hash": "60fba3ca57ca71dfcc3361abf3b2b3c199e6f4a0b1c1d4e7d8ac12cb9a7de986",
  "confirmations": 511,
  "strippedsize": 226,
  "size": 226,
  "weight": 904,
  "height": 1,
  "version": 536870912,
  "versionHex": "20000000",
  "merkleroot": "2ca49406de60010f88876ac142f7846647942b33522a3a10bc493df480cffb34",
  "tx": [
    "2ca49406de60010f88876ac142f7846647942b33522a3a10bc493df480cffb34"
  ],
......
}

The command getblock returns the txid of the block. The "tx" field contains the txid.

$ ./src/bitcoin-cli -regtest gettransaction  2ca49406de60010f88876ac142f7846647942b33522a3a10bc493df480cffb34
{
  "amount": 50.00000000,
  "confirmations": 511,
  "generated": true,
  "blockhash": "60fba3ca57ca71dfcc3361abf3b2b3c199e6f4a0b1c1d4e7d8ac12cb9a7de986",
  "blockindex": 0,
  "blocktime": 1499419297,
  "txid": "2ca49406de60010f88876ac142f7846647942b33522a3a10bc493df480cffb34",
  "walletconflicts": [
  ],
  "time": 1499419297,
  "timereceived": 1499419297,
  "bip125-replaceable": "no",
  "details": [
    {
      "account": "",
      "address": "mhTqgiUaBFPgA2kPrKqDNVqh8xsrkBMkGu",
      "category": "generate",
      "amount": 50.00000000,
      "vout": 0
    }
  ],
  "hex": "02000000010000000000000000000000000000000000000000000000000000000000000000ffffffff03510101ffffffff0200f2052a01000000232102910a020f84bca55765fdfc4ef9e788cd6ee137242338533543f6914979fb7ce5ac0000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf900000000"
}

The command gettransaction returns the object about the given transaction.

3)  In this analysis, the source code that handles gettransaction RPC will be walked through.

For gettransaction rpc

wallet/rpcwallet.cpp
UniValue gettransaction(const JSONRPCRequest& request)

The function firstly get a pointer to the CWallet object.
It creates a CWalletTx object, CAmount object, calls WalletTxToJSON() to display the output in JSON format. The ListTransactions() will include the sent and received transaction. Then, it calls EncodeHexTx().

core_write.cpp
The EncodeHexTx(const CTransaction& tx, const int serialFlags)  encodes a string, and returns the UniValue to the RPC caller.

No comments:

Post a Comment