示例#1
0
 def NewCoinbaseTX(self,dest):
     #创建基本交易
     # subsidy = 50  # 每次获得的奖励
     data = "Genesis Block Of ATCoin"
     tx = self.mineTran(data,dest)
     mer = MerkleTree.Merkle([tx])  #创世区块,里面只包含一个交易信息,也就是挖矿奖励,不包含任何的输出,典型的先有鸡操作。
     return mer
示例#2
0
    def dataReceived(self, data):
        """
        :param
        data: 接收交易的的文本
        :接收文本 - -->处理文本 - -->发送文本
        发送给客户端最后的文本
        """
        if data:
            content = data.decode("utf-8")
            if content[-5:] != "Finsh":
                self._data_buffer = self._data_buffer + content
            else:
                content = self._data_buffer + content[:-5]

                if content[0:14] == "Initialization":
                    if content.split("|")[1] == Utils().addrversion:
                        self.senData("OK")
                    else:
                        self.senData("False")

                elif content[0:4] == "Tran":
                    stc = PickleStr.toObj((content[4:]))
                    trans = MerkleTree.verifyTran([stc])  #对交易进行验证,防止错误交易蔓延
                    if trans:
                        db = Conlmdb("tranpool")
                        for tran in trans:
                            db.put(tran.ID, tran)
                        self.senData("GetTran")
                    else:
                        self.senData("ErrorTran")
                else:
                    Log.Error("Server TTSS Invalid data(" + self.clnt + "): ")
示例#3
0
 def verify_deployment(self, dest):
     try:
         for item in os.listdir(self.deploysource):
             obj = mt.Diff(dest + "/" + item,
                           self.deploysource + "/" + item)
             obj.make_trees()
             return obj.valid
     except Exception as e:
         print e
         return 0
示例#4
0
 def addblock(self):
     """
     添加区块
     data中含有
     source:来源地址
     dest:目的地址
     amount:转账数量
     """
     #添加区块
     db = Conlmdb("tranpool")
     mer = None
     tsize = db.dbsize()
     keys = []#存储正在被挖矿的交易
     if tsize >= 5:
         dtrans = db.rawiter()
         trans = []
         for key,value in dtrans.items():
             keys.append(key)
             trans.append(PickleStr.toObj(value))
             if value[:1] != b"U":
                 db.set(key, b"U"+value)
         trans.append(self.mineTran("520",Wallet().Address))
         mer = MerkleTree.Merkle(trans).getresult()
     if mer:
         block = Block(mer,self.db.get("l"))
         if POW().VailPow(block):
             self.db.put(block.Headers.gethash(), str(block))
             self.db.delete("l")
             self.db.put("l", block.Headers.gethash())
             for key in keys:
                 #删除挖矿成功的交易
                 db.delete(key)
         else:
             pass
     else:
         pass
示例#5
0
__author__ = 'pramodkumar'
import sys
import MerkleTree as pr
import os
with open("Output.txt","w") as f:
    sys.stdout=f
    obj = pr.Diff(os.getcwd()+'/f1.txt', os.getcwd()+'/f3.txt')
    obj.make_trees()

示例#6
0
 def BuildTxRoot(self):
     tree = MerkleTree.MerkleTree()
     return tree.CreateTree(self.transactions)
# CS266: Fall 2019
# HW1
#
# Pratik Prajapati
# Ashraf Saber
#

import MerkleTree as mtree
import Block as blk

# a test script to check various functions of the Block() class.
#
txList = ['alice', 'carol', 'duck', 'bob']

txList.sort()
m = mtree.MerkleTree(txList)
m.generateTree()

# just any random hash to test
prevHash = '9f9d51bc70ef21ca5c14f307980a29d8'

b = blk.Block(prevHash, m)
blockHash = b.mineBlock()
print('mined nonce = %s' % (blockHash))
# CS266: Fall 2019
# HW1
#
# Pratik Prajapati
# Ashraf Saber
#

import ECC
import MerkleTree as mtree

# script to test various functions of Merkle Tree class
# validateProof() is special function which would validate the proof generated by getMembershipProof()

data_list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']

m = mtree.MerkleTree(data_list)
m.generateTree()
m.postOrderPrintTree()
# print(m.getRootHash())
data = ECC.hash('f')
path = m.getMembershipProof(data)

if path is not None:
    print('path = ' + str(path))
else:
    print('membership failed')

isValid = m.validateProof(path)
if isValid:
    print('membership path is valid')
else:
示例#9
0
    res = keynum
    keynum += 1
    return [str(res)+"x",str(res)+"y"]

def S(m, x):
    return m + "|C:|" + x

def V(m, sig, y):
    num = ""
    i = 0
    while i < len(y)-1:
        num += y[i]
        i += 1
    return sig == m + "|C:|" + num + "x"
# Generar un MerkleTree
Merky = MerkleTree(4,[K, S, V],mihash)
# Recuperar su raíz
root = Merky.get_pk()

# Funcion para imprimir (de forma muy simplifista) el árbol
def printTree(roo):
    if roo.getIzq() != None: printTree(roo.getIzq())
    print "(" + str(roo.getAltura()) + " - " + roo.getElement() + ") "
    if roo.getDer() != None: printTree(roo.getDer())

print Merky.sk
printTree(root)
print "\n\n"
# Obtener verificador (el firmador es parte del árbol, el verificador es un objeto aparte)
very = Merky.get_verifier()
mens = "mensaje"