Skip to content

Commit 8a11ed8

Browse files
committed
Adds logging to proxy
* record actions invoked on proxied contract * add a few types for recording and error reporting
1 parent ac1fdf1 commit 8a11ed8

File tree

3 files changed

+67
-7
lines changed

3 files changed

+67
-7
lines changed

contracts/logging_engine/src/engine.rs

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
use soroban_sdk::{contractimpl, log, BytesN, Env, Map};
1+
use soroban_sdk::{contractimpl, log, panic_with_error, vec, BytesN, Env, Map, Vec};
2+
use crate::types::{Action, ActionItem, ProxyError};
3+
24
mod game_engine {
35
soroban_sdk::contractimport!(file = "../game_engine.wasm");
46
}
57

8+
const ACTIONS: &str = "actions";
69
const ENGINE_ID: &str = "engine";
710

811
pub struct LoggingEngine;
912
#[contractimpl]
1013
impl LoggingEngine {
1114
pub fn wrap(env: Env, engine_id: BytesN<32>) {
1215
env.storage().set(&ENGINE_ID, &engine_id);
16+
env.storage()
17+
.set::<&str, Vec<ActionItem>>(&ACTIONS, &vec![&env]);
1318
log!(&env, "🗒️ logger engine taking notes");
1419
}
1520

@@ -20,6 +25,15 @@ impl LoggingEngine {
2025
game_engine::Client::new(&env, &Self::engine_id(env.clone()))
2126
}
2227

28+
pub fn actions(env: Env) -> Vec<ActionItem> {
29+
env.storage().get(&ACTIONS).unwrap().unwrap()
30+
}
31+
fn log_action(env: &Env, action: &ActionItem) {
32+
let mut actions = Self::actions(env.clone());
33+
actions.push_back(*action);
34+
env.storage().set(&ACTIONS, &actions);
35+
}
36+
2337
/// wrapping interface implemention
2438
pub fn init(
2539
env: Env,
@@ -34,7 +48,7 @@ impl LoggingEngine {
3448
) {
3549
if !env.storage().has(&ENGINE_ID) {
3650
log!(&env, "Call 'wrap' first");
37-
panic!();
51+
panic_with_error!(&env, ProxyError::NotWrapped);
3852
}
3953

4054
Self::get_engine(&env).init(
@@ -49,19 +63,41 @@ impl LoggingEngine {
4963
);
5064
}
5165
pub fn p_turn(env: Env, direction: game_engine::Direction) -> Result<(), game_engine::Error> {
52-
Ok(Self::get_engine(&env).p_turn(&direction))
66+
if let Err(Ok(e)) = Self::get_engine(&env).try_p_turn(&direction) {
67+
return Err(e);
68+
}
69+
Self::log_action(&env, &ActionItem(Action::Turn, direction as u32));
70+
Ok(())
5371
}
5472
pub fn p_move(env: Env, times: Option<u32>) -> Result<(), game_engine::Error> {
55-
Ok(Self::get_engine(&env).p_move(&times))
73+
if let Err(Ok(e)) = Self::get_engine(&env).try_p_move(&times) {
74+
return Err(e);
75+
}
76+
Self::log_action(&env, &ActionItem(Action::Move, 1));
77+
Ok(())
5678
}
5779
pub fn p_shoot(env: Env) -> Result<(), game_engine::Error> {
58-
Ok(Self::get_engine(&env).p_shoot())
80+
let p = Self::get_engine(&env).p_points();
81+
if let Err(Ok(e)) = Self::get_engine(&env).try_p_shoot() {
82+
return Err(e);
83+
}
84+
let hits = Self::get_engine(&env).p_points() - p;
85+
Self::log_action(&env, &ActionItem(Action::Shoot, hits));
86+
Ok(())
5987
}
6088
pub fn p_harvest(env: Env) -> Result<(), game_engine::Error> {
61-
Ok(Self::get_engine(&env).p_harvest())
89+
if let Err(Ok(e)) = Self::get_engine(&env).try_p_harvest() {
90+
return Err(e);
91+
}
92+
Self::log_action(&env, &ActionItem(Action::Harvest, 1));
93+
Ok(())
6294
}
6395
pub fn p_upgrade(env: Env) -> Result<(), game_engine::Error> {
64-
Ok(Self::get_engine(&env).p_upgrade())
96+
if let Err(Ok(e)) = Self::get_engine(&env).try_p_upgrade() {
97+
return Err(e);
98+
}
99+
Self::log_action(&env, &ActionItem(Action::Upgrade, 1));
100+
Ok(())
65101
}
66102
pub fn p_pos(env: Env) -> game_engine::Point {
67103
Self::get_engine(&env).p_pos()

contracts/logging_engine/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#![no_std]
22
pub mod engine;
3+
mod types;

contracts/logging_engine/src/types.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use soroban_sdk::{contracterror, contracttype};
2+
3+
#[contracterror]
4+
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd)]
5+
#[repr(u32)]
6+
pub enum ProxyError {
7+
UnknownErr = 10,
8+
NotWrapped = 11,
9+
}
10+
11+
#[contracttype]
12+
#[derive(Copy, Clone, Debug, PartialEq, PartialOrd)]
13+
pub enum Action {
14+
Upgrade,
15+
Shoot,
16+
Harvest,
17+
Turn,
18+
Move,
19+
}
20+
21+
#[contracttype]
22+
#[derive(Copy, Clone)]
23+
pub struct ActionItem(pub Action, pub u32);

0 commit comments

Comments
 (0)