def __init__(self, previoushash, list_of_transactions=None): self.version = 1 self.previousHash = previoushash self.transactions = list_of_transactions self.hashPreviousBlock = double_hash( encode_and_concenate(previoushash)) self.TimeStamp = date.datetime.now() self.bits = 0x207fffff self.nonce = 0
def __init__(self, index, header, list_of_transactions): self.MagicNumber = 0xD9B4BEF9 self.blocksize = self.__sizeof__() self.BlockHeader = header self.index = index self.TransactionCounter = len(list_of_transactions) self.Transactions = list_of_transactions self.to_dsha = encode_and_concenate([self.BlockHeader.hashPreviousBlock, self.BlockHeader.TimeStamp, self.BlockHeader.bits, self.BlockHeader.nonce, self.BlockHeader.hashMerkleRoot]) self.BlockHash = double_hash(self.to_dsha)
def __init__(self, header, target): self.MagicNumber = 0xD9B4BEF9 self.transaction_hashes = [] self.transactions = [] self.hash_prev_block = header.hashPreviousBlock self.hash_merkle_block = None #gets updated later once transactions are added self.target = target self.nonce = header.nonce self.to_dsha = encode_and_concenate([header.hashPreviousBlock, header.TimeStamp, header.bits, header.nonce]) self.BlockHash = double_hash(self.to_dsha)
def __init__(self, previoushash, list_of_transactions): self.version = 1 self.previousHash = previoushash self.hashPreviousBlock = double_hash( encode_and_concenate(previoushash)) self.hashMerkleRoot = merkle.MerkleTreeHash([ transaction.TransactionHash for transaction in list_of_transactions ], math.ceil(math.log2(len(list_of_transactions)))) self.TimeStamp = date.datetime.now() self.bits = 0 self.nonce = 0
def __init__(self, inputs=None, outputs=None): self.version = 1 self.ListOfInputs = ['input1', 'input2', 'other' ] if inputs is None else inputs self.ListOfOutputs = ['output', 'output_second', 'other2' ] if outputs is None else outputs self.InCounter = len(self.ListOfInputs) self.OutCounter = len(self.ListOfOutputs) self._string_to_hash = encode_and_concenate( [self.version, self.InCounter, self.OutCounter] + self.ListOfOutputs + self.ListOfInputs) self.TransactionHash = double_hash(self._string_to_hash)
def mine_this_block(self): current_block_hash = double_hash(encode_and_concenate(self.update())) print('current block hash = {}, target hash = {}'.format(current_block_hash, self.target)) #print("current block decimal = {} target decimal = {}".format(int(current_block_hash, 16), int(self.target, 16))) if int(current_block_hash, 16) < int(self.target, 16): print('Block was mined! You\'ve been rewarded {} ECC!'.format(20)) self.transactions[0].set_current_total(20) print("current EchoCoin total: {}".format(self.transactions[0].current_total)) return True else: self.nonce += 1 return False
def __init__(self, output, inputs=None): self.version = 1 self.ListOfInputs = ['input1', 'input2', 'other' ] if inputs is None else inputs self.ListOfOutputs = [ output.value, output.index, output.script ] if output is not None else ['a' + str(random())] self.InCounter = len(self.ListOfInputs) self.OutCounter = len(self.ListOfOutputs) self._string_to_hash = encode_and_concenate( [self.version, self.InCounter, self.OutCounter] + self.ListOfOutputs + self.ListOfInputs) self.TransactionHash = double_hash(self._string_to_hash)
def miner(transaction_pool): last_block_header = '0'*64 # genesis header last_block_target = '%064x' % (0x7ffff * 2 ** (8 * (0x20 - 3))) #uncomment the line below for harder difficulty #last_block_target = '000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff' # init the block chains block_chain = BlockChain() block = Block(Header(last_block_header), last_block_target) # use variable to keep track of where in the pool we left off reading from # so that we're not going to re-mine the same block left_off_in_pool = 0 # add coinbase transaction block.add_transaction(CoinbaseTransaction(0)) for i in range(1, 9): block.add_transaction(transaction_pool.transactions[i]) left_off_in_pool = i # now that our block is full, we can start to mine it. while not block.mine_this_block(): continue block_chain.add_block(block) print("current blockchain height ", block_chain.size_of_chain()) sleep(5) # our new header for block 2 will need the hash of block 1 last_block_header = double_hash(encode_and_concenate([block_chain.latest_block().BlockHash])) block_2 = Block(Header(last_block_header), last_block_target) block_2.add_transaction(CoinbaseTransaction(block.transactions[0].current_total)) for i in range(left_off_in_pool+9): block_2.add_transaction(transaction_pool.transactions[i]) left_off_in_pool = i # now that our block is full, we can start to mine it. while not block_2.mine_this_block(): continue block_chain.add_block(block_2) print("current blockchain height ", block_chain.size_of_chain()) print() for i, block_added in enumerate(block_chain.blocks): print('Block #{} was added. It took {} steps to find it.'.format(i, block_added.nonce))
def add_transaction(self, new_transac): if not self.block_is_full(): self.transaction_hashes.append(new_transac.TransactionHash) self.transactions.append(new_transac) self.hash_merkle_block = double_hash(encode_and_concenate(str('-'.join(self.transaction_hashes))))