-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathbatch.rs
151 lines (124 loc) · 3.72 KB
/
batch.rs
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
use ibc::events::IbcEvent;
use ibc_proto::google::protobuf::Any;
use prost::Message;
use tendermint_rpc::endpoint::broadcast::tx_sync::Response;
use crate::chain::cosmos::retry::send_tx_with_account_sequence_retry;
use crate::chain::cosmos::types::account::Account;
use crate::chain::cosmos::types::config::TxConfig;
use crate::chain::cosmos::types::tx::TxSyncResult;
use crate::chain::cosmos::wait::wait_for_block_commits;
use crate::config::types::{MaxMsgNum, MaxTxSize, Memo};
use crate::error::Error;
use crate::keyring::KeyEntry;
pub async fn send_batched_messages_and_wait_commit(
config: &TxConfig,
max_msg_num: MaxMsgNum,
max_tx_size: MaxTxSize,
key_entry: &KeyEntry,
account: &mut Account,
tx_memo: &Memo,
messages: Vec<Any>,
) -> Result<Vec<IbcEvent>, Error> {
if messages.is_empty() {
return Ok(Vec::new());
}
let mut tx_sync_results = send_messages_as_batches(
config,
max_msg_num,
max_tx_size,
key_entry,
account,
tx_memo,
messages,
)
.await?;
wait_for_block_commits(
&config.chain_id,
&config.rpc_client,
&config.rpc_address,
&config.rpc_timeout,
&mut tx_sync_results,
)
.await?;
let events = tx_sync_results
.into_iter()
.flat_map(|el| el.events)
.collect();
Ok(events)
}
pub async fn send_batched_messages_and_wait_check_tx(
config: &TxConfig,
max_msg_num: MaxMsgNum,
max_tx_size: MaxTxSize,
key_entry: &KeyEntry,
account: &mut Account,
tx_memo: &Memo,
messages: Vec<Any>,
) -> Result<Vec<Response>, Error> {
if messages.is_empty() {
return Ok(Vec::new());
}
let batches = batch_messages(max_msg_num, max_tx_size, messages)?;
let mut responses = Vec::new();
for batch in batches {
let response =
send_tx_with_account_sequence_retry(config, key_entry, account, tx_memo, batch, 0)
.await?;
responses.push(response);
}
Ok(responses)
}
async fn send_messages_as_batches(
config: &TxConfig,
max_msg_num: MaxMsgNum,
max_tx_size: MaxTxSize,
key_entry: &KeyEntry,
account: &mut Account,
tx_memo: &Memo,
messages: Vec<Any>,
) -> Result<Vec<TxSyncResult>, Error> {
if messages.is_empty() {
return Ok(Vec::new());
}
let batches = batch_messages(max_msg_num, max_tx_size, messages)?;
let mut tx_sync_results = Vec::new();
for batch in batches {
let events_per_tx = vec![IbcEvent::default(); batch.len()];
let response =
send_tx_with_account_sequence_retry(config, key_entry, account, tx_memo, batch, 0)
.await?;
let tx_sync_result = TxSyncResult {
response,
events: events_per_tx,
};
tx_sync_results.push(tx_sync_result);
}
Ok(tx_sync_results)
}
fn batch_messages(
max_msg_num: MaxMsgNum,
max_tx_size: MaxTxSize,
messages: Vec<Any>,
) -> Result<Vec<Vec<Any>>, Error> {
let max_message_count = max_msg_num.to_usize();
let max_tx_size = max_tx_size.into();
let mut batches = vec![];
let mut current_count = 0;
let mut current_size = 0;
let mut current_batch = vec![];
for message in messages.into_iter() {
current_count += 1;
current_size += message.encoded_len();
current_batch.push(message);
if current_count >= max_message_count || current_size >= max_tx_size {
let insert_batch = current_batch.drain(..).collect();
batches.push(insert_batch);
current_count = 0;
current_size = 0;
}
}
if !current_batch.is_empty() {
batches.push(current_batch);
}
Ok(batches)
}