Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function ONBLOCK(block){
- try{
- block=block.toJSON();
- block.tally={seen:0,caught:0};
- var b={
- "hash": block.hash
- , "prevHash": block.prevBlock
- , "txs": []
- };
- for(var t=1;t<block.txs.length;t+=1){
- block.txs[t].ntxid='';
- for(var i=0;i<block.txs[t].inputs.length;i+=1){
- if(block.txs[t].ntxid.length){block.txs[t].ntxid+=',';}
- 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+')';
- }
- for(var o=0;o<block.txs[t].outputs.length;o+=1){
- 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+')';
- }
- var hmac=forge.hmac.create();
- /*block.txs[t].ntxid=*/hmac.start('sha1',block.txs[t].ntxid);
- b.txs.push(block.txs[t]);//used to find a malleated tx
- //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!!!)
- if(/*found||*/ntxids[b.txs[b.txs.length-1].ntxid]){
- /*if(found&&!ntxids[b.txs[b.txs.length-1].ntxid]){
- console.log({
- error:'found a tx with malleated ntxid'
- , in_ntxids:{ntxid:found}
- , in_block:{ntxid:block.txs[t].ntxid,hash:block.txs[t].hash}
- },'@ONBLOCK-found.'+NAME);}*/
- block.tally.seen+=1;
- block.txs[t].seen=true;
- delete ntxids[b.txs[b.txs.length-1].ntxid];
- }
- else{
- 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
- for(var i=0;i<block.txs[t].outputs.length;i+=1){
- 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)
- var tx=block.txs[t];
- var bx={
- _id: tx.hash
- , type: 'tx'
- , state: B.db.bitcoin_watched.ADDRS.cache[tx.outputs[i].address].incomming//change/payment
- , confirmations: 'calculate'
- , 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!)
- , satoshis: bcoin.btc.Amount.value(tx.outputs[i].value)
- , address: tx.outputs[i].address
- , prv: B.db.bitcoin_watched.ADDRS.cache[tx.outputs[i].address].prv
- , site: B.db.bitcoin_watched.ADDRS.cache[tx.outputs[i].address].site
- , outputIndex: i//needed to check unspents
- , sequenceNumber: 0//don't lock
- , script: tx.outputs[i].script//needed to check unspents
- , ntxid: tx.ntxid
- , block: b.hash
- };
- B.db.bitcoin_watched[bx._id]=bx;//beats the race condition (mod saves it below vvvv)
- console.log({bx:bx},'@ONBLOCK-b4-client-send-utx-payment.'+NAME);
- 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);}}
- JSON.console([':::onutx-temp:::',bx]);
- block.tally.caught+=1;
- block.txs[t].caught=true;
- }}}}
- block.seen=(block.tally.seen+block.tally.caught)+'/'+block.txs.length;
- console.log({tally:block.seen,percentage:(((block.tally.seen+block.tally.caught)/block.txs.length)*100)+'%'},'@ONBLOCK-block-seen.'+NAME);
- block.ntxids=ntxids;
- if(!B.db.bitcoin_watched.BLOCKS.cache[b.hash]){
- B.db.bitcoin_watched.BLOCKS.cache[b.hash]={
- hash: b.hash
- , prevHash: b.prevHash
- , height: (!B.db.bitcoin_watched.BLOCKS.cache[b.prevHash]?HEIGHT:B.db.bitcoin_watched.BLOCKS.cache[b.prevHash].height+1)
- , txs: []
- };
- var h=0,bloc=B.db.bitcoin_watched.BLOCKS.cache;
- for(var k in bloc){
- if(h<bloc[k].height){h=bloc[k].height;}//get the highest block for the new height
- if(bloc[k].height<HEIGHT-6){//we're ideally keeping a chain of 8 here <- because the HEIGHT is updated below
- if(!bloc[bloc[k].prevHash]){//if a blocks prevhash is an undefined block
- for(var n=0;n<bloc[k].txs.length;n+=1){
- var id=bloc[k].txs[n];//is only a tx id hash typeof string
- console.log({watched_id:B.db.bitcoin_watched[id],id:id},'@ONBLOCK-b4-spendable/orphaned.'+NAME);
- 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
- (function(id){setTimeout(function(){spendableChange(id);},0);return;})(id);
- }
- else if(B.db.bitcoin_watched[id]&&['utx-payment','utx-change'].indexOf(B.db.bitcoin_watched[id].state)!==-1){
- B.mod('watched',id,['warn="orphaned"'],{},'orphaned',null);
- ////
- ////
- //// todo!!!
- ////
- ////
- }}
- delete B.db.bitcoin_watched.BLOCKS.cache[k];
- }}}
- HEIGHT=h;
- if(BLOCKAVG.length==10){BLOCKAVG.shift();}BLOCKAVG.push(+new Date());
- function after(){
- try{client.send(aes.en(JSON.stringify({f:'block',d:{avg:BLOCKAVG,height:HEIGHT}})));}catch(ERROR){console.log(ERROR,'@ONBLOCK-client-send-block.'+NAME);}
- B.mod('watched','BLOCKS',['cache='+JSON.stringify(B.db.bitcoin_watched.BLOCKS.cache)],{},'cache BLOCKS',function(r,ERROR){
- if(ERROR){console.log(ERROR,'@ONBLOCK-mod-BLOCKS-cache.'+NAME);}
- 'JSON.console(B.db.bitcoin_watched.BLOCKS.cache)';
- 'console.log({HEIGHT:HEIGHT})';
- var weekold=(+new Date())+10080000;
- for(var k in ntxids){if(ntxids[k][0]>weekold){delete ntxids[k];}}//remove 1 week unseens
- 'console.log(ntxids)';
- fs.writeFile(__dirname+'/../db/bitcoin_latestblock/debug.json',JSON.stringify(block,undefined,'\t'));
- });}
- if(!b.txs.length){after();}//coinbase or none
- else{
- (function loop_hashes(i){
- function update(_id,malleated){
- console.log({watched_id:B.db.bitcoin_watched[id],id:id},'@ONBLOCK-b4-replace-"utx-".'+NAME);
- 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)
- B.mod('watched',_id,[
- 'state="'+(B.db.bitcoin_watched[_id].state.replace('utx-',''))+'"'
- , 'confirmations="calculate"'
- , 'height='+B.db.bitcoin_watched.BLOCKS.cache[b.hash].height
- , 'block="'+b.hash+'"'//refer to the block it was in (for traceability)
- , 'delete ntxid']
- ,{},'onblk',function(_,ERROR){
- if(ERROR){console.log(ERROR,'@ONBLOCK-mod-watched-utx-to-tx.'+NAME);}
- if(b.txs[(i+=1)]){loop_hashes(i);}else{after();}
- });}
- if(B.db.bitcoin_watched[b.txs[i].hash]){update(b.txs[i].hash,true);}
- else{
- var malleated;
- 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;}}
- if(malleated){
- B.mov('watched',malleated._id,b.txs[i].hash,'rename malleated tx hash',function(r,ERROR){
- if(ERROR){console.log(ERROR,'@ONBLOCK-mov-rename-malleated-tx-hash.'+NAME);}
- if(r[0]){update(b.txs[i].hash,false);}
- else if(b.txs[(i+=1)]){loop_hashes(i);}else{after();}
- });}
- else if(b.txs[(i+=1)]){loop_hashes(i);}else{after();}
- }})(0);}}
- }catch(e){console.log(e);}
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement