예제 #1
0
    def test_nonce_minus_pending_nonce_equal_tx_per_block_fails(self):
        w = Wallet()
        expected_processor = secrets.token_bytes(32)

        balances_key = '{}{}{}{}{}'.format('currency',
                                           config.INDEX_SEPARATOR,
                                           'balances',
                                           config.DELIMITER,
                                           w.verifying_key().hex())

        self.nonce_manager.set(balances_key, 500000)

        self.nonce_manager.set_nonce(processor=expected_processor, sender=w.verifying_key(), nonce=1)
        self.nonce_manager.set_pending_nonce(processor=expected_processor, sender=w.verifying_key(), nonce=16)

        tx = TransactionBuilder(w.verifying_key(),
                                contract='currency',
                                function='transfer',
                                kwargs={'amount': 10, 'to': 'jeff'},
                                stamps=500000,
                                processor=expected_processor,
                                nonce=16)

        tx.sign(w.signing_key())
        tx_bytes = tx.serialize()
        tx_struct = transaction_capnp.NewTransaction.from_bytes_packed(tx_bytes)

        with self.assertRaises(transaction.TransactionTooManyPendingException):
            transaction_is_valid(tx=tx_struct, expected_processor=expected_processor, driver=self.nonce_manager)
예제 #2
0
    def test_submission_prepended_with_con_succeeds(self):
        w = Wallet()
        expected_processor = secrets.token_bytes(32)

        balances_key = '{}{}{}{}{}'.format('currency',
                                           config.INDEX_SEPARATOR,
                                           'balances',
                                           config.DELIMITER,
                                           w.verifying_key().hex())

        self.nonce_manager.set(balances_key, 500000)

        tx = TransactionBuilder(w.verifying_key(),
                                contract='submission',
                                function='submit_contract',
                                kwargs={'name': 'con_bad_name', 'code': 'blah'},
                                stamps=3000,
                                processor=expected_processor,
                                nonce=0)

        tx.sign(w.signing_key())
        tx_bytes = tx.serialize()
        tx_struct = transaction_capnp.NewTransaction.from_bytes_packed(tx_bytes)

        transaction_is_valid(tx=tx_struct, expected_processor=expected_processor, driver=self.nonce_manager)
예제 #3
0
    def test_process_good_tx(self):
        txb = TransactionBuilder(self.wallet.verifying_key(),
                                 contract='currency',
                                 function='transfer',
                                 kwargs={
                                     'to': 'jeff',
                                     'amount': 10000
                                 },
                                 stamps=100000,
                                 processor=b'\x00' * 32,
                                 nonce=0)

        txb.sign(self.wallet.signing_key())
        tx_bytes = txb.serialize()

        tx = transaction_capnp.NewTransaction.from_bytes_packed(tx_bytes)
        print(tx)

        results = processor.process_transaction(tx)

        balance_from = results['state_changes']['currency.balances:{}'.format(
            self.wallet.vk.encode().hex())]
        balance_jeff = results['state_changes']['currency.balances:jeff']

        self.assertEqual(float(balance_from), 1000000000 - 10000)
        self.assertEqual(float(balance_jeff), 10000)
예제 #4
0
    def test_sending_transfer_of_most_money_doesnt_fail_if_enough_stamps(self):
        self.nonce_manager.set_var('stamp_cost', 'S', ['value'], value=3000)

        w = Wallet()
        expected_processor = secrets.token_bytes(32)

        balances_key = '{}{}{}{}{}'.format('currency',
                                           config.INDEX_SEPARATOR,
                                           'balances',
                                           config.DELIMITER,
                                           w.verifying_key().hex())

        self.nonce_manager.set(balances_key, 500000)
        tx = TransactionBuilder(w.verifying_key(),
                                contract='currency',
                                function='transfer',
                                kwargs={'amount': 499990, 'to': 'jeff'},
                                stamps=3000,
                                processor=expected_processor,
                                nonce=0)

        tx.sign(w.signing_key())
        tx_bytes = tx.serialize()
        tx_struct = transaction_capnp.NewTransaction.from_bytes_packed(tx_bytes)

        transaction_is_valid(tx=tx_struct, expected_processor=expected_processor, driver=self.nonce_manager)
