def encode_validator(v): ed25519_public_key = v['pub_key']['data'] # NOTE: tendermint expects public to be encoded in go-amino format pub_key = PubKey(type='ed25519', data=bytes.fromhex(ed25519_public_key)) return Validator(pub_key=pub_key, address=b'', power=v['power'])
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'
def generate_validator(): addr = codecs.decode(generate_address(), 'hex') pk, _ = generate_key_pair() pub_key = PubKey(type='ed25519', data=pk.encode()) val = Validator(address=addr, power=10, pub_key=pub_key) return val