Skip to content

Add subcommands for RPC #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2018
Merged
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
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = ["CodeChain Team <[email protected]>"]
[dependencies]
byteorder = "1.2"
clap = { version = "2", features = ["yaml"] }
codechain-miner = { git = "https://github.com/CodeChain-io/codechain-miner.git", rev = "96e200d" }
codechain-miner = { git = "https://github.com/CodeChain-io/codechain-miner.git", rev = "820e11e" }
cuckoo = { git = "https://github.com/CodeChain-io/rust-cuckoo.git", rev = "280cab9c" }
ethereum-types = "0.3.2"
env_logger = "0.5.10"
Expand Down
40 changes: 31 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,23 @@ The resulting binary file can be found at `target/release/codechain-cuckoo-miner
## Usage

```
codechain-cuckoo-miner [OPTIONS]
codechain-cuckoo-miner [OPTIONS] [SUBCOMMAND]
```

### Usage Examples
* N=0x10, M=0x8, L=6, listening on port **3333**, submitting on port **8080**, **1** concurrent jobs :

* N=0x10, M=0x8, L=6, **1** concurrent jobs :

Using HTTP (listening on port **3333**, submitting on port **8080**)
```
codechain-cuckoo-miner -n 0x10 -m 0x8 -l 6 -j 1 http -p 3333 -s 8080
```

or

Using Stratum
```
codechain-cuckoo-miner -n 0x10 -m 0x8 -l 6 -p 3333 -s 8080 -j 1
codechain-cuckoo-miner -n 0x10 -m 0x8 -l 6 -j 1 stratum
```

## Configuration
Expand All @@ -30,12 +40,24 @@ codechain-cuckoo-miner -n 0x10 -m 0x8 -l 6 -p 3333 -s 8080 -j 1

| Option | Description | Default | Required |
| :----: | ------------------------------ |:-------------:|:--------:|
| `-p` | Port number to receive job | 3333 | No |
| `-s` | Port number to submit solution | 8080 | No |
| `-j` | The number of concurrent jobs | 1 | No |
| `-n` | Number of vertices in graph | None | Yes |
| `-m` | Number of edges in graph | None | Yes |
| `-l` | Length of cycle to detect | None | Yes |
| `-j` | Number of concurrent jobs | 1 | No |

### RPC Subcommands

* HTTP

| Option | Description | Default | Required |
| :----: | ------------------------------ |:-------------:|:--------:|
| `-p` | Port number to receive job | 3333 | No |
| `-s` | Port number to submit solution | 8008 | No |

* Stratum

| Option | Description | Default | Required |
| :----: | ------------------------------ |:-------------:|:--------:|
| `-n` | Number of vertices in graph | None | Yes |
| `-m` | Number of edges in graph | None | Yes |
| `-l` | Length of cycle to detect | None | Yes |
| `-p` | Port number to stratum server | 3333 | No |
| `-i` | Miner name | | No |
| `-w` | Miner password | | No |
43 changes: 33 additions & 10 deletions src/cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@ version_short: "v"
author: CodeChain Team <[email protected]>
about: CodeChian Cuckoo Miner
args:
- listening port:
short: p
long: port
takes_value: true
default_value: "3333"
- submitting port:
short: s
long: submit
takes_value: true
default_value: "8080"
- max vertex:
short: n
long: max-vertex
Expand All @@ -34,3 +24,36 @@ args:
long: jobs
takes_value: true
default_value: "1"

subcommands:
- http:
about: HTTP RPC for Miner
args:
- listening port:
short: p
long: port
takes_value: true
default_value: "3333"
- submitting port:
short: s
long: submit
takes_value: true
default_value: "8080"
- stratum:
about: Stratum RPC for Miner
args:
- server port:
short: p
long: port
takes_value: true
default_value: "8008"
- miner name:
short: i
long: name
takes_value: true
default_value: ""
- miner pwd:
short: w
long: pwd
takes_value: true
default_value: ""
16 changes: 5 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,29 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use codechain_miner::{Config, HttpConfig, RpcConfig, Worker};
use codechain_miner::{Config, RpcConfig, Worker};

use super::worker::CuckooWorker;

#[derive(Clone, Copy)]
#[derive(Clone)]
pub struct CuckooConfig {
pub max_vertex: usize,
pub max_edge: usize,
pub cycle_length: usize,

pub listening_port: u16,
pub submitting_port: u16,

pub rpc_config: RpcConfig,
pub concurrent_jobs: u16,
}

impl Config for CuckooConfig {
fn rpc_config(&self) -> RpcConfig {
RpcConfig::Http(HttpConfig {
listen_port: self.listening_port,
submitting_port: self.submitting_port,
})
self.rpc_config.clone()
}

fn jobs(&self) -> usize {
self.concurrent_jobs as usize
}

fn worker(&self) -> Box<Worker> {
Box::new(CuckooWorker::new(*self))
Box::new(CuckooWorker::new(self.clone()))
}
}
29 changes: 24 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extern crate rustc_hex;
mod config;
mod worker;

use codechain_miner::run;
use codechain_miner::{run, HttpConfig, RpcConfig, StratumConfig};

use self::config::CuckooConfig;

Expand All @@ -49,19 +49,38 @@ fn get_options() -> Result<CuckooConfig, String> {
let yaml = load_yaml!("./cli.yml");
let matches = clap::App::from_yaml(yaml).get_matches();

let listening_port = value_t!(matches, "listening port", u16).map_err(|_| "Invalid listening port")?;
let submitting_port = value_t!(matches, "submitting port", u16).map_err(|_| "Invalid submitting port")?;
let max_vertex = hex_value_t!(matches, "max vertex").map_err(|e| format!("Invalid max vertex: {}", e))?;
let max_edge = hex_value_t!(matches, "max edge").map_err(|e| format!("Invalid max edge: {}", e))?;
let cycle_length = value_t!(matches, "cycle length", usize).map_err(|_| "Invalid cycle length")?;
let concurrent_jobs = value_t!(matches, "concurrent jobs", u16).map_err(|_| "Invalid concurrent jobs")?;

let rpc_config: RpcConfig = if let Some(ref matches) = matches.subcommand_matches("http") {
let listening_port = value_t!(matches, "listening port", u16).map_err(|_| "Invalid listening port")?;
let submitting_port = value_t!(matches, "submitting port", u16).map_err(|_| "Invalid submitting port")?;

RpcConfig::Http(HttpConfig {
listen_port: listening_port,
submitting_port,
})
} else if let Some(ref matches) = matches.subcommand_matches("stratum") {
let server_port = value_t!(matches, "server port", u16).map_err(|_| "Invalid server port")?;
let miner_name = value_t!(matches, "miner name", String).map_err(|_| "Invalid miner name")?;
let miner_pwd = value_t!(matches, "miner pwd", String).map_err(|_| "Invalid miner password")?;

RpcConfig::Stratum(StratumConfig {
port: server_port,
id: miner_name,
pwd: miner_pwd,
})
} else {
return Err(format!("Invalid RPC Config"))
};

Ok(CuckooConfig {
max_vertex,
max_edge,
cycle_length,
listening_port,
submitting_port,
rpc_config,
concurrent_jobs,
})
}
Expand Down