Skip to content

Commit 5520b92

Browse files
authored
Merge pull request #1312 from Phala-Network/revert-1309-revert-1305-driver-history
Revert "Revert "pink: Add API driver_history""
2 parents f1524b1 + 924215e commit 5520b92

File tree

3 files changed

+87
-7
lines changed

3 files changed

+87
-7
lines changed

crates/pink-drivers/system/lib.rs

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,41 @@ pub use system::System;
1010
mod system {
1111
use super::pink;
1212
use alloc::string::String;
13+
use alloc::vec::Vec;
1314
use ink::{codegen::Env, storage::Mapping};
1415
use pink::system::{CodeType, ContractDeposit, ContractDepositRef, DriverError, Error, Result};
1516
use pink::{HookPoint, PinkEnvironment};
1617

1718
use this_crate::{version_tuple, VersionTuple};
1819

20+
/// A new driver is set.
21+
#[ink(event)]
22+
pub struct DriverChanged {
23+
#[ink(topic)]
24+
name: String,
25+
previous: Option<AccountId>,
26+
current: AccountId,
27+
}
28+
29+
/// A new administrator is added.
30+
#[ink(event)]
31+
pub struct AdministratorAdded {
32+
user: AccountId,
33+
}
34+
1935
/// Pink's system contract.
2036
#[ink(storage)]
2137
pub struct System {
2238
/// The owner of the contract(the cluster).
2339
owner: AccountId,
2440
/// The administrators
2541
administrators: Mapping<AccountId, ()>,
26-
/// The drivers
42+
/// The drivers (deprecated)
2743
drivers: Mapping<String, AccountId>,
44+
/// The drivers
45+
drivers2: Mapping<String, (BlockNumber, AccountId)>,
46+
/// The history of drivers
47+
drivers_history: Mapping<String, Vec<(BlockNumber, AccountId)>>,
2848
}
2949

3050
impl System {
@@ -34,6 +54,8 @@ mod system {
3454
owner: Self::env().caller(),
3555
administrators: Default::default(),
3656
drivers: Default::default(),
57+
drivers2: Default::default(),
58+
drivers_history: Default::default(),
3759
}
3860
}
3961

@@ -96,7 +118,9 @@ mod system {
96118
#[ink(message)]
97119
fn grant_admin(&mut self, contract_id: AccountId) -> Result<()> {
98120
self.ensure_owner()?;
99-
self.administrators.insert(contract_id, &());
121+
self.administrators.insert(&contract_id, &());
122+
self.env()
123+
.emit_event(AdministratorAdded { user: contract_id });
100124
Ok(())
101125
}
102126

@@ -109,13 +133,36 @@ mod system {
109133
}
110134
_ => {}
111135
}
112-
self.drivers.insert(name, &contract_id);
136+
137+
let previous = self.get_driver2(name.clone());
138+
if let Some((block, previous)) = previous {
139+
if previous == contract_id {
140+
return Ok(());
141+
}
142+
let mut history = self.drivers_history.get(&name).unwrap_or_default();
143+
history.push((block, previous));
144+
self.drivers_history.insert(&name, &history);
145+
}
146+
self.drivers2
147+
.insert(&name, &(self.env().block_number(), contract_id));
148+
self.env().emit_event(DriverChanged {
149+
name,
150+
previous: previous.map(|(_, id)| id),
151+
current: contract_id,
152+
});
113153
Ok(())
114154
}
115155

116156
#[ink(message)]
117157
fn get_driver(&self, name: String) -> Option<AccountId> {
118-
self.drivers.get(&name)
158+
self.get_driver2(name).map(|(_, id)| id)
159+
}
160+
161+
#[ink(message)]
162+
fn get_driver2(&self, name: String) -> Option<(BlockNumber, AccountId)> {
163+
self.drivers2
164+
.get(&name)
165+
.or_else(|| self.drivers.get(&name).map(|id| (0, id)))
119166
}
120167

121168
#[ink(message)]
@@ -222,6 +269,16 @@ mod system {
222269
fn code_exists(&self, code_hash: [u8; 32], code_type: CodeType) -> bool {
223270
pink::ext().code_exists(code_hash.into(), code_type.is_sidevm())
224271
}
272+
273+
#[ink(message)]
274+
fn code_hash(&self, account: AccountId) -> Option<ink::primitives::Hash> {
275+
self.env().code_hash(&account).ok()
276+
}
277+
278+
#[ink(message)]
279+
fn driver_history(&self, name: String) -> Option<Vec<(BlockNumber, AccountId)>> {
280+
self.drivers_history.get(&name)
281+
}
225282
}
226283

227284
impl ContractDeposit for System {

crates/pink-drivers/tokenomic/lib.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ mod tokenomic {
1010

1111
type Result<T> = core::result::Result<T, Error>;
1212

13+
#[ink(event)]
14+
pub struct WeightChanged {
15+
#[ink(topic)]
16+
contract_id: AccountId,
17+
weight: u32,
18+
}
19+
1320
#[ink(storage)]
1421
pub struct PhatTokenomic {}
1522

@@ -40,7 +47,12 @@ mod tokenomic {
4047
const CENTS: Balance = 10_000_000_000;
4148
let system = SystemRef::instance();
4249
let weight = deposit / CENTS;
43-
system.set_contract_weight(contract_id, weight.try_into().unwrap_or(u32::MAX))?;
50+
let weight = weight.try_into().unwrap_or(u32::MAX);
51+
system.set_contract_weight(contract_id.clone(), weight)?;
52+
self.env().emit_event(WeightChanged {
53+
contract_id,
54+
weight
55+
});
4456
Ok(())
4557
}
4658
}

crates/pink/pink-extension/src/system.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use pink_extension_macro as pink;
44

55
use alloc::string::String;
6+
use alloc::vec::Vec;
67
use scale::{Decode, Encode};
78

89
use crate::{AccountId, Balance, Hash};
@@ -64,11 +65,13 @@ pub trait System {
6465
fn set_driver(&mut self, name: String, contract_id: AccountId) -> Result<()>;
6566

6667
/// Get driver contract id for `name`.
67-
///
68-
/// The caller must be the owner of the cluster or an administrator.
6968
#[ink(message)]
7069
fn get_driver(&self, name: String) -> Option<AccountId>;
7170

71+
/// Get driver contract id for `name` and the set block number.
72+
#[ink(message)]
73+
fn get_driver2(&self, name: String) -> Option<(crate::BlockNumber, AccountId)>;
74+
7275
/// Deploy a sidevm instance attached to a given contract.
7376
///
7477
/// The caller must be an administrator.
@@ -125,6 +128,14 @@ pub trait System {
125128
/// Check if the code is already uploaded to the cluster with given code hash.
126129
#[ink(message)]
127130
fn code_exists(&self, code_hash: Hash, code_type: CodeType) -> bool;
131+
132+
/// Get the current code hash of given contract.
133+
#[ink(message)]
134+
fn code_hash(&self, account: AccountId) -> Option<ink::primitives::Hash>;
135+
136+
/// Get the history of given driver.
137+
#[ink(message)]
138+
fn driver_history(&self, name: String) -> Option<Vec<(crate::BlockNumber, AccountId)>>;
128139
}
129140

130141
/// Errors that can occur upon calling a driver contract.

0 commit comments

Comments
 (0)