example on how to find all spends to an address

jl777

Active Member
Feb 26, 2016
279
345
I am adding a higher level layer to the iguanacore and needed to efficiently find all tx with a spend to a specific address. I was pleased at how simple the entire scan was:

Code:
int32_t iguana_datachain_scan(struct supernet_info *myinfo,struct iguana_info *coin,uint8_t rmd160[20])
{
    int64_t deposits,crypto777_payment; uint32_t lastunspentind,unspentind; int32_t i,j,num,uheight; struct iguana_bundle *bp; struct iguana_ramchain *ramchain; struct iguana_ramchaindata *rdata; struct iguana_pkhash P; struct iguana_unspent *U,*u; struct iguana_txid *T,*tx;
    for (i=num=0; i<coin->bundlescount; i++)
    {
        if ( (bp= coin->bundles[i]) != 0 )
        {
            ramchain = 0;
            if ( iguana_pkhashfind(coin,&ramchain,&deposits,&lastunspentind,&P,rmd160,i,i) != 0 )
            {
                if ( ramchain != 0 && (rdata= ramchain->H.data) != 0 )
                {
                    unspentind = lastunspentind;
                    U = RAMCHAIN_PTR(rdata,Uoffset);
                    T = RAMCHAIN_PTR(rdata,Toffset);
                    while ( unspentind > 0 )
                    {
                        tx = &T[U[unspentind].txidind];
                        for (crypto777_payment=j=0; j<tx->numvouts; j++)
                        {
                            u = &U[tx->firstvout + j];
                            uheight = iguana_uheight(coin,ramchain->height,T,rdata->numtxids,u);
                            crypto777_payment = datachain_update(myinfo,0,coin,bp,rmd160,crypto777_payment,u->type,uheight,(((uint64_t)bp->hdrsi << 32) | unspentind),u->value,u->fileid,u->scriptpos,u->scriptlen);
                        }
                        num++;
                        unspentind = U[unspentind].prevunspentind;
                    }
                }
            }
        }
    }
    return(num);
}
You will notice there are no database calls and only two internal calls needed: pkhashfind and uheight. internally I index based on the rmd160 (pkhash) so given a pkhash it is a hashtable lookup in a bundle to find the tail of the linked list. uheight is just a simple converter from the unspentind coordinate space to block height:

Code:
int32_t iguana_uheight(struct iguana_info *coin,int32_t bundleheight,struct iguana_txid *T,int32_t numtxids,struct iguana_unspent *up)
{
    if ( up->txidind > 0 && up->txidind < numtxids )
        return(bundleheight + T[up->txidind].bundlei);
    else return(bundleheight);
}
All the data structures are either read-only memory mapped files or in RAM data that is directly accessible using C structures. Each bundle of 2000 blocks are separately indexed, which does means potentially doing 200+ iterations, but in practice a lot of times you just scan backwards till you find what you need and being totally parallel if performance becomes an issue, it can be multithreaded.

In any case, if an address doesnt appear in the bundle, the initial pkhashfind will return null and that is all that is needed to be done for that bundle. Also, the linked list nature means that the number of iterations is directly proportional to the total number of transactions
 

_mr_e

Active Member
Aug 28, 2015
159
266
Very cool, happy to see a lot of progress being made on this! Thanks for your hard work!
 

jl777

Active Member
Feb 26, 2016
279
345
thanks! very few bugs being reported in the iguanacore so I have been adding code to make things more useful, also making very good progress on using BTC as a reference clock. The design was reviewed positively by Anonymint, but not until I fixed some game theory problems with my initial approach.

what this means is that BTC will become the heartbeat at the center of many other cryptos
 
  • Like
Reactions: grewalsatinder

grewalsatinder

New Member
Mar 3, 2016
10
7
Sounds something Amazing!

What problem does this solve? Was it not doable before, or it was slow to do or difficult to do?

I understand using iguanacore searching something will take very less time than other database driven blockchains as iguanacore instead uses archive bundles while searching and doing parallel execution.
 

jl777

Active Member
Feb 26, 2016
279
345
actually right now it is fast enough, I dont bother with parallel searching.

What this enables is running hundreds (or thousands?) of smartchains at the same time, each one being a datachain with some special enhancement

I am building a layer so that MGW can be implemented in a few pages of code

It is taking literally ALL day to import a single privkey into my vericoind wallet. And I need to import a second one. With iguana it would have taken just a second as all full nodes have complete history of all transactions for all addresses

so yes, this sort of thing was theoretically possible before, but if something takes a full day to run, odds are you wont do it unless you really have to. and no way to do 100 at once. with virtual chains, each having some sort of custom processing, I need to make things as efficient as possible