Skip to content

Commit 1702d65

Browse files
committed
Refactor sign_remote
1 parent 79f5ea1 commit 1702d65

File tree

1 file changed

+19
-31
lines changed
  • chains/nomad-substrate/src

1 file changed

+19
-31
lines changed

chains/nomad-substrate/src/aws.rs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -77,46 +77,34 @@ impl AwsPair {
7777
self.pubkey
7878
}
7979

80-
/// Try to sign `message` using our remote signer. Accept a `delay` so that
81-
/// this can be called repeatedly with a backoff
82-
async fn try_sign_remote(
83-
&self,
84-
message: &[u8],
85-
delay: Duration,
86-
) -> Result<AwsSignature, AwsSignerError> {
87-
sleep(delay).await;
88-
// Sign and map between our remote and local 65-byte ECDSA sigs
89-
self.signer
90-
.sign_message(message)
91-
.await
92-
.map(Into::<AwsSignature>::into)
93-
}
94-
9580
/// Try to sign `message` `max_retries` times with an exponential backoff between attempts.
9681
/// If we hit `max_retries` `panic` since we're unable to return an error here.
9782
fn sign_remote(&self, message: &[u8]) -> AwsSignature {
98-
let mut times_attempted = 0;
99-
let mut delay = Duration::from_millis(0);
10083
tokio::runtime::Builder::new_current_thread()
10184
.enable_all()
10285
.build()
10386
.expect("unable to create tokio::runtime (this should never happen)")
10487
.block_on(async {
105-
loop {
106-
match self.try_sign_remote(message, delay).await {
107-
Ok(signature) => return signature,
108-
Err(error) => {
109-
times_attempted += 1;
110-
delay = Duration::from_millis(self.min_retry_ms.pow(times_attempted));
111-
if times_attempted == self.max_retries {
112-
panic!(
113-
"giving up after attempting to sign message {} times: {:?}",
114-
times_attempted, error,
115-
)
116-
}
117-
}
118-
}
88+
let mut error = None;
89+
for i in 0..self.max_retries {
90+
sleep(Duration::from_millis(self.min_retry_ms.pow(i))).await;
91+
error = Some(
92+
match self
93+
.signer
94+
.sign_message(message)
95+
.await
96+
.map(Into::<AwsSignature>::into)
97+
{
98+
Ok(signature) => return signature,
99+
Err(error) => error,
100+
},
101+
);
119102
}
103+
panic!(
104+
"giving up after attempting to sign message {} times: {:?}",
105+
self.max_retries,
106+
error.unwrap(),
107+
);
120108
})
121109
}
122110
}

0 commit comments

Comments
 (0)