update sqlite and sqlite_runner

This commit is contained in:
boneyard93501 2021-03-05 16:46:00 -06:00
parent 52acea7ea3
commit 0b40bbe6dd
7 changed files with 244 additions and 115 deletions

View File

@ -0,0 +1,88 @@
(xor
(seq
(seq
(seq
(seq
(seq
(seq
(seq
(call relay ("op" "identity") [])
(call node_1 (service_1 "get_latest_block") [api_key] hex_block_result)
)
(seq
(call relay ("op" "identity") [])
(call %init_peer_id% (returnService "run") [hex_block_result])
)
)
(seq
(seq
(call relay ("op" "identity") [])
(call node_2 (service_2 "hex_to_int") [hex_block_result] int_block_result)
)
(seq
(call relay ("op" "identity") [])
(call %init_peer_id% (returnService "run") [int_block_result])
)
)
)
(seq
(seq
(call relay ("op" "identity") [])
(call node_1 (service_1 "get_block") [api_key int_block_result] block_result)
)
(seq
(call relay ("op" "identity") [])
(call %init_peer_id% (returnService "run") [block_result])
)
)
)
(seq
(seq
(call relay ("op" "identity") [])
(call sqlite_node (sqlite_service "update_reward_blocks") [block_result] insert_result)
)
(seq
(call relay ("op" "identity") [])
(call %init_peer_id% (returnService "run") [insert_result])
)
)
)
(seq
(seq
(call relay ("op" "identity") [])
(call sqlite_node (sqlite_service "get_latest_reward_block") [] select_result)
)
(seq
(call relay ("op" "identity") [])
(call %init_peer_id% (returnService "run") [select_result])
)
)
)
(seq
(seq
(seq
(call relay ("op" "identity") [])
(call sqlite_node (sqlite_service "get_reward_block") [int_block_result] select_result_2)
)
(seq
(call relay ("op" "identity") [])
(call %init_peer_id% (returnService "run") [select_result_2])
)
)
(seq
(seq
(call relay ("op" "identity") [])
(call sqlite_node (sqlite_service "get_miner_rewards") [select_result_2.$.["block_miner"]!] select_result_3)
)
(seq
(call relay ("op" "identity") [])
(call %init_peer_id% (returnService "run") [select_result_3])
)
)
)
)
(seq
(call relay ("op" "identity") [])
(call %init_peer_id% (returnService "run") ["XOR FAILED" %last_error%])
)
)

View File

@ -1,60 +0,0 @@
mbp16~/localdev/fluence-examples/multi-service(multi-service|✚3…) % fldist new_service --env testnet -s BtN9ikmpcSRgBWgwLF9yoSZeyuzmYvc5Czyn8XRy514P -n eth_block_getters --ms artifacts/curl_adapter.wasm:curl_cfg.json artifacts/block_getter.wasm:block_getter_cfg.json --node 12D3KooWEXNUbCXooUwHrHBbrmjsrpHXoEphPwbjQXEGyzbqKnE9
client seed: BtN9ikmpcSRgBWgwLF9yoSZeyuzmYvc5Czyn8XRy514P
client peerId: 12D3KooWJLm7n3GpZasv5gsAkta8BYBKHHC19PZ6U8kzWNaabjNz
node peerId: 12D3KooWEXNUbCXooUwHrHBbrmjsrpHXoEphPwbjQXEGyzbqKnE9
uploading blueprint eth_block_getters to node 12D3KooWEXNUbCXooUwHrHBbrmjsrpHXoEphPwbjQXEGyzbqKnE9 via client 12D3KooWJLm7n3GpZasv5gsAkta8BYBKHHC19PZ6U8kzWNaabjNz
creating service 05a5b09a-205e-4a33-9d5f-5afb5498749a
service id: b7fa9b30-af88-471c-888a-8f4a5a9e7d9d
service created successfully
mbp16~/localdev/fluence-examples/multi-service(multi-service|✚3…) % fldist new_service --env testnet -s BtN9ikmpcSRgBWgwLF9yoSZeyuzmYvc5Czyn8XRy514P -n eth_block_getters --ms artifacts/curl_adapter.wasm:curl_cfg.json artifacts/block_getter.wasm:block_getter_cfg.json --node 12D3KooWHk9BjDQBUqnavciRPhAYFvqKBe4ZiPPvde7vDaqgn5er
client seed: BtN9ikmpcSRgBWgwLF9yoSZeyuzmYvc5Czyn8XRy514P
client peerId: 12D3KooWJLm7n3GpZasv5gsAkta8BYBKHHC19PZ6U8kzWNaabjNz
node peerId: 12D3KooWHk9BjDQBUqnavciRPhAYFvqKBe4ZiPPvde7vDaqgn5er
uploading blueprint eth_block_getters to node 12D3KooWHk9BjDQBUqnavciRPhAYFvqKBe4ZiPPvde7vDaqgn5er via client 12D3KooWJLm7n3GpZasv5gsAkta8BYBKHHC19PZ6U8kzWNaabjNz
creating service 04ad020a-1f1d-447b-b114-84d01752e181
service id: 98ebdf65-4153-41d6-b170-1f7a1c5ed2b1
service created successfully
mbp16~/localdev/fluence-examples/multi-service(multi-service|✚3…) %
mbp16~/localdev/fluence-examples/multi-service(multi-service|✚3…) % fldist new_service --env testnet -s BtN9ikmpcSRgBWgwLF9yoSZeyuzmYvc5Czyn8XRy514P -n eth_block_getters --ms artifacts/hex_converter.wasm:hex_converter_cfg.json --node 12D3KooWMhVpgfQxBLkQkJed8VFNvgN4iE6MD7xCybb1ZYWW2Gtz
client seed: BtN9ikmpcSRgBWgwLF9yoSZeyuzmYvc5Czyn8XRy514P
client peerId: 12D3KooWJLm7n3GpZasv5gsAkta8BYBKHHC19PZ6U8kzWNaabjNz
node peerId: 12D3KooWMhVpgfQxBLkQkJed8VFNvgN4iE6MD7xCybb1ZYWW2Gtz
uploading blueprint eth_block_getters to node 12D3KooWMhVpgfQxBLkQkJed8VFNvgN4iE6MD7xCybb1ZYWW2Gtz via client 12D3KooWJLm7n3GpZasv5gsAkta8BYBKHHC19PZ6U8kzWNaabjNz
creating service 0bcf9eef-25e3-42a9-9efb-8a8462fe1814
service id: 29bb30e4-2b01-46fc-845b-1acd983dc690
service created successfully
mbp16~/localdev/fluence-examples/multi-service(multi-service|✚3…) % fldist new_service --env testnet -s BtN9ikmpcSRgBWgwLF9yoSZeyuzmYvc5Czyn8XRy514P -n eth_block_getters --ms artifacts/hex_converter.wasm:hex_converter_cfg.json --node 12D3KooWCKCeqLPSgMnDjyFsJuWqREDtKNHx1JEBiwaMXhCLNTRb
client seed: BtN9ikmpcSRgBWgwLF9yoSZeyuzmYvc5Czyn8XRy514P
client peerId: 12D3KooWJLm7n3GpZasv5gsAkta8BYBKHHC19PZ6U8kzWNaabjNz
node peerId: 12D3KooWCKCeqLPSgMnDjyFsJuWqREDtKNHx1JEBiwaMXhCLNTRb
uploading blueprint eth_block_getters to node 12D3KooWCKCeqLPSgMnDjyFsJuWqREDtKNHx1JEBiwaMXhCLNTRb via client 12D3KooWJLm7n3GpZasv5gsAkta8BYBKHHC19PZ6U8kzWNaabjNz
creating service dcc2b852-4676-4f3b-b1d4-48bf859440e1
service id: 44d82fb1-e6a2-4652-85f4-b1110083f79b
service created successfully
mbp16~/localdev/fluence-examples/multi-service(multi-service|✚3…) % fldist new_service --env testnet -s BtN9ikmpcSRgBWgwLF9yoSZeyuzmYvc5Czyn8XRy514P -n eth_block_getters --ms artifacts/extract_miner_address.wasm:extract_miner_address_cfg.json --node 12D3KooWBSdm6TkqnEFrgBuSkpVE3dR1kr6952DsWQRNwJZjFZBv
client seed: BtN9ikmpcSRgBWgwLF9yoSZeyuzmYvc5Czyn8XRy514P
client peerId: 12D3KooWJLm7n3GpZasv5gsAkta8BYBKHHC19PZ6U8kzWNaabjNz
node peerId: 12D3KooWBSdm6TkqnEFrgBuSkpVE3dR1kr6952DsWQRNwJZjFZBv
uploading blueprint eth_block_getters to node 12D3KooWBSdm6TkqnEFrgBuSkpVE3dR1kr6952DsWQRNwJZjFZBv via client 12D3KooWJLm7n3GpZasv5gsAkta8BYBKHHC19PZ6U8kzWNaabjNz
creating service c146b1d6-7621-474c-9299-55bd0bbc0b55
service id: 891a92f6-1c5c-459d-a8f5-d38add381a5f
service created successfully
mbp16~/localdev/fluence-examples/multi-service(multi-service|✚3…) % fldist new_service --env testnet -s BtN9ikmpcSRgBWgwLF9yoSZeyuzmYvc5Czyn8XRy514P -n eth_block_getters --ms artifacts/extract_miner_address.wasm:extract_miner_address_cfg.json --node 12D3KooWF7gjXhQ4LaKj6j7ntxsPpGk34psdQicN2KNfBi9bFKXg
client seed: BtN9ikmpcSRgBWgwLF9yoSZeyuzmYvc5Czyn8XRy514P
client peerId: 12D3KooWJLm7n3GpZasv5gsAkta8BYBKHHC19PZ6U8kzWNaabjNz
node peerId: 12D3KooWF7gjXhQ4LaKj6j7ntxsPpGk34psdQicN2KNfBi9bFKXg
uploading blueprint eth_block_getters to node 12D3KooWF7gjXhQ4LaKj6j7ntxsPpGk34psdQicN2KNfBi9bFKXg via client 12D3KooWJLm7n3GpZasv5gsAkta8BYBKHHC19PZ6U8kzWNaabjNz
creating service 0c93fe3c-8be2-4466-a1ad-f12aebfee872
service id: d60cf6ee-5f2c-4726-8c70-953f7e7519b4
service created successfully

View File

@ -14,4 +14,5 @@ log = "0.4.8"
fce-sqlite-connector = "=0.2.0"
serde = {version = "1.0.118", features=["derive"]}
serde_json = "1.0.64"
once_cell = "1.4.1"
once_cell = "1.4.1"
uuid = { version = "0.8.2", features = ["serde", "v4"] }

View File

@ -0,0 +1,4 @@
{
"name": "sqlite3",
"mem_pages_count": 100
}

View File

@ -1,3 +0,0 @@
{
"name": "Sqlite3"
}

View File

@ -0,0 +1,10 @@
{
"name": "Sqlite Paywall",
"mem_page_count": 1,
"preopened_files": [
"/var"
],
"mapped_dirs": {
"var": "./var"
}
}

View File

@ -19,6 +19,8 @@ use fluence::WasmLoggerBuilder;
use fce_sqlite_connector;
use fce_sqlite_connector::{Connection, State, Value};
use uuid::Uuid;
use std::path::{Path, PathBuf};
use serde::Deserialize;
use serde_json;
@ -31,6 +33,16 @@ fn main() {
// WasmLoggerBuilder::new().build().unwrap();
}
const KOVAN_ACCT: &str = "";
pub enum EthereumChains {
MAINNET = 1,
ROPSTEN = 3,
RINKEBY = 4,
GOERLI = 5,
KOVAN = 42,
}
fn get_connection() -> Connection {
Connection::open(DB_PATH).unwrap()
@ -53,9 +65,18 @@ fn create_table(conn: &Connection) -> std::result::Result<(), fce_sqlite_connect
timestamp integer not null,
amount integer not null,
unit text not null,
unique(tx_number, chain_id)
available integer not null,
unique(chain_id, tx_number)
);
create table if not exists costs (
chain_id integer not null primary key,
query_cost integer not null,
cost_unit string not null ,
currency string not null
);
insert or ignore into costs values(42, 10, 'gwei', 'eth');
",
);
res
@ -74,25 +95,34 @@ pub fn init_service() -> bool {
#[fce]
pub fn update_reward_blocks(data_string: String) -> bool {
let conn = fce_sqlite_connector::open(DB_PATH).unwrap();
create_table(&conn).unwrap();
let data_string = "{\"status\":\"1\",\"message\":\"OK\",\"result\":{\"blockNumber\":\"11973516\",\"timeStamp\":\"1614884018\",\"blockMiner\":\"0x99c85bb64564d9ef9a99621301f22c9993cb89e3\",\"blockReward\":\"4640547346291918049\",\"uncles\":[],\"uncleInclusionReward\":\"0\"}}";
println!("{}", data_string);
let obj:serde_json::Value = serde_json::from_str(&data_string).unwrap();
let obj = obj["result"].clone();
println!("result obj: {:?}", obj);
println!("blockNumber: {:?}", obj["blockNumber"]);
let conn = fce_sqlite_connector::open(DB_PATH).unwrap();
println!("crate table: {:?}", create_table(&conn).unwrap());
// let data_string = "{\"status\":\"1\",\"message\":\"OK\",\"result\":{\"blockNumber\":\"11980217\",\"timeStamp\":\"1614972187\",\"blockMiner\":\"0x2a0eee948fbe9bd4b661adedba57425f753ea0f6\",\"blockReward\":\"4106318342441412983\",\"uncles\":[],\"uncleInclusionReward\":\"0\"}}";
// let obj:serde_json::Value = serde_json::from_str(&data_string).unwrap();
let insert = "insert or ignore into reward_blocks values(?, ?, ?, ?)";
let mut ins_cur = conn.prepare(insert).unwrap().cursor();
let insert = ins_cur.bind(
&[Value::Integer(i64::from_str_radix(obj["result"]["blockNumber"].as_str().unwrap(), 10).unwrap()),
Value::Integer(i64::from_str_radix(obj["result"]["timeStamp"].as_str().unwrap(), 10).unwrap()),
Value::String(obj["result"]["blockMiner"].to_string()),
Value::Integer(i64::from_str_radix(obj["result"]["blockReward"].as_str().unwrap(), 10).unwrap()),
&[Value::Integer(i64::from_str_radix(obj["blockNumber"].as_str().unwrap(), 10).unwrap()),
Value::Integer(i64::from_str_radix(obj["timeStamp"].as_str().unwrap(), 10).unwrap()),
Value::String(obj["blockMiner"].to_string()),
Value::Integer(i64::from_str_radix(obj["blockReward"].clone().as_str().unwrap(), 10).unwrap()),
]
);
println!("insert: {:?}", insert);
if insert.is_ok() {
ins_cur.next().unwrap();
let mut select = conn.prepare("select * from reward_blocks").unwrap().cursor();
@ -106,35 +136,13 @@ pub fn update_reward_blocks(data_string: String) -> bool {
false
}
#[fce]
pub fn get_latest_reward_block() -> Vec<String> {
// let db_path = "/tmp/db.sqlite";
let conn = fce_sqlite_connector::open(DB_PATH).unwrap();
let mut result:Vec<String> = Vec::new();
let select = conn.prepare("select * from reward_blocks order by block_number desc limit 1");
match select {
Ok(s) => {
let mut select = s.cursor();
while let Some(row) = select.next().unwrap() {
println!("select row {:?}", row);
println!("{}, {}", row[0].as_integer().unwrap(), row[2].as_string().unwrap());
result.push(format!("{}, {}", row[0].as_integer().unwrap(), row[2].as_string().unwrap()));
}
}
Err(e) => log::error!("no bueno"), // result.push(format!("{:?}",e))
}
result
}
#[fce]
#[derive(Debug)]
pub struct RewardBlock {
pub block_number: i64,
pub timestamp: i64,
pub block_miner: String,
pub block_reward: i64,
pub block_reward: String,
}
impl RewardBlock {
@ -143,16 +151,49 @@ impl RewardBlock {
block_number: row[0].as_integer().unwrap(),
timestamp: row[1].as_integer().unwrap(),
block_miner: row[2].as_string().unwrap().into(),
block_reward: row[3].as_integer().unwrap(),
block_reward: row[3].as_integer().unwrap().to_string(),
}
}
fn from_err() -> Self {
RewardBlock {
block_number: -1,
timestamp: -1,
block_miner: String::from(""),
block_reward: String::from(""),
}
}
}
#[fce]
pub fn get_reward_block(block_number: u32) -> Vec<RewardBlock> {
pub fn get_latest_reward_block() -> RewardBlock {
// let db_path = "/tmp/db.sqlite";
let conn = fce_sqlite_connector::open(DB_PATH).unwrap();
let mut reward_block = RewardBlock::from_err();
let select = conn.prepare("select * from reward_blocks order by block_number desc limit 1");
let result = match select {
Ok(s) => {
let mut select = s.cursor();
while let Some(row) = select.next().unwrap() {
println!("get_latest_reward_block: {:?}", row);
reward_block = RewardBlock::from_row(row);
};
return reward_block;
}
Err(e) => reward_block,
};
result
}
#[fce]
pub fn get_reward_block(block_number: u32) -> RewardBlock {
let conn = fce_sqlite_connector::open(DB_PATH).unwrap();
let mut result:Vec<RewardBlock> = Vec::new();
let mut reward_block = RewardBlock::from_err();
let stmt = "select * from reward_blocks where block_number = ?";
let select = conn.prepare(stmt);
match select {
@ -160,43 +201,91 @@ pub fn get_reward_block(block_number: u32) -> Vec<RewardBlock> {
let mut select = s.cursor();
select.bind(&[Value::Integer(block_number as i64)]).unwrap();
while let Some(row) = select.next().unwrap() {
let rblock:RewardBlock = RewardBlock::from_row(row);
result.push(rblock);
}
println!("get_reward_block: {:?}", row);
reward_block = RewardBlock::from_row(row);
};
return reward_block;
}
Err(e) => log::error!("suck it"), //(format!("{:?}",e))
Err(e) => reward_block
}
result
}
#[fce]
pub fn get_miner_rewards(miner_address: String) -> Vec<i64> {
pub struct MinerRewards {
pub miner_address: String,
pub rewards: Vec<String>
}
impl MinerRewards {
fn new(miner_address: String) -> Self {
let rewards:Vec<String> = Vec::new();
MinerRewards {
miner_address,
rewards
}
}
}
#[fce]
pub fn get_miner_rewards(miner_address: String) -> MinerRewards {
let conn = fce_sqlite_connector::open(DB_PATH).unwrap();
let mut result:Vec<i64> = Vec::new();
let stmt = "select block_reward from reward_blocks where block_miner = ?";
let select = conn.prepare(stmt);
let mut miner_rewards = MinerRewards::new(miner_address.clone());
match select {
Ok(s) => {
let mut select = s.cursor();
select.bind(&[Value::String(miner_address)]).unwrap();
select.bind(&[Value::String(miner_address)]).unwrap();
while let Some(row) = select.next().unwrap() {
println!("reward row {:?}", row);
result.push(row[0].as_integer().unwrap());
miner_rewards.rewards.push(row[0].as_string().unwrap().into());
}
}
Err(e) => log::error!("suck it"), //(format!("{:?}",e))
}
result
miner_rewards
}
/*
fn get_tx(tx_string: String) {
#[fce]
pub fn get_account(chain_id:u32) -> String {
KOVAN_ACCT.into()
}
fn update_payments(conn: &Connection) {
fn deposit(tx_string: String, chain_id:u32) {
if chain_id != 42 {
return "only kovan network, 42, is currently available";
}
// check deposit, get amount
// if valid tx
let stmt = "select * from credits where tx_id = ? and chain_id = ? ";
// if exists: ...alloc
// else:
// credit = amount fro wei to gwei
// select cost from costs where chain_id = ?
let stmt = "insert into credits values (?, ?, ?)";
let my_uuid = Uuid::new_v4();
}
fn get_balance(reference_id: String, ) {
}
fn update_payments() {
}
}
*/