import hashlib
class Vblock:
def __init__(self, data, pbh):
self.data = data
self.pbh = pbh
self.vbh = self.calculate_vbh()
def calculate_vbh(self):
return hashlib.sha256((self.data + self.pbh).encode()).hexdigest()
# 創建Vchain
vblock1 = Vblock("Transaction 1 verified", "0")
vblock2 = Vblock("Transaction 2 verified", vblock1.vbh)
vblock3 = Vblock("Transaction 3 verified", vblock2.vbh)
# 驗證Vblock 3的完整性
def verify_vblock(vblock, prev_vblock):
if vblock.pbh == prev_vblock.vbh:
print("Verification successful. Vblock", vblock.data, "is intact.")
else:
print("Verification failed. Vblock", vblock.data, "may have been tampered.")
verify_vblock(vblock3, vblock2)
aW1wb3J0IGhhc2hsaWIKCmNsYXNzIFZibG9jazoKICAgIGRlZiBfX2luaXRfXyhzZWxmLCBkYXRhLCBwYmgpOgogICAgICAgIHNlbGYuZGF0YSA9IGRhdGEKICAgICAgICBzZWxmLnBiaCA9IHBiaAogICAgICAgIHNlbGYudmJoID0gc2VsZi5jYWxjdWxhdGVfdmJoKCkKCiAgICBkZWYgY2FsY3VsYXRlX3ZiaChzZWxmKToKICAgICAgICByZXR1cm4gaGFzaGxpYi5zaGEyNTYoKHNlbGYuZGF0YSArIHNlbGYucGJoKS5lbmNvZGUoKSkuaGV4ZGlnZXN0KCkKCiMg5Ym15bu6VmNoYWluCnZibG9jazEgPSBWYmxvY2soIlRyYW5zYWN0aW9uIDEgdmVyaWZpZWQiLCAiMCIpCnZibG9jazIgPSBWYmxvY2soIlRyYW5zYWN0aW9uIDIgdmVyaWZpZWQiLCB2YmxvY2sxLnZiaCkKdmJsb2NrMyA9IFZibG9jaygiVHJhbnNhY3Rpb24gMyB2ZXJpZmllZCIsIHZibG9jazIudmJoKQoKIyDpqZforYlWYmxvY2sgM+eahOWujOaVtOaApwpkZWYgdmVyaWZ5X3ZibG9jayh2YmxvY2ssIHByZXZfdmJsb2NrKToKICAgIGlmIHZibG9jay5wYmggPT0gcHJldl92YmxvY2sudmJoOgogICAgICAgIHByaW50KCJWZXJpZmljYXRpb24gc3VjY2Vzc2Z1bC4gVmJsb2NrIiwgdmJsb2NrLmRhdGEsICJpcyBpbnRhY3QuIikKICAgIGVsc2U6CiAgICAgICAgcHJpbnQoIlZlcmlmaWNhdGlvbiBmYWlsZWQuIFZibG9jayIsIHZibG9jay5kYXRhLCAibWF5IGhhdmUgYmVlbiB0YW1wZXJlZC4iKQoKdmVyaWZ5X3ZibG9jayh2YmxvY2szLCB2YmxvY2syKQ==