1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*use actix_web::web::Data;
use async_trait::async_trait;
use autonomi::{PointerAddress, SecretKey};
use autonomi::pointer::PointerTarget;
use indexmap::IndexMap;
use log::{debug, info};
use sha2::Digest;
use tokio::sync::Mutex;
use crate::client::client_harness::ClientHarness;
use crate::client::command::error::CommandError;
use crate::client::command::Command;
pub struct UpdatePointerCommand {
id: u128,
client_harness: Data<Mutex<ClientHarness>>,
owner: SecretKey,
target: PointerTarget,
counter: Option<u64>,
}
impl UpdatePointerCommand {
pub fn new(client_harness: Data<Mutex<ClientHarness>>, owner: SecretKey, target: PointerTarget, counter: Option<u64>) -> Self {
let id = rand::random::<u128>();
Self { id, client_harness, owner, target, counter }
}
}
const STRUCT_NAME: &'static str = "UpdatePointerCommand";
#[async_trait]
impl Command for UpdatePointerCommand {
async fn execute(&self) -> Result<(), CommandError> {
let client = self.client_harness.get_ref().lock().await.get_client().await?;
let pointer_address_hex = PointerAddress::new(self.owner.public_key()).to_hex();
debug!("updating pointer at [{}] async", pointer_address_hex);
match self.counter {
Some(counter) => {
// decrement counter, as pointer_update_from will increment
let pointer = ant_protocol::storage::Pointer::new(&self.owner, counter - 1, self.target.clone());
client.pointer_update_from(&pointer, &self.owner, self.target.clone()).await?;
},
None => client.pointer_update(&self.owner, self.target.clone()).await?
}
info!("pointer at address [{}] updated successfully", pointer_address_hex);
Ok(())
}
fn action_hash(&self) -> Vec<u8> {
let mut hasher = sha2::Sha256::new();
hasher.update(STRUCT_NAME);
hasher.update(self.owner.to_hex());
hasher.update(self.target.to_hex());
hasher.finalize().to_ascii_lowercase()
}
fn id(&self) -> u128 {
self.id
}
fn name(&self) -> String {
STRUCT_NAME.to_string()
}
fn properties(&self) -> IndexMap<String, String> {
let mut properties = IndexMap::new();
properties.insert("owner".to_string(), self.owner.to_hex());
properties.insert("target".to_string(), self.target.to_hex());
if let Some(counter) = self.counter {
properties.insert("counter".to_string(), counter.to_string());
}
properties
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::anttp_config::AntTpConfig;
use clap::Parser;
use ant_evm::EvmNetwork;
use autonomi::ChunkAddress;
fn create_test_command(counter: Option<u64>) -> UpdatePointerCommand {
let config = AntTpConfig::try_parse_from(&[
"anttp",
"--app-private-key",
"55dcbc4624699d219b8ec293339a3b81e68815397f5a502026784d8122d09fce",
]).unwrap();
let client_harness = ClientHarness::new(EvmNetwork::ArbitrumOne, config.clone());
let owner = config.get_app_private_key().unwrap();
let target = PointerTarget::ChunkAddress(ChunkAddress::from_hex("a40e045a6fbed33b27039aa8383c9dbf286e19a7265141c2da3085e0c8571527").unwrap());
UpdatePointerCommand::new(
Data::new(Mutex::new(client_harness)),
owner,
target,
counter,
)
}
#[test]
fn test_update_pointer_command_new() {
let counter = Some(10);
let command = create_test_command(counter);
assert_eq!(command.counter, counter);
assert_eq!(command.name(), "UpdatePointerCommand");
}
#[test]
fn test_update_pointer_command_properties() {
let counter = Some(10);
let command = create_test_command(counter);
let properties = command.properties();
assert_eq!(properties.get("counter").unwrap(), "10");
assert!(properties.contains_key("owner"));
assert!(properties.contains_key("target"));
}
#[test]
fn test_update_pointer_command_properties_no_counter() {
let command = create_test_command(None);
let properties = command.properties();
assert!(!properties.contains_key("counter"));
}
}*/