用于双重支出和签名验证的区块链教程



我一直在从基础上研究区块链,并逐行研究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

我认为Transaction类中应该有一种验证函数。。。但是不太确定该怎么办。我希望看到一些关于如何处理这件事的绝妙想法。

您可以从技术上"尝试花费";你想多少次硬币就多少次。但是,只有一个事务可以添加到块中,其他事务将被拒绝。剩余的事务也不能添加到较新的块中,因为输出不再是UTXO。

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

最新更新