Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 138 additions & 0 deletions ONE_BRANCH_TO_RULE_THEM_ALL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# One Branch to Rule Them All

This branch refactors the PHPCoin codebase to support multiple networks (mainnet and testnet) from a single `main` branch. The active network is determined by the contents of a `./chain_id` file.

## What was done

* **Multi-network support:** The codebase was refactored to support multiple networks from a single branch.
* **`chain_id` file:** The `./chain_id` file is now the source of truth for determining the active network. `00` for mainnet, `01` for testnet.
* **Network-specific configurations:** Per-chain files for config, checkpoints, rewards, coinspecs, and genesis have been created.
* **Dynamic loading:** The code now dynamically loads the correct network-specific files based on the `chain_id`.
* **Refactored network identification:** The code has been refactored to use `CHAIN_ID` instead of `NETWORK` for network identification.
* **Updated scripts:** The `install_node.sh`, `build/docker/miner/start.sh`, and `build/docker/node/docker_start.sh` scripts have been updated to be driven by the `chain_id` file.
* **Safe defaults:** The system now safely defaults to mainnet if the `chain_id` file is missing or invalid.

## Updating Existing Installations

### From `main` (mainnet)

1. **Back up the database:**
```bash
sudo mysqldump phpcoin > phpcoin_backup.sql
```
2. **Back up the application directory:**
```bash
sudo cp -a /var/www/phpcoin-mainnet /var/www/phpcoin-mainnet.bak
```
3. **Move the installation directory:**
```bash
sudo mv /var/www/phpcoin-mainnet /var/www/phpcoin
```
4. **Update the code:**
```bash
cd /var/www/phpcoin
git pull origin one_branch_to_rule_them_all
```
5. **Rename the database:**
```bash
sudo mysql -e "RENAME DATABASE phpcoin TO phpcoin_00;"
```
6. **Create the `chain_id` file:**
```bash
echo "00" > chain_id
```
7. **Handle the configuration file:**
```bash
cd /var/www/phpcoin
mv config/config.inc.php config/config.inc.php.bak
cp config/config-sample.inc.php config/config.inc.php
```
*After this, you will need to manually copy your old settings from `config.inc.php.bak` to the new `config.inc.php`.*
8. **Re-run the install script to update configurations:**
```bash
cd /var/www/phpcoin
sudo bash scripts/install_node.sh
```

### From `test` (testnet)

1. **Back up the database:**
```bash
sudo mysqldump phpcoin > phpcoin_backup.sql
```
2. **Back up the application directory:**
```bash
sudo cp -a /var/www/phpcoin-testnet /var/www/phpcoin-testnet.bak
```
3. **Move the installation directory:**
```bash
sudo mv /var/www/phpcoin-testnet /var/www/phpcoin
```
4. **Update the code:**
```bash
cd /var/www/phpcoin
git pull origin one_branch_to_rule_them_all
```
5. **Rename the database:**
```bash
sudo mysql -e "RENAME DATABASE phpcoin TO phpcoin_01;"
```
6. **Create the `chain_id` file:**
```bash
echo "01" > chain_id
```
7. **Handle the configuration file:**
```bash
cd /var/www/phpcoin
mv config/config.inc.php config/config.inc.php.bak
cp config/config-sample.inc.php config/config.inc.php
```
*After this, you will need to manually copy your old settings from `config.inc.php.bak` to the new `config.inc.php`.*
8. **Re-run the install script to update configurations:**
```bash
cd /var/www/phpcoin
sudo bash scripts/install_node.sh
```

## What needs to be tested

### 1. Installation

* **`install_node.sh`**
* Run the script with a `chain_id` file containing `00` and verify that a mainnet node is installed.
* Check that the `phpcoin_00` database is created.
* Check that the nginx configuration is created for port 80.
* Check that the mainnet blockchain is imported.
* Run the script with a `chain_id` file containing `01` and verify that a testnet node is installed.
* Check that the `phpcoin_01` database is created.
* Check that the nginx configuration is created for port 81.
* Check that the testnet blockchain is imported.
* Run the script without a `chain_id` file and verify that a mainnet node is installed by default.

### 2. Docker

* **Node**
* Build and run the node container with `CHAIN_ID=00` and verify that a mainnet node is started.
* Build and run the node container with `CHAIN_ID=01` and verify that a testnet node is started.
* **Miner**
* Build and run the miner container with a `chain_id` file containing `00` and verify that it connects to the mainnet mining node.
* Build and run the miner container with a `chain_id` file containing `01` and verify that it connects to the testnet mining node.

### 3. Application Logic

* **Database Connection**
* Verify that the application connects to the `phpcoin_00` database when `chain_id` is `00`.
* Verify that the application connects to the `phpcoin_01` database when `chain_id` is `01`.
* **Configuration Loading**
* Verify that the correct `config.XX.php`, `checkpoints.XX.php`, `rewards.XX.php`, `coinspec.XX.php`, and `genesis.XX.php` files are loaded based on the `chain_id`.
* **Network-specific Logic**
* Verify that all logic that was previously dependent on the `NETWORK` constant now correctly uses the `CHAIN_ID` constant.
* Specifically check the following files:
* `include/class/Blockchain.php`
* `include/class/Transaction.php`
* `web/apps/common/include/top.php`
* `web/apps/explorer/address.php`

## Auto-update

The auto-update feature is compatible with the One Branch system. The `chain_id` of the node will be preserved during the auto-update process. The auto-updater is also branch-aware, meaning it will pull updates for the currently checked-out git branch, making it safe to use on development branches.
37 changes: 13 additions & 24 deletions build/docker/miner/README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
Docker image for running PHPCoin miner.
# PHPCoin miner on docker

You can run the phpcoin miner using docker.

Start with command:
```
docker run -itd --name phpcoin-miner \
-e NETWORK=<NETWORK> \
-e NODE=<NODE> \
-e ADDRESS=<ADDRESS> \
-e CPU=<CPU> \
-e ADDRESS=<YOUR_ADDRESS> \
-e NODE=<NODE_URL> \
-e CPU=<CPU_PERCENT> \
-e THREADS=<THREADS> \
-v /path/to/your/phpcoin/chain_id:/phpcoin/chain_id \
phpcoin/miner
```


Options:

NETWORK: mainnet(default) or testnet

NODE: specify mining node or use default network nodes

ADDRESS: address where mining rewards goes

CPU: maximum usage of single CPU (default 100%)

THREADS: number of CPU threads to use (default all)
Explanation of parameters:

Check miner status
```
docker logs -f phpcoin-miner
```
ADDRESS: your address where you will receive the mining reward
NODE: the url of the node where you are mining
CPU: the percent of cpu to be used for mining
THREADS: number of threads to be used for mining
/path/to/your/phpcoin/chain_id: the path to your `chain_id` file. This is used to determine whether to mine on mainnet or testnet.
9 changes: 7 additions & 2 deletions build/docker/miner/start.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#!/bin/bash

CHAIN_ID_FILE="/phpcoin/chain_id"
if [ -f "$CHAIN_ID_FILE" ]; then
CHAIN_ID=$(cat "$CHAIN_ID_FILE")
else
CHAIN_ID="00"
fi

NETWORK="${NETWORK:-mainnet}"
ADDRESS="${ADDRESS:-PtD756PoeBfw6KgCLqjpD4sYdZsaFu536F}"
CPU="${CPU:-100}"
NUM_THREADS=$(nproc --all)
THREADS="${THREADS:-$NUM_THREADS}"

MINING_NODE=https://m1.phpcoin.net
if [ "$NETWORK" = "testnet" ]
if [ "$CHAIN_ID" = "01" ]
then
MINING_NODE=https://miner1.phpcoin.net
fi
Expand Down
3 changes: 0 additions & 3 deletions build/docker/node/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
FROM ubuntu:22.04

ARG NETWORK=mainnet
ENV NETWORK=$NETWORK

ENV DEBIAN_FRONTEND=noninteractive

EXPOSE 80
Expand Down
26 changes: 14 additions & 12 deletions build/docker/node/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
This is docker image for PHPCoin node (mainnet and testnet).
# PHPCoin node on docker

On first run container will download install script, install node and restore latest blockchain database
You can run your own phpcoin node using docker.

In order to preserve config file with private keys you should create volume and start container with volume:
This will run a full node, with miner enabled, open api, and admin panel.

```
docker volume create phpcoin-config
docker run -itd --name phpcoin -p 81:80 -e EXT_PORT=81 -v phpcoin-config:/var/www/phpcoin/config phpcoin/node
```
To run a mainnet node:
docker run -itd --name phpcoin-main -p 80:80 -e EXT_PORT=80 -e CHAIN_ID=00 -v phpcoin-config-main:/var/www/phpcoin/config phpcoin/node

