示例#1
0
def test_exit_when_tm_ver_not_supported(b):
    from bigchaindb import App

    app = App(b)
    p = ProtocolHandler(app)

    with pytest.raises(SystemExit):
        p.process('info', types.Request(info=types.RequestInfo(version='2')))
def test_exit_when_tm_ver_not_supported(b):
    from bigchaindb import App

    app = App(b)
    p = ProtocolHandler(app)

    with pytest.raises(SystemExit):
        p.process('info', types.Request(info=types.RequestInfo(version='2')))
def test_handler():
    p = ProtocolHandler(SApp())

    r = to_response_echo('hello')
    data = p.process('echo', r)
    res, err = read_message(BytesIO(data), types.Response)
    assert res
    assert 'echo' == res.WhichOneof("value")
    assert res.echo.message == 'hello'

    data = p.process('flush', None)
    res, err = read_message(BytesIO(data), types.Response)
    assert res
    assert 'flush' == res.WhichOneof("value")

    data = p.process('info', None)
    res, err = read_message(BytesIO(data), types.Response)
    assert res
    assert 'info' == res.WhichOneof("value")
    assert res.info.data == 'hellothere'

    r = to_request_deliver_tx(b'0x1234')
    data = p.process('deliver_tx', r)
    res, err = read_message(BytesIO(data), types.Response)
    assert res
    assert 'deliver_tx' == res.WhichOneof("value")
    assert res.deliver_tx.code == 0
    assert res.deliver_tx.data == b'0x1234'

    r = to_request_check_tx(b'0x1234')
    data = p.process('check_tx', r)
    res, err = read_message(BytesIO(data), types.Response)
    assert res
    assert 'check_tx' == res.WhichOneof("value")
    assert res.check_tx.code == 0
    assert res.check_tx.data == b'0x1234'

    r = to_request_query(data=b'name')
    data = p.process('query', r)
    res, err = read_message(BytesIO(data), types.Response)
    assert res
    assert 'query' == res.WhichOneof("value")
    assert res.query.key == b'name'
    assert res.query.value == b'dave'
    assert res.query.code == 0
    assert res.query.proof == b'0x12'
def test_app(tb):
    from bigchaindb.tendermint import App
    from bigchaindb.tendermint.utils import calculate_hash
    from bigchaindb.common.crypto import generate_key_pair
    from bigchaindb.models import Transaction

    b = tb
    app = App(b)
    p = ProtocolHandler(app)

    data = p.process('info', None)
    res, err = read_message(BytesIO(data), types.Response)
    assert res
    assert res.info.last_block_app_hash == b''
    assert res.info.last_block_height == 0
    assert not b.get_latest_block()

    p.process('init_chain', None)
    block0 = b.get_latest_block()
    assert block0
    assert block0['height'] == 0
    assert block0['app_hash'] == ''

    alice = generate_key_pair()
    bob = generate_key_pair()
    tx = Transaction.create([alice.public_key],
                            [([bob.public_key], 1)])\
                    .sign([alice.private_key])
    etxn = json.dumps(tx.to_dict()).encode('utf8')

    r = to_request_check_tx(etxn)
    data = p.process('check_tx', r)
    res, err = read_message(BytesIO(data), types.Response)
    assert res
    assert res.check_tx.code == 0

    r = types.Request()
    r.begin_block.hash = b''
    p.process('begin_block', r)

    r = to_request_deliver_tx(etxn)
    data = p.process('deliver_tx', r)
    res, err = read_message(BytesIO(data), types.Response)
    assert res
    assert res.deliver_tx.code == 0

    new_block_txn_hash = calculate_hash([tx.id])

    r = types.Request()
    r.end_block.height = 1
    data = p.process('end_block', r)
    res, err = read_message(BytesIO(data), types.Response)
    assert res
    assert 'end_block' == res.WhichOneof('value')

    new_block_hash = calculate_hash([block0['app_hash'], new_block_txn_hash])

    data = p.process('commit', None)
    res, err = read_message(BytesIO(data), types.Response)
    assert res
    assert res.commit.code == 0
    assert res.commit.data == new_block_hash.encode('utf-8')
    assert b.get_transaction(tx.id).id == tx.id

    block0 = b.get_latest_block()
    assert block0
    assert block0['height'] == 1
    assert block0['app_hash'] == new_block_hash

    # empty block should not update height
    r = types.Request()
    r.begin_block.hash = new_block_hash.encode('utf-8')
    p.process('begin_block', r)

    r = types.Request()
    r.end_block.height = 2
    p.process('end_block', r)

    data = p.process('commit', None)
    assert res.commit.data == new_block_hash.encode('utf-8')

    block0 = b.get_latest_block()
    assert block0
    assert block0['height'] == 1

    # when empty block is generated hash of previous block should be returned
    assert block0['app_hash'] == new_block_hash
