feature: adds basic chat generators to Kathy (#101)

* feature: adds static, random, and ordered list ChatGenerator types to kathy

* refactor: adds enum interior state and no longer returns vec

* feature: ordered list variant uses counter and gen_chat returns Option<Message>

* refactor: replaces usize with AtomicUsize

* rebase: fixes errors after agent refactor

* feature: adds config blocks for generators and instantiation from settings

* fix: removes rwlock

* feature: makes destination and recipient configurable for static messages

* fix: runs cargo update and fixes length.clone() --> *length
buddies-main-deployment
Luke Tchang 4 years ago committed by GitHub
parent 8fdb50a40e
commit c3c1c184d9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 86
      rust/Cargo.lock
  2. 1
      rust/kathy/Cargo.toml
  3. 3
      rust/kathy/config/default.toml
  4. 86
      rust/kathy/src/kathy.rs
  5. 52
      rust/kathy/src/settings.rs

86
rust/Cargo.lock generated

@ -21,9 +21,9 @@ dependencies = [
[[package]]
name = "adler"
version = "0.2.3"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee2a4ec343196209d6594e19543ae87a39f96d5534d7174822a3ad825dd6ed7e"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aes"
@ -493,7 +493,7 @@ dependencies = [
[[package]]
name = "ethers"
version = "0.2.0"
source = "git+https://github.com/gakonst/ethers-rs#e88da4501c430ff17a6ba1517c521ed8d00e3c75"
source = "git+https://github.com/gakonst/ethers-rs#c18dad6122c06dc8255d1aabce5856611b3216bb"
dependencies = [
"ethers-contract",
"ethers-core",
@ -505,7 +505,7 @@ dependencies = [
[[package]]
name = "ethers-contract"
version = "0.2.0"
source = "git+https://github.com/gakonst/ethers-rs#e88da4501c430ff17a6ba1517c521ed8d00e3c75"
source = "git+https://github.com/gakonst/ethers-rs#c18dad6122c06dc8255d1aabce5856611b3216bb"
dependencies = [
"ethers-contract-abigen",
"ethers-contract-derive",
@ -523,7 +523,7 @@ dependencies = [
[[package]]
name = "ethers-contract-abigen"
version = "0.2.0"
source = "git+https://github.com/gakonst/ethers-rs#e88da4501c430ff17a6ba1517c521ed8d00e3c75"
source = "git+https://github.com/gakonst/ethers-rs#c18dad6122c06dc8255d1aabce5856611b3216bb"
dependencies = [
"Inflector",
"anyhow",
@ -540,7 +540,7 @@ dependencies = [
[[package]]
name = "ethers-contract-derive"
version = "0.2.0"
source = "git+https://github.com/gakonst/ethers-rs#e88da4501c430ff17a6ba1517c521ed8d00e3c75"
source = "git+https://github.com/gakonst/ethers-rs#c18dad6122c06dc8255d1aabce5856611b3216bb"
dependencies = [
"ethers-contract-abigen",
"ethers-core",
@ -553,7 +553,7 @@ dependencies = [
[[package]]
name = "ethers-core"
version = "0.2.0"
source = "git+https://github.com/gakonst/ethers-rs#e88da4501c430ff17a6ba1517c521ed8d00e3c75"
source = "git+https://github.com/gakonst/ethers-rs#c18dad6122c06dc8255d1aabce5856611b3216bb"
dependencies = [
"arrayvec",
"bytes",
@ -577,7 +577,7 @@ dependencies = [
[[package]]
name = "ethers-middleware"
version = "0.2.0"
source = "git+https://github.com/gakonst/ethers-rs#e88da4501c430ff17a6ba1517c521ed8d00e3c75"
source = "git+https://github.com/gakonst/ethers-rs#c18dad6122c06dc8255d1aabce5856611b3216bb"
dependencies = [
"async-trait",
"ethers-contract",
@ -599,7 +599,7 @@ dependencies = [
[[package]]
name = "ethers-providers"
version = "0.2.0"
source = "git+https://github.com/gakonst/ethers-rs#e88da4501c430ff17a6ba1517c521ed8d00e3c75"
source = "git+https://github.com/gakonst/ethers-rs#c18dad6122c06dc8255d1aabce5856611b3216bb"
dependencies = [
"async-trait",
"auto_impl",
@ -624,7 +624,7 @@ dependencies = [
[[package]]
name = "ethers-signers"
version = "0.2.0"
source = "git+https://github.com/gakonst/ethers-rs#e88da4501c430ff17a6ba1517c521ed8d00e3c75"
source = "git+https://github.com/gakonst/ethers-rs#c18dad6122c06dc8255d1aabce5856611b3216bb"
dependencies = [
"async-trait",
"elliptic-curve 0.9.4",
@ -839,9 +839,9 @@ dependencies = [
[[package]]
name = "h2"
version = "0.3.0"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b67e66362108efccd8ac053abafc8b7a8d86a37e6e48fc4f6f7485eb5e9e6a5"
checksum = "d832b01df74254fe364568d6ddc294443f61cbec82816b60904303af87efae78"
dependencies = [
"bytes",
"fnv",
@ -854,7 +854,6 @@ dependencies = [
"tokio",
"tokio-util",
"tracing",
"tracing-futures",
]
[[package]]
@ -1028,9 +1027,9 @@ checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
[[package]]
name = "js-sys"
version = "0.3.47"
version = "0.3.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5cfb73131c35423a367daf8cbd24100af0d077668c8c2943f0e7dd775fef0f65"
checksum = "dc9f84f9b115ce7843d60706df1422a916680bfdfcbdb0447c5614ff9d7e4d78"
dependencies = [
"wasm-bindgen",
]
@ -1059,6 +1058,7 @@ dependencies = [
"log",
"optics-base",
"optics-core",
"rand 0.8.3",
"serde 1.0.123",
"serde_json",
"thiserror",
@ -1165,9 +1165,9 @@ checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
[[package]]
name = "miniz_oxide"
version = "0.4.3"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f2d26ec3309788e423cfbf68ad1800f061638098d76a83681af979dc4eda19d"
checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b"
dependencies = [
"adler",
"autocfg",
@ -1270,9 +1270,9 @@ checksum = "a9a7ab5d64814df0fe4a4b5ead45ed6c5f181ee3ff04ba344313a6c80446c5d4"
[[package]]
name = "once_cell"
version = "1.6.0"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ad167a2f54e832b82dbe003a046280dceffe5227b5f79e08e363a29638cfddd"
checksum = "10acf907b94fc1b1a152d08ef97e7759650268cf986bf127f387e602b02c7e5a"
[[package]]
name = "opaque-debug"
@ -1391,9 +1391,9 @@ checksum = "2386b4ebe91c2f7f51082d4cefa145d030e33a1842a96b12e4885cc3c01f7a55"
[[package]]
name = "parity-scale-codec"
version = "2.0.0"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75c823fdae1bb5ff5708ee61a62697e6296175dc671710876871c853f48592b3"
checksum = "0cd3dab59b5cf4bc81069ade0fc470341a1ef3ad5fa73e5a8943bed2ec12b2e8"
dependencies = [
"arrayvec",
"bitvec 0.20.1",
@ -1865,9 +1865,9 @@ dependencies = [
[[package]]
name = "security-framework"
version = "2.0.0"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1759c2e3c8580017a484a7ac56d3abc5a6c1feadf88db2f3633f12ae4268c69"
checksum = "2dfd318104249865096c8da1dfabf09ddbb6d0330ea176812a62ec75e40c4166"
dependencies = [
"bitflags",
"core-foundation",
@ -1878,9 +1878,9 @@ dependencies = [
[[package]]
name = "security-framework-sys"
version = "2.0.0"
version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f99b9d5e26d2a71633cc4f2ebae7cc9f874044e0c351a27e17892d76dce5678b"
checksum = "dee48cdde5ed250b0d3252818f646e174ab414036edb884dde62d80a3ac6082d"
dependencies = [
"core-foundation-sys",
"libc",
@ -1937,9 +1937,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.62"
version = "1.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea1c6153794552ea7cf7cf63b1231a25de00ec90db326ba6264440fa08e31486"
checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79"
dependencies = [
"itoa",
"ryu",
@ -2242,9 +2242,9 @@ checksum = "360dfd1d6d30e05fda32ace2c8c70e9c0a9da713275777f5a4dbb8a1893930c6"
[[package]]
name = "tracing"
version = "0.1.24"
version = "0.1.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f77d3842f76ca899ff2dbcf231c5c65813dea431301d6eb686279c15c4464f12"
checksum = "01ebdc2bb4498ab1ab5f5b73c5803825e60199229ccba0698170e3be0e7f959f"
dependencies = [
"cfg-if",
"pin-project-lite",
@ -2473,9 +2473,9 @@ checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]]
name = "wasm-bindgen"
version = "0.2.70"
version = "0.2.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55c0f7123de74f0dab9b7d00fd614e7b19349cd1e2f5252bbe9b1754b59433be"
checksum = "7ee1280240b7c461d6a0071313e08f34a60b0365f14260362e5a2b17d1d31aa7"
dependencies = [
"cfg-if",
"serde 1.0.123",
@ -2485,9 +2485,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.70"
version = "0.2.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7bc45447f0d4573f3d65720f636bbcc3dd6ce920ed704670118650bcd47764c7"
checksum = "5b7d8b6942b8bb3a9b0e73fc79b98095a27de6fa247615e59d096754a3bc2aa8"
dependencies = [
"bumpalo",
"lazy_static",
@ -2500,9 +2500,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.20"
version = "0.4.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3de431a2910c86679c34283a33f66f4e4abd7e0aec27b6669060148872aadf94"
checksum = "8e67a5806118af01f0d9045915676b22aaebecf4178ae7021bc171dab0b897ab"
dependencies = [
"cfg-if",
"js-sys",
@ -2512,9 +2512,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.70"
version = "0.2.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b8853882eef39593ad4174dd26fc9865a64e84026d223f63bb2c42affcbba2c"
checksum = "e5ac38da8ef716661f0f36c0d8320b89028efe10c7c0afde65baffb496ce0d3b"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
@ -2522,9 +2522,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.70"
version = "0.2.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4133b5e7f2a531fa413b3a1695e925038a05a71cf67e87dafa295cb645a01385"
checksum = "cc053ec74d454df287b9374ee8abb36ffd5acb95ba87da3ba5b7d3fe20eb401e"
dependencies = [
"proc-macro2",
"quote",
@ -2535,9 +2535,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.70"
version = "0.2.71"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd4945e4943ae02d15c13962b38a5b1e81eadd4b71214eee75af64a4d6a4fd64"
checksum = "7d6f8ec44822dd71f5f221a5847fb34acd9060535c1211b70a05844c0f6383b1"
[[package]]
name = "watcher"
@ -2562,9 +2562,9 @@ dependencies = [
[[package]]
name = "web-sys"
version = "0.3.47"
version = "0.3.48"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c40dc691fc48003eba817c38da7113c15698142da971298003cac3ef175680b3"
checksum = "ec600b26223b2948cedfde2a0aa6756dcf1fef616f43d7b3097aaf53a6c4d92b"
dependencies = [
"js-sys",
"wasm-bindgen",

@ -20,6 +20,7 @@ tokio = {version = "1.0.1", features = ["rt", "macros"]}
tracing = "0.1.22"
tracing-futures = "0.2.4"
tracing-subscriber = "0.2.15"
rand = "0.8.3"
optics-base = {path = "../optics-base"}
optics-core = {path = "../optics-core"}

@ -1,4 +1,5 @@
message-interval = 100
chat-gen-config = { type = "static", destination = 1, recipient = "recipient", message = "static message" }
[home]
name = "celo"
@ -11,4 +12,4 @@ rpc-style = "Ethereum"
[[replicas]]
# empty vector.
# no replicas needed for updater
# no replicas needed for kathy

@ -1,6 +1,11 @@
use async_trait::async_trait;
use ethers::core::types::H256;
use std::sync::atomic::{AtomicUsize, Ordering};
use tokio::time::{interval, Interval};
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use color_eyre::Result;
use optics_base::{
@ -11,13 +16,10 @@ use optics_core::Message;
use crate::settings::Settings;
decl_agent!(
/// Chatty Kathy
Kathy {
interval_seconds: u64,
generator: ChatGenerator,
}
);
decl_agent!(Kathy {
interval_seconds: u64,
generator: ChatGenerator,
});
impl Kathy {
pub fn new(interval_seconds: u64, generator: ChatGenerator, core: AgentCore) -> Self {
@ -41,8 +43,8 @@ impl OpticsAgent for Kathy {
async fn from_settings(settings: Settings) -> Result<Self> {
Ok(Self::new(
settings.message_interval,
ChatGenerator::Default,
settings.as_ref().try_into_core().await?,
settings.chat_gen.into(),
settings.base.try_into_core().await?,
))
}
@ -50,18 +52,32 @@ impl OpticsAgent for Kathy {
let mut interval = self.interval();
loop {
let message = self.generator.gen_chat();
self.home().enqueue(&message).await?;
if let Some(message) = self.generator.gen_chat() {
self.home().enqueue(&message).await?;
} else {
return Ok(());
}
interval.tick().await;
}
}
}
/// Generators for messages
#[derive(Copy, Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "kebab-case")]
#[derive(Debug)]
pub enum ChatGenerator {
#[serde(other)]
Static {
destination: u32,
recipient: H256,
message: String,
},
OrderedList {
messages: Vec<String>,
counter: AtomicUsize,
},
Random {
length: usize,
},
Default,
}
@ -72,9 +88,47 @@ impl Default for ChatGenerator {
}
impl ChatGenerator {
pub fn gen_chat(&self) -> Message {
fn rand_string(length: usize) -> String {
thread_rng()
.sample_iter(&Alphanumeric)
.take(length)
.map(char::from)
.collect()
}
pub fn gen_chat(&self) -> Option<Message> {
match self {
ChatGenerator::Default => Default::default(),
ChatGenerator::Default => Some(Default::default()),
ChatGenerator::Static {
destination,
recipient,
message,
} => Some(Message {
destination: destination.to_owned(),
recipient: recipient.to_owned(),
body: message.clone().into(),
}),
ChatGenerator::OrderedList { messages, counter } => {
if counter.load(Ordering::SeqCst) >= messages.len() {
return None;
}
let msg = Message {
destination: Default::default(),
recipient: Default::default(),
body: messages[counter.load(Ordering::SeqCst)].clone().into(),
};
// Increment counter to next message in list
counter.fetch_add(1, Ordering::SeqCst);
Some(msg)
}
ChatGenerator::Random { length } => Some(Message {
destination: Default::default(),
recipient: Default::default(),
body: Self::rand_string(*length).into(),
}),
}
}
}

@ -1,10 +1,62 @@
//! Configuration
use ethers::core::types::H256;
use std::sync::atomic::AtomicUsize;
use crate::kathy::ChatGenerator;
use optics_base::decl_settings;
#[derive(Debug, serde::Deserialize)]
#[serde(tag = "type", rename_all = "kebab-case")]
pub enum ChatGenConfig {
Static {
destination: u32,
recipient: H256,
message: String,
},
OrderedList {
messages: Vec<String>,
},
Random {
length: usize,
},
#[serde(other)]
Default,
}
impl Default for ChatGenConfig {
fn default() -> Self {
Self::Default
}
}
impl Into<ChatGenerator> for ChatGenConfig {
fn into(self) -> ChatGenerator {
match self {
Self::Static {
destination,
recipient,
message,
} => ChatGenerator::Static {
destination,
recipient,
message,
},
Self::OrderedList { messages } => ChatGenerator::OrderedList {
messages,
counter: AtomicUsize::new(0),
},
Self::Random { length } => ChatGenerator::Random { length },
Self::Default => ChatGenerator::Default,
}
}
}
decl_settings!(
Settings {
"OPT_KATHY",
message_interval: u64,
#[serde(default)] chat_gen: ChatGenConfig,
}
);

Loading…
Cancel
Save