For running testnet node run container with other parameters:
```
docker volume create phpcoin-config-test
docker run -itd --name phpcoin-test -p 91:80 -e EXT_PORT=91 -e NETWORK=testnet -v phpcoin-config-test:/var/www/phpcoin/config phpcoin/node
```
To run a testnet node:
docker run -itd --name phpcoin-test -p 91:80 -e EXT_PORT=91 -e CHAIN_ID=01 -v phpcoin-config-test:/var/www/phpcoin/config phpcoin/node

This will map the config folder to a volume, so you can keep the node configuration.
You can check the node logs by running:
docker logs -f phpcoin-main

To enter the node shell, run:
docker exec -it phpcoin-main bash
12 changes: 6 additions & 6 deletions build/docker/node/docker_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
FILE=/first-run
if test -f "$FILE"; then
echo "First run node"
wget https://phpcoin.net/scripts/install_node.sh -O /install_node.sh
chmod +x install_node.sh
if [ "$NETWORK" = "mainnet" ]
then
/install_node.sh --docker
if [ "$CHAIN_ID" = "01" ]; then
echo "01" > /var/www/phpcoin/chain_id
else
/install_node.sh --docker --network testnet
echo "00" > /var/www/phpcoin/chain_id
fi
wget https://phpcoin.net/scripts/install_node.sh -O /install_node.sh
chmod +x install_node.sh
/install_node.sh --docker
rm $FILE
rm /install_node.sh
else
Expand Down
105 changes: 105 additions & 0 deletions config/config.00.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Default config file
*/
// Default database connection
$_config['db_connect'] = 'mysql:host=localhost;dbname=phpcoin_00;charset=utf8';
$_config['db_user'] = 'phpcoin';
$_config['db_pass'] = 'phpcoin';

// Allow others to connect to the node api (if set to false, only the below 'allowed_hosts' are allowed)
$_config['public_api'] = true;

// Hosts that are allowed to mine on this node
$_config['allowed_hosts'] = ['*'];

// The initial peers to sync from
$_config['initial_peer_list'] = [
'https://main1.phpcoin.net',
'https://main2.phpcoin.net',
'https://main3.phpcoin.net'
];

// does not peer with any of the peers. Uses the seed peers and syncs only from those peers. Requires a cronjob on sync.php
$_config['passive_peering'] = false;

// limit number of peers for propagate
$_config['peers_limit']=30;

// set custom interface to which server listens
//$_config['interface']="";

// set custom proxy for outgoing requests
//$_config['proxy']="";

// set node to offline, do not send or receive peer requests
$_config['offline']=false;

// set outgoing proxy for peer requests
$_config['proxy']=null;

// The maximum transactions to accept from a single peer
$_config['peer_max_mempool'] = 100;

// Block accepting transfers from addresses blacklisted by the PHPCoin devs
$_config['use_official_blacklist'] = true;

// Recheck the last blocks
$_config['sync_recheck_blocks'] = 10;

// Enable setting a new hostname (should be used only if you want to change the hostname)
$_config['allow_hostname_change'] = false;

// Enable log output to the specified file
$_config['enable_logging'] = true;

// The specified file to write to (this should not be publicly visible)
$_config['log_file'] = 'tmp/phpcoin.log';

// Log verbosity (default 0, maximum 5)
$_config['log_verbosity'] = 0;

// Log to server log, e.g. error_log to apache
$_config['server_log'] = false;

/**
* Miner config
*/
$_config['miner']=false;
$_config['miner_public_key']="";
$_config['miner_private_key']="";
$_config['miner_cpu']=0;

/**
* Generator config
*/
$_config['generator']=false;
$_config['generator_public_key']="";
$_config['generator_private_key']="";

/**
* Allow web admin of node
*/
$_config['admin']=false;
$_config['admin_password']='';
//login to admin panel with private key
$_config['admin_public_key']='';

/**
* Masternode configuration
*/
$_config['masternode']=false;
$_config['masternode_public_key']="";
$_config['masternode_private_key']="";

/**
* Configuration for decentralized apps
*/
$_config['dapps']=false;
$_config['dapps_public_key']="";
$_config['dapps_private_key']="";
$_config['dapps_anonymous']=false;
$_config['dapps_disable_auto_propagate']=true;

// set server to maintenance mode
//$_config['maintenance']=1;
Loading