示例#1
0
def test_transactions():
    bob = Key.generate()
    alice = Key.generate()

    t1 = Transaction()
    t1.to = alice.address()
    t1.nonce = 2
    t1.value = 11
    t1.call = 'CREATE'
    t1.params = ExObj(20, 0, 'dave')
    raw1 = t1.sign(bob).encode()
    assert (raw1)

    # decode outer transaction and check
    tback = Transaction.decode(raw1)
    assert (2 == tback.nonce)
    assert (11 == tback.value)
    assert (b'CREATE' == tback.call)
    assert (bob.address() == tback.sender)
    assert (alice.address() == tback.to)
    assert (Key.verify(bob.publickey(), tback.signature))

    # decode params and check
    pback = tback.decode_params(ExObj)
    assert (20 == pback.x)
    assert (b'dave' == pback.title)
示例#2
0
def test_ok_with_no_params():
    bob = Key.generate()
    alice = Key.generate()

    # Bob send Tx to alice
    t1 = Transaction()
    t1.to = alice.address()
    t1.nonce = 1
    t1.value = 0
    t1.call = 'TRANSFER'
    raw1 = t1.sign(bob).encode()
    assert (raw1)

    # Thaw the tx and check it
    tback = Transaction.decode(raw1)
    assert (1 == tback.nonce)
    assert (0 == tback.value)
    assert (b'TRANSFER' == tback.call)
    # sender was set in the sign
    assert (bob.address() == tback.sender)
    assert (alice.address() == tback.to)

    assert (Key.verify(bob.publickey(), tback.signature))
    # Just for the heck of it...
    assert (Key.verify(alice.publickey(), tback.signature) == False)

    # Try to decode_params even through there's none
    pback = tback.decode_params(ExObj)
    assert (None == pback)
示例#3
0
def test_signing():
    bob = Key.generate()
    alice = Key.generate()

    # Check the address
    addy = bob.address(tohex=False)
    assert(addy)
    assert(len(addy) == 20)

    # Check the publickey
    assert(len(bob.publickey()) == nacl.bindings.crypto_sign_PUBLICKEYBYTES)

    # Sign and verify
    bob_sig = bob.sign(b'hello')
    bob_raw_pk = bob.publickey()
    bob_hex_pk = bob.publickey(tohex=True)

    assert(Key.verify(bob_raw_pk, bob_sig))
    assert(Key.verify(bob_hex_pk, bob_sig))

    # Check it fails on by wrong key
    alice = Key.generate()
    assert(Key.verify(alice.publickey(), bob_sig) == False)

    # can load key from privatekey hex
    #bob_again = Key(bob.privatekey(tohex=True))
    #assert(Key.verify(bob_again.publickey(), bob_sig))
    a = bob.privatekey()
    b = from_hex(bob.privatekey(tohex=True))
    assert(a == b)

    z = Key.fromPrivateKey(bob.privatekey(tohex=True))
    assert(z.privatekey() == bob.privatekey())
    assert(z.publickey() == bob.publickey())
    assert(z.address() == bob.address())

    assert(Key.verify(z.publickey(), bob_sig))

    seed1 = keccak(b'bob')
    h = Key.generate(seed=seed1)
    i = Key.generate(seed=seed1)
    assert(len(h.address()) == 20)
    assert(i.address() == h.address())

    # Test create_address
    addy2 = create_address(bob.publickey(tohex=True))
    assert(addy2 == bob.address())
示例#4
0
def test_storage():
    alice = Key.generate()
    bob = Key.generate()
    acct = Account.create_account(bob.publickey(tohex=True))

    dbfile = home_dir('temp', 'test.db')
    state, _ = State.load_state(dbfile)
    state.chain_id = 'testchain1'
    storage = Storage(state)

    h1 = storage.commit()

    # Note: Haven't added bob to storage!
    storage.unconfirmed.increment_nonce(bob.address())
    bobs = storage.unconfirmed.get_account(bob.address())
    assert (b'' == bobs)

    storage.unconfirmed.update_account(acct)
    storage.unconfirmed.increment_nonce(bob.address())
    storage.unconfirmed.increment_nonce(bob.address())
    bobs = storage.unconfirmed.get_account(bob.address())
    assert (2 == bobs.nonce)

    alice_acct = Account.create_account(alice.publickey(tohex=True))
    storage.confirmed.update_account(alice_acct)
    storage.confirmed.increment_nonce(alice.address())
    alices = storage.confirmed.get_account(alice.address())
    assert (1 == alices.nonce)
    h2 = storage.commit()
    assert (h1 != h2)

    state2, _ = State.load_state(dbfile)
    storage2 = Storage(state2)
    alices = storage2.unconfirmed.get_account(alice.address())
    assert (1 == alices.nonce)
    bobs = storage2.unconfirmed.get_account(bob.address())
    assert (b'' == bobs)

    if os.path.exists(dbfile):
        os.remove(dbfile)
示例#5
0
def test_state_account():
    bob = Key.generate()
    acct = Account.create_account(bob.publickey(tohex=True))
    st, _ = State.load_state('')
    r1 = st.storage.root_hash

    st.update_account(acct)
    r2 = st.storage.root_hash
    assert (r1 != r2)
    a = st.get_account(bob.address())

    assert (a)
    assert (a.pubkey == bob.publickey())

    st.update_account(None)
    assert (r2 == st.storage.root_hash)
示例#6
0
def test_cache():
    bob = Key.generate()
    acct = Account.create_account(bob.publickey(tohex=True))

    dbfile = home_dir('temp', 'test.db')
    state, _ = State.load_state(dbfile)
    state.chain_id = 'testchain1'
    cache = StateCache(state)
    h1 = cache.commit()

    cache.update_account(acct)
    a = cache.get_account(bob.address())
    assert (a.address() == bob.address())

    cache.put_data(b'a', b'one')
    assert (b'one' == cache.get_data(b'a'))
    cache.put_data(b'a', b'two')
    assert (b'two' == cache.get_data(b'a'))

    cache.increment_nonce(bob.address())
    cache.increment_nonce(bob.address())
    cache.increment_nonce(bob.address())
    cache.increment_nonce(bob.address())
    b = cache.get_account(bob.address())
    assert (4 == b.nonce)

    state.last_block_height = 1
    cache.commit()
    h2 = state.save()
    assert (h1 != h2)
    state.close()

    # Reload from state and make sure all data is the same
    state2, _ = State.load_state(dbfile)
    assert (b'testchain1' == state2.chain_id)
    assert (1 == state2.last_block_height)
    # Should match our last apphash
    assert (h2 == state2.storage.root_hash)

    bobs = state2.get_account(bob.address())
    assert (4 == bobs.nonce)
    state2.close()

    if os.path.exists(dbfile):
        os.remove(dbfile)
示例#7
0
def test_account():
    bob = Key.generate()
    acct = Account.create_account(bob.publickey(tohex=True))

    assert (acct)
    assert (acct.nonce == 0)
    assert (acct.balance == 0)
    assert (bob.address() == acct.address())
    assert (bob.publickey() == acct.pubkey)

    serial = rlp.encode(acct, sedes=Account)
    acct_revived = rlp.decode(serial, sedes=Account)
    assert (acct_revived)
    assert (acct_revived.nonce == 0)
    assert (acct_revived.balance == 0)
    assert (bob.address() == acct_revived.address())
    assert (bob.publickey() == acct_revived.pubkey)

    acct_revived.allow_changes()
    acct_revived.nonce = 1
    assert (acct_revived.nonce == 1)
示例#8
0
def test_app_api():
    app = TendermintApp("")

    @app.on_initialize()
    def create_accts(db):
        ba = Account.create_account(bob.publickey())
        aa = Account.create_account(alice.publickey())
        db.update_account(ba)
        db.update_account(aa)
        db.put_data(b'count', int_to_big_endian(10))
        db.put_data(b'name', b'dave')

    @app.on_query('/count')
    def get_count(key, db):
        return db.get_data(key)

    # Run the app in mock mode (doesn't need tendermint)
    app.mock_run()

    # Run requests against API

    # Note: /tx_nonce is built in
    req = to_request_query(path='/tx_nonce', data=bob.address())
    resp = app.query(req)
    assert(resp.code == 0)
    assert(0 == big_endian_to_int(resp.value))

    req = to_request_query(path='/count', data=b'count')
    resp = app.query(req)
    assert(0 == resp.code)
    assert(10 == big_endian_to_int(resp.value))

    # Try some transactions

    # Check tx First
    # Missing sender - signature
    t = Transaction()
    raw = t.encode()
    r = to_request_check_tx(raw)
    resp = app.check_tx(r)
    assert(resp.code == 1)
    assert(resp.log == 'No Sender - is the Tx signed?')

    # No account
    n = Key.generate()
    t = Transaction()
    t.sender = n.address()
    raw = t.encode()
    r = to_request_check_tx(raw)
    resp = app.check_tx(r)
    assert(resp.code == 1)
    assert(resp.log == 'Account not found')

    # Bad nonce
    t = Transaction()
    t.nonce = 10
    raw = t.sign(bob).encode()
    r = to_request_check_tx(raw)
    resp = app.check_tx(r)
    assert(resp.code == 1)
    assert(resp.log == 'Bad nonce')

    # Bad balance
    t = Transaction()
    t.nonce = 0
    t.value = 100
    raw = t.sign(alice).encode()
    r = to_request_check_tx(raw)
    resp = app.check_tx(r)
    assert(resp.code == 1)
    assert(resp.log == 'Insufficient balance for transfer')

    # Finally good check transaction
    t = Transaction()
    t.nonce = 1
    raw = t.sign(alice).encode()
    r = to_request_check_tx(raw)
    resp = app.check_tx(r)
    assert(resp.code == 0)
示例#9
0
import pytest

import rlp
from rlp.sedes import big_endian_int

# only used here for testing
from abci.messages import *

from tendermint import TendermintApp, Transaction

from tendermint.keys import Key
from tendermint.accounts import Account
from tendermint.utils import big_endian_to_int, int_to_big_endian

bob = Key.generate()
alice = Key.generate()

def test_app_api():
    app = TendermintApp("")

    @app.on_initialize()
    def create_accts(db):
        ba = Account.create_account(bob.publickey())
        aa = Account.create_account(alice.publickey())
        db.update_account(ba)
        db.update_account(aa)
        db.put_data(b'count', int_to_big_endian(10))
        db.put_data(b'name', b'dave')

    @app.on_query('/count')
    def get_count(key, db):