{"version":3,"sources":["https://lively-kernel.org/lively4/lively4-stable/src/blockchain/model/blockchainNode/networkComponent.js"],"names":["cloneDeep","NETWORK_LATENCY","NetworkComponent","constructor","node","_node","_addSelfToGlobalListOfPeers","getBlockchainLength","blockchain","size","getLatestBlockDate","headOfChain","timestamp","requestBlockchain","relevantBlockchainProvider","peers","forEach","peer","receiveBlockchain","_blockchain","Array","push","_simulateNetwork","sendToDestinationSocket","Promise","sleep","setTimeout","propagateTransaction","transaction","receiveTransaction","bind","propagateBlock","block","receiveBlock","handleBlock","handleTransaction","handleBlockchain"],"mappings":";;;;;;AAASA,e,8BAAAA,S;;;;;;;;;;;;;;;;;;;;AAET,YAAMC,kBAAkB,CAAxB;;;;;;;;;;;;;;;AAEe,YAAMC,gBAAN,CAAuB;;AAEpCC,oBAAYC,IAAZ,EAAkB;AAChB,eAAKC,KAAL,GAAaD,IAAb;AACA,eAAKE,2BAAL;AACD;;AAEDC,8BAAsB;AACpB,iBAAO,KAAKF,KAAL,CAAWG,UAAX,CAAsBC,IAAtB,EAAP;AACD;;AAEDC,6BAAqB;AACnB,iBAAO,KAAKL,KAAL,CAAWG,UAAX,CAAsBG,WAAtB,CAAkCC,SAAzC;AACD;;AAEDC,4BAAoB;AAClB;AACA,cAAIC,6BAA6B,IAAjC;AACAZ,2BAAiBa,KAAjB,CAAuBC,OAAvB,CAA+BC,QAAQ;AACrC,gBAAGA,SAAS,IAAZ,EAAkB;AAChB,kBAAIH,8BAA8B,IAA9B,IAAsCA,2BAA2BP,mBAA3B,KAAmDU,KAAKV,mBAAL,EAAzF,IACGO,2BAA2BP,mBAA3B,MAAoDU,KAAKV,mBAAL,EAApD,IACIO,2BAA2BJ,kBAA3B,KAAkDO,KAAKP,kBAAL,EAF7D,EAIK;AACHI,6CAA6BG,IAA7B;AACD;AACF;AACF,WAVD;AAWA,cAAG,CAACH,0BAAJ,EAAgC;AAC9B;AACD;AACD;AACA,eAAKI,iBAAL,CAAuBlB,UAAUc,2BAA2BT,K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ACD;AACF;;AApFmC;;yBAAjBX,gB","file":"networkComponent.js","sourcesContent":["import { cloneDeep } from 'src/external/lodash/lodash.js';\n\nconst NETWORK_LATENCY = 1;\n\nexport default class NetworkComponent {\n  \n  constructor(node) {\n    this._node = node;\n    this._addSelfToGlobalListOfPeers();\n  }\n  \n  getBlockchainLength() {\n    return this._node.blockchain.size();\n  }\n  \n  getLatestBlockDate() {\n    return this._node.blockchain.headOfChain.timestamp;\n  }\n  \n  requestBlockchain() {\n    // select random peer\n    let relevantBlockchainProvider = null;\n    NetworkComponent.peers.forEach(peer => {\n      if(peer !== this) {\n        if (relevantBlockchainProvider == null || relevantBlockchainProvider.getBlockchainLength() < peer.getBlockchainLength()\n           || (relevantBlockchainProvider.getBlockchainLength() == peer.getBlockchainLength()\n                && relevantBlockchainProvider.getLatestBlockDate() < peer.getLatestBlockDate()\n              )\n           ) {\n          relevantBlockchainProvider = peer;\n        } \n      }\n    });\n    if(!relevantBlockchainProvider) {\n      return;\n    }\n    // request blockchain\n    this.receiveBlockchain(cloneDeep(relevantBlockchainProvider._node._blockchain));\n    //this._simulateNetwork(() => peer.provideBlockchain(this));\n  }\n  \n  // simplified Peer-Handling: All peers are known via central source of truth\n  _addSelfToGlobalListOfPeers() {\n    if(NetworkComponent.peers == null) {\n      NetworkComponent.peers = new Array();\n    }\n    NetworkComponent.peers.push(this);\n  }\n  \n  async _simulateNetwork(sendToDestinationSocket) {\n    await new Promise(sleep => setTimeout(sleep, NETWORK_LATENCY * 1000));\n    sendToDestinationSocket();\n  }\n  \n  // Sending to peers\n  \n  propagateTransaction(transaction) {\n    NetworkComponent.peers.forEach(peer => {\n      if(peer !== this) {\n        this._simulateNetwork(peer.receiveTransaction.bind(peer, cloneDeep(transaction)));\n      }\n    });\n  }\n  \n  propagateBlock(block) {\n    NetworkComponent.peers.forEach(peer => {\n      if(peer !== this) {\n        this._simulateNetwork(peer.receiveBlock.bind(peer, cloneDeep(block)));\n      }\n    });\n  }\n  \n  // Receiving from peers\n  \n  receiveBlock(block) {\n    this._node.handleBlock(block);\n  }\n  \n  receiveTransaction(transaction) {\n    this._node.handleTransaction(transaction);\n  }\n  \n  receiveBlockchain(blockchain) {\n    if(blockchain) {\n      this._node.handleBlockchain(blockchain);\n    } else {\n      this.requestBlockchain();\n    }\n  }\n  \n}"]}