用于双重消费和签名验证的区块链教程

2024-05-29 05:53:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在从基础开始研究区块链,并逐行研究TPCoin python代码。 在查看代码时,我发现没有任何验证方法可以防止双重支出问题或拒绝具有无效签名的事务请求

我正在学习以下文章: https://morioh.com/p/7bfc126c22c2

class Transaction:

    def __init__( self, sender, recipient, value ):
        self.sender = sender 
        self.recipient = recipient 
        self.value = value
        self.time = datetime.datetime.now()
        self.signer = ""
    
    def to_dict( self ):

        if self.sender == "Genesis":
            identity = "Genesis"
        else:
            identity = self.sender.identity

        return collections.OrderedDict( { 'sender': identity, 'recipient': self.recipient, 'value': self.value, 'time' : self.time } )

    def sign_transaction( self ):

        private_key = self.sender._private_key
        signer = PKCS1_v1_5.new( private_key )
        h = SHA.new( str( self.to_dict( ) ).encode( 'utf8' ) )
        self.signer = binascii.hexlify( signer.sign( h ) ).decode( 'ascii' )
        return self.signer

    def display_transaction( self ):
        dict = self.to_dict( )
        print ("sender: " + dict['sender'])
        print ('-----')
        print ("recipient: " + dict['recipient'])
        print ('-----')
        print ("value: " + str(dict['value']))
        print ('-----')
        print ("time: " + str(dict['time']))
        print ('-----')
        print ("signature: " + self.signer)
        print ('-----')

    def validate_transaction( self ):
        ### Double spending? Signature Verification?
        return

我认为在事务类中应该有一种验证函数……但是 不太清楚该怎么办。 我想看到一些关于如何处理这个问题的好主意


Tags: tokeyselfreturntimevaluedefprivate
1条回答
网友
1楼 · 发布于 2024-05-29 05:53:22

从技术上讲,你可以“尝试”花掉你想要的硬币。但是,只有一个事务可以添加到块中,其他事务将被拒绝。剩余事务也不能添加到较新的块中,因为输出不再是UTXO

棘手的是,当两个不同的矿工从同一个输出中抓取两个不同的事务时,存在网络延迟,因此他们都将块添加到自己的链中。在这一点上,您需要了解最长链的概念,了解孤立链、为什么我们要开始确认、工作证明等

相关问题 更多 >

    热门问题