Skip to content

Commit

Permalink
Merge branch 'release/1.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
tleekkul committed Mar 2, 2022
2 parents 9e97267 + 704007c commit d415806
Show file tree
Hide file tree
Showing 12 changed files with 334 additions and 73 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changelog

## [Unreleased]

### Changed

- Upgrade SmartBCH from v0.3.5 to v0.4.2
- SmartBCH now use smartbch's official docker image
134 changes: 95 additions & 39 deletions bch-devsuite
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import subprocess
import sys
import time
from dataclasses import asdict, dataclass
from datetime import datetime
from enum import Enum
from pathlib import Path
from typing import List, Optional
from urllib.parse import urlparse

import inquirer
import toml
import typer
from eth_account import Account
from jinja2 import Environment, FileSystemLoader
Expand All @@ -24,9 +26,6 @@ __version__ = "0.1.0"
DEFAULT_DOCKER_NETWORK = "bch-devsuite-network"
DEFAULT_REGTEST_BLOCKS = 200
DEFAULT_WAIT_TIME = 6
BCH_GENERATE_ADDRESS = "bchreg:qzerfrqkvsclmrlx3hzl0j2ac2eh0k88wqa0efygtm"
BCH_WIF = "cUDjy47QLpAUyv1beivDPoYCHjCRVqwRMwCuSnkdF4teVFBDD1ip"



def subprocess_run(command):
Expand Down Expand Up @@ -195,26 +194,28 @@ def handle_smartbch():
if config.network == Network.REGTEST:
typer.echo("Generate some private keys only used for test")
output = subprocess_run(
f"docker run --rm -i ghcr.io/actorforth/smartbch:v0.3.5 gen-test-keys -n 10"
f"docker run --rm -i smartbch/smartbchd:v0.4.2 gen-test-keys -n 10"
)
config.smartbch = SmartBCHConf(
test_keys=output.stdout.decode().strip().split("\n")
enabled=True, test_keys=output.stdout.decode().strip().split("\n")
)
elif config.network in (Network.TESTNET, Network.MAINNET):
config.smartbch = SmartBCHConf(enabled=True)


def handle_bch_rpc_conf():
host = inquirer.text(message="BCH RPC host", default="http://bch-node")
port = inquirer.text(message="BCH RPC port", default=config.rpc_port())
username = inquirer.text(message="BCH RPC username", default="actorforth")
password = inquirer.text(message="BCH RPC password (randomly generated by default)", default=''.join(random.choices(string.ascii_letters + string.digits, k=32)))
password = inquirer.text(
message="BCH RPC password (randomly generated by default)",
default="".join(random.choices(string.ascii_letters + string.digits, k=32)),
)
config.bch_rpc_conf = RPCConf(
host=host,
port=port,
username=username,
password=password
host=host, port=port, username=username, password=password
)


component_handlers = {
"local_node": handle_local_node,
"restapi": handle_restapi,
Expand All @@ -226,30 +227,6 @@ success = typer.style("✓", fg=typer.colors.GREEN, bold=True)
failed = typer.style("✖", fg=typer.colors.RED, bold=True)


def build_config():
network = inquirer.list_input(
message="Network?", choices=NETWORKS, default=NETWORKS[0]
)
config.network = network
components: List[str] = inquirer.checkbox(
message="Which components do you want to install?",
choices=COMPONENTS,
default=[e[1] for e in COMPONENTS],
)
for component in components:
component_handlers[component]()
handle_bch_rpc_conf()
docker_network = inquirer.text(
message="docker network", default=DEFAULT_DOCKER_NETWORK
)
config.docker_network = docker_network

exposed_ports = typer.confirm("Exposed port?")
config.exposed_ports = exposed_ports
print(json.dumps(asdict(config), indent=2))
typer.confirm("Confirm?", abort=True)


class DockerMisconfiguredException(Exception):
pass

Expand Down Expand Up @@ -369,7 +346,13 @@ def setup_docker_compose():
)
)
with open(".lock", "w") as lock_file:
json.dump(asdict(config), lock_file)
json.dump(
{
**asdict(config),
**{"version": __version__, "generated_at": datetime.now().isoformat()},
},
lock_file,
)
typer.echo(success)


