Skip to content

Commit

Permalink
add light client test (#24)
Browse files Browse the repository at this point in the history
support ckb-light-client
  • Loading branch information
gpBlockchain authored Nov 23, 2023
1 parent ff53bc8 commit 1e0c0c8
Show file tree
Hide file tree
Showing 17 changed files with 778 additions and 56 deletions.
2 changes: 1 addition & 1 deletion download.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import requests
from tqdm import tqdm

versions = ['0.109.0', '0.110.2', '0.111.0','0.112.0-rc3'] # Replace with your versions
versions = ['0.109.0', '0.110.2', '0.111.0', '0.112.1'] # Replace with your versions
DOWNLOAD_DIR = "download"
SYSTEMS = {
'Windows': {
Expand Down
2 changes: 1 addition & 1 deletion download_ckb_light_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import requests
from tqdm import tqdm

versions = ['0.2.4'] # Replace with your versions
versions = ['0.2.4', '0.3.0', '0.3.1'] # Replace with your versions
DOWNLOAD_DIR = "download"
SYSTEMS = {
'Windows': {
Expand Down
18 changes: 14 additions & 4 deletions framework/ckb_light_client_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,24 @@ class CKBLightRPCClient:
def __init__(self, url):
self.url = url

def set_scripts(self, script_status):
return self.call("set_scripts", [script_status])
def set_scripts(self, script_status, command="all"):
return self.call("set_scripts", [script_status, command])

def get_scripts(self):
return self.call("get_scripts", [])

def get_cells_capacity(self,script):
return self.call("get_cells_capacity",[script])
def get_cells_capacity(self, script):
return self.call("get_cells_capacity", [script])

def fetch_transaction(self, tx_hash):
return self.call("fetch_transaction", [tx_hash])


def send_transaction(self,tx):
return self.call("send_transaction",[tx])

def fetch_transaction(self,tx_hash):
return self.call("fetch_transaction",[tx_hash])

def call(self, method, params):

Expand Down
95 changes: 91 additions & 4 deletions framework/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,46 @@ def __init__(self, url):
def get_tip_block_number(self):
return int(self.call("get_tip_block_number", []), 16)

def get_block_economic_state(self, block_hash):
return self.call("get_block_economic_state", [block_hash])

def get_block_filter(self, block_hash):
return self.call("get_block_filter", [block_hash])

def get_banned_addresses(self):
return self.call("get_banned_addresses", [])

def set_ban(self, address, command, ban_time, absolute, reason):
return self.call("set_ban", [address, command, ban_time, absolute, reason])

def get_current_epoch(self):
return self.call("get_current_epoch", [])

def get_epoch_by_number(self, epoch_number):
return self.call("get_epoch_by_number", [epoch_number])

def get_fork_block(self, block_hash, verbosity):
return self.call("get_fork_block", [block_hash, verbosity])

def get_header_by_number(self, block_number, verbosity):
return self.call("get_header_by_number", [block_number])
# , verbosity])

# return self.call("get_header_by_number", [block_number, verbosity])
# return self.call("get_header_by_number",[block_number, verbosity])

def get_indexer_tip(self):
return self.call("get_indexer_tip", [])

def local_node_info(self):
return self.call("local_node_info", [])

def ping_peers(self):
return self.call("ping_peers", [])

def remove_node(self, peer_id):
return self.call("remove_node", [peer_id])

# //"QmUsZHPbjjzU627UZFt4k8j6ycEcNvXRnVGxCPKqwbAfQS",
# "/ip4/192.168.2.100/tcp/8114"
def add_node(self, peer_id, peer_address):
Expand All @@ -26,6 +60,9 @@ def add_node(self, peer_id, peer_address):
def get_block_hash(self, block_number_hex):
return self.call("get_block_hash", [block_number_hex])

def get_block_median_time(self, block_hash):
return self.call("get_block_median_time", [block_hash])

def get_block(self, block_hash, verbosity=None, with_cycles=None):
return self.call("get_block", [block_hash, verbosity, with_cycles])

Expand All @@ -46,6 +83,9 @@ def get_block_by_number(self, block_number, verbosity=None, with_cycles=None):
def get_transaction_and_witness_proof(self, tx_hashes, block_hash=None):
return self.call("get_transaction_and_witness_proof", [tx_hashes, block_hash])

def sync_state(self):
return self.call("sync_state", [])

def truncate(self, block_hash):
return self.call("truncate", [block_hash])

Expand All @@ -64,20 +104,58 @@ def generate_block(self):
def get_deployments_info(self):
return self.call("get_deployments_info", [])

def get_blockchain_info(self):
return self.call("get_blockchain_info", [])

def get_cells(self, search_key, order, limit, after):
return self.call("get_cells", [search_key, order, limit, after])

def get_block_template(self, bytes_limit=None, proposals_limit=None, max_version=None):
return self.call("get_block_template", [bytes_limit, proposals_limit, max_version])
return self.call("get_block_template", [
# bytes_limit, proposals_limit, max_version
])

def calculate_dao_field(self, block_template):
return self.call("calculate_dao_field", [block_template])

def generate_block_with_template(self, block_template):
return self.call("generate_block_with_template", [block_template])

def calculate_dao_maximum_withdraw(self, out_point, kind):
return self.call("calculate_dao_maximum_withdraw", [out_point, kind])

def clear_banned_addresses(self):
return self.call("clear_banned_addresses", [])

def tx_pool_info(self):
return self.call("tx_pool_info", [])

def get_tip_header(self):
return self.call("get_tip_header", [])
def tx_pool_ready(self):
return self.call("tx_pool_ready", [])

def get_tip_header(self, verbosity=None):
return self.call("get_tip_header", [verbosity])

def verify_transaction_proof(self, tx_proof):
return self.call("verify_transaction_proof", [tx_proof])

def get_transaction(self, tx_hash, verbosity=None, only_committed=None):
if verbosity is None and only_committed is None:
return self.call("get_transaction", [tx_hash])
return self.call("get_transaction", [tx_hash, verbosity, only_committed])

def get_transactions(self, search_key, order, limit, after):
return self.call("get_transactions", [search_key, order, limit,after])

def dry_run_transaction(self, tx):
return self.call("dry_run_transaction", [tx])

def estimate_cycles(self, tx):
return self.call("estimate_cycles", [tx])

def get_transaction_proof(self, tx_hash, block_hash):
return self.call("get_transaction_proof", [tx_hash, block_hash])

def send_transaction(self, tx, outputs_validator="passthrough"):
return self.call("send_transaction", [tx, outputs_validator])

Expand All @@ -87,6 +165,9 @@ def get_raw_tx_pool(self, verbose=None):
def clear_tx_pool(self):
return self.call("clear_tx_pool", [])

def generate_block(self):
return self.call("generate_block", [])

def get_peers(self):
return self.call("get_peers", [])

Expand All @@ -102,9 +183,15 @@ def get_live_cell(self, index, tx_hash, with_data=True):
def submit_block(self, work_id, block):
return self.call("submit_block", [work_id, block])

def subscribe(self,topic):
return self.call("subscribe",[topic])

def get_cells_capacity(self, script):
return self.call("get_cells_capacity", [script])

def get_current_epoch(self):
return self.call("get_current_epoch", [])

def call(self, method, params):

headers = {'content-type': 'application/json'}
Expand Down Expand Up @@ -137,5 +224,5 @@ def call(self, method, params):

if __name__ == '__main__':
client = RPCClient("https://testnet.ckb.dev/")
number = client.get_tip_block_number()
number = client.subscribe("new_tip_header")
print(number)
2 changes: 1 addition & 1 deletion framework/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def clean_all_nodes(self):

def restart_all_node(self, clean_data=False):
for node in self.ckb_nodes:
node.restart(clean_data=True)
node.restart(clean_data=clean_data)
self.connected_all_nodes()

def get_all_nodes_height(self):
Expand Down
5 changes: 4 additions & 1 deletion framework/test_light_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

class CkbLightClientConfigPath(Enum):
V0_2_4 = ("source/template/ckb_light_client/0.2.4/testnet.toml.j2", "download/0.2.4/ckb-light-client")
V0_3_0 = ("source/template/ckb_light_client/0.3.0/testnet.toml.j2", "download/0.3.0/ckb-light-client")
V0_3_1 = ("source/template/ckb_light_client/0.3.0/testnet.toml.j2", "download/0.3.1/ckb-light-client")
CURRENT_TEST = ("source/template/ckb_light_client/0.3.0/testnet.toml.j2", "download/0.3.1/ckb-light-client")

def __init__(self, ckb_light_client_config_path, ckb_light_bin_path):
self.ckb_light_client_config_path = ckb_light_client_config_path
Expand Down Expand Up @@ -65,7 +68,7 @@ def getClient(self) -> CKBLightRPCClient:
return self.client

def stop(self):
run_command(f"kill $(lsof -t -i:{self.rpc_port})")
run_command(f"kill $(lsof -t -i:{self.rpc_port})",False)
time.sleep(3)

def clean(self):
Expand Down
19 changes: 14 additions & 5 deletions framework/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ class CkbNodeConfigPath(Enum):
"source/template/ckb/v112/ckb.toml.j2",
"source/template/ckb/v112/ckb-miner.toml.j2",
"source/template/ckb/v112/specs/dev.toml",
"download/0.112.0"
"download/0.112.1"
)

V112 = (
"source/template/ckb/v112/ckb.toml.j2",
"source/template/ckb/v112/ckb-miner.toml.j2",
"source/template/ckb/v112/specs/dev.toml",
"download/0.112.0"
"download/0.112.1"
)

CURRENT_MAIN = ("source/template/ckb/v111/ckb.toml.j2",
"source/template/ckb/v111/ckb-miner.toml.j2",
CURRENT_MAIN = ("source/template/ckb/v112/ckb.toml.j2",
"source/template/ckb/v112/ckb-miner.toml.j2",
"source/template/specs/mainnet.toml.j2",
"download/0.111.0")
"download/0.112.1")
V111 = (
"source/template/ckb/v111/ckb.toml.j2",
"source/template/ckb/v111/ckb-miner.toml.j2",
Expand All @@ -44,6 +44,12 @@ class CkbNodeConfigPath(Enum):
"source/template/specs/mainnet.toml.j2",
"download/0.110.2"
)
V110_TEST = (
"source/template/ckb/v110/ckb.toml.j2",
"source/template/ckb/v110/ckb-miner.toml.j2",
"source/template/specs/testnet.toml.j2",
"download/0.110.2"
)

V109 = ("source/template/ckb/v109/ckb.toml.j2", "source/template/ckb/v109/ckb-miner.toml.j2",
"source/template/ckb/v109/specs/dev.toml", "download/0.109.0")
Expand Down Expand Up @@ -135,6 +141,7 @@ def start(self):
time.sleep(3)

def stop(self):
self.stop_miner()
# run_command("kill {pid}".format(pid=self.ckb_pid))
# self.ckb_pid = -1
port = self.rpcUrl.split(":")[-1]
Expand Down Expand Up @@ -173,6 +180,8 @@ def clean(self):
run_command("rm -rf {ckb_dir}".format(ckb_dir=self.ckb_dir))

def start_miner(self):
if self.ckb_miner_pid != -1:
return
self.ckb_miner_pid = run_command(
"cd {ckb_dir} && ./ckb miner > ckb.miner.log 2>&1 &".format(ckb_dir=self.ckb_dir))
# replace check height upper
Expand Down
13 changes: 6 additions & 7 deletions prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ make prod
cp target/release/ckb-cli ../source/ckb-cli
cd ../
cp download/0.110.2/ckb-cli ./source/ckb-cli-old
#
#cp -r download/0.111.0 download/0.112.0
#git clone https://github.com/nervosnetwork/ckb.git
#cd ckb
#make prod
#cp target/prod/ckb ../download/0.112.0/ckb
#
git clone https://github.com/nervosnetwork/ckb-light-client.git
cd ckb-light-client
cargo build --release
cd ../
mkdir -p download/0.2.5
cp ckb-light-client/target/release/ckb-light-client download/0.2.5
47 changes: 47 additions & 0 deletions source/template/ckb_light_client/0.3.0/testnet.toml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# chain = "mainnet"
# chain = "testnet"
# chain = "your_path_to/dev.toml"
chain = "{{ ckb_light_client_chain }}"

[store]
path = "data/store"

[network]
path = "data/network"

listen_addresses = {{ ckb_light_client_network_listen_addresses | default(["/ip4/0.0.0.0/tcp/8118"]) | to_json }}
### Specify the public and routable network addresses
# public_addresses = []

# Node connects to nodes listed here to discovery other peers when there's no local stored peers.
# When chain.spec is changed, this usually should also be changed to the bootnodes in the new chain.
bootnodes = {{ ckb_light_client_network_bootnodes | default([]) | to_json }}

### Whitelist-only mode
# whitelist_only = false
### Whitelist peers connecting from the given IP addresses
# whitelist_peers = []

### Enable `SO_REUSEPORT` feature to reuse port on Linux, not supported on other OS yet
# reuse_port_on_linux = true

max_peers = 125
max_outbound_peers = 2
# 2 minutes
ping_interval_secs = 120
# 20 minutes
ping_timeout_secs = 1200
connect_outbound_interval_secs = 15
# If set to true, try to register upnp
upnp = false
# If set to true, network service will add discovered local address to peer store, it's helpful for private net development
discovery_local_address = false
# If set to true, random cleanup when there are too many inbound nodes
# Ensure that itself can continue to serve as a bootnode node
bootnode_mode = false

[rpc]
# Light client rpc is designed for self hosting, exposing to public network is not recommended and may cause security issues.
# By default RPC only binds to localhost, thus it only allows accessing from the same machine.
listen_address = "{{ ckb_light_client_rpc_listen_address | default("127.0.0.1:9000") }}"

4 changes: 2 additions & 2 deletions source/template/specs/testnet.toml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ lock.hash_type = "type"


[params]
genesis_epoch_length = {{ ckb_params_genesis_epoch_length }}
genesis_epoch_length = {{ ckb_params_genesis_epoch_length | default(1) }}
permanent_difficulty_in_dummy = true


[pow]
func = "{{ ckb_pow_func }}"
func = "{{ ckb_pow_func | default("Dummy") }}"
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ pkill ckb
sleep 3
rm -rf tmp
rm -rf report/report.html
echo echo "run cusscessful"
echo "run cusscessful"
Loading

0 comments on commit 1e0c0c8

Please sign in to comment.