Exemplo n.º 1
0
class TestTransaction(TestCase):
    def setUp(self):
        private_key = "685cf62751cef607271ed7190b6a707405c5b07ec0830156e748c0c2ea4a2cfe"
        out = {"38fb65b08416b9870067b6cba63fa50a81bc78c8": 100}
        self.transaction = Transaction(1, out)
        self.transaction.sign(private_key)

    def test_id(self):
        self.assertEqual(
            self.transaction.id(),
            "d1ed0b9ab80eb6dcacb8d54cc164700e34a1950fbe0589a181b158568f7c4041")

    def test_address(self):
        self.assertEqual(self.transaction.address(),
                         "c70f4891d2ce22b1f62492605c1d5c2fc1a8ef47")

    def test_value(self):
        self.assertEqual(self.transaction.value(), 100)

    def test_serialize(self):
        self.assertEqual(
            self.transaction.serialize().hex(),
            "01000138fb65b08416b9870067b6cba63fa50a81bc78c8640000000000000034c4ac66523f355dba984e99baff0d991096bcf52b64909201a604b78fb48433106b598de5a8a69a79655414338dc43f8f197ed0d607e29f12d6f67b6fb852a301"
        )

    def test_deserialize(self):
        serialized = self.transaction.serialize()
        temp_transaction = Transaction.deserialize(serialized)
        self.assertEqual(serialized, temp_transaction.serialize())
Exemplo n.º 2
0
def create_transaction_handler():
    parser = ArgumentParser(description="Creates transaction object")
    parser.add_argument("-nonce", type=int, required=True)
    parser.add_argument("-out",
                        type=str,
                        help="{\\\"address\\\":amount,...}",
                        required=True)
    parser.add_argument("-private_key", type=str, required=True)
    args = parser.parse_args(sys.argv[2:])
    transaction = Transaction(args.nonce, json.loads(args.out))
    transaction.sign(args.private_key)
    print(transaction.serialize().hex())
Exemplo n.º 3
0
class TestBlockchain(TestCase):
    def setUp(self):
        self.blockchain = Blockchain()
        address = "c70f4891d2ce22b1f62492605c1d5c2fc1a8ef47"
        timestamp = 1579861388
        self.genesis = Block(0, "0" * 64, address, 0, timestamp, 0)
        self.transaction = Transaction(
            0, {"0000000000000000000000000000000000000000": 5})
        self.transaction.sign(
            "685cf62751cef607271ed7190b6a707405c5b07ec0830156e748c0c2ea4a2cfe")
        self.block = Block(1, self.genesis.hash(), address, 0, timestamp + 60,
                           0, [self.transaction])
        self.blockchain.add_block(self.genesis)
        self.blockchain.add_block(self.block)

    def test_get_latest_block(self):
        self.assertEqual(self.block.hash(),
                         self.blockchain.get_latest_block().hash())

    def test_get_genesis_block(self):
        self.assertEqual(self.genesis.hash(),
                         self.blockchain.get_genesis_block().hash())

    def test_get_block(self):
        self.assertEqual(self.block.hash(),
                         self.blockchain.get_block(self.block.hash()).hash())

    def test_get_block_hash(self):
        self.assertEqual(self.block.hash(), self.blockchain.get_block_hash(1))

    def test_get_transaction(self):
        self.assertEqual(
            self.transaction.id(),
            self.blockchain.get_block(self.block.hash()).transactions[0].id())

    def test_get_balance(self):
        self.assertEqual(
            5,
            self.blockchain.get_balance(
                "0000000000000000000000000000000000000000"))

    def test_get_nonce(self):
        self.assertEqual(
            1,
            self.blockchain.get_nonce(
                "c70f4891d2ce22b1f62492605c1d5c2fc1a8ef47"))