예제 #5
0
    def test_all_valid_with_stamps_when_balance_is_set(self):
        w = Wallet()
        expected_processor = secrets.token_bytes(32)

        tx = TransactionBuilder(w.verifying_key(),
                                contract='currency',
                                function='transfer',
                                kwargs={'amount': 10, 'to': 'jeff'},
                                stamps=500000,
                                processor=expected_processor,
                                nonce=0)

        tx.sign(w.signing_key())
        tx_bytes = tx.serialize()
        tx_struct = transaction_capnp.NewTransaction.from_bytes_packed(tx_bytes)

        balances_key = '{}{}{}{}{}'.format('currency',
                                           config.INDEX_SEPARATOR,
                                           'balances',
                                           config.DELIMITER,
                                           tx.payload.sender.hex())

        self.nonce_manager.set(balances_key, 500000)

        transaction_is_valid(tx=tx_struct, expected_processor=expected_processor, driver=self.nonce_manager)
        balance = self.nonce_manager.get(balances_key) or 0

        self.assertEqual(balance, 500000)
def trigger(pkg=None, iaddr=None):
    my_wallet = validate_key(restart=False)
    pepper = pkg  #TODO replace with verified pepper pkg
    kwargs = {
        'pepper': pepper,
        'initiator_vk': my_wallet.verifying_key().hex()
    }
    vk = my_wallet.verifying_key()

    SERVER = f'http://{iaddr}:18080'

    nonce_req = requests.get('{}/nonce/{}'.format(
        SERVER,
        my_wallet.verifying_key().hex()))
    nonce = nonce_req.json()['nonce']

    #TODO bail out if vk is not in list of master nodes

    pack = TransactionBuilder(sender=vk,
                              contract='upgrade',
                              function='trigger_upgrade',
                              kwargs=kwargs,
                              stamps=100_000,
                              processor=vk,
                              nonce=nonce)

    pack.sign(my_wallet.signing_key())
    m = pack.serialize()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(cil_interface(server=iaddr, packed_data=m,
                                          sleep=2))
def abort_upgrade(iaddr):
    q = ask(question='Are you sure to abort version upgrade')

    if q is True:
        file = input("Enter patch to constitution:")
        constitution = resolve_constitution(fp=file)
        proc = constitution.get("masternodes", "")

        my_wallet = validate_key()

        SERVER = f'http://{iaddr}:18080'

        nonce_req = requests.get('{}/nonce/{}'.format(
            SERVER,
            my_wallet.verifying_key().hex()))
        nonce = nonce_req.json()['nonce']

        kwargs = {'vk': my_wallet.verifying_key().hex()}

        pack = TransactionBuilder(sender=my_wallet.verifying_key(),
                                  contract='upgrade',
                                  function='reset_contract',
                                  kwargs=kwargs,
                                  stamps=100_000,
                                  processor=bytes.fromhex(proc[0]),
                                  nonce=nonce)

        pack.sign(my_wallet.signing_key())
        m = pack.serialize()
        loop = asyncio.get_event_loop()
        loop.run_until_complete(
            cil_interface(server=iaddr, packed_data=m, sleep=2))
예제 #8
0
    def test_processor_and_nonce_correct_but_not_enough_stamps_returns_false(self):
        w = Wallet()
        expected_processor = secrets.token_bytes(32)

        tx = TransactionBuilder(w.verifying_key(),
                                contract='currency',
                                function='transfer',
                                kwargs={'amount': 10, 'to': 'jeff'},
                                stamps=500000,
                                processor=expected_processor,
                                nonce=0)

        tx.sign(w.signing_key())
        tx_bytes = tx.serialize()
        tx_struct = transaction_capnp.NewTransaction.from_bytes_packed(tx_bytes)

        with self.assertRaises(transaction.TransactionSenderTooFewStamps):
            transaction_is_valid(tx=tx_struct, expected_processor=expected_processor, driver=self.nonce_manager)

        balances_key = '{}{}{}{}{}'.format('currency',
                                           config.INDEX_SEPARATOR,
                                           'balances',
                                           config.DELIMITER,
                                           tx.payload.sender.hex())

        balance = self.nonce_manager.get(balances_key) or 0

        self.assertEqual(balance, 0)
