{"version":3,"sources":["https://lively-kernel.org/lively4/lively4-stable/src/blockchain/model/blockchainNode/miner.js"],"names":["MiningProof","Block","TransactionCollection","MINING_INTERVAL","Miner","constructor","blockchainNode","_blockchainNode","_transactions","_subscribers","subscribe","subscriber","callback","push","unsubscribe","filter","subscription","addTransaction","transaction","isVerified","add","forEach","invalidateTransactions","block","transactions","remove","mine","hasExited","miningDifficulty","Math","log10","blockchain","size","miningProof","work","wallet","headOfChain","hash","propagateBlock","console","log"],"mappings":";;;;;;AAAOA,iB;;AACAC,W;;AACAC,2B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEP,YAAMC,kBAAkB,CAAxB,C,CAA2B;;;;;;;;;;;;;;;AAEZ,YAAMC,KAAN,CAAY;AACzBC,oBAAYC,cAAZ,EAA4B;AAC1B,eAAKC,eAAL,GAAuBD,cAAvB;AACA,eAAKE,aAAL,GAAqB,IAAIN,qBAAJ,EAArB;AACA,eAAKO,YAAL,GAAoB,EAApB;AACD;;AAEDC,kBAAUC,UAAV,EAAsBC,QAAtB,EAAgC;AAC9B,eAAKH,YAAL,CAAkBI,IAAlB,CAAuB,EAAC,cAAcF,UAAf,EAA2B,YAAYC,QAAvC,EAAvB;AACD;;AAEDE,oBAAYH,UAAZ,EAAwB;AACtB,eAAKF,YAAL,GAAoB,KAAKA,YAAL,CAAkBM,MAAlB,CAAyBC,gBAAgBA,aAAa,YAAb,MAA+BL,UAAxE,CAApB;AACD;;AAEDM,uBAAeC,WAAf,EAA4B;AAC1B,cAAG,CAACA,YAAYC,UAAZ,EAAJ,EAA8B;AAC5B;AACD;AACD,eAAKX,aAAL,CAAmBY,GAAnB,CAAuBF,WAAvB;AACA,eAAKT,YAAL,CAAkBY,OAAlB,CAA0BL,gBAAgBA,aAAa,UAAb,EAAyBE,WAAzB,CAA1C;AACD;;AAEDI,+BAAuBC,KAAvB,EAA8B;AAC5BA,gBAAMC,YAAN,CAAmBH,OAAnB,CAA2BH,eAAe;AACxC,iBAAKV,aAAL,CAAmBiB,MAAnB,CAA0BP,WAA1B;AACD,WAFD;AAGD;;AAED,cAAMQ,IAAN,GAAa;AACX,cAAI,KAAKnB,eAAL,CAAqBoB,SAAzB,EAAoC;AAClC;AACD;;AAED,gBAAMC,mBAAmBC,KAAKC,KAAL,CAAW,KAAKvB,eAAL,CAAqBwB,UAArB,CAAgCC,IAAhC,EAAX,CAAzB;AACA,gBAAMC,cAAc,IAAIjC,WAAJ,CAAgB4B,gBAAhB,CAApB;AACA,gBAAMK,YAAYC,IAAZ,EAAN;AACA,gBAAMX,QAAQ,IAAItB,KAAJ,CACZ,KAAKM,eAAL,CAAqB4B,MADT,EAEZ,KAAK3B,aAFO,EAGZyB,WAHY,EAIZ,KAAK1B,eAAL,CAAqBwB,UAArB,CAAgCK,WAAhC,CAA4CC,IAJhC,CAAd;AAMA,eAAK7B,aAAL,GAAqB,IAAIN,qBAAJ,EAArB;AACA,eAAKK,eAAL,CAAqB+B,cAArB,CAAoCf,KAApC;AACAgB,kBAAQC,GAAR,CAAY,6CAAZ;AACD;AA9CwB;;yBAANpC,K","file":"miner.js","sourcesContent":["import MiningProof from '../block/miningProof.js';\nimport Block from '../block/block.js';\nimport TransactionCollection from '../transaction/transactionCollection.js';\n\nconst MINING_INTERVAL = 6; // in seconds\n\nexport default class Miner {\n  constructor(blockchainNode) {\n    this._blockchainNode = blockchainNode;\n    this._transactions = new TransactionCollection();\n    this._subscribers = [];\n  }\n  \n  subscribe(subscriber, callback) {\n    this._subscribers.push({'subscriber': subscriber, 'callback': callback});\n  }\n  \n  unsubscribe(subscriber) {\n    this._subscribers = this._subscribers.filter(subscription => subscription['subscriber'] !== subscriber);\n  }\n  \n  addTransaction(transaction) {\n    if(!transaction.isVerified()) {\n      return;\n    }\n    this._transactions.add(transaction);\n    this._subscribers.forEach(subscription => subscription['callback'](transaction));\n  }\n  \n  invalidateTransactions(block) {\n    block.transactions.forEach(transaction => {\n      this._transactions.remove(transaction);\n    });\n  }\n  \n  async mine() {\n    if (this._blockchainNode.hasExited) {\n      return;\n    }\n    \n    const miningDifficulty = Math.log10(this._blockchainNode.blockchain.size());\n    const miningProof = new MiningProof(miningDifficulty);\n    await miningProof.work();\n    const block = new Block(\n      this._blockchainNode.wallet,\n      this._transactions,\n      miningProof,\n      this._blockchainNode.blockchain.headOfChain.hash\n    );\n    this._transactions = new TransactionCollection();\n    this._blockchainNode.propagateBlock(block);\n    console.log('[BLOCKCHAIN] Successfully mined a new block');\n  }\n}"]}