示例#1
0
def test_info(b):
    r = RequestInfo(version=__tm_supported_versions__[0])
    app = App(b)

    res = app.info(r)
    assert res.last_block_height == 0
    assert res.last_block_app_hash == b''

    b.store_block(Block(app_hash='1', height=1, transactions=[])._asdict())
    res = app.info(r)
    assert res.last_block_height == 1
    assert res.last_block_app_hash == b'1'

    # simulate a migration and assert the height is shifted
    b.store_abci_chain(2, 'chain-XYZ')
    app = App(b)
    b.store_block(Block(app_hash='2', height=2, transactions=[])._asdict())
    res = app.info(r)
    assert res.last_block_height == 0
    assert res.last_block_app_hash == b'2'

    b.store_block(Block(app_hash='3', height=3, transactions=[])._asdict())
    res = app.info(r)
    assert res.last_block_height == 1
    assert res.last_block_app_hash == b'3'

    # it's always the latest migration that is taken into account
    b.store_abci_chain(4, 'chain-XYZ-new')
    app = App(b)
    b.store_block(Block(app_hash='4', height=4, transactions=[])._asdict())
    res = app.info(r)
    assert res.last_block_height == 0
    assert res.last_block_app_hash == b'4'
示例#2
0
def test_encoding_decoding():
    echo = Request(echo=RequestEcho(message="hello"))
    #echo = to_request_echo('hello')
    raw = write_message(echo)
    buffer = BytesIO(raw)
    req = next(read_messages(buffer, Request))
    assert 'echo' == req.WhichOneof("value")

    info = Request(info=RequestInfo(version="18.0"))
    raw1 = write_message(info)
    buffer1 = BytesIO(raw1)
    req = next(read_messages(buffer1, Request))
    assert 'info' == req.WhichOneof("value")
示例#3
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'
示例#4
0
def test_info_aborts_if_chain_is_not_synced(b):
    b.store_abci_chain(0, 'chain-XYZ', False)

    with pytest.raises(SystemExit):
        App(b).info(RequestInfo())