def propose(self): proposer = self.cm.proposer(self.height, self.round) self.log('in propose', proposer=proposer, proposal=self.proposal, lock=self.lock) if proposer != self.cm.coinbase: return if self.proposal: assert self.proposal.signature.address == self.cm.coinbase assert self.lock return lockset = self.cm.last_valid_lockset self.log('in creating proposal', lockset=lockset) if self.round == 0 or lockset.has_noquorum: cl = self.cm.last_committing_lockset # quorum which signs prev block assert cl.has_quorum block = Block(self.cm.coinbase, self.hm.height, self.round, self.cm.head.hash, cl) proposal = BlockProposal(self.signature(), lockset, block) elif lockset.has_quorum_possible: bh = lockset.has_quorum_possible proposal = VotingInstruction(self.signature(), lockset, bh) else: raise Exception('invalid lockset') self.log('created proposal', p=proposal) self.proposal = proposal return proposal
def test_comparisons(): s11 = Signature(0, 1, 1) s12 = Signature(0, 1, 2) s21 = Signature(0, 2, 1) s22 = Signature(0, 2, 2) assert s22.hr > s21.hr > s12.hr > s11.hr assert s11.hr == s11.hr assert s12.hr > s11.hr assert s12.hr >= s11.hr assert s21.hr > s11.hr assert s21.hr >= s11.hr assert s21.hr > s12.hr assert not s21.hr <= s12.hr assert s22.hr > s21.hr assert s22.hr == s22.hr assert s22.hr >= s22.hr m11 = SignedMessage(s11) m12 = SignedMessage(s12) m21 = SignedMessage(s21) m22 = SignedMessage(s22) assert m22.hr > m21.hr > m12.hr > m11.hr def mk_ls(h, r): ls = LockSet() votes = [ Locked(Signature(i, h, r), '0' * 32) for i in range(ls.eligible_votes) ] for i, v in enumerate(votes): ls.add(v) return ls ls11, ls12, ls21, ls22 = (mk_ls(*s.hr) for s in (s11, s12, s21, s22)) assert ls22.hr > ls21.hr > ls12.hr > ls11.hr block = Block(1, 2, 2, '0' * 32, ls12) assert block bp = BlockProposal(s22, ls21, block) bp2 = BlockProposal(s22, ls21, block) assert bp.hash == bp2.hash assert bp == bp2 assert bp in set([bp2]) assert bp.hr == block.hr assert block.hr > ls12.hr assert block.hr == s22.hr assert bp.hr == ls22.hr l = Locked(s22, bp.hash) l2 = Locked(s22, bp.hash) assert l.hr == l2.hr assert l in set([l2])
def test_BlockProposal(): ls = LockSet() for i in range(10): s = Signature(i, 2, 3) ls.add(Locked(s, '0' * 32)) bls = LockSet() for i in range(10): s = Signature(i, 1, 2) bls.add(Locked(s, '0' * 32)) assert len(ls) == 10 assert ls.has_quorum block = Block(1, 2, 4, '0' * 32, bls) p = BlockProposal(Signature(1, 2, 4), ls, block) p2 = BlockProposal(Signature(1, 2, 4), ls, block) assert p == p2
def __init__(self, block_id, **props): Block.__init__(self, block_id, **props) self.type = "snippet"
def __init__(self, block_id, **props): Block.__init__(self, block_id, **props) self.type = "rssfeed"
def __init__(self, block_id, **props): Block.__init__(self, block_id, **props) self.type = "ext_content"
def __init__(self, k=1): Block.__init__(self) self.changek = k
def __init__(self, out=2): Block.__init__(self) self._changeOut = out
def start_simulation(self): mempool = Block(max_gas_size=self.max_block_size) blockchain = [] current_block = Block(max_gas_size=self.max_block_size) while not self.auction.is_end_of_auction(): # 0) check block time due if self.auction.is_block_time_due(): if self.debug: logging.info( 'Block executed: block %d, block gas %d, total fees %d' % (self.auction.current_block_number(), current_block.total_gas(), self.auction.block_price(current_block))) self.auction.execute_block() blockchain.append(current_block) current_block = Block(max_gas_size=self.max_block_size) # 1) tx recv #for _ in range(self.txs_congestion_ratio): tx = self.txs.next_transaction() mempool.add_tx(tx) wait_secs = self.txs.next_wait_time_ms() #/ self.txs_congestion_ratio self.auction.sleep_wait_virtual(wait_secs) if self.debug: logging.info('transaction: '+str(tx)) logging.info('new mempool size: %d' % len(mempool)) logging.info('wait time seconds:'+str(wait_secs)) # 2) optional, choose new tx for block mempool, chosen_tx = self.proposer.choose_tx_and_remove(mempool) logging.info('chosen tx: ' + str(chosen_tx)) if chosen_tx and current_block.can_add_tx_size(chosen_tx): if chosen_tx: current_block.add_tx(chosen_tx) if self.debug: logging.info('chosen tx added to block #%d, new number of txs: %d' % (self.auction.current_block_number(),len(current_block))) logging.info('block filled %.2f percent' % (100*current_block.total_gas() / self.max_block_size)) if chosen_tx and not current_block.can_add_tx_size(chosen_tx): # going back to mempool mempool.add_tx(chosen_tx)
def __init__(self, k=1): Block.__init__(self)
def __init__(self, block_id, **props): Block.__init__(self, block_id, **props) self.type = "image"
def __init__(self, k=1): Block.__init__(self) self.inp = 2 self._changeInput = self.inp
def __init__(self): Block.__init__(self) self.changeConstant = 1 self.inp = 0 self.inp_signals = [self.changeConstant] # значение по умолчанию
def __init__(self): Block.__init__(self) self.pure = False self.x = [] self.y = [] self.out = 0
def test_Block(): genesis = mk_genesis(range(10)) b1 = Block(1, 2, 3, genesis.hash, genesis.lockset) b2 = Block(1, 2, 3, genesis.hash, genesis.lockset) assert b1 == b2