Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
6,971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ONBLOCK(block){
  2.  
  3.     try{
  4.  
  5.     block=block.toJSON();
  6.     block.tally={seen:0,caught:0};
  7.     var b={
  8.         "hash":             block.hash
  9.     ,   "prevHash":         block.prevBlock
  10.     ,   "txs":              []
  11.         };
  12.     for(var t=1;t<block.txs.length;t+=1){
  13.         block.txs[t].ntxid='';
  14.         for(var i=0;i<block.txs[t].inputs.length;i+=1){
  15.             if(block.txs[t].ntxid.length){block.txs[t].ntxid+=',';}
  16.             block.txs[t].ntxid+='input(prevhash:'+block.txs[t].inputs[i].prevout.hash+',address:'+block.txs[t].inputs[i].address+',index:'+block.txs[t].inputs[i].prevout.index+',sequence:'+block.txs[t].inputs[i].sequence+')';
  17.             }
  18.         for(var o=0;o<block.txs[t].outputs.length;o+=1){
  19.             block.txs[t].ntxid+=',output(address:'+(block.txs[t].outputs[o].address||'null-data')+',value:'+parseFloat(block.txs[t].outputs[o].value).toFixed(8)+',index:'+o+')';
  20.             }
  21.         var hmac=forge.hmac.create();
  22.         /*block.txs[t].ntxid=*/hmac.start('sha1',block.txs[t].ntxid);
  23.         b.txs.push(block.txs[t]);//used to find a malleated tx
  24.         //var found;for(var n in ntxids){if(ntxids[n][1]==block.txs[t].hash){found=n;break;}}if(found){delete ntxids[found];}//temp bug patch up memory overload (bad deletes everything so we see nothing!!!)
  25.         if(/*found||*/ntxids[b.txs[b.txs.length-1].ntxid]){
  26.             /*if(found&&!ntxids[b.txs[b.txs.length-1].ntxid]){
  27.                 console.log({
  28.                     error:'found a tx with malleated ntxid'
  29.                 ,   in_ntxids:{ntxid:found}
  30.                 ,   in_block:{ntxid:block.txs[t].ntxid,hash:block.txs[t].hash}
  31.                 },'@ONBLOCK-found.'+NAME);}*/
  32.             block.tally.seen+=1;
  33.             block.txs[t].seen=true;
  34.             delete ntxids[b.txs[b.txs.length-1].ntxid];
  35.             }
  36.         else{
  37.             var found;for(var n in ntxids){if(ntxids[n][1]==block.txs[t].hash){found=n;break;}}if(found){delete ntxids[found];}//temp bug patch up memory overload
  38.             for(var i=0;i<block.txs[t].outputs.length;i+=1){
  39.                 if(block.txs[t].outputs[i].address&&B.db.bitcoin_watched.ADDRS.cache[block.txs[t].outputs[i].address]){//don't miss txs if we have been down! (or if we never saw it the the mempool)
  40.                     var tx=block.txs[t];
  41.                     var bx={
  42.                         _id:                tx.hash
  43.                     ,   type:               'tx'
  44.                     ,   state:              B.db.bitcoin_watched.ADDRS.cache[tx.outputs[i].address].incomming//change/payment
  45.                     ,   confirmations:      'calculate'
  46.                     ,   height:             HEIGHT//save the height at which it showed up as unconfirmed (it might be in a block we missed! in that case we will be inaccurate but at least we did not miss it!)
  47.                     ,   satoshis:           bcoin.btc.Amount.value(tx.outputs[i].value)
  48.                     ,   address:            tx.outputs[i].address
  49.                     ,   prv:                B.db.bitcoin_watched.ADDRS.cache[tx.outputs[i].address].prv
  50.                     ,   site:               B.db.bitcoin_watched.ADDRS.cache[tx.outputs[i].address].site
  51.                     ,   outputIndex:        i//needed to check unspents
  52.                     ,   sequenceNumber:     0//don't lock
  53.                     ,   script:             tx.outputs[i].script//needed to check unspents
  54.                     ,   ntxid:              tx.ntxid
  55.                     ,   block:              b.hash
  56.                         };
  57.                     B.db.bitcoin_watched[bx._id]=bx;//beats the race condition (mod saves it below vvvv)
  58.                     console.log({bx:bx},'@ONBLOCK-b4-client-send-utx-payment.'+NAME);
  59.                     if(bx.state=='utx-payment'){try{client.send(aes.en(JSON.stringify({f:'utx',d:bx})));}catch(ERROR){console.log(ERROR,'@ONBLOCK-client-send-utx-payment.'+NAME);}}
  60.                     JSON.console([':::onutx-temp:::',bx]);
  61.                     block.tally.caught+=1;
  62.                     block.txs[t].caught=true;
  63.                     }}}}
  64.     block.seen=(block.tally.seen+block.tally.caught)+'/'+block.txs.length;
  65.     console.log({tally:block.seen,percentage:(((block.tally.seen+block.tally.caught)/block.txs.length)*100)+'%'},'@ONBLOCK-block-seen.'+NAME);
  66.     block.ntxids=ntxids;
  67.     if(!B.db.bitcoin_watched.BLOCKS.cache[b.hash]){
  68.         B.db.bitcoin_watched.BLOCKS.cache[b.hash]={
  69.             hash:       b.hash
  70.         ,   prevHash:   b.prevHash
  71.         ,   height:     (!B.db.bitcoin_watched.BLOCKS.cache[b.prevHash]?HEIGHT:B.db.bitcoin_watched.BLOCKS.cache[b.prevHash].height+1)
  72.         ,   txs:        []
  73.             };
  74.         var h=0,bloc=B.db.bitcoin_watched.BLOCKS.cache;
  75.         for(var k in bloc){
  76.             if(h<bloc[k].height){h=bloc[k].height;}//get the highest block for the new height
  77.             if(bloc[k].height<HEIGHT-6){//we're ideally keeping a chain of 8 here <- because the HEIGHT is updated below
  78.                 if(!bloc[bloc[k].prevHash]){//if a blocks prevhash is an undefined block
  79.                     for(var n=0;n<bloc[k].txs.length;n+=1){
  80.                         var id=bloc[k].txs[n];//is only a tx id hash typeof string
  81.                         console.log({watched_id:B.db.bitcoin_watched[id],id:id},'@ONBLOCK-b4-spendable/orphaned.'+NAME);
  82.                         if(B.db.bitcoin_watched[id]&&B.db.bitcoin_watched[id].state=='change'){//change must be old before we can trust making it spendable and before we can delete the trail of where it came from
  83.                             (function(id){setTimeout(function(){spendableChange(id);},0);return;})(id);
  84.                             }
  85.                         else if(B.db.bitcoin_watched[id]&&['utx-payment','utx-change'].indexOf(B.db.bitcoin_watched[id].state)!==-1){
  86.                             B.mod('watched',id,['warn="orphaned"'],{},'orphaned',null);
  87.                             ////
  88.                             ////
  89.                             //// todo!!!
  90.                             ////
  91.                             ////
  92.                             }}
  93.                     delete B.db.bitcoin_watched.BLOCKS.cache[k];
  94.                     }}}
  95.         HEIGHT=h;
  96.         if(BLOCKAVG.length==10){BLOCKAVG.shift();}BLOCKAVG.push(+new Date());
  97.         function after(){
  98.             try{client.send(aes.en(JSON.stringify({f:'block',d:{avg:BLOCKAVG,height:HEIGHT}})));}catch(ERROR){console.log(ERROR,'@ONBLOCK-client-send-block.'+NAME);}
  99.             B.mod('watched','BLOCKS',['cache='+JSON.stringify(B.db.bitcoin_watched.BLOCKS.cache)],{},'cache BLOCKS',function(r,ERROR){
  100.                 if(ERROR){console.log(ERROR,'@ONBLOCK-mod-BLOCKS-cache.'+NAME);}
  101.                 'JSON.console(B.db.bitcoin_watched.BLOCKS.cache)';
  102.                 'console.log({HEIGHT:HEIGHT})';
  103.                 var weekold=(+new Date())+10080000;
  104.                 for(var k in ntxids){if(ntxids[k][0]>weekold){delete ntxids[k];}}//remove 1 week unseens
  105.                 'console.log(ntxids)';
  106.                 fs.writeFile(__dirname+'/../db/bitcoin_latestblock/debug.json',JSON.stringify(block,undefined,'\t'));
  107.                 });}
  108.         if(!b.txs.length){after();}//coinbase or none
  109.         else{
  110.             (function loop_hashes(i){
  111.                 function update(_id,malleated){
  112.                     console.log({watched_id:B.db.bitcoin_watched[id],id:id},'@ONBLOCK-b4-replace-"utx-".'+NAME);
  113.                     B.db.bitcoin_watched.BLOCKS.cache[b.hash].txs.push(_id);//list only our txhashes in the block cache (if orphaned we can locate and put a debug_warn)
  114.                     B.mod('watched',_id,[
  115.                         'state="'+(B.db.bitcoin_watched[_id].state.replace('utx-',''))+'"'
  116.                     ,   'confirmations="calculate"'
  117.                     ,   'height='+B.db.bitcoin_watched.BLOCKS.cache[b.hash].height
  118.                     ,   'block="'+b.hash+'"'//refer to the block it was in (for traceability)
  119.                     ,   'delete ntxid']
  120.                     ,{},'onblk',function(_,ERROR){
  121.                         if(ERROR){console.log(ERROR,'@ONBLOCK-mod-watched-utx-to-tx.'+NAME);}
  122.                         if(b.txs[(i+=1)]){loop_hashes(i);}else{after();}
  123.                         });}
  124.                 if(B.db.bitcoin_watched[b.txs[i].hash]){update(b.txs[i].hash,true);}
  125.                 else{
  126.                     var malleated;
  127.                     for(var j=0;j<B.db.bitcoin_watched.length;j+=1){if((B.db.bitcoin_watched[j].ntxid+'')==(b.txs[i].ntxid+'')){malleated=B.db.bitcoin_watched[j];break;}}
  128.                     if(malleated){
  129.                         B.mov('watched',malleated._id,b.txs[i].hash,'rename malleated tx hash',function(r,ERROR){
  130.                             if(ERROR){console.log(ERROR,'@ONBLOCK-mov-rename-malleated-tx-hash.'+NAME);}
  131.                             if(r[0]){update(b.txs[i].hash,false);}
  132.                             else if(b.txs[(i+=1)]){loop_hashes(i);}else{after();}
  133.                             });}
  134.                     else if(b.txs[(i+=1)]){loop_hashes(i);}else{after();}
  135.                     }})(0);}}
  136.  
  137. }catch(e){console.log(e);}
  138.                 };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement