示例#1
0
def test1():
    # In this scenario we are processing messages step by step

    print('Starting call chain (step by step)...')
    t_value = 4276994270
    contract1.call_method('ping_neighbor',
                          dict(neighbor=neighbor2, value=t_value))

    # Get internal message that was created by previous call
    msg_ping = ts4.peek_msg()
    assert eq(neighbor1, msg_ping.src)
    assert eq(neighbor2, msg_ping.dst)
    assert msg_ping.is_call('ping')
    assert eq(t_value, int(msg_ping.params['request']))

    # Dispatch created message
    ts4.dispatch_one_message()

    # Pick up event that was created by called method of the callee contract
    msg_event1 = ts4.pop_event()

    # Check correctness of event addresses
    assert msg_event1.is_event('ReceivedRequest',
                               src=neighbor2,
                               dst=ts4.Address(None))
    assert eq(t_value, int(msg_event1.params['request']))

    # Get internal message that was created by last call
    msg_pong = ts4.peek_msg()
    assert eq(neighbor2, msg_pong.src)
    assert eq(neighbor1, msg_pong.dst)
    assert msg_pong.is_call('pong')
    assert eq(t_value, int(msg_pong.params['reply']))

    # Dispatch next message
    ts4.dispatch_one_message()

    # Pick up last event and check its parameters
    msg_event2 = ts4.pop_event()
    assert msg_event2.is_event('ReceivedReply',
                               src=neighbor1,
                               dst=ts4.Address(None))
    assert eq(t_value, int(msg_event2.params['reply']))

    # Working with raw JSON data is not always convenient. That's why we
    # provide a way to decode data:
    event2 = contract1.decode_event(msg_event2)
    assert eq(t_value, event2.reply)
示例#2
0
def test2():
    print('Transfer with payload')
    # Deploy the sender's contract and register nickname to be used in the output
    sender = ts4.BaseContract('tutorial09', {}, nickname='Sender')
    balance_sender = sender.balance

    # Deploy the another one recipient's contract and register nickname to be used in the output
    recipient = ts4.BaseContract('tutorial09', {}, nickname='Recipient')
    addr_recipient = recipient.address
    balance_recipient = recipient.balance

    # Send grams to the recipient without payload
    amount = 2 * ts4.GRAM
    comment = 'some comment'
    params = dict(addr=addr_recipient,
                  amount=amount,
                  comment=ts4.str2bytes(comment))
    sender.call_method('send_grams_with_payload', params)

    # Dispatch created message
    ts4.dispatch_one_message()

    # Сheck the current balance of the sender and recipient
    sender.ensure_balance(balance_sender - amount)
    recipient.ensure_balance(balance_recipient + amount)

    # Pick up event that was created by called method of the called contract
    event = ts4.pop_event()
    decoded = recipient.decode_event(event)
    # Check correctness of the received data
    assert eq(comment, decoded.comment)
    assert eq(amount, decoded.amount)
示例#3
0
def test2():
    # In most cases it is not necessary to control each message (while possible),
    # so here is the shorter version of the same scenario

    print('Starting call chain (in one step)...')
    t_value = 255
    contract1.call_method('ping_neighbor', dict(neighbor=neighbor2, value=t_value))

    # Dispatch all internal messages in one step
    ts4.dispatch_messages()

    # Skip first event
    ts4.pop_event()

    # Processing last event
    msg_event = ts4.pop_event()

    # Ensure that dst address is empty (one more variant)
    assert msg_event.is_event('ReceivedReply', src = neighbor1, dst = ts4.Address(None))
    assert eq(t_value, int(msg_event.params['reply']))