Expand All @@ -379,11 +362,13 @@ def setup_smartbch():
"""
pass


def make_executable(path):
mode = os.stat(path).st_mode
mode |= (mode & 0o444) >> 2 # copy R bits to X
mode |= (mode & 0o444) >> 2 # copy R bits to X
os.chmod(path, mode)


def setup_slpdb():
if config.slp:
typer.echo(f"Setting up SLPDB...", nl=False)
Expand All @@ -393,9 +378,10 @@ def setup_slpdb():
config=config, rpc_port=config.rpc_port()
)
)
make_executable('./configs/slpdb-config.sh')
make_executable("./configs/slpdb-config.sh")
typer.echo(success)


def pull_images():
"""
Pull docker image
Expand Down Expand Up @@ -520,17 +506,54 @@ def finish():
# TODO: Show open ports for each component


def read_config(input_file: Path):
global config
with open(input_file, "r") as f:
cf = toml.loads(f.read())
# network
config.network = Network(cf["network"])
# local node
if "local_node" in cf and cf["local_node"]:
cf_local_node = cf["local_node"]
config.local_node = LocalNode(
node=cf_local_node["node"],
regtest_blocks=cf_local_node.get("regtest_blocks"),
)
# rest service
if "rest_service" in cf and cf["rest_service"]:
config.rest_service = RestService(cf["rest_service"])
# slp
if "slp" in cf and cf["slp"]:
config.slp = SLPMongoConf(
username=cf["slp"]["username"], password=cf["slp"]["password"]
)
if "smartbch" in cf and cf["smartbch"]:
config.smartbch = SmartBCHConf(
enabled=cf["smartbch"]["enabled"],
test_keys=cf["smartbch"].get("test_keys", []),
)
config.bch_rpc_conf = RPCConf(
host=cf["bch_rpc_conf"]["host"],
port=cf["bch_rpc_conf"]["port"],
username=cf["bch_rpc_conf"]["username"],
password=cf["bch_rpc_conf"]["password"],
)


