コード例 #1
0
ファイル: test_node.py プロジェクト: zunbeam/swiftchain
    def test_write_data(self):

        tester_node = Node("Tester")
        blockchain = Blockchain(g_data="Test",
                                node_addr=tester_node.get_node_addr())

        tester_node.write_data(data="Written successfully", chain=blockchain)
        last_block = blockchain.get_last_block()

        self.assertEqual("Written successfully", last_block.get_data())
コード例 #2
0
    def test_get_ledger_size(self):

        tester_node = Node("Tester")
        blockchain = Blockchain()

        self.assertEqual(1, blockchain.get_ledger_size())

        for i in range(100):
            tester_node.write_data(data=str(i), chain=blockchain)

        self.assertEqual(101, blockchain.get_ledger_size())
コード例 #3
0
    def test_get_difficulty(self):

        tester_node = Node("Tester")
        blockchain = Blockchain(diff_threshold=10)

        self.assertEqual(1, blockchain.get_difficulty())

        for i in range(105):
            tester_node.write_data(data=str(i), chain=blockchain, max_tries=15)

        # Expected failure if not all blocks could be mined:
        self.assertTrue(blockchain.get_difficulty() > 9)
コード例 #4
0
    def test_get_blocks_by_range(self):

        tester_node = Node("Tester")
        blockchain = Blockchain()

        for i in range(100):
            tester_node.write_data(data=str(i), chain=blockchain)

        blocks = blockchain.get_blocks_by_range(5)
        data = [block.get_data() for block in blocks]

        self.assertEqual(['95', '96', '97', '98', '99'], data)
コード例 #5
0
ファイル: test_node.py プロジェクト: zunbeam/swiftchain
    def test_read_data_by_range(self):

        tester_node = Node("Tester")
        blockchain = Blockchain(g_data="Test",
                                node_addr=tester_node.get_node_addr())

        for i in range(100):
            tester_node.write_data(data=str(i), chain=blockchain)
        output = tester_node.read_data_by_range(5, chain=blockchain)

        with self.assertRaises(Exception) as context:
            tester_node.read_data_by_range(range=102, chain=blockchain)

        self.assertTrue("exceeds size of ledger." in str(context.exception))
        self.assertEqual(['95', '96', '97', '98', '99'], output)
コード例 #6
0
ファイル: test_node.py プロジェクト: zunbeam/swiftchain
    def test_read_data_by_meta(self):

        tester_node = Node("Tester")
        blockchain = Blockchain()

        for i in range(10):
            tester_node.write_data(data=str(i),
                                   chain=blockchain,
                                   meta_data="Meta " + str(i))

        data = tester_node.read_data_by_meta(meta="Meta 9", chain=blockchain)
        self.assertEqual(["9"], data)

        with self.assertRaises(Exception) as context:
            tester_node.read_data_by_meta(meta="Not contained inside",
                                          chain=blockchain)

        self.assertTrue("Ledger does not contain" in str(context.exception))
コード例 #7
0
ファイル: test_node.py プロジェクト: zunbeam/swiftchain
    def test_get_blocks_by_meta(self):

        tester_node = Node("Tester")
        blockchain = Blockchain()

        for i in range(10):
            tester_node.write_data(data=str(i),
                                   chain=blockchain,
                                   meta_data="Meta " + str(i))

        blocks = tester_node.get_blocks_by_meta(meta="Meta 2",
                                                chain=blockchain)

        with self.assertRaises(Exception) as context:
            tester_node.get_blocks_by_meta(meta="Not contained inside",
                                           chain=blockchain)

        self.assertTrue("Ledger does not contain" in str(context.exception))
        self.assertEqual("2", blocks[0].get_data())
コード例 #8
0
ファイル: test_node.py プロジェクト: zunbeam/swiftchain
    def test_get_block_by_index(self):

        tester_node = Node("Tester")
        blockchain = Blockchain()

        for i in range(10):
            tester_node.write_data(data=str(i),
                                   chain=blockchain,
                                   meta_data="Meta " + str(i))

        block = tester_node.get_block_by_index(index=7, chain=blockchain)

        self.assertEqual('6', block.get_data())

        with self.assertRaises(Exception) as context:
            tester_node.get_block_by_index(index=22, chain=blockchain)

        self.assertTrue("Requested index exceeds size of ledger." in str(
            context.exception))
コード例 #9
0
    def test_find_consensus(self):

        tester_node = Node("Tester")

        blockchain1 = Blockchain(diff_threshold=10, g_data="Fiat Lux!")
        blockchain2 = Blockchain(diff_threshold=7, g_data="Fiat Lux!")

        for i in range(100):
            tester_node.write_data(data=str(i), chain=blockchain1)

        for i in range(80):
            tester_node.write_data(data=str(i), chain=blockchain2)

        # Blockchain 2 has a higher difficulty threshold, thus it will have a
        # higher cumulative proof-of-work despite mining fewer blocks:
        self.assertFalse(blockchain2.find_consensus(blockchain1))
        self.assertTrue(blockchain1.find_consensus(blockchain2))
        self.assertEqual(blockchain1.get_last_block(),
                         blockchain2.get_last_block())
コード例 #10
0
from swiftchain import Blockchain, Node

# Create a node, which will mine blocks on the given chains:
user = Node("Tester")

# Create a blockchain with a difficulty threshold of 10,
# meaning that the difficulty is raised every 10 blocks.
first_chain = Blockchain(diff_threshold=10,
                         g_data="First Blockchain",
                         node_addr=user.get_node_addr())

# Create a blockchain with a difficulty threshold of 7,
# meaning that the difficulty is raised every 7 blocks.
second_chain = Blockchain(diff_threshold=7,
                          g_data="Second Blockchain",
                          node_addr=user.get_node_addr())

# Mine 100 blocks on the first chain and 80 blocks on the second chain:
for i in range(100):
    if i < 80: user.write_data(data=str(i), chain=second_chain)
    user.write_data(data=str(i), chain=first_chain)

print(
    f"Last Block ID of first chain (before consensus): {first_chain.get_last_block().get_block_id()}"
)
print(
    f"Last Block ID of second chain (before consensus): {second_chain.get_last_block().get_block_id()}\n"
)

print("Finding consensus...\n")
コード例 #11
0
ファイル: test_node.py プロジェクト: zunbeam/swiftchain
    def test_get_node_name(self):

        tester_node = Node("Tester")
        self.assertEqual("Tester", tester_node.get_node_name())
コード例 #12
0
from swiftchain import Blockchain, Node

# Create a Node with some user name.
# The hash generated from this user name is used as the Node address:
user = Node("Some user name to be hashed")

# Create a blockchain with a difficulty threshold of 7,
# meaning that the difficulty is increased every 7 blocks.
chain = Blockchain(diff_threshold=7,
                   g_data="Fiat Lux!",
                   node_addr=user.get_node_addr())

# Mine 100 blocks on the above chain.
# The amount of time this takes will vary depending on your system,
# with some degree of luck thrown in.
for i in range(100):
    user.write_data(data="Hello " + str(i), chain=chain)

# Print the content of the last 5 blocks onto the screen:
print(user.read_data_by_range(5, chain=chain))

# Output: ['Hello 95', 'Hello 96', 'Hello 97', 'Hello 98', 'Hello 99']