예제 #9
0
def make_tx(processor, contract_name, function_name, kwargs={}):
    w = Wallet()
    batch = TransactionBuilder(sender=w.verifying_key(),
                               contract=contract_name,
                               function=function_name,
                               kwargs=kwargs,
                               stamps=10000,
                               processor=processor,
                               nonce=0)

    batch.sign(w.signing_key())
    b = batch.serialize()

    tx = transaction_capnp.Transaction.from_bytes_packed(b)

    currency_contract = 'currency'
    balances_hash = 'balances'

    balances_key = '{}{}{}{}{}'.format(currency_contract,
                                       config.INDEX_SEPARATOR, balances_hash,
                                       config.DELIMITER,
                                       w.verifying_key().hex())

    driver = ContractDriver()
    driver.set(balances_key, 1_000_000)
    driver.commit()

    return tx
def make_tx_packed(sender,
                   server,
                   contract_name,
                   function_name,
                   kwargs={},
                   stamps=10_000):
    wallet = Wallet(seed=sender)

    nonce_req = requests.get('{}/nonce/{}'.format(
        server,
        wallet.verifying_key().hex()))
    nonce = nonce_req.json()['nonce']
    processor = bytes.fromhex(nonce_req.json()['processor'])

    batch = TransactionBuilder(sender=wallet.verifying_key(),
                               contract=contract_name,
                               function=function_name,
                               kwargs=kwargs,
                               stamps=stamps,
                               processor=processor,
                               nonce=nonce)

    batch.sign(sender)
    b = batch.serialize()

    return b
예제 #11
0
def random_packed_tx(nonce=0, processor=None, give_stamps=False):
    w = Wallet()

    processor = secrets.token_bytes(32) if processor is None else processor
    stamps = random.randint(100_000, 1_000_000)

    if give_stamps:
        balances_key = '{}{}{}{}{}'.format('currency', config.INDEX_SEPARATOR,
                                           'balances', config.DELIMITER,
                                           w.verifying_key().hex())

        N.set(balances_key, stamps + 1000)

    tx = TransactionBuilder(
        w.verifying_key(),
        contract=secrets.token_hex(8),
        function=secrets.token_hex(8),
        kwargs={secrets.token_hex(8): secrets.token_hex(8)},
        stamps=stamps,
        processor=processor,
        nonce=nonce)

    tx.sign(w.signing_key())

    #tx.proof = b'\x00' * 32
    #tx.proof_generated = True

    packed_tx = transaction_capnp.Transaction.from_bytes_packed(tx.serialize())
    return packed_tx
    def test_serialize_if_not_signed_returns_none(self):
        w = Wallet()
        tx = TransactionBuilder(sender=w.verifying_key().hex(),
                                stamps=1000000,
                                contract='currency',
                                function='transfer',
                                kwargs={'amount': 'b'},
                                processor=b'\x00'*32,
                                nonce=0)

        self.assertIsNone(tx.serialize())
    def test_serialize_returns_bytes(self):
        w = Wallet()
        tx = TransactionBuilder(sender=w.verifying_key().hex(),
                                stamps=1000000,
                                contract='currency',
                                function='transfer',
                                kwargs={'amount': 'b'},
                                processor=b'\x00'*32,
                                nonce=0)

        tx.sign(w.signing_key())

        tx_packed = tx.serialize()

        self.assertTrue(verify_packed_tx(w.verifying_key(), tx_packed))
    def test_generate_proof_flips_true(self):
        w = Wallet()
        tx = TransactionBuilder(sender=w.verifying_key().hex(),
                                stamps=1000000,
                                contract='currency',
                                function='transfer',
                                kwargs={'amount': 'b'},
                                processor=b'\x00'*32,
                                nonce=0)

        self.assertFalse(tx.proof_generated)

        tx.generate_proof()

        self.assertTrue(tx.proof_generated)
예제 #15
0
    def check_ready_quorum(self, sk=None):
        my_wallet = Wallet.from_sk(sk=sk)
        kwargs = {'vk': my_wallet.verifying_key()}

        pack = TransactionBuilder(sender=my_wallet.verifying_key(),
                                  contract='upgrade',
                                  function='check_vote_state',
                                  kwargs=kwargs,
                                  stamps=1_000_000,
                                  processor=my_wallet.verifying_key(),
                                  nonce=0)

        pack.sign(my_wallet.signing_key())
        m = pack.serialize()

        return m
