update consensus service

This commit is contained in:
boneyard93501
2021-11-07 15:19:09 -06:00
parent 831f3ad675
commit 48f4ca3106
14 changed files with 140 additions and 4 deletions

View File

@ -64,3 +64,27 @@ func delete_records(password: string, node: string, relay:string) -> DBResult:
<- result
-- Example to collect many node validations and run them through a simple frequency count
-- algorithm
data EIPLocation:
node: string
relay: string
service ConsensusService("ConsensusService"):
consensus([]bool) -> bool
func eip_consensus((signature: string, node_locations:[]EIPLocation, service_node: string, consensus_service: string) -> bool:
result: *bool
for loc in node_locations par:
on node via relay:
res <- DataProvider.get_record(signature)
result <- res.ts_validation
on service_node:
ConsensusService consensus_service
consensus <- ConsensusService.consensus(result)
<- consensus

View File

@ -1,4 +0,0 @@
# Simple COnsensus Service
## Overview
TBD

BIN
services/.DS_Store vendored Normal file

Binary file not shown.

View File

@ -0,0 +1,13 @@
#V2
i
call consensus consensus [[1,1,1,0,0,0], 0.666]
call consensus consensus [[true,true, true, false,false,false], 0.666]
call consensus consensus [[true,true, true, true,false,false], 0.666]
i
call consensus consensus [[true,true, true, true,false,false], 0.666]
call consensus consensus [[true,true, true, false,false,false], 0.666]
1> i\nLoaded modules interface:\ndata CResult:\n stderr: string\n stdout: []Consensus\ndata Consensus:\n n: u64\n threshold: f64\n valid: u64\n invalid: u64\n consensus: bool\n\nconsensus:\n fn consensus(payload: []bool, threshold: f64) -> CResult\n\n2> call consensus consensus [[true,true, true, true,false,false], 0.666]\nresult: Object({"stderr": String(""), "stdout": Array([Object({"consensus": Bool(true), "invalid": Number(2), "n": Number(6), "threshold": Number(0.666), "valid": Number(4)})])})\n elapsed time: 96.859µs\n\n3> call consensus consensus [[true,true, true, false,false,false], 0.666]\nresult: Object({"stderr": String(""), "stdout": Array([Object({"consensus": Bool(false), "invalid": Number(3), "n": Number(6), "threshold": Number(0.666), "valid": Number(3)})])})\n elapsed time: 52.831µs\n\n4>
1> i\nLoaded modules interface:\ndata CResult:\n stderr: string\n stdout: []Consensus\ndata Consensus:\n n: u64\n threshold: f64\n valid: u64\n invalid: u64\n consensus: bool\n\nconsensus:\n fn consensus(payload: []bool, threshold: f64) -> CResult\n\n2> call consensus consensus [[true,true, true, true,false,false], 0.666]\nresult: Object({"stderr": String(""), "stdout": Array([Object({"consensus": Bool(true), "invalid": Number(2), "n": Number(6), "threshold": Number(0.666), "valid": Number(4)})])})\n elapsed time: 96.859µs\n\n3> call consensus consensus [[true,true, true, false,false,false], 0.666]\nresult: Object({"stderr": String(""), "stdout": Array([Object({"consensus": Bool(false), "invalid": Number(3), "n": Number(6), "threshold": Number(0.666), "valid": Number(3)})])})\n elapsed time: 52.831µs\n\n4>call consensus consensus [[true,true, false, false,false,false], 0.666]
1> i\nLoaded modules interface:\ndata CResult:\n stderr: string\n stdout: []Consensus\ndata Consensus:\n n: u64\n threshold: f64\n valid: u64\n invalid: u64\n consensus: bool\n\nconsensus:\n fn consensus(payload: []bool, threshold: f64) -> CResult\n\n2> call consensus consensus [[true,true, true, true,false,false], 0.666]\nresult: Object({"stderr": String(""), "stdout": Array([Object({"consensus": Bool(true), "invalid": Number(2), "n": Number(6), "threshold": Number(0.666), "valid": Number(4)})])})\n elapsed time: 96.859µs\n\n3> call consensus consensus [[true,true, true, false,false,false], 0.666]\nresult: Object({"stderr": String(""), "stdout": Array([Object({"consensus": Bool(false), "invalid": Number(3), "n": Number(6), "threshold": Number(0.666), "valid": Number(3)})])})\nlapsed time: 52.831µs\n\n4>call consensus consensus [[true,true, false, false,false,false], 0.666]
i
call consensus consensus [[true,true, false, false,false,false], 0.666]

View File

@ -0,0 +1,82 @@
# Simple Consensus Service
The simple consensus service is a Wasm module comprised of one function that takes a list of EIP-712 validations in form of booleans representing `valid` and `invalid` proposals or votes and a threshold to calculate the consensus over the input vector. Note that the return boolean is true if the ratio **(valid proposals / all proposals) >= threshold value**:
```rust
// services/consensus/src/main.rs
// ...
#[marine]
#[derive(Default, Debug)]
pub struct Consensus {
pub n: u64,
pub threshold: f64,
pub valid: u64,
pub invalid: u64,
pub consensus: bool,
}
#[marine]
#[derive(Default, Debug)]
pub struct CResult {
pub stderr: String,
pub stdout: Vec<Consensus>, // always length 0 or 1
}
pub fn consensus(payload: Vec<bool>, threshold: f64) -> CResult
// ...
```
See './src/main.rs' for the complete code and tests. The service can easily be improved including with the introducing of authentication for a (global) consensus threshold and minimum input items. Also, with little effort we could change he return value to an absolute consensus tuple or even replace the boolean inputs with the signed validations.
For local interaction with the module, we can use the REPL:
```bash
mrepl configs/Config.toml
```
and then:
```bash
1> i
Loaded modules interface:
data CResult:
stderr: string
stdout: []Consensus
data Consensus:
n: u64
threshold: f64
valid: u64
invalid: u64
consensus: bool
consensus:
fn consensus(payload: []bool, threshold: f64) -> CResult
2> call consensus consensus [[true,true, true, true,false,false], 0.666]
result: Object({"stderr": String(""), "stdout": Array([Object({"consensus": Bool(true), "invalid": Number(2), "n": Number(6), "threshold": Number(0.666), "valid": Number(4)})])})
elapsed time: 96.859µs
3> call consensus consensus [[true,true, true, false,false,false], 0.666]
result: Object({"stderr": String(""), "stdout": Array([Object({"consensus": Bool(false), "invalid": Number(3), "n": Number(6), "threshold": Number(0.666), "valid": Number(3)})])})
elapsed time: 52.831µs
4>
```
As expected, for a two-thirds threshold, `[true,true, true, true,false,false]` gives us a consensus for `valid`, whereas `[true,true, true, false,false,false]` does not.
To compile the service, in the '~/eip712-validation-node/services/consensus' directory, run:
```bash
./scripts/build.sh
```
and a `consensus.wasm` file should be in the `artifacts` directory, which we can deploy with the
```bash
./scripts/deploy.sh
```
which writes the output to the `deployed_service_data.txt` file.

View File

@ -0,0 +1 @@
{ "name": "consensus", "mem_page_count": 15 }

View File

@ -0,0 +1,5 @@
client seed: Atj1pJWgYdPouPjDtfjR4CKtFmVkZTpdgRLVfr1sEsWa
client peerId: 12D3KooWSsr82uqKsm6B3qkNkRXZ7x3tvHerMqPC23ipSAdK2RDM
relay peerId: 12D3KooWHLxVhUQyAuZe6AHMB29P7wkvTNMn7eDMcsqimJYLKREf
service id: 932e002f-12b9-48cd-9616-7f15bb1e4ef3
service created successfully

View File

@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -o errexit -o nounset -o pipefail
# build wasms
./build.sh
(
fldist new_service \
--name "simple-consensus" \
--modules artifacts/consensus.wasm:configs/consensus_cfg.json \
--verbose \
>> \
deployed_service_data.txt
)