def test_good_sbc_returns_true(self):
        tx_1 = {
            'something': 'who_cares'
        }

        tx_2 = {
            'something_else': 'who_cares'
        }

        txs = [encode(tx).encode() for tx in [tx_1, tx_2]]
        expected_tree = merklize(txs)

        w = Wallet()

        input_hash = 'something'
        signature = w.sign(expected_tree[0])

        sbc = {
            'subblock': 1,
            'transactions': [tx_1, tx_2],
            'input_hash': input_hash,
            'signer': w.verifying_key,
            'merkle_tree': {
                'signature': signature,
                'leaves': expected_tree
            }
        }

        s = contender.SBCInbox()

        self.assertTrue(s.sbc_is_valid(sbc, 1))
    def test_merkle_leaf_complex_objects_works(self):
        tx_1_raw = b'{"hash": "503fa157e4d990f78f2b032731dffb398f6ff642b6bbc305817e24dd42b33402", "result": "None", "stamps_used": 198, "state": [{"key": "rewards.S:current_votes:masternodes", "value": 20}, {"key": "rewards.S:current_votes:delegates", "value": 20}, {"key": "rewards.S:current_votes:blackhole", "value": 5}, {"key": "rewards.S:current_votes:foundation", "value": 5}, {"key": "rewards.S:current_votes:developer", "value": 450}, {"key": "rewards.S:has_voted:b0fc27299da14bc08834df9c70d73074f3e511a5a91321d4fa413f3401144918", "value": true}, {"key": "rewards.S:vote_count", "value": 5}, {"key": "rewards.S:value", "value": [{"__fixed__": "0.04"}, {"__fixed__": "0.04"}, {"__fixed__": "0.01"}, {"__fixed__": "0.01"}, {"__fixed__": "0.9"}]}, {"key": "rewards.S:election_start", "value": null}, {"key": "currency.balances:b0fc27299da14bc08834df9c70d73074f3e511a5a91321d4fa413f3401144918", "value": {"__fixed__": "535.30200000"}}], "status": 0, "transaction": {"metadata": {"signature": "ceb4630c8be71f2c0c1e059790609fafa6ed948b7fceb4bb44c36dca46848fac8d55fe27f539036fee5bda6d772fff852b622393eab593aa6dce67f93f7d8f08", "timestamp": 1601411941}, "payload": {"contract": "election_house", "function": "vote", "kwargs": {"policy": "rewards", "value": [4, 4, 1, 1, 90]}, "nonce": 3, "processor": "5b09493df6c18d17cc883ebce54fcb1f5afbd507533417fe32c006009a9c3c4a", "sender": "b0fc27299da14bc08834df9c70d73074f3e511a5a91321d4fa413f3401144918", "stamps_supplied": 999}}}'

        tx_1 = decode(tx_1_raw)

        txs = [encode(tx).encode() for tx in [tx_1]]
        expected_tree = merklize(txs)

        w = Wallet()

        input_hash = 'something'
        signature = w.sign(expected_tree[0])

        sbc = {
            'subblock': 1,
            'transactions': [tx_1],
            'input_hash': input_hash,
            'signer': w.verifying_key,
            'merkle_tree': {
                'signature': signature,
                'leaves': expected_tree
            }
        }

        s = contender.SBCInbox()

        self.assertTrue(s.sbc_is_valid(sbc, 1))
    def test_subblock_with_bad_sb_idx_returns_false(self):
        sbc = {
            'subblock': 1
        }

        s = contender.SBCInbox()

        self.assertFalse(s.sbc_is_valid(sbc, 2))
    def test_verify_signature_not_valid_no_transactions(self):
        w = Wallet()
        w2 = Wallet()

        input_hash = 'something'
        signature = w2.sign(input_hash)

        sbc = {
            'subblock': 1,
            'transactions': [],
            'input_hash': input_hash,
            'signer': w.verifying_key,
            'merkle_tree': {
                'signature': signature
            }
        }

        s = contender.SBCInbox()

        self.assertFalse(s.sbc_is_valid(sbc, 1))
    def test_process_message_good_and_bad_sbc_doesnt_pass_to_q(self):
        ### GOOD SBC
        tx_1_1 = {
            'something': 'who_cares'
        }

        tx_1_2 = {
            'something_else': 'who_cares'
        }

        txs = [encode(tx).encode() for tx in [tx_1_1, tx_1_2]]
        expected_tree = merklize(txs)

        w = Wallet()

        input_hash = 'something'
        signature = w.sign(expected_tree[0])

        sbc_1 = {
            'subblock': 0,
            'transactions': [tx_1_1, tx_1_2],
            'input_hash': input_hash,
            'signer': w.verifying_key,
            'merkle_tree': {
                'signature': signature,
                'leaves': expected_tree
            }
        }

        ### BAD SBC
        tx_2_1 = {
            'something': 'who_cares2'
        }

        tx_2_2 = {
            'something_else': 'who_cares2'
        }

        txs = [encode(tx).encode() for tx in [tx_2_1, tx_2_2]]
        expected_tree = merklize(txs)

        w = Wallet()

        input_hash = 'something2'
        signature = w.sign(expected_tree[0])

        expected_tree[1] = 'crap'

        sbc_2 = {
            'subblock': 1,
            'transactions': [tx_2_1, tx_2_2],
            'input_hash': input_hash,
            'signer': w.verifying_key,
            'merkle_tree': {
                'signature': signature,
                'leaves': expected_tree
            }
        }

        s = contender.SBCInbox()

        loop = asyncio.get_event_loop()
        if loop.is_closed():
            loop = asyncio.new_event_loop()
        loop.run_until_complete(s.process_message([sbc_1, sbc_2]))

        self.assertEqual(s.q, [])