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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use std::sync::Arc;
use ethereum_types::U256;
use jsonrpc_core::{
futures::{Async, Future, IntoFuture, Poll},
Error, Result,
};
use types::transaction::SignedTransaction;
use super::{Accounts, PostSign, SignWith, WithToken};
use v1::helpers::{errors, nonce, FilledTransactionRequest};
#[derive(Debug, Clone, Copy)]
enum ProspectiveSignerState {
TryProspectiveSign,
WaitForPostSign,
WaitForNonce,
}
pub struct ProspectiveSigner<P: PostSign> {
signer: Arc<dyn Accounts>,
filled: FilledTransactionRequest,
chain_id: Option<u64>,
reserved: nonce::Reserved,
password: SignWith,
state: ProspectiveSignerState,
prospective: Option<WithToken<SignedTransaction>>,
ready: Option<nonce::Ready>,
post_sign: Option<P>,
post_sign_future: Option<<P::Out as IntoFuture>::Future>,
}
impl<P: PostSign> ProspectiveSigner<P> {
pub fn new(
signer: Arc<dyn Accounts>,
filled: FilledTransactionRequest,
chain_id: Option<u64>,
reserved: nonce::Reserved,
password: SignWith,
post_sign: P,
) -> Self {
let supports_prospective = signer.supports_prospective_signing(&filled.from, &password);
ProspectiveSigner {
signer,
filled,
chain_id,
reserved,
password,
state: if supports_prospective {
ProspectiveSignerState::TryProspectiveSign
} else {
ProspectiveSignerState::WaitForNonce
},
prospective: None,
ready: None,
post_sign: Some(post_sign),
post_sign_future: None,
}
}
fn sign(&self, nonce: &U256) -> Result<WithToken<SignedTransaction>> {
self.signer.sign_transaction(
self.filled.clone(),
self.chain_id,
*nonce,
self.password.clone(),
)
}
fn poll_reserved(&mut self) -> Poll<nonce::Ready, Error> {
self.reserved
.poll()
.map_err(|_| errors::internal("Nonce reservation failure", ""))
}
}
impl<P: PostSign> Future for ProspectiveSigner<P> {
type Item = P::Item;
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
use self::ProspectiveSignerState::*;
loop {
match self.state {
TryProspectiveSign => {
match self.poll_reserved()? {
Async::NotReady => {
self.state = WaitForNonce;
self.prospective = Some(self.sign(self.reserved.prospective_value())?);
}
Async::Ready(nonce) => {
self.state = WaitForPostSign;
self.post_sign_future = Some(
self.post_sign
.take()
.expect("post_sign is set on creation; qed")
.execute(self.sign(nonce.value())?)
.into_future(),
);
self.ready = Some(nonce);
}
}
}
WaitForNonce => {
let nonce = try_ready!(self.poll_reserved());
let prospective = match (self.prospective.take(), nonce.matches_prospective()) {
(Some(prospective), true) => prospective,
_ => self.sign(nonce.value())?,
};
self.ready = Some(nonce);
self.state = WaitForPostSign;
self.post_sign_future = Some(
self.post_sign
.take()
.expect("post_sign is set on creation; qed")
.execute(prospective)
.into_future(),
);
}
WaitForPostSign => {
if let Some(fut) = self.post_sign_future.as_mut() {
match fut.poll()? {
Async::Ready(item) => {
let nonce = self.ready.take().expect(
"nonce is set before state transitions to WaitForPostSign; qed",
);
nonce.mark_used();
return Ok(Async::Ready(item));
}
Async::NotReady => return Ok(Async::NotReady),
}
} else {
panic!("Poll after ready.");
}
}
}
}
}
}