def is_valid_chain(chain): prev = bk.Block('1', '1', '1') prev.hash = '0' * 64 for b in chain: bv.validate(b) if prev != None: assert prev.hash == b.previous_hash, "Invalid blockchain!" prev = b return True
def test_blockchain(self): t = tn.CoinbaseTransaction(wt.pubToAddress(wt.privToPub(pk))) t.sign(pk) genesis_block = bk.Block(time.time(), '0' * 64, s.Serializer.serialize(t)) genesis_block.mine(2) bv.validate(genesis_block) '''genesis''' tr = tn.Transaction(wt.pubToAddress(wt.privToPub(pk)), '12pULxeW59imr1mWs8AjQogji8H77KD3KT', 5) tr.sign(pk) sr = s.Serializer.serialize(tr) lst = [] lst.append(s.Serializer.serialize(t)) lst.append(sr) lst.append(sr) lst.append(sr) b1 = bk.Block(time.time(), genesis_block.hash, lst) b1.mine(2) bv.validate(b1) b2 = bk.Block(time.time(), b1.hash, lst) b2.mine(2) bv.validate(b2) chain = [] chain.append(genesis_block) chain.append(b1) chain.append(b2) #b2.timestamp = "1241" self.assertTrue(is_valid_chain(chain)) chain
def new_block(): global BLOCKCHAIN if request.method == 'POST': b = request.get_json() #d = json.loads(b.decode('utf-8')) blck = to_object(b) if block_validator.validate(blck): BLOCKCHAIN.append(blck) "[TODO] Remove transactions from mempool" "[TODO] Remove spended utxo and added new" with open('blocks/' + '%04d' % int(BLOCKCHAIN.index(blck)) + '.block', 'w') as outfile: json.dump(b, outfile) return 'Added'
def test_genesis_block(self): t = tn.CoinbaseTransaction(wt.pubToAddress(wt.privToPub(pk))) t.sign(pk) b = bk.Block('123142.124', '0' * 64, s.Serializer.serialize(t)) b.mine(2) bv.validate(b)