示例#5
0
def test_app(tb, init_chain_request):
    from bigchaindb import App
    from bigchaindb.tendermint_utils import calculate_hash
    from bigchaindb.common.crypto import generate_key_pair
    from bigchaindb.models import Transaction

    b = tb
    app = App(b)
    p = ProtocolHandler(app)

    data = p.process('info',
                     types.Request(info=types.RequestInfo(version='2')))
    res = next(read_messages(BytesIO(data), types.Response))
    assert res
    assert res.info.last_block_app_hash == b''
    assert res.info.last_block_height == 0
    assert not b.get_latest_block()

    p.process('init_chain', types.Request(init_chain=init_chain_request))
    block0 = b.get_latest_block()
    assert block0
    assert block0['height'] == 0
    assert block0['app_hash'] == ''

    pk = codecs.encode(init_chain_request.validators[0].pub_key.data,
                       'base64').decode().strip('\n')
    [validator] = b.get_validators(height=1)
    assert validator['pub_key']['data'] == pk
    assert validator['voting_power'] == 10

    alice = generate_key_pair()
    bob = generate_key_pair()
    tx = Transaction.create([alice.public_key],
                            [([bob.public_key], 1)])\
                    .sign([alice.private_key])
    etxn = json.dumps(tx.to_dict()).encode('utf8')

    r = types.Request(check_tx=types.RequestCheckTx(tx=etxn))
    data = p.process('check_tx', r)
    res = next(read_messages(BytesIO(data), types.Response))
    assert res
    assert res.check_tx.code == 0

    r = types.Request()
    r.begin_block.hash = b''
    p.process('begin_block', r)

    r = types.Request(deliver_tx=types.RequestDeliverTx(tx=etxn))
    data = p.process('deliver_tx', r)
    res = next(read_messages(BytesIO(data), types.Response))
    assert res
    assert res.deliver_tx.code == 0

    new_block_txn_hash = calculate_hash([tx.id])

    r = types.Request(end_block=types.RequestEndBlock(height=1))
    data = p.process('end_block', r)
    res = next(read_messages(BytesIO(data), types.Response))
    assert res
    assert 'end_block' == res.WhichOneof('value')

    new_block_hash = calculate_hash([block0['app_hash'], new_block_txn_hash])

    data = p.process('commit', None)
    res = next(read_messages(BytesIO(data), types.Response))
    assert res.commit.data == new_block_hash.encode('utf-8')
    assert b.get_transaction(tx.id).id == tx.id

    block0 = b.get_latest_block()
    assert block0
    assert block0['height'] == 1
    assert block0['app_hash'] == new_block_hash

    # empty block should not update height
    r = types.Request()
    r.begin_block.hash = new_block_hash.encode('utf-8')
    p.process('begin_block', r)

    r = types.Request()
    r.end_block.height = 2
    p.process('end_block', r)

    data = p.process('commit', None)
    res = next(read_messages(BytesIO(data), types.Response))
    assert res.commit.data == new_block_hash.encode('utf-8')

    block0 = b.get_latest_block()
    assert block0
    assert block0['height'] == 1

    # when empty block is generated hash of previous block should be returned
    assert block0['app_hash'] == new_block_hash
示例#6
0
def test_handler():
    app = ExampleApp()
    p = ProtocolHandler(app)

    # Echo
    req = Request(echo=RequestEcho(message='hello'))
    raw = p.process('echo', req)
    resp = __deserialze(raw)
    assert resp.echo.message == 'hello'

    # Flush
    req = Request(flush=RequestFlush())
    raw = p.process('flush', req)
    resp = __deserialze(raw)
    assert isinstance(resp.flush, ResponseFlush)

    # Info
    req = Request(info=RequestInfo(version='16'))
    raw = p.process('info', req)
    resp = __deserialze(raw)
    assert resp.info.version == '16'
    assert resp.info.data == 'hello'
    assert resp.info.last_block_height == 0
    assert resp.info.last_block_app_hash == b'0x12'

    # init_chain
    val_a = ValidatorUpdate(power=10,
                            pub_key=PubKey(type='amino_encoded',
                                           data=b'a_pub_key'))
    val_b = ValidatorUpdate(power=10,
                            pub_key=PubKey(type='amino_encoded',
                                           data=b'b_pub_key'))

    v = [val_a, val_b]
    req = Request(init_chain=RequestInitChain(validators=v))
    raw = p.process('init_chain', req)
    resp = __deserialze(raw)
    assert isinstance(resp.init_chain, ResponseInitChain)

    # check_tx
    req = Request(check_tx=RequestCheckTx(tx=b'helloworld'))
    raw = p.process('check_tx', req)
    resp = __deserialze(raw)
    assert resp.check_tx.code == CodeTypeOk
    assert resp.check_tx.data == b'helloworld'
    assert resp.check_tx.log == 'bueno'

    # deliver_tx
    req = Request(deliver_tx=RequestDeliverTx(tx=b'helloworld'))
    raw = p.process('deliver_tx', req)
    resp = __deserialze(raw)
    assert resp.deliver_tx.code == CodeTypeOk
    assert resp.deliver_tx.data == b'helloworld'
    assert resp.deliver_tx.log == 'bueno'

    # query
    req = Request(query=RequestQuery(path='/dave', data=b'0x12'))
    raw = p.process('query', req)
    resp = __deserialze(raw)
    assert resp.query.code == CodeTypeOk
    assert resp.query.value == b'0x12'

    # begin_block
    req = Request(begin_block=RequestBeginBlock(hash=b'0x12'))
    raw = p.process('begin_block', req)
    resp = __deserialze(raw)
    assert isinstance(resp.begin_block, ResponseBeginBlock)

    # end_block
    req = Request(end_block=RequestEndBlock(height=10))
    raw = p.process('end_block', req)
    resp = __deserialze(raw)
    assert resp.end_block.validator_updates
    assert len(resp.end_block.validator_updates) == 2
    assert resp.end_block.validator_updates[0].pub_key.data == b'a_pub_key'
    assert resp.end_block.validator_updates[1].pub_key.data == b'b_pub_key'

    # Commit
    req = Request(commit=RequestCommit())
    raw = p.process('commit', req)
    resp = __deserialze(raw)
    assert resp.commit.data == b'0x1234'

    # No match
    raw = p.process('whatever', None)
    resp = __deserialze(raw)
    assert resp.exception.error == "ABCI request not found"

    # set_option
    req = Request(set_option=RequestSetOption(key='name', value='dave'))
    raw = p.process('set_option', req)
    resp = __deserialze(raw)
    assert resp.set_option.code == CodeTypeOk
    assert resp.set_option.log == 'name=dave'
