Skip to content

Commit 8af0f30

Browse files
committed
feat(actors): run signature manager on its own thread
1 parent c7dfbba commit 8af0f30

File tree

2 files changed

+92
-32
lines changed

2 files changed

+92
-32
lines changed

node/src/actors/node.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub fn run(config: Config, callback: fn()) -> Result<(), failure::Error> {
2727
config_mngr::start();
2828
actix::Arbiter::spawn(config_mngr::set(config).map_err(|_| System::current().stop()));
2929

30+
// Start StorageManager actor & SignatureManager
3031
storage_mngr::start();
3132
signature_mngr::start();
3233

node/src/signature_mngr.rs

+91-32
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,10 @@ use witnet_protected::ProtectedString;
2727

2828
/// Start the signature manager
2929
pub fn start() {
30-
let addr = SignatureManager::start_default();
30+
let addr = SignatureManagerAdapter::start_default();
3131
actix::SystemRegistry::set(addr);
3232
}
3333

34-
/// Set the key used to sign
35-
pub fn set_key(key: SK) -> impl Future<Item = (), Error = failure::Error> {
36-
let addr = SignatureManager::from_registry();
37-
addr.send(SetKey(key)).flatten()
38-
}
39-
4034
/// Sign a piece of (Hashable) data with the stored key.
4135
///
4236
/// This might fail if the manager has not been initialized with a key
@@ -53,31 +47,31 @@ where
5347
///
5448
/// This might fail if the manager has not been initialized with a key
5549
pub fn sign_data(data: [u8; 32]) -> impl Future<Item = KeyedSignature, Error = failure::Error> {
56-
let addr = SignatureManager::from_registry();
50+
let addr = SignatureManagerAdapter::from_registry();
5751
addr.send(Sign(data.to_vec())).flatten()
5852
}
5953

6054
/// Get the public key hash.
6155
///
6256
/// This might fail if the manager has not been initialized with a key
6357
pub fn pkh() -> impl Future<Item = PublicKeyHash, Error = failure::Error> {
64-
let addr = SignatureManager::from_registry();
58+
let addr = SignatureManagerAdapter::from_registry();
6559
addr.send(GetPkh).flatten()
6660
}
6761

6862
/// Get the public key.
6963
///
7064
/// This might fail if the manager has not been initialized with a key
7165
pub fn public_key() -> impl Future<Item = PublicKey, Error = failure::Error> {
72-
let addr = SignatureManager::from_registry();
66+
let addr = SignatureManagerAdapter::from_registry();
7367
addr.send(GetPublicKey).flatten()
7468
}
7569

7670
/// Create a VRF proof for the provided message with the stored key
7771
pub fn vrf_prove(
7872
message: VrfMessage,
7973
) -> impl Future<Item = (VrfProof, Hash), Error = failure::Error> {
80-
let addr = SignatureManager::from_registry();
74+
let addr = SignatureManagerAdapter::from_registry();
8175
addr.send(VrfProve(message)).flatten()
8276
}
8377

@@ -100,9 +94,13 @@ impl SignatureManager {
10094
}
10195

10296
struct SetKey(SK);
97+
10398
struct Sign(Vec<u8>);
99+
104100
struct GetPkh;
101+
105102
struct GetPublicKey;
103+
106104
struct VrfProve(VrfMessage);
107105

108106
fn persist_master_key(master_key: ExtendedSK) -> impl Future<Item = (), Error = failure::Error> {
@@ -134,7 +132,7 @@ fn create_master_key() -> Box<dyn Future<Item = SK, Error = failure::Error>> {
134132
}
135133

136134
impl Actor for SignatureManager {
137-
type Context = Context<Self>;
135+
type Context = SyncContext<Self>;
138136

139137
fn started(&mut self, ctx: &mut Self::Context) {
140138
log::debug!("Signature Manager actor has been started!");
@@ -148,29 +146,9 @@ impl Actor for SignatureManager {
148146
.ok();
149147

150148
self.secp = Some(CryptoEngine::new());
151-
152-
storage_mngr::get::<_, ExtendedSecretKey>(&MASTER_KEY)
153-
.and_then(move |master_key_from_storage| {
154-
master_key_from_storage.map_or_else(create_master_key, |master_key| {
155-
let master_key: ExtendedSK = master_key.into();
156-
let fut = futures::future::ok(master_key.into());
157-
158-
Box::new(fut)
159-
})
160-
})
161-
.map_err(|e| log::error!("Couldn't initialize Signature Manager: {}", e))
162-
.into_actor(self)
163-
.map(|secret_key, act, _ctx| {
164-
act.set_key(secret_key);
165-
})
166-
.wait(ctx);
167149
}
168150
}
169151

170-
impl Supervised for SignatureManager {}
171-
172-
impl SystemService for SignatureManager {}
173-
174152
impl Message for SetKey {
175153
type Result = Result<(), failure::Error>;
176154
}
@@ -267,3 +245,84 @@ impl Handler<VrfProve> for SignatureManager {
267245
}
268246
}
269247
}
248+
249+
struct SignatureManagerAdapter {
250+
crypto: Addr<SignatureManager>,
251+
}
252+
253+
impl Supervised for SignatureManagerAdapter {}
254+
255+
impl SystemService for SignatureManagerAdapter {}
256+
257+
impl Default for SignatureManagerAdapter {
258+
fn default() -> Self {
259+
let crypto = SyncArbiter::start(1, SignatureManager::default);
260+
Self { crypto }
261+
}
262+
}
263+
264+
impl Actor for SignatureManagerAdapter {
265+
type Context = Context<Self>;
266+
267+
fn started(&mut self, ctx: &mut Self::Context) {
268+
log::debug!("Signature Manager Adapter actor has been started!");
269+
let crypto = self.crypto.clone();
270+
271+
storage_mngr::get::<_, ExtendedSecretKey>(&MASTER_KEY)
272+
.and_then(move |master_key_from_storage| {
273+
master_key_from_storage.map_or_else(create_master_key, |master_key| {
274+
let master_key: ExtendedSK = master_key.into();
275+
let fut = futures::future::ok(master_key.into());
276+
277+
Box::new(fut)
278+
})
279+
})
280+
.and_then(move |master_key| crypto.send(SetKey(master_key)).flatten())
281+
.map_err(|err| {
282+
log::error!("Failed to configure master key: {}", err);
283+
System::current().stop_with_code(1);
284+
})
285+
.into_actor(self)
286+
.wait(ctx);
287+
}
288+
}
289+
290+
impl Handler<SetKey> for SignatureManagerAdapter {
291+
type Result = ResponseFuture<(), failure::Error>;
292+
293+
fn handle(&mut self, msg: SetKey, _ctx: &mut Self::Context) -> Self::Result {
294+
Box::new(self.crypto.send(msg).flatten())
295+
}
296+
}
297+
298+
impl Handler<Sign> for SignatureManagerAdapter {
299+
type Result = ResponseFuture<KeyedSignature, failure::Error>;
300+
301+
fn handle(&mut self, msg: Sign, _ctx: &mut Self::Context) -> Self::Result {
302+
Box::new(self.crypto.send(msg).flatten())
303+
}
304+
}
305+
306+
impl Handler<GetPkh> for SignatureManagerAdapter {
307+
type Result = ResponseFuture<PublicKeyHash, failure::Error>;
308+
309+
fn handle(&mut self, msg: GetPkh, _ctx: &mut Self::Context) -> Self::Result {
310+
Box::new(self.crypto.send(msg).flatten())
311+
}
312+
}
313+
314+
impl Handler<GetPublicKey> for SignatureManagerAdapter {
315+
type Result = ResponseFuture<PublicKey, failure::Error>;
316+
317+
fn handle(&mut self, msg: GetPublicKey, _ctx: &mut Self::Context) -> Self::Result {
318+
Box::new(self.crypto.send(msg).flatten())
319+
}
320+
}
321+
322+
impl Handler<VrfProve> for SignatureManagerAdapter {
323+
type Result = ResponseFuture<(VrfProof, Hash), failure::Error>;
324+
325+
fn handle(&mut self, msg: VrfProve, _ctx: &mut Self::Context) -> Self::Result {
326+
Box::new(self.crypto.send(msg).flatten())
327+
}
328+
}

0 commit comments

Comments
 (0)