init sqlite service

This commit is contained in:
boneyard93501 2021-03-04 18:38:00 -06:00
parent 9c948c79e6
commit 52acea7ea3
6 changed files with 487 additions and 0 deletions

241
multi-service/sqlite/Cargo.lock generated Normal file
View File

@ -0,0 +1,241 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "fce-sqlite-connector"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c02ba19dff7f195418416b249423384761a964447dc40e7427f448687ac60b56"
dependencies = [
"fluence 0.4.2",
]
[[package]]
name = "fluence"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "84c621ecf5ba628a3a66c4571cb63b9e57de5b8e1ffbb4636873349d04ed9c4c"
dependencies = [
"fluence-sdk-macro 0.3.3",
"fluence-sdk-main 0.3.3",
]
[[package]]
name = "fluence"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31068fe4873478f9c59f01230dc6f23d504d8c11a36d6b6c51693b20c6d2570e"
dependencies = [
"fluence-sdk-macro 0.4.2",
"fluence-sdk-main 0.4.2",
]
[[package]]
name = "fluence-sdk-macro"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3ed152de2a9f59c5494a5ead9d85b40cc1588696d10bb69129addebf0239efb"
dependencies = [
"fluence-sdk-wit 0.3.3",
]
[[package]]
name = "fluence-sdk-macro"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "76ce80dc2d7c3db828fb31b95e75c372fde7a9a25dfa2b0d9beac07379b890a6"
dependencies = [
"fluence-sdk-wit 0.4.2",
]
[[package]]
name = "fluence-sdk-main"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ed74c32aded592c2a4c2d11a4586369e2d9f793e6c7b500a2a22f4a61769784"
dependencies = [
"fluence-sdk-macro 0.3.3",
"log",
"serde",
]
[[package]]
name = "fluence-sdk-main"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29cce53288e08ed86ebe36c64d8eb641a95e1d0f7f75da969b29e16ab0d7d406"
dependencies = [
"fluence-sdk-macro 0.4.2",
"log",
"serde",
]
[[package]]
name = "fluence-sdk-wit"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a65bb11a2ebb063b8809e43c1837d32e44c12ba3c281e55f2fcf5c5819a6a9ad"
dependencies = [
"proc-macro2",
"quote",
"serde",
"serde_json",
"syn",
"uuid",
]
[[package]]
name = "fluence-sdk-wit"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56d835812bab8fb6f261b9e37355b9bf0a28a16123580145ce0fe44ef4363144"
dependencies = [
"proc-macro2",
"quote",
"serde",
"serde_json",
"syn",
"uuid",
]
[[package]]
name = "getrandom"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "itoa"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
[[package]]
name = "libc"
version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "265d751d31d6780a3f956bb5b8022feba2d94eeee5a84ba64f4212eedca42213"
[[package]]
name = "log"
version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
dependencies = [
"cfg-if",
]
[[package]]
name = "once_cell"
version = "1.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3"
[[package]]
name = "proc-macro2"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ryu"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]]
name = "serde"
version = "1.0.118"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06c64263859d87aa2eb554587e2d23183398d617427327cf2b3d0ed8c69e4800"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.118"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c84d3526699cd55261af4b941e4e725444df67aa4f9e6a3564f18030d12672df"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "sqlite-runner"
version = "0.1.0"
dependencies = [
"fce-sqlite-connector",
"fluence 0.3.3",
"log",
"once_cell",
"serde",
"serde_json",
]
[[package]]
name = "syn"
version = "1.0.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c700597eca8a5a762beb35753ef6b94df201c81cca676604f547495a0d7f0081"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]]
name = "uuid"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
dependencies = [
"getrandom",
]
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"

View File

@ -0,0 +1,17 @@
[package]
name = "sqlite-runner"
version = "0.1.0"
authors = ["boneyard93501 <4523011+boneyard93501@users.noreply.github.com>"]
edition = "2018"
[[bin]]
name = "sqlite_runner"
path = "src/main.rs"
[dependencies]
fluence = { version="0.3.3", features = ["logger"]}
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"

View File

@ -0,0 +1,14 @@
modules_dir = "artifacts/"
[[module]]
name = "sqlite3"
mem_pages_count = 100
logger_enabled = false
[[module]]
name = "sqlite_runner"
mem_pages_count = 1
logger_enabled = false
preopened_files = ["/var"]
mapped_dirs = { "var" = "./var" }

10
multi-service/sqlite/build.sh Executable file
View File

@ -0,0 +1,10 @@
#!/bin/sh
# This script builds all subprojects and puts all created Wasm modules in one dir
# cargo update
fce build --release
# rm artifacts/*
cp target/wasm32-wasi/release/sqlite_runner.wasm artifacts/
# wget https://github.com/fluencelabs/sqlite/releases/download/v0.10.0_w/sqlite3.wasm
# mv sqlite3.wasm artifacts/

View File

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

View File

@ -0,0 +1,202 @@
/*
* Copyright 2021 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use fluence::fce; ///, WasmLoggerBuilder};
use fluence::WasmLoggerBuilder;
use fce_sqlite_connector;
use fce_sqlite_connector::{Connection, State, Value};
use std::path::{Path, PathBuf};
use serde::Deserialize;
use serde_json;
// const DB_PATH: &str = "/tmp/db_1.sqlite";
const DB_PATH: &str = "/tmp/db1254.sqlite";
fn main() {
// WasmLoggerBuilder::new().build().unwrap();
}
fn get_connection() -> Connection {
Connection::open(DB_PATH).unwrap()
}
fn create_table(conn: &Connection) -> std::result::Result<(), fce_sqlite_connector::Error> {
let res = conn.execute(
"
create table if not exists reward_blocks (
block_number integer not null primary key,
timestamp integer not null,
block_miner text not null,
block_reward integer not null
);
create table if not exists payments (
tx_number text not null primary key,
chain_id integer not null,
timestamp integer not null,
amount integer not null,
unit text not null,
unique(tx_number, chain_id)
);
",
);
res
}
#[fce]
pub fn init_service() -> bool {
let conn = fce_sqlite_connector::open(DB_PATH).unwrap();
let res = create_table(&conn);
match res {
Ok(_) => true,
Err(_) => false,
}
}
#[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\"}}";
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()),
]
);
if insert.is_ok() {
ins_cur.next().unwrap();
let mut select = conn.prepare("select * from reward_blocks").unwrap().cursor();
while let Some(row) = select.next().unwrap() {
println!("select row {:?}", row);
println!("{}, {}", row[0].as_integer().unwrap(), row[2].as_string().unwrap());
}
return true;
}
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,
}
impl RewardBlock {
fn from_row(row: &[Value]) -> Self {
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(),
}
}
}
#[fce]
pub fn get_reward_block(block_number: u32) -> Vec<RewardBlock> {
let conn = fce_sqlite_connector::open(DB_PATH).unwrap();
let mut result:Vec<RewardBlock> = Vec::new();
let stmt = "select * from reward_blocks where block_number = ?";
let select = conn.prepare(stmt);
match select {
Ok(s) => {
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);
}
}
Err(e) => log::error!("suck it"), //(format!("{:?}",e))
}
result
}
#[fce]
pub fn get_miner_rewards(miner_address: String) -> Vec<i64> {
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);
match select {
Ok(s) => {
let mut select = s.cursor();
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());
}
}
Err(e) => log::error!("suck it"), //(format!("{:?}",e))
}
result
}
fn get_tx(tx_string: String) {
}
fn update_payments(conn: &Connection) {
}