예제 #16
0
def send_tx(sender, contract, function, kwargs={}):
    nonce_req = requests.get('{}/nonce/{}'.format(
        SERVER,
        sender.verifying_key().hex()))
    nonce = nonce_req.json()['nonce']
    processor = bytes.fromhex(nonce_req.json()['processor'])
    tx = TransactionBuilder(sender.verifying_key(),
                            contract=contract,
                            function=function,
                            kwargs=kwargs,
                            stamps=100_000,
                            processor=processor,
                            nonce=nonce)
    tx.sign(sender.signing_key())
    packed_tx = tx.serialize()
    res = submit_transaction(packed_tx)
    print(res.text)
예제 #17
0
    def test_processor_is_expected_but_nonce_is_incorrect_returns_false(self):
        w = Wallet()
        expected_processor = secrets.token_bytes(32)

        tx = TransactionBuilder(w.verifying_key(),
                                contract='currency',
                                function='transfer',
                                kwargs={'amount': 10, 'to': 'jeff'},
                                stamps=500000,
                                processor=expected_processor,
                                nonce=1)

        tx.sign(w.signing_key())
        tx_bytes = tx.serialize()
        tx_struct = transaction_capnp.NewTransaction.from_bytes_packed(tx_bytes)

        with self.assertRaises(transaction.TransactionNonceInvalid):
            transaction_is_valid(tx=tx_struct, expected_processor=expected_processor, driver=self.nonce_manager)
 def test_init(self):
     w = Wallet()
     TransactionBuilder(sender=w.verifying_key().hex(),
                        stamps=1000000,
                        contract='currency',
                        function='transfer',
                        kwargs={'amount': 'b'},
                        processor=b'\x00' * 32,
                        nonce=0)
 def test_passing_float_in_contract_kwargs_raises_assertion(self):
     w = Wallet()
     with self.assertRaises(AssertionError):
         TransactionBuilder(sender=w.verifying_key().hex(),
                            stamps=1000000,
                            contract='currency',
                            function='transfer',
                            kwargs={'amount': 123.00},
                            processor=b'\x00' * 32,
                            nonce=0)
예제 #20
0
    def test_non_strict_fails_if_same_nonce(self):
        w = Wallet()
        expected_processor = secrets.token_bytes(32)

        balances_key = '{}{}{}{}{}'.format('currency',
                                           config.INDEX_SEPARATOR,
                                           'balances',
                                           config.DELIMITER,
                                           w.verifying_key().hex())

        self.nonce_manager.set(balances_key, 500000)

        tx = TransactionBuilder(w.verifying_key(),
                                contract='currency',
                                function='transfer',
                                kwargs={'amount': 10, 'to': 'jeff'},
                                stamps=500000,
                                processor=expected_processor,
                                nonce=0)

        tx.sign(w.signing_key())
        tx_bytes = tx.serialize()
        tx_struct = transaction_capnp.NewTransaction.from_bytes_packed(tx_bytes)

        transaction_is_valid(tx=tx_struct, expected_processor=expected_processor, driver=self.nonce_manager,
                             strict=False)

        tx = TransactionBuilder(w.verifying_key(),
                                contract='currency',
                                function='transfer',
                                kwargs={'amount': 10, 'to': 'jeff'},
                                stamps=500000,
                                processor=expected_processor,
                                nonce=0)

        tx.sign(w.signing_key())
        tx_bytes = tx.serialize()
        tx_struct = transaction_capnp.NewTransaction.from_bytes_packed(tx_bytes)

        with self.assertRaises(transaction.TransactionNonceInvalid):
            transaction_is_valid(tx=tx_struct, expected_processor=expected_processor, driver=self.nonce_manager,
                                 strict=False)
예제 #21
0
def make_good_tx(processor):
    w = Wallet()
    balances_key = '{}{}{}{}{}'.format('currency', config.INDEX_SEPARATOR,
                                       'balances', config.DELIMITER,
                                       w.verifying_key().hex())
    n.set(balances_key, 500000)
    tx = TransactionBuilder(w.verifying_key(),
                            contract='currency',
                            function='transfer',
                            kwargs={
                                'amount': 10,
                                'to': 'jeff'
                            },
                            stamps=500000,
                            processor=processor,
                            nonce=0)

    tx.sign(w.signing_key())
    tx_bytes = tx.serialize()
    return tx_bytes