@app.command()
def init(
version: Optional[bool] = typer.Option(
None, "--version", callback=version_callback, is_eager=True
)
),
config_file: Optional[Path] = typer.Option(
None, "-f", "--config-file", prompt=True
),
):
"""
Initialise bch-devsuite
"""
ensure_clean()
build_config()
read_config(config_file)
validate_dependencies()
typer.echo("\n##############")
typer.echo("# SETTING UP #")
Expand All @@ -548,6 +571,39 @@ def init(
finish()


@app.command()
def build_config(output: Path):
"""
Build configuration and output to a file
"""
network = inquirer.list_input(
message="Network?", choices=NETWORKS, default=NETWORKS[0]
)
config.network = network
components: List[str] = inquirer.checkbox(
message="Which components do you want to install?",
choices=COMPONENTS,
default=[e[1] for e in COMPONENTS],
)
for component in components:
component_handlers[component]()
handle_bch_rpc_conf()
docker_network = inquirer.text(
message="docker network", default=DEFAULT_DOCKER_NETWORK
)
config.docker_network = docker_network

exposed_ports = typer.confirm("Exposed port?")
config.exposed_ports = exposed_ports
print(toml.dumps(asdict(config)))
typer.confirm("Confirm?", abort=True)
with open(output, "w") as f:
f.write(toml.dumps(asdict(config)))
typer.echo(
f"Config file generated and saved at {output.absolute()}, you can initialize the bch-devsuite stack with ./bch-devsuite init {output.absolute()}"
)


def remove(name: str, path: str):
typer.echo(f"Removing {name}...", nl=False)
subprocess.run(f"rm -rf {path}", shell=True, check=True)
Expand Down
3 changes: 3 additions & 0 deletions clean
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ deleteIfExists "SLPDB data" "./data/mongodb"
# Remove indexer data files
deleteIfExists "indexer data" "./data/fulcrum"

# Remove smartbch data
deleteIfExists "SmartBCH data" "./data/smartbchd"

# Remove config file and lock
deleteIfExists "config file" "./config.json"
deleteIfExists "config file" "./configs/bitcoin-unlimited.conf"
Expand Down
79 changes: 57 additions & 22 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

To run this node, you must have the follow software installed on your local machine:

* Docker (including docker-compose)
* Python 3
* Virtualenv (`sudo apt install python3-venv`)
* Openssl
* Git
- Docker (including docker-compose)
- Python 3
- Virtualenv (`sudo apt install python3-venv`)
- Openssl
- Git

### Cloning this repository

Expand All @@ -25,28 +25,65 @@ source ./venv/bin/activate
pip install -r requirements.txt
```

### Configuration

bch-devsuite initialize the infrastructure from configuration file, see configuration for more details.

### Setup infrastructure

This will check that the necessary software is installed, and then it will download and prepare the docker containers. For a full list of options, run ./setup with no arguments to see its usage.
#### 1. Generate configuration

**NOTE:** If you wish to have any custom changes applied to the Bitcoin Unlimited, or REST API services, be sure to apply those changes within the _bitcoin.conf_, _fulcrum-config.conf_, or _restapi-config.sh_ files, respectively, before executing the setup script. Bitcoin Cash Node can alternatively be used with fulcrum, by commenting out bitcoin unlimited and uncommenting bitcoin cash node and fulcrum. These can not currently run simultaneously, and one or the other must be used.
You can run this command and follow the command-line wizard

```bash
./bch-devsuite init
# First we select a network, which must be regtest, mainnet or testnet
# Next we specify the node, bitcoin unlimited or bitcoincash node
# Next we select a rest interface, bchrest is a fork of the rest.bitcoin.com api, while bchapi is a rest utilized with the bch-js library
# Slp is optional, selecting this option enables slpdb, slpserve and mongodb
# Smartbch is optional, if the network is regtest, this also generate 10 test keys.
./bch-devsuite build-config /tmp/bch-devsuite.toml
```

![build-config screencast](./assets/build-config.gif "build-config screencast")

or create a config file by hand, note that you can remove the part if you don't want that component. See [Configuration Examples](./configuration-examples.md) for more examples.

```toml
# Example TOML config file

network = "mainnet" # mainnet | testnet | regtest
rest_service = "bchrest" # bchrest or bchapi
exposed_ports = true
docker_network = "bch-devsuite-network" # docker-network name

[local_node] # Local node configuration
node = "bu" # bu (bitcoin unlimited) or bchn (bitcoin cash node)
wait_time = 6 # a delay before running initializes command. Need to be high if you run on a slow system.

[slp] # SLPDB
username = "actorforth" # MongoDB username
password = "123ldsfoijqwerj" # MongoDB password

[smartbch] # SmartBCH
enabled = true
test_keys = [ "0b7cdf43329298b26d34d311b25d39f19c60fff25ba45b121284f91e12f17658", "b4d85a7a944b08bab74d0e9e9d612ee409649b382e4de500ee3bd7b7e9c6954f", "216fe772968f326d1b992da744db79fcf06cf6f1142d18086fb4b5a7005cdb8f", "adb378c6b0b9b9cb6190c88cbcaa992388f8e37f1d9c7fc791d08201d04047dc", "09b13dbd311823699802bad7240315021f9e79fe029cc0c0a7a15ab614f303d3", "7c5f4f8eb1f8a82dc9f243350082a1542b2d77d09832023b5cf8f158196a717e", "2917909f71ca82665e6f9ab50b05ecc869f49b9157d0b17976ccd000b3987e29", "989a02864785024b8488d4b22bbdea98048389c18879c18a95e72fbca11c0048", "e473abfa2982915d2cadb204dd41e41afce1b4e8851783a6b1356f5e6784774b", "e3bab3da3a55ac52b241f5d4c2066125b47e197316339540536f66ff92f38585"] # test accounts, only needed for regtest

[bch_rpc_conf]
host = "http://bch-node" # BCH RPC host, leave it to be "http://bch-node" if you also run BCH local node with bch-devsuite
port = "8332" # BCH RPC port
username = "actorforth" # BCH RPC username
password = "BWrzap0bqMjezoeHtOzgOOcUgzkxaL6w" # BCH RPC password
```

**NOTE:** A RPC password and username prompt will appear for the node, these values will be stored in generated docker-compose.yml file.
#### 2. Init infrastructure

Run:

**NOTE:** The SLP option will prompt the user to input a MONGODB username and password, this is to prevent external parties from modifying your database if the ports are exposed.
```bash
./bch-devsuite init -f /tmp/bch-devsuite.toml
```

This command will read configuration file, generate files, check necessary softwares is installed, and then it will download and prepare the docker containers.
![init screencast](./assets/init.gif "init screencast")

### Running infrastructure

Execute the _services_ script to start the node, indexer, rest API, and/or SLPDB (depending which ones chose in the _setup_ script).
Execute the _services_ script to start the node, indexer, rest API, and/or SLPDB (depending which ones defined in configuration file).

```bash
./bch-devsuite start
Expand All @@ -60,7 +97,7 @@ Execute the _services_ script to start the node, indexer, rest API, and/or SLPDB

Expected result

```
```json
{
"chain": "regtest",
"blocks": 200,
Expand Down Expand Up @@ -96,10 +133,8 @@ Expected result
}
}
],
"bip9_softforks": {
},
"bip135_forks": {
}
"bip9_softforks": {},
"bip135_forks": {}
}
```

Expand All @@ -116,7 +151,7 @@ Once you decide to call it a day, you can shut down your local environment by ex
If you experience any issues, or would like to completely erase the current wallet and node containers, run the following script:

```bash
./clean
sudo ./clean
```

**NOTE:** this command may need to be ran with sudo while on linux
Expand Down
4 changes: 2 additions & 2 deletions docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ Two REST APIs can be run: [rest.bitcoin.com](https://github.com/ActorForth/rest.

#### 1. rest.bitcoin.com tech stack

![rest.bitcoin.com Tech Stack](assets/rest-bitcoin-com\_techstack.jpg)
![rest.bitcoin.com Tech Stack](assets/rest-bitcoin-com_techstack.jpg)

All these components work together in a stack, which means that if the lower part is missing then some of the API in [rest.bitcoin.com](https://github.com/ActorForth/rest.bitcoin.com) might not function properly.

The main reason why you should use all of our fork stacks is because we maintain and guarantee that all of the components will work well with the Regtest address format. Allowing you to test locally more easily.

The blue part is the node that you choose. The default node is Bitcoin unlimited and there is Bitcoin Cash node as an option as well. Currently, this node is always required.
The blue part is the node that you choose. The default node is Bitcoin unlimited and there is Bitcoin Cash node as an option as well.

The red part is an indexer. ElectrsCash is for indexing Bitcoin cash transactions and aggregate BCH balance per address. Without these, you won't be able to query the amount of BCH you own in a particular address. OpenSight is a shim micro service to make ElectrsCash compatible with [rest.bitcoin.com](https://github.com/ActorForth/rest.bitcoin.com) API interface. [SLPDB](slp.dev/tooling/slpdb/#what-is-slpdb) is an indexer for SLP token, color coin on top of Bitcoin Cash, this will be verified if the data relate to the color coin is valid and checks if the coin follows the SLP off-chain consensus.

Expand Down
Binary file added docs/assets/build-config.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/init.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d415806

Please sign in to comment.