Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 80d6720

Browse files
authored
Cob/sequential trx requests (#278)
* add sorting of trx requests in transaction pool format re-remove split_once directive * fix test * fix test
1 parent 7337a1c commit 80d6720

File tree

5 files changed

+89
-17
lines changed

5 files changed

+89
-17
lines changed

chains/scripts/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
usage: scripts to be run in the context of the repl
2+
yarn repl -s path_to_my_script.js

chains/scripts/add_users.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
async function loadem(starting_page = 0) {
2+
var cash_nonce = await api.query.cash.nonces({eth: saddle.account});
3+
await feedAccount(cash_nonce, starting_page);
4+
}
5+
6+
async function feedAccount(n, r, accounts = [] ) {
7+
const user = saddle.account
8+
var accounts
9+
var cash_nonce = n
10+
if (r > 2000) return;
11+
12+
if (accounts.length == 0 || i == accounts.length) {
13+
console.log(r)
14+
accounts =
15+
await fetch(`https://api.compound.finance/api/v2/account?page_number=${r}&page_size=200`)
16+
.then(r => r.json())
17+
.then( b => b.accounts.map( x => x.address))
18+
i = 0;
19+
r++;
20+
}
21+
22+
console.log(accounts)
23+
for (const a of accounts) {
24+
// let a = accounts[i]
25+
let request = `(Transfer ${1234567 + cash_nonce} Cash Eth:${a})`
26+
let req = `${cash_nonce}:${request}`
27+
let sig = await saddle.web3.eth.sign(req, user)
28+
console.log("🅰️", a)
29+
console.log("🎲", req)
30+
let tx = api.tx.cash.execTrxRequest(request, {'Eth': [user, sig]}, cash_nonce)
31+
32+
await tx.send(({ status }) => {
33+
if (status.isDropped) {
34+
console.log(`dopped ${status.events}`);
35+
}
36+
if (status.isInvalid) {
37+
console.log(`invalid ${status.events}`);
38+
}
39+
if (status.isInBlock) {
40+
console.log(`included in ${status}`);
41+
42+
}
43+
if (status.isFinalized) {
44+
console.log(`${request} finailzied in ${status}`);
45+
}
46+
})
47+
cash_nonce++
48+
}
49+
feedAccount(cash_nonce, r)
50+
}

node/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ default = ['with-parity-db']
9898
with-parity-db=['sc-client-db/with-parity-db']
9999
with-rocks-db=['sc-client-db/with-kvdb-rocksdb']
100100
runtime-benchmarks = [
101+
'with-rocks-db',
101102
'gateway-runtime/runtime-benchmarks',
102103
'frame-benchmarking-cli',
103104
]

pallets/cash/src/internal/exec_trx_request.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn is_minimally_valid_trx_request<T: Config>(
4949
request: Vec<u8>,
5050
signature: ChainAccountSignature,
5151
nonce: Nonce,
52-
) -> Result<ChainAccount, Reason> {
52+
) -> Result<(ChainAccount, Nonce), Reason> {
5353
// Basic request validity checks - valid symbols and parsable request
5454
let request_str: &str = str::from_utf8(&request[..]).map_err(|_| Reason::InvalidUTF8)?;
5555
trx_request::parse_request(request_str)?;
@@ -59,13 +59,8 @@ pub fn is_minimally_valid_trx_request<T: Config>(
5959
.recover_account(&prepend_nonce(&request, nonce)[..])
6060
.map_err(|_| Reason::SignatureAccountMismatch)?;
6161

62-
// Nonce check
6362
let current_nonce = Nonces::get(sender);
64-
require!(
65-
nonce == current_nonce,
66-
Reason::IncorrectNonce(nonce, current_nonce)
67-
);
68-
Ok(sender)
63+
Ok((sender, current_nonce))
6964
}
7065

7166
pub fn exec_trx_request<T: Config>(

pallets/cash/src/internal/validate_trx.rs

+34-10
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,29 @@ pub fn validate_unsigned<T: Config>(
8888

8989
match (signer_res, nonce) {
9090
(Err(e), _) => Err(ValidationError::InvalidTrxRequest(e)),
91-
(Ok(sender), nonce) => Ok(ValidTransaction::with_tag_prefix(
92-
"Gateway::exec_trx_request",
93-
)
94-
.priority(UNSIGNED_TXS_PRIORITY)
95-
.longevity(UNSIGNED_TXS_LONGEVITY)
96-
.and_provides((sender, nonce))
97-
.propagate(true)
98-
.build()),
91+
(Ok((sender, current_nonce)), nonce) => {
92+
// Nonce check
93+
if current_nonce == 0 || *nonce == current_nonce {
94+
Ok(
95+
ValidTransaction::with_tag_prefix("Gateway::exec_trx_request")
96+
.priority(UNSIGNED_TXS_PRIORITY)
97+
.longevity(UNSIGNED_TXS_LONGEVITY)
98+
.and_provides((sender, nonce))
99+
.propagate(true)
100+
.build(),
101+
)
102+
} else {
103+
Ok(
104+
ValidTransaction::with_tag_prefix("Gateway::exec_trx_request")
105+
.priority(UNSIGNED_TXS_PRIORITY)
106+
.longevity(UNSIGNED_TXS_LONGEVITY)
107+
.and_requires((sender, nonce - 1))
108+
.and_provides((sender, nonce))
109+
.propagate(true)
110+
.build(),
111+
)
112+
}
113+
}
99114
}
100115
}
101116
Call::publish_signature(chain_id, notice_id, signature) => {
@@ -414,7 +429,7 @@ mod tests {
414429
}
415430

416431
#[test]
417-
fn test_exec_trx_request_invalid_request_wrong_nonce() {
432+
fn test_exec_trx_request_valid_request_wrong_nonce() {
418433
new_test_ext().execute_with(|| {
419434
let request: Vec<u8> = String::from(
420435
"(Extract 50000000 Cash Eth:0xfc04833Ca66b7D6B4F540d4C2544228f64a25ac2)",
@@ -434,12 +449,21 @@ mod tests {
434449

435450
Nonces::insert(ChainAccount::Eth(eth_address), nonce - 1);
436451

452+
let exp = ValidTransaction::with_tag_prefix("Gateway::exec_trx_request")
453+
.priority(UNSIGNED_TXS_PRIORITY)
454+
.longevity(UNSIGNED_TXS_LONGEVITY)
455+
.and_requires((ChainAccount::Eth(eth_address), nonce - 1))
456+
.and_provides((ChainAccount::Eth(eth_address), nonce))
457+
.propagate(true)
458+
.build();
459+
460+
437461
assert_eq!(
438462
validate_unsigned(
439463
TransactionSource::InBlock {},
440464
&Call::exec_trx_request::<Test>(request, signature, nonce),
441465
),
442-
Err(ValidationError::InvalidTrxRequest(Reason::IncorrectNonce(nonce, nonce - 1)))
466+
Ok(exp)
443467
);
444468
});
445469
}

0 commit comments

Comments
 (0)