예제 #22
0
    def trigger(self, sk=None):
        my_wallet = Wallet.from_sk(sk=sk)
        pepper = 'RAMDOM'  # TODO replace with verified pepper pkg
        kwargs = {'pepper': pepper, 'vk': my_wallet.verifying_key()}
        vk = my_wallet.verifying_key()

        #TODO bail out if vk is not in list of master nodes

        pack = TransactionBuilder(sender=vk,
                                  contract='upgrade',
                                  function='trigger_upgrade',
                                  kwargs=kwargs,
                                  stamps=1_000_000,
                                  processor=vk,
                                  nonce=0)

        pack.sign(my_wallet.signing_key())
        m = pack.serialize()

        return m
예제 #23
0
    def test_processor_and_nonce_correct_increments_pending_nonce_by_one(self):
        w = Wallet()
        expected_processor = secrets.token_bytes(32)

        tx = TransactionBuilder(w.verifying_key(),
                                contract='currency',
                                function='transfer',
                                kwargs={'amount': 10, 'to': 'jeff'},
                                stamps=10000,
                                processor=expected_processor,
                                nonce=0)

        tx.sign(w.signing_key())
        tx_bytes = tx.serialize()
        tx_struct = transaction_capnp.NewTransaction.from_bytes_packed(tx_bytes)

        transaction_is_valid(tx=tx_struct, expected_processor=expected_processor, driver=self.nonce_manager)

        pending_nonce = self.nonce_manager.get_pending_nonce(expected_processor, w.verifying_key())

        self.assertEqual(pending_nonce, 1)
    def test_passing_non_supported_type_in_contract_kwargs_raises_assertion(self):
        w = Wallet()
        with self.assertRaises(AssertionError):
            TransactionBuilder(sender=w.verifying_key().hex(),
                               stamps=1000000,
                               contract='currency',
                               function='transfer',
                               kwargs={'amount': ['b']},
                               processor=b'\x00'*32,
                               nonce=0)
# def __init__(self, sender, contract: str, function: str, kwargs: dict, stamps: int, processor: bytes,
#                  nonce: int):
예제 #25
0
def make_bad_tx():
    w = Wallet()
    balances_key = '{}{}{}{}{}'.format('currency', config.INDEX_SEPARATOR,
                                       'balances', config.DELIMITER,
                                       w.verifying_key().hex())
    n.set(balances_key, 500000)
    tx = TransactionBuilder(w.verifying_key(),
                            contract='currency',
                            function='transfer',
                            kwargs={
                                'amount': 10,
                                'to': 'jeff'
                            },
                            stamps=500000,
                            processor=b'\x00' * 32,
                            nonce=0)

    tx.sign(w.signing_key())
    tx_bytes = tx.serialize()
    #tx_struct = transaction_capnp.Transaction.from_bytes_packed(tx_bytes)
    return tx_bytes
def vote(iaddr):

    enable = ask("Authorize auto upgrade to new version")
    my_wallet = validate_key(
        restart=enable)  # Currently reboot is set for auto restart to kick in
    file = input("Enter patch to constitution:")

    constitution = resolve_constitution(fp=file)

    # TODO randomize proc in future
    proc = constitution.get("masternodes", "")

    SERVER = f'http://{iaddr}:18080'

    nonce_req = requests.get('{}/nonce/{}'.format(
        SERVER,
        my_wallet.verifying_key().hex()))
    nonce = nonce_req.json()['nonce']
    #vk = my_wallet.verifying_key()

    kwargs = {'vk': my_wallet.verifying_key().hex()}

    pack = TransactionBuilder(sender=my_wallet.verifying_key(),
                              contract='upgrade',
                              function='vote',
                              kwargs=kwargs,
                              stamps=100_000,
                              processor=bytes.fromhex(proc[0]),
                              nonce=nonce)

    pack.sign(my_wallet.signing_key())
    m = pack.serialize()

    loop = asyncio.get_event_loop()
    loop.run_until_complete(cil_interface(server=iaddr, packed_data=m,
                                          sleep=2))
예제 #27
0
    def test_environment_variables_are_working_as_they_should(self):
        test_contract = '''
a = Variable()
b = Variable()
c = Variable()

@export
def capture():
    a.set(block_hash)
    b.set(block_num)
    c.set(now)
        '''

        self.client.submit(test_contract, name='testing')

        tx = TransactionBuilder(sender='stu',
                                contract='testing',
                                function='capture',
                                kwargs={},
                                stamps=100_000,
                                processor=b'\x00' * 32,
                                nonce=0)
        tx.sign(Wallet().signing_key())
        tx.serialize()

        tx_batch = transaction_list_to_transaction_batch([tx.struct],
                                                         wallet=Wallet())
        w = Wallet()
        b = Delegate(socket_base='tcp://127.0.0.1', wallet=w, ctx=self.ctx)

        now = Datetime._from_datetime(
            datetime.utcfromtimestamp(tx_batch.timestamp))

        results = b.execute_work([(tx_batch.timestamp, tx_batch)])

        tx = results[0].transactions[0]

        a, b, c = tx.state

        self.assertEqual(a.key, b'testing.a')
        self.assertEqual(
            a.value,
            b'"0000000000000000000000000000000000000000000000000000000000000000"'
        )

        self.assertEqual(b.key, b'testing.b')
        self.assertEqual(b.value, b'0')

        self.assertEqual(c.key, b'testing.c')
        self.assertEqual(c.value, encode(now).encode())
예제 #28
0
    def test_execute_work_single_transaction(self):
        test_contract = '''
v = Variable()

@construct
def seed():
    v.set('hello')

@export
def set(var):
    v.set(var)

@export
def get():
    return v.get()
        '''

        self.client.submit(test_contract, name='testing')

        tx = TransactionBuilder(sender='stu',
                                contract='testing',
                                function='set',
                                kwargs={'var': 'jeff'},
                                stamps=100_000,
                                processor=b'\x00' * 32,
                                nonce=0)
        tx.sign(Wallet().signing_key())
        tx.serialize()

        tx_batch = transaction_list_to_transaction_batch([tx.struct],
                                                         wallet=Wallet())

        b = Delegate(socket_base='tcp://127.0.0.1',
                     wallet=Wallet(),
                     ctx=self.ctx,
                     bootnodes=bootnodes,
                     constitution=constitution)

        b.execute_work([(1, tx_batch)])
예제 #29
0
    def test_build_sbc_from_work_results(self):
        test_contract = '''
v = Variable()

@construct
def seed():
    v.set('hello')

@export
def set(var):
    v.set(var)

@export
def get():
    return v.get()
        '''
        print('ok')
        self.client.submit(test_contract, name='testing')
        print('here')
        tx = TransactionBuilder(sender='stu',
                                contract='testing',
                                function='set',
                                kwargs={'var': 'jeff'},
                                stamps=100_000,
                                processor=b'\x00' * 32,
                                nonce=0)
        tx.sign(Wallet().signing_key())
        tx.serialize()

        tx_batch = transaction_list_to_transaction_batch([tx.struct],
                                                         wallet=Wallet())

        b = Delegate(socket_base='tcp://127.0.0.1',
                     wallet=Wallet(),
                     ctx=self.ctx,
                     constitution=constitution)

        print(sbc)
예제 #30
0
    def test_execute_tx_returns_successful_output(self):
        test_contract = '''
v = Variable()

@construct
def seed():
    v.set('hello')

@export
def set(var):
    v.set(var)

@export
def get():
    return v.get()
                '''

        self.client.submit(test_contract, name='testing')

        tx = TransactionBuilder(sender='stu',
                                contract='testing',
                                function='set',
                                kwargs={'var': 'jeff'},
                                stamps=100_000,
                                processor=b'\x00' * 32,
                                nonce=0)
        tx.sign(Wallet().signing_key())
        tx.serialize()

        result = execution.execute_tx(self.client, tx.struct)

        print(result)

        self.assertEqual(result.status, 0)
        self.assertEqual(result.state[0].key, b'testing.v')
        self.assertEqual(result.state[0].value, b'"jeff"')
        self.assertEqual(result.stampsUsed, 0)