示例#7
0
def test_app(b, init_chain_request):
    from bigchaindb import App
    from bigchaindb.tendermint_utils import calculate_hash
    from bigchaindb.common.crypto import generate_key_pair
    from bigchaindb.models import Transaction

    app = App(b)
    p = ProtocolHandler(app)

    data = p.process('info',
                     types.Request(info=types.RequestInfo(version=__tm_supported_versions__[0])))
    res = next(read_messages(BytesIO(data), types.Response))
    assert res
    assert res.info.last_block_app_hash == b''
    assert res.info.last_block_height == 0
    assert not b.get_latest_block()

    p.process('init_chain', types.Request(init_chain=init_chain_request))
    block0 = b.get_latest_block()
    assert block0
    assert block0['height'] == 0
    assert block0['app_hash'] == ''

    pk = codecs.encode(init_chain_request.validators[0].pub_key.data, 'base64').decode().strip('\n')
    [validator] = b.get_validators(height=1)
    assert validator['public_key']['value'] == pk
    assert validator['voting_power'] == 10

    alice = generate_key_pair()
    bob = generate_key_pair()
    tx = Transaction.create([alice.public_key],
                            [([bob.public_key], 1)])\
                    .sign([alice.private_key])
    etxn = json.dumps(tx.to_dict()).encode('utf8')

    r = types.Request(check_tx=types.RequestCheckTx(tx=etxn))
    data = p.process('check_tx', r)
    res = next(read_messages(BytesIO(data), types.Response))
    assert res
    assert res.check_tx.code == 0

    r = types.Request()
    r.begin_block.hash = b''
    p.process('begin_block', r)

    r = types.Request(deliver_tx=types.RequestDeliverTx(tx=etxn))
    data = p.process('deliver_tx', r)
    res = next(read_messages(BytesIO(data), types.Response))
    assert res
    assert res.deliver_tx.code == 0

    new_block_txn_hash = calculate_hash([tx.id])

    r = types.Request(end_block=types.RequestEndBlock(height=1))
    data = p.process('end_block', r)
    res = next(read_messages(BytesIO(data), types.Response))
    assert res
    assert 'end_block' == res.WhichOneof('value')

    new_block_hash = calculate_hash([block0['app_hash'], new_block_txn_hash])

    data = p.process('commit', None)
    res = next(read_messages(BytesIO(data), types.Response))
    assert res.commit.data == new_block_hash.encode('utf-8')
    assert b.get_transaction(tx.id).id == tx.id

    block0 = b.get_latest_block()
    assert block0
    assert block0['height'] == 1
    assert block0['app_hash'] == new_block_hash

    # empty block should not update height
    r = types.Request()
    r.begin_block.hash = new_block_hash.encode('utf-8')
    p.process('begin_block', r)

    r = types.Request()
    r.end_block.height = 2
    p.process('end_block', r)

    data = p.process('commit', None)
    res = next(read_messages(BytesIO(data), types.Response))
    assert res.commit.data == new_block_hash.encode('utf-8')

    block0 = b.get_latest_block()
    assert block0
    assert block0['height'] == 2

    # when empty block is generated hash of previous block should be returned
    assert block0['app_hash'] == new_block_hash