How to simply implement a mempool for a blockchain in Python?

Viewed 27

I'm currently following along a few tutorials on how to build my own blockchain with python, but I've been trying to make a mempool (or, just somewhere to store transactions) before adding them to the block but I realized my method would send too much requests and make my server run slow.

Is there any way how I can implement a "mempool"? Thanks!

1 Answers

when you create a class

class Blockchain:
    def __init__(self, public_key, node_id):
        # evey blockchain starts with a genesis block
        self.__chain=[genesis_block]
        #  this is called transactions_pool
        self.mem_pool=[]
       
        # set other data

   def add_transaction(self):
      # create a transaction
      transaction = Transaction() # whatever args it takes
      # verify the transaction
      # then add it to mem_pool
      self.mem_pool.append(transaction)
 

  def mine_block(self):
      copied_transactions = self.mem_pool[:]
      # then run the logic to mine block
      # after successfully mined the block, set mem_pool as empty
       self.mem_pool = []
Related