Bitcoin Forum
May 02, 2024, 05:45:32 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Looking for code which actually pull and push data into chainstate database  (Read 151 times)
vishwas (OP)
Newbie
*
Offline Offline

Activity: 1
Merit: 0


View Profile
November 30, 2018, 05:57:00 AM
Last edit: November 30, 2018, 06:29:14 AM by vishwas
 #1

I am trying to debug Bitcoin core.
What I am looking for, which part of the code actually push transaction inside `chainstate` database.

Here is what I have found so far:

Form new transaction object based on RPC parameters -> Add the transaction to wallet database and into the transaction pool.

-  `prepareTransaction()`  - some parameter checks,
- `AvailableCoins()` - coins available in wallet database, `mapWallet` object
- `CreateTransaction()` - Prepare new transaction object (`txNew`) by getting script
- `sendCoins()`
- `CommitTransaction()`
- `AddToWallet()` - Add tx to wallet database
- `RelayWalletTransaction()`

So I have 2 question here:


- Where and when exactly the transactions are stored into transaction pool ?
- When `chainstate` database is updated?

##Editing

- `AcceptToMemoryPoolWithTime()` - is the method where it actually submits to the pool, I guess.
- `CTxMemPool` is the data structure of the mempool.

If that is true, I am left with the 2nd question.


TIA.
1714628732
Hero Member
*
Offline Offline

Posts: 1714628732

View Profile Personal Message (Offline)

Ignore
1714628732
Reply with quote  #2

1714628732
Report to moderator
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
btc-room101
Member
**
Offline Offline

Activity: 182
Merit: 30


View Profile WWW
December 03, 2018, 04:16:48 AM
 #2

If your running a full node with -txindex=1

then you can run this python code, and learn how the memory-pool is managed, how it fills up, and how certain transactions are picked off, ... the names used are pretty much the same as used in C++ source as python, its just that python is easier for interactive debugging

It tends to average about 40k transactions, but a lot are bogus or never accepted,

For me when I want data like your looking for, I use this sort of code 'live' run it say evey few minutes and use the bloom to kick out what you have alredy pushed into the database your building,

You can also have another bloom kept around to show if that tx was actually excepted and passed on to the block-chain,

Personally I wouldn't mess with the source of the C++ server for btc, I would just use the RPC's to grab out data, as you don't want to break the server, and you certainly don't want to have them block your ip cuz your trying to play games.

You could also talk directly to the chainstate database, but I don't see the point, when the pool is right there for your taking. Much easier to play with RPC in python than to make SQL calls to a database. You could 'hack' up the C++ btc core server, and put in all kinds of debug points and dump info , but the logs have this  info too,

...

cat get-mempool3.py

from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException

from pybloom import BloomFilter

global sblf #make bloom filter globally available

from collections import Counter
#         dup = [k for k,v in Counter(sl).items() if v>1]

import base58

if __name__ == '__main__':

    rpc_user = "hacker"
    rpc_password = "system"
    #rpc_connection = AuthServiceProxy("http://%s:%s@192.168.10.106:8332" % (rpc_user, rpc_password), timeout=3000)
    rpc_connection = AuthServiceProxy("http://%s:%s@192.168.10.102:8332" % (rpc_user, rpc_password), timeout=3000)
    r = rpc_connection

    best_block_hash = rpc_connection.getbestblockhash()

    # dump current block count height

    blkcnt = rpc_connection.getblockcount()

    print("getblockcount = %s" % blkcnt)

    #    import pdb; pdb.set_trace()

    mplist=r.getrawmempool() # get list of all tx's in mempool

    mpe= r.getmempoolentry(mplist[0])

    BLMSIZ = 5120000

    # rblf  = BloomFilter(capacity=5120000, error_rate=0.001)
    import os.path
    if (os.path.exists('memorypool.blm')):
        with open("mempool.blm", "r") as myfile:
            sblf = BloomFilter.fromfile(myfile)
    else:
        sblf = BloomFilter(capacity=BLMSIZ, error_rate=0.001)

        with open("mempool.blm", "w") as myfile:
            sblf.add(mpe)  # mark memory pool entry in bloom filter
            sblf.tofile(myfile)

    # for all transactions in pool get the in/out address list
    alist=[]
    ie=iu=0
    i = 1
    for mpe in mplist:
       
        # may not be correct what if there is not yet a tx for this entry??
        seen = sblf.add(mpe) # mark as seen, if seen before skip work
        if seen and i > 1:
            print 'BLF SKIP', i, mpe
            continue

        if i%100==0:
            print "@ ", i,len(mplist)
        i = i +1
# sometimes the entry has already expired
        try:
            mptx= r.getmempoolentry(mpe)
        except:
            #print "MPTX Fail mpe", mpe
            ie = ie + 1
            pass

        if 'wtxid' in mptx  :
            txid = mptx['wtxid']
        else :
            continue

#        print 'MP txid,mptx', txid,mptx
#        print mptx.keys()

# sometimes the txid's in the memory pool are dead or bad, but study them all to learn
        try :
            tx = r.getrawtransaction(txid,True)
        except:
            iu = iu + 1
            #print "WTXID Fail txid/mptx", iu, txid, mptx

            #pass
            continue

        for o in tx['vout']:
            #for s in o['scriptPubKey']:
             if 'scriptPubKey' in o:
                s = o['scriptPubKey'] 
                if 'addresses' in s:
                    for a in s['addresses']:
                        alist.append(a)
                if 'depends' in s:
                    d = s['depends']
                    if len(d)>0:
                        print "DEPENDS", d

        with open('pending-mptx.txt', 'w' ) as fp:
            alist = set(alist) # get rid of duplicates
            #print len(a),a
            for a in alist:
                if len(a) == 34 or len(a) == 33: # a may already be a hash160 value
                    h = base58.b58decode(a).encode('hex')[2:42]
                    fp.write( "%s:%s\n" % ( a, h  ) )
                else :
                    print "ABNORMAL ADDR",len(a),a
                    h = a
                #fp.write( "%s:%s\n" % ( a, h  ) )
            alist=[] # clear list for next round

    print "ADDR Found", len(alist)
    print "Fail ie,iu,len(mplist)", ie, iu, len(mplist)
   

    # save current bloom filter to file

    with open("mempool.blm", "w") as myfile:
        sblf.tofile(myfile)  # close bloom-filter

'''        for i in tx['vin']:
            for s in i['scriptSig']:
                if 'addresses' in i:
                    for a in i['addresses']:
                        alist.append(a)
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!