forked from crypto-org-chain/chain-main
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…ypto-org-chain#253) Solution: add grpc query, tx broadcast integration test generate proto+grpc code on the fly
- Loading branch information
1 parent
88a364c
commit 46962db
Showing
14 changed files
with
308 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
[flake8] | ||
max-line-length = 88 | ||
extend-ignore = E203 | ||
exclude = .git,__pycache__,./pystarport/pystarport/cosmos-sdk,./pystarport/pystarport/tendermint,./pystarport/pystarport/proto_python |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import json | ||
|
||
from pystarport.proto_python.grpc_util import GrpcUtil, GrpcUtilTxBroadcast | ||
|
||
from .utils import wait_for_new_blocks | ||
|
||
|
||
def test_query_validators(cluster): | ||
wait_for_new_blocks(cluster, 5) | ||
grpc_ip_port = cluster.ipport_grpc(0) | ||
grpc = GrpcUtil(grpc_ip_port) | ||
grpc_response = grpc.get_validators() | ||
grpc_validators = grpc_response.validators | ||
validators = cluster.validators() | ||
operators = {} | ||
for cli_validator in validators: | ||
operator_address = cli_validator["operator_address"] | ||
operators[operator_address] = cli_validator | ||
|
||
for grpc_validator in grpc_validators: | ||
assert grpc_validator.operator_address in operators | ||
|
||
|
||
def test_tx_broadcast(cluster, tmp_path): | ||
tx_txt = tmp_path / "tx.txt" | ||
sign_txt = tmp_path / "sign.txt" | ||
encode_txt = tmp_path / "encode.txt" | ||
signer1_addr = cluster.address("signer1") | ||
signer2_addr = cluster.address("signer2") | ||
signer2_balance = cluster.balance(signer2_addr) | ||
tx_amount = 5 | ||
single_tx = cluster.transfer( | ||
signer1_addr, signer2_addr, f"{tx_amount}basecro", generate_only=True | ||
) | ||
json.dump(single_tx, tx_txt.open("w")) | ||
signed_tx = cluster.sign_single_tx(tx_txt, signer1_addr) | ||
json.dump(signed_tx, sign_txt.open("w")) | ||
encoded_tx = cluster.encode_signed_tx(sign_txt) | ||
encode_file = open(encode_txt, "wb") | ||
encode_file.write(encoded_tx) | ||
encode_file.close() | ||
grpc_ip_port = cluster.ipport_grpc_tx(0) | ||
grpc = GrpcUtilTxBroadcast(grpc_ip_port) | ||
grpc.send_tx_in_base64(encoded_tx) | ||
wait_for_new_blocks(cluster, 2) | ||
new_signer2_balance = cluster.balance(signer2_addr) | ||
assert new_signer2_balance == signer2_balance + tx_amount |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/bin/bash | ||
cd ./pystarport/pystarport | ||
nix-shell . --run "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import os | ||
import sys | ||
from pathlib import Path | ||
|
||
proto_folder = Path(os.path.abspath(__file__)).parent.joinpath("proto_python") | ||
sys.path.append(str(proto_folder)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/bin/bash | ||
OUTPUT=./proto_python | ||
COSMOS=./cosmos-sdk | ||
TENDERMINT=./tendermint | ||
TMP=$(whereis grpc_python_plugin) | ||
PLUGIN="$(cut -d' ' -f2 <<<"$TMP")" | ||
mkdir $OUTPUT | ||
git clone --branch v0.40.0-rc2 https://github.com/cosmos/cosmos-sdk.git | ||
git clone --branch v0.34.0-rc5 https://github.com/tendermint/tendermint.git | ||
cp -Rf $COSMOS/third_party/proto/* $COSMOS/proto/ | ||
# cosmos | ||
protoc --proto_path=$COSMOS/proto --proto_path=$COSMOS/third_party/proto --python_out=$OUTPUT $(find $COSMOS/proto/cosmos -iname "*.proto") --grpc_python_out=$OUTPUT --plugin=protoc-gen-grpc_python=$PLUGIN | ||
# cosmos third-party | ||
protoc --proto_path=$COSMOS/third_party/proto --proto_path=$COSMOS/proto --python_out=$OUTPUT $(find $COSMOS/third_party/proto -iname "*.proto") --grpc_python_out=$OUTPUT --plugin=protoc-gen-grpc_python=$PLUGIN | ||
# tendermint | ||
protoc --proto_path=$TENDERMINT/proto --proto_path=$TENDERMINT/third_party/proto --python_out=$OUTPUT $(find $TENDERMINT/proto/tendermint -iname "*.proto") --grpc_python_out=$OUTPUT --plugin=protoc-gen-grpc_python=$PLUGIN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ pkgs ? import ../../nix { }, commit ? "" }: | ||
with pkgs; | ||
pkgs.mkShell { | ||
buildInputs = [ | ||
python38 | ||
protobuf3_13 | ||
grpc | ||
git | ||
]; | ||
shellHook = '' | ||
./convert.sh ''; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import base64 | ||
|
||
import grpc | ||
|
||
import cosmos.bank.v1beta1.tx_pb2 | ||
import cosmos.bank.v1beta1.tx_pb2_grpc | ||
import cosmos.crypto.ed25519.keys_pb2 | ||
import cosmos.staking.v1beta1.query_pb2 | ||
import cosmos.staking.v1beta1.query_pb2_grpc | ||
import tendermint.rpc.grpc.types_pb2_grpc | ||
|
||
|
||
# for query only | ||
class GrpcUtil: | ||
def __init__(self, ip_port): | ||
self.ip_port = ip_port | ||
|
||
def get_validators(self): | ||
channel = grpc.insecure_channel(self.ip_port) | ||
stub = cosmos.staking.v1beta1.query_pb2_grpc.QueryStub(channel) | ||
response = stub.Validators( | ||
cosmos.staking.v1beta1.query_pb2.QueryValidatorsRequest() | ||
) | ||
return response | ||
|
||
|
||
# for tx broadcast only | ||
class GrpcUtilTxBroadcast: | ||
def __init__(self, ip_port): | ||
self.ip_port = ip_port | ||
|
||
def send_tx_in_base64(self, tx_base64): | ||
tx_raw_bytes = base64.b64decode(tx_base64) | ||
channel = grpc.insecure_channel(self.ip_port) | ||
stub = tendermint.rpc.grpc.types_pb2_grpc.BroadcastAPIStub(channel) | ||
request = tendermint.rpc.grpc.types_pb2.RequestBroadcastTx() | ||
request.tx = tx_raw_bytes | ||
response = stub.BroadcastTx(request) | ||
return response |