Skip to content
Draft
3 changes: 1 addition & 2 deletions Cargo.lock

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

12 changes: 6 additions & 6 deletions packages/examples/cosmos-bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,22 @@ impl KolmeApp for CosmosBridgeApp {
&self.genesis
}

fn new_state(&self) -> Result<Self::State> {
fn new_state(&self) -> Result<Self::State, KolmeError> {
Ok(State { hi_count: 0 })
}

async fn execute(
&self,
ctx: &mut ExecutionContext<'_, Self>,
msg: &Self::Message,
) -> Result<()> {
) -> Result<(), KolmeError> {
match msg {
BridgeMessage::SayHi {} => ctx.state_mut().hi_count += 1,
BridgeMessage::SendTo { address, amount } => {
let chain = match address.get_address_hrp().as_str() {
"osmo" => ExternalChain::OsmosisTestnet,
"neutron" => ExternalChain::NeutronTestnet,
_ => anyhow::bail!("Unsupported wallet address: {address}"),
_ => return Err(KolmeError::other("Unsupported wallet address")),
};
ctx.withdraw_asset(
AssetId(1),
Expand All @@ -184,11 +184,11 @@ struct RandomU32;
impl<App> KolmeDataRequest<App> for RandomU32 {
type Response = u32;

async fn load(self, _: &App) -> Result<Self::Response> {
async fn load(self, _: &App) -> Result<Self::Response, KolmeDataError> {
Ok(rand::random())
}

async fn validate(self, _: &App, _: &Self::Response) -> Result<()> {
async fn validate(self, _: &App, _: &Self::Response) -> Result<(), KolmeDataError> {
// No validation possible
Ok(())
}
Expand Down Expand Up @@ -219,7 +219,7 @@ pub async fn serve(kolme: Kolme<CosmosBridgeApp>, bind: SocketAddr) -> Result<()
}
Ok(Err(e)) => {
set.abort_all();
return Err(e);
return Err(e.into());
}
Ok(Ok(())) => (),
}
Expand Down
12 changes: 6 additions & 6 deletions packages/examples/kademlia-discovery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,15 @@ impl KolmeApp for KademliaTestApp {
&self.genesis
}

fn new_state(&self) -> Result<Self::State> {
fn new_state(&self) -> Result<Self::State, KolmeError> {
Ok(State { hi_count: 0 })
}

async fn execute(
&self,
ctx: &mut ExecutionContext<'_, Self>,
msg: &Self::Message,
) -> Result<()> {
) -> Result<(), KolmeError> {
match msg {
KademliaTestMessage::SayHi {} => ctx.state_mut().hi_count += 1,
}
Expand All @@ -128,11 +128,11 @@ struct RandomU32;
impl<App> KolmeDataRequest<App> for RandomU32 {
type Response = u32;

async fn load(self, _: &App) -> Result<Self::Response> {
async fn load(self, _: &App) -> Result<Self::Response, KolmeDataError> {
Ok(rand::random())
}

async fn validate(self, _: &App, _: &Self::Response) -> Result<()> {
async fn validate(self, _: &App, _: &Self::Response) -> Result<(), KolmeDataError> {
// No validation possible
Ok(())
}
Expand Down Expand Up @@ -247,7 +247,7 @@ pub async fn new_version_node(api_server_port: u16) -> Result<()> {
}
Ok(Err(e)) => {
set.abort_all();
return Err(e);
return Err(e.into());
}
Ok(Ok(())) => (),
}
Expand Down Expand Up @@ -315,7 +315,7 @@ pub async fn validators(
}
Ok(Err(e)) => {
set.abort_all();
return Err(e);
return Err(e.into());
}
Ok(Ok(())) => (),
}
Expand Down
6 changes: 3 additions & 3 deletions packages/examples/p2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ impl KolmeApp for SampleKolmeApp {
&self.genesis
}

fn new_state(&self) -> Result<Self::State> {
fn new_state(&self) -> Result<Self::State, KolmeError> {
Ok(SampleState { hi_count: 0 })
}

async fn execute(
&self,
ctx: &mut ExecutionContext<'_, Self>,
msg: &Self::Message,
) -> Result<()> {
) -> Result<(), KolmeError> {
match msg {
SampleMessage::SayHi {} => ctx.state_mut().hi_count += 1,
}
Expand Down Expand Up @@ -150,7 +150,7 @@ pub async fn api_server(bind: SocketAddr) -> Result<()> {
}
Ok(Err(e)) => {
set.abort_all();
return Err(e);
return Err(e.into());
}
Ok(Ok(())) => (),
}
Expand Down
59 changes: 36 additions & 23 deletions packages/examples/six-sigma/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,27 +199,35 @@ impl KolmeApp for SixSigmaApp {
&self.genesis
}

fn new_state(&self) -> Result<Self::State> {
fn new_state(&self) -> Result<Self::State, KolmeError> {
Ok(State::new([admin_secret_key().public_key()]))
}

async fn execute(
&self,
ctx: &mut ExecutionContext<'_, Self>,
msg: &Self::Message,
) -> Result<()> {
) -> Result<(), KolmeError> {
match msg {
AppMessage::SendFunds { asset_id, amount } => {
ctx.state_mut().send_funds(*asset_id, *amount)
ctx.state_mut()
.send_funds(*asset_id, *amount)
.map_err(KolmeError::other)?;
Ok(())
}
AppMessage::Init => {
let signing_key = ctx.get_signing_key();
ctx.state_mut().initialize(signing_key)
ctx.state_mut()
.initialize(signing_key)
.map_err(KolmeError::other)?;
Ok(())
}
AppMessage::AddMarket { id, asset_id, name } => {
let signing_key = ctx.get_signing_key();
ctx.state_mut()
.add_market(signing_key, *id, *asset_id, name)
.map_err(KolmeError::other)?;
Ok(())
}
AppMessage::PlaceBet {
wallet,
Expand All @@ -232,22 +240,26 @@ impl KolmeApp for SixSigmaApp {
.get_account_balances(&sender)
.map_or(Default::default(), Clone::clone);
let odds = ctx.load_data(OddsSource).await?;
let change = ctx.state_mut().place_bet(
sender,
balances,
*market_id,
Wallet(wallet.clone()),
*amount,
*outcome,
odds,
)?;
let change = ctx
.state_mut()
.place_bet(
sender,
balances,
*market_id,
Wallet(wallet.clone()),
*amount,
*outcome,
odds,
)
.map_err(KolmeError::other)?;
change_balance(ctx, &change)
}
AppMessage::SettleMarket { market_id, outcome } => {
let signing_key = ctx.get_signing_key();
let balance_changes =
ctx.state_mut()
.settle_market(signing_key, *market_id, *outcome)?;
let balance_changes = ctx
.state_mut()
.settle_market(signing_key, *market_id, *outcome)
.map_err(KolmeError::other)?;
for change in balance_changes {
change_balance(ctx, &change)?
}
Expand All @@ -269,7 +281,7 @@ impl fmt::Debug for SixSigmaApp {
fn change_balance(
ctx: &mut ExecutionContext<'_, SixSigmaApp>,
change: &BalanceChange,
) -> Result<()> {
) -> Result<(), KolmeError> {
match change {
BalanceChange::Mint {
asset_id,
Expand Down Expand Up @@ -310,18 +322,18 @@ struct OddsSource;
impl<App> KolmeDataRequest<App> for OddsSource {
type Response = Odds;

async fn load(self, _: &App) -> Result<Self::Response> {
async fn load(self, _: &App) -> Result<Self::Response, KolmeDataError> {
Ok([dec!(1.8), dec!(2.5), dec!(6.5)])
}

async fn validate(self, _: &App, _: &Self::Response) -> Result<()> {
async fn validate(self, _: &App, _: &Self::Response) -> Result<(), KolmeDataError> {
// No validation possible
Ok(())
}
}

pub struct Tasks {
pub set: JoinSet<Result<()>>,
pub set: JoinSet<Result<(), KolmeError>>,
pub processor: Option<AbortHandle>,
pub listener: Option<AbortHandle>,
pub approver: Option<AbortHandle>,
Expand Down Expand Up @@ -358,7 +370,8 @@ impl Tasks {

pub fn spawn_api_server(&mut self) {
let api_server = ApiServer::new(self.kolme.clone());
self.api_server = Some(self.set.spawn(api_server.run(self.bind)));
let bind = self.bind;
self.api_server = Some(self.set.spawn(api_server.run(bind)));
}
}

Expand Down Expand Up @@ -386,7 +399,7 @@ pub async fn serve(
}
Ok(Err(e)) => {
tasks.set.abort_all();
return Err(e);
return Err(e.into());
}
Ok(Ok(())) => (),
}
Expand Down Expand Up @@ -478,7 +491,7 @@ impl TxLogger {
Self { kolme, path }
}

async fn run(self) -> Result<()> {
async fn run(self) -> Result<(), KolmeError> {
let mut file = File::create(self.path)?;
let mut height = BlockHeight::start();
loop {
Expand Down
6 changes: 3 additions & 3 deletions packages/examples/solana-cosmos-bridge/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ impl KolmeApp for SolanaCosmosBridgeApp {
&self.genesis
}

fn new_state(&self) -> Result<Self::State> {
fn new_state(&self) -> Result<Self::State, KolmeError> {
Ok(State)
}

async fn execute(
&self,
ctx: &mut ExecutionContext<'_, Self>,
msg: &Self::Message,
) -> Result<()> {
) -> Result<(), KolmeError> {
match msg {
BridgeMessage::ToSolana { to, amount } => {
ctx.withdraw_asset(
Expand Down Expand Up @@ -202,7 +202,7 @@ pub async fn serve(
}
Ok(Err(e)) => {
set.abort_all();
return Err(e);
return Err(e.into());
}
Ok(Ok(())) => (),
}
Expand Down
4 changes: 2 additions & 2 deletions packages/integration-tests/tests/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ impl KolmeApp for SampleKolmeApp {
&self.genesis
}

fn new_state(&self) -> anyhow::Result<Self::State> {
fn new_state(&self) -> Result<Self::State, KolmeError> {
Ok(SampleState {})
}

async fn execute(
&self,
_ctx: &mut ExecutionContext<'_, Self>,
_msg: &Self::Message,
) -> anyhow::Result<()> {
) -> Result<(), KolmeError> {
Ok(())
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/integration-tests/tests/cosmos-migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ impl KolmeApp for SampleKolmeApp {
&self.genesis
}

fn new_state(&self) -> anyhow::Result<Self::State> {
fn new_state(&self) -> Result<Self::State, KolmeError> {
Ok(SampleState {})
}

async fn execute(
&self,
_ctx: &mut ExecutionContext<'_, Self>,
_msg: &Self::Message,
) -> anyhow::Result<()> {
) -> Result<(), KolmeError> {
Ok(())
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/integration-tests/tests/key-rotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,15 @@ impl KolmeApp for SampleKolmeApp {
&self.genesis
}

fn new_state(&self) -> anyhow::Result<Self::State> {
fn new_state(&self) -> Result<Self::State, KolmeError> {
Ok(SampleState {})
}

async fn execute(
&self,
_ctx: &mut ExecutionContext<'_, Self>,
_msg: &Self::Message,
) -> anyhow::Result<()> {
) -> Result<(), KolmeError> {
Ok(())
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/integration-tests/tests/solana-migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ impl KolmeApp for SampleKolmeApp {
&self.genesis
}

fn new_state(&self) -> anyhow::Result<Self::State> {
fn new_state(&self) -> Result<Self::State, KolmeError> {
Ok(SampleState {})
}

async fn execute(
&self,
_ctx: &mut ExecutionContext<'_, Self>,
_msg: &Self::Message,
) -> anyhow::Result<()> {
) -> Result<(), KolmeError> {
Ok(())
}
}
Expand Down
Loading