Bitcoin Forum
May 05, 2024, 03:18:50 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Bitcoin Core noob question - how are CTransaction objects created?  (Read 861 times)
Melbustus (OP)
Legendary
*
Offline Offline

Activity: 1722
Merit: 1003



View Profile
July 09, 2015, 05:48:01 PM
 #1

I see in net.cpp where protocol messages are read in, and various places throughout the codebase where CTransaction objects are used, but I don't see where any CTransaction objects (or CMutableTransaction objs) are initially created.

What am I missing? How are the bytes read in from CNode::ReceiveMsgBytes() turned into CTransaction objects (for inclusion in the mempool, etc)?

Bitcoin is the first monetary system to credibly offer perfect information to all economic participants.
Be very wary of relying on JavaScript for security on crypto sites. The site can change the JavaScript at any time unless you take unusual precautions, and browsers are not generally known for their airtight security.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
DumbFruit
Sr. Member
****
Offline Offline

Activity: 433
Merit: 254


View Profile
July 09, 2015, 07:23:27 PM
Last edit: July 09, 2015, 08:35:05 PM by DumbFruit
 #2

I don't do C++ programming, so I apologize if this is totally wrong, but I think it's created here starting on line 1783;
https://github.com/bitcoin/bitcoin/blob/master/src/wallet/wallet.cpp

It gets pushed out as a CWalletTx and gets parsed into CTransaction/CMutableTransaction on the miner side.

By their (dumb) fruits shall ye know them indeed...
Melbustus (OP)
Legendary
*
Offline Offline

Activity: 1722
Merit: 1003



View Profile
July 09, 2015, 08:33:20 PM
 #3

Thanks DumbFruit, though I was looking for where txns are created when processing messages from other nodes (as opposed to within my node's wallet).

Found it, though: https://github.com/bitcoin/bitcoin/blob/master/src/main.cpp#L4195
Code:
...
CTransaction tx;
vRecv >> tx;
...

I was looking for a "new CTransaction" somewhere, which doesn't exist since the above is how the tx obj is created from the bytes read into a CNetMessage obj. Guess I have to refamiliarize myself with C++'s serialization operators (it's been almost 20yrs).


For reference, this was also helpful: https://en.bitcoin.it/wiki/Satoshi_Client_Sockets_and_Messages



Bitcoin is the first monetary system to credibly offer perfect information to all economic participants.
achow101
Moderator
Legendary
*
expert
Offline Offline

Activity: 3388
Merit: 6581


Just writing some code


View Profile WWW
July 09, 2015, 09:20:16 PM
 #4

Another thing to help you as you look through bitcoin, the official documentation. You can find it here: https://dev.visucore.com/bitcoin/doxygen/

theymos
Administrator
Legendary
*
Offline Offline

Activity: 5194
Merit: 12972


View Profile
July 09, 2015, 10:14:41 PM
 #5

I was looking for a "new CTransaction" somewhere, which doesn't exist since the above is how the tx obj is created from the bytes read into a CNetMessage obj. Guess I have to refamiliarize myself with C++'s serialization operators (it's been almost 20yrs).

new is almost never used in modern C++.

That's not a "C++ serialization operator". In Bitcoin Core, data streams like CDataStream (vRecv there) overload the >> and << operators to do serialization. The actual serialization is mostly done by code in serialize.h via the ADD_SERIALIZE_METHODS and READWRITE macros used in each class that can be serialized.

So vRecv >> tx is actually vRecv.operator>>(tx), which ends up callings tx.SerializationOp(vRecv, ...), which applies the READWRITE macro to each serialized CTransaction field (plus maybe some other work), which either directly unserializes the value for simple types, or calls the value's SerializationOp method to do it.

The point of this somewhat complicated setup is that you just need to do vRecv >> tx to (securely) unserialize a whole transaction, and CTransaction only needs to have a few lines of code to make this happen.

1NXYoJ5xU91Jp83XfVMHwwTUyZFK64BoAD
Melbustus (OP)
Legendary
*
Offline Offline

Activity: 1722
Merit: 1003



View Profile
July 10, 2015, 12:42:44 AM
 #6

I was looking for a "new CTransaction" somewhere, which doesn't exist since the above is how the tx obj is created from the bytes read into a CNetMessage obj. Guess I have to refamiliarize myself with C++'s serialization operators (it's been almost 20yrs).

new is almost never used in modern C++.

That's not a "C++ serialization operator". In Bitcoin Core, data streams like CDataStream (vRecv there) overload the >> and << operators to do serialization. The actual serialization is mostly done by code in serialize.h via the ADD_SERIALIZE_METHODS and READWRITE macros used in each class that can be serialized.

So vRecv >> tx is actually vRecv.operator>>(tx), which ends up callings tx.SerializationOp(vRecv, ...), which applies the READWRITE macro to each serialized CTransaction field (plus maybe some other work), which either directly unserializes the value for simple types, or calls the value's SerializationOp method to do it.

The point of this somewhat complicated setup is that you just need to do vRecv >> tx to (securely) unserialize a whole transaction, and CTransaction only needs to have a few lines of code to make this happen.


Aha....thank you. That sets my mind in the right direction. Appreciate the detail.

Bitcoin is the first monetary system to credibly offer perfect information to all economic participants.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!