class BlockChainTestCase(unittest.TestCase): filename = 'test_blockchain.dat' content = ['The quick brown fox jumps over the lazy dog.', 'Another day, another 50 cents.', "All's well that ends well", 'Four score and seven years ago...'] zero_digest = bytes(32) def setUp(self): self.block_chain = BlockChain() self.factory = BlockFactory() def tearDown(self): del self.block_chain del self.factory def test_saveToFile(self): num_contents = len(self.content) for i in range(num_contents): self.createBlock(self.content[i]) block_count = self.block_chain.saveToFile(self.filename) self.assertEqual(block_count, num_contents, 'Incorrect block count') def test_loadFromFile(self): blocks_loaded = self.block_chain.loadFromFile(self.filename) for i in range(blocks_loaded): content = self.block_chain.getBlockContent(i) content_string = content.decode('utf-8') self.assertEqual(self.content[i], content_string, 'Wrong content loaded') def test_walkChain(self): # Load the block chain blocks_loaded = self.block_chain.loadFromFile(self.filename) self.assertTrue(blocks_loaded > 0, "No blocks loaded!") # start with the top digest = self.block_chain.getLastBlockDigest() while digest != self.zero_digest: block = self.block_chain.getBlockWithDigest(digest) content = block.getContent() logging.debug('content: %s; digest: %s', content.decode('utf-8'), digest.hex()) digest = block.getPreviousBlockHash() def createBlock(self, text): content = text.encode() block_object = self.factory.createNew(content) pbhd = self.block_chain.getLastBlockDigest() block_object.farm(pbhd) height = self.block_chain.addBlock(block_object)
PrivateChannel_Network = np.zeros( (size, size)) #Graph of PrivateChannel among users PrivateChannel_Network[:][:] = 999 for i in range(size): PrivateChannel_Network[i][i] = 0 #First Transaction of the server is setting the primary properties in Admin account Base_Trx = Transaction(sender=000, receiver=AdminID, amount=Total_Token, obj=['Equipment', Total_Equipment]) Transactions_que.append(Base_Trx) Transactions_que.append(Admin) #Block's default setting [Genesis] <- [Admin data] GameData = BlockChain() GameData.addBlock(data=Transactions_que) Transactions_que = [] #Create Miner Miner = Miner(MinerID, GameData) def MakeUserAccount(UserID, Users): """ UserID : <int> Users : <dict> Returns account (Account object) """ #Make a Account and for the scenario, #Assume every User has 10 equipment and 10000 tokens user = Account(UserID)
from blockchain import BlockChain myChain = BlockChain() myChain.addBlock("first block") myChain.addBlock("second block") myChain.addBlock("third block") myChain.addBlock("forth block") for block in myChain.getBlocks(): print block.getData() print myChain.getBlock(1).getData()
from blockchain import BlockChain if __name__ == "__main__" : blockchain = BlockChain() firstBlock = blockchain.firstBlock() blockchain.addBlock(firstBlock) for i in range(0, 100): newBlock = blockchain.generateBlock() blockchain.addBlock(newBlock) print "Block #{} Added!".format(newBlock.index) print "Hash: {}\n".format(newBlock.hash)