繁体   English   中英

如何在 Python 中简单地为区块链实现内存池?

[英]How to simply implement a mempool for a blockchain in Python?

我目前正在关注一些关于如何使用 python 构建自己的区块链的教程,但我一直在尝试在将它们添加到块之前创建一个内存池(或者,只是在某个地方存储交易),但我意识到我的方法会发送太多请求并使我的服务器运行缓慢。

有什么办法可以实现“内存池”吗? 谢谢!

当您创建 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 = []

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM