def rollback_tx_metadata(state: State, block, batch): fee_reward = 0 for protobuf_txn in block.transactions: txn = Transaction.from_pbdata(protobuf_txn) fee_reward += txn.fee TransactionMetadata.remove_tx_metadata(state, txn, batch) txn = Transaction.from_pbdata(block.transactions[0]) # Coinbase Transaction state._update_total_coin_supply(fee_reward - txn.amount, batch) LastTransactions._remove_last_tx(state, block, batch)
def update_tx_metadata(state: State, block, batch) -> bool: fee_reward = 0 for protobuf_txn in block.transactions: txn = Transaction.from_pbdata(protobuf_txn) fee_reward += txn.fee if not TransactionMetadata.put_tx_metadata(state, txn, block.block_number, block.timestamp, batch): return False txn = Transaction.from_pbdata(block.transactions[0]) # Coinbase Transaction state._update_total_coin_supply(txn.amount - fee_reward, batch) LastTransactions._update_last_tx(state, block, batch) return True
def test_get_last_txs(self): self.assertEqual(LastTransactions.get_last_txs(self.state), []) alice_xmss = get_alice_xmss() block = Block() tx1 = TransferTransaction.create( addrs_to=[get_some_address(0), get_some_address(1)], amounts=[1, 2], message_data=None, fee=0, xmss_pk=alice_xmss.pk) block._data.transactions.extend([tx1.pbdata]) LastTransactions._update_last_tx(self.state, block, None) last_txns = LastTransactions.get_last_txs(self.state) # Test Case: When there is only 1 last txns self.assertEqual(len(last_txns), 1) self.assertEqual(last_txns[0].to_json(), tx1.to_json())
def get_last_txs(self): try: last_txn = LastTransactions.deserialize( self._db.get_raw(b'last_txn')) except: # noqa return [] txs = [] for tx_metadata in last_txn.tx_metadata: data = tx_metadata.transaction tx = Transaction.from_pbdata(data) txs.append(tx) return txs
def _remove_last_tx(self, block, batch): if len(block.transactions) == 0: return try: last_txn = LastTransactions.deserialize( self._db.get_raw(b'last_txn')) except: # noqa return for protobuf_txn in block.transactions: txn = Transaction.from_pbdata(protobuf_txn) i = 0 while i < len(last_txn.tx_metadata): tx = Transaction.from_pbdata( last_txn.tx_metadata[i].transaction) if txn.txhash == tx.txhash: del last_txn.tx_metadata[i] break i += 1 self._db.put_raw(b'last_txn', last_txn.serialize(), batch)
def _update_last_tx(self, block, batch): if len(block.transactions) == 0: return last_txn = LastTransactions() try: last_txn = LastTransactions.deserialize( self._db.get_raw(b'last_txn')) except: # noqa pass for protobuf_txn in block.transactions[-20:]: txn = Transaction.from_pbdata(protobuf_txn) if isinstance(txn, CoinBase): continue last_txn.add(txn, block.block_number, block.timestamp) self._db.put_raw(b'last_txn', last_txn.serialize(), batch)