-
Notifications
You must be signed in to change notification settings - Fork 710
/
client.rs
797 lines (686 loc) · 24.6 KB
/
client.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
// This file is part of Substrate.
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! The client connects to the source substrate chain
//! and is used by the rpc server to query and send transactions to the substrate chain.
use crate::{
rlp,
runtime::GAS_PRICE,
subxt_client::{
revive::calls::types::EthTransact, runtime_types::pallet_revive::storage::ContractInfo,
},
TransactionLegacySigned, LOG_TARGET,
};
use codec::Encode;
use futures::{stream, StreamExt};
use jsonrpsee::types::{ErrorCode, ErrorObjectOwned};
use pallet_revive::{
create1,
evm::{
Block, BlockNumberOrTag, BlockNumberOrTagOrHash, Bytes256, GenericTransaction, ReceiptInfo,
SyncingProgress, SyncingStatus, TransactionSigned, H160, H256, U256,
},
EthContractResult,
};
use sp_runtime::traits::{BlakeTwo256, Hash};
use sp_weights::Weight;
use std::{
collections::{HashMap, VecDeque},
sync::Arc,
time::Duration,
};
use subxt::{
backend::{
legacy::{rpc_methods::SystemHealth, LegacyRpcMethods},
rpc::{
reconnecting_rpc_client::{Client as ReconnectingRpcClient, ExponentialBackoff},
RpcClient,
},
},
config::Header,
error::RpcError,
storage::Storage,
Config, OnlineClient,
};
use subxt_client::transaction_payment::events::TransactionFeePaid;
use thiserror::Error;
use tokio::sync::{watch::Sender, RwLock};
use crate::subxt_client::{self, system::events::ExtrinsicSuccess, SrcChainConfig};
/// The substrate block type.
pub type SubstrateBlock = subxt::blocks::Block<SrcChainConfig, OnlineClient<SrcChainConfig>>;
/// The substrate block number type.
pub type SubstrateBlockNumber = <<SrcChainConfig as Config>::Header as Header>::Number;
/// The substrate block hash type.
pub type SubstrateBlockHash = <SrcChainConfig as Config>::Hash;
/// Type alias for shared data.
pub type Shared<T> = Arc<RwLock<T>>;
/// The runtime balance type.
pub type Balance = u128;
/// The cache maintains a buffer of the last N blocks,
#[derive(Default)]
struct BlockCache<const N: usize> {
/// A double-ended queue of the last N blocks.
/// The most recent block is at the back of the queue, and the oldest block is at the front.
buffer: VecDeque<Arc<SubstrateBlock>>,
/// A map of blocks by block number.
blocks_by_number: HashMap<SubstrateBlockNumber, Arc<SubstrateBlock>>,
/// A map of blocks by block hash.
blocks_by_hash: HashMap<H256, Arc<SubstrateBlock>>,
/// A map of receipts by hash.
receipts_by_hash: HashMap<H256, ReceiptInfo>,
/// A map of Signed transaction by hash.
signed_tx_by_hash: HashMap<H256, TransactionSigned>,
/// A map of receipt hashes by block hash.
tx_hashes_by_block_and_index: HashMap<H256, HashMap<U256, H256>>,
}
fn unwrap_subxt_err(err: &subxt::Error) -> String {
match err {
subxt::Error::Rpc(err) => unwrap_rpc_err(err),
_ => err.to_string(),
}
}
fn unwrap_rpc_err(err: &subxt::error::RpcError) -> String {
match err {
subxt::error::RpcError::ClientError(err) => match err
// TODO use the re-export from subxt once available
.downcast_ref::<jsonrpsee::core::ClientError>()
{
Some(jsonrpsee::core::ClientError::Call(call_err)) => call_err.message().to_string(),
Some(other_err) => other_err.to_string(),
None => err.to_string(),
},
_ => err.to_string(),
}
}
/// The error type for the client.
#[derive(Error, Debug)]
pub enum ClientError {
/// A [`subxt::Error`] wrapper error.
#[error("{}",unwrap_subxt_err(.0))]
SubxtError(#[from] subxt::Error),
/// A [`RpcError`] wrapper error.
#[error("{}",unwrap_rpc_err(.0))]
RpcError(#[from] RpcError),
/// A [`codec::Error`] wrapper error.
#[error(transparent)]
CodecError(#[from] codec::Error),
/// The dry run failed.
#[error("Dry run failed")]
DryRunFailed,
/// A decimal conversion failed.
#[error("Conversion failed")]
ConversionFailed,
/// The block hash was not found.
#[error("Hash not found")]
BlockNotFound,
/// The transaction fee could not be found
#[error("TransactionFeePaid event not found")]
TxFeeNotFound,
/// The cache is empty.
#[error("Cache is empty")]
CacheEmpty,
}
const GENERIC_ERROR_CODE: ErrorCode = ErrorCode::ServerError(-32000);
// Convert a `ClientError` to an RPC `ErrorObjectOwned`.
impl From<ClientError> for ErrorObjectOwned {
fn from(value: ClientError) -> Self {
log::debug!(target: LOG_TARGET, "ClientError: {value:?}");
ErrorObjectOwned::owned::<()>(GENERIC_ERROR_CODE.code(), value.to_string(), None)
}
}
/// The number of recent blocks maintained by the cache.
/// For each block in the cache, we also store the EVM transaction receipts.
pub const CACHE_SIZE: usize = 256;
impl<const N: usize> BlockCache<N> {
fn latest_block(&self) -> Option<&Arc<SubstrateBlock>> {
self.buffer.back()
}
/// Insert an entry into the cache, and prune the oldest entry if the cache is full.
fn insert(&mut self, block: SubstrateBlock) {
if self.buffer.len() >= N {
if let Some(block) = self.buffer.pop_front() {
log::trace!(target: LOG_TARGET, "Pruning block: {}", block.number());
let hash = block.hash();
self.blocks_by_hash.remove(&hash);
self.blocks_by_number.remove(&block.number());
if let Some(entries) = self.tx_hashes_by_block_and_index.remove(&hash) {
for hash in entries.values() {
self.receipts_by_hash.remove(hash);
}
}
}
}
let block = Arc::new(block);
self.buffer.push_back(block.clone());
self.blocks_by_number.insert(block.number(), block.clone());
self.blocks_by_hash.insert(block.hash(), block);
}
}
/// A client connect to a node and maintains a cache of the last `CACHE_SIZE` blocks.
#[derive(Clone)]
pub struct Client {
/// The inner state of the client.
inner: Arc<ClientInner>,
/// A watch channel to signal cache updates.
pub updates: tokio::sync::watch::Receiver<()>,
}
/// The inner state of the client.
struct ClientInner {
api: OnlineClient<SrcChainConfig>,
rpc_client: ReconnectingRpcClient,
rpc: LegacyRpcMethods<SrcChainConfig>,
cache: Shared<BlockCache<CACHE_SIZE>>,
chain_id: u64,
max_block_weight: Weight,
native_to_evm_ratio: U256,
}
impl ClientInner {
/// Create a new client instance connecting to the substrate node at the given URL.
async fn from_url(url: &str) -> Result<Self, ClientError> {
let rpc_client = ReconnectingRpcClient::builder()
.retry_policy(ExponentialBackoff::from_millis(100).max_delay(Duration::from_secs(10)))
.build(url.to_string())
.await?;
let api = OnlineClient::<SrcChainConfig>::from_rpc_client(rpc_client.clone()).await?;
let cache = Arc::new(RwLock::new(BlockCache::<CACHE_SIZE>::default()));
let rpc = LegacyRpcMethods::<SrcChainConfig>::new(RpcClient::new(rpc_client.clone()));
let (native_to_evm_ratio, chain_id, max_block_weight) =
tokio::try_join!(native_to_evm_ratio(&api), chain_id(&api), max_block_weight(&api))?;
Ok(Self { api, rpc_client, rpc, cache, chain_id, max_block_weight, native_to_evm_ratio })
}
/// Convert a native balance to an EVM balance.
fn native_to_evm_decimals(&self, value: U256) -> U256 {
value.saturating_mul(self.native_to_evm_ratio)
}
/// Convert an evm balance to a native balance.
fn evm_to_native_decimals(&self, value: U256) -> U256 {
value / self.native_to_evm_ratio
}
/// Get the receipt infos from the extrinsics in a block.
async fn receipt_infos(
&self,
block: &SubstrateBlock,
) -> Result<HashMap<H256, (TransactionSigned, ReceiptInfo)>, ClientError> {
// Get extrinsics from the block
let extrinsics = block.extrinsics().await?;
// Filter extrinsics from pallet_revive
let extrinsics = extrinsics.iter().flat_map(|ext| {
let ext = ext.ok()?;
let call = ext.as_extrinsic::<EthTransact>().ok()??;
let tx = rlp::decode::<TransactionLegacySigned>(&call.payload).ok()?;
let from = tx.recover_eth_address().ok()?;
let contract_address = if tx.transaction_legacy_unsigned.to.is_none() {
Some(create1(&from, tx.transaction_legacy_unsigned.nonce.try_into().ok()?))
} else {
None
};
Some((from, tx, contract_address, ext))
});
// Map each extrinsic to a receipt
stream::iter(extrinsics)
.map(|(from, tx, contract_address, ext)| async move {
let events = ext.events().await?;
let tx_fees =
events.find_first::<TransactionFeePaid>()?.ok_or(ClientError::TxFeeNotFound)?;
let gas_price = tx.transaction_legacy_unsigned.gas_price;
let gas_used = (tx_fees.tip.saturating_add(tx_fees.actual_fee))
.checked_div(gas_price.as_u128())
.unwrap_or_default();
let success = events.has::<ExtrinsicSuccess>()?;
let transaction_index = ext.index();
let transaction_hash = BlakeTwo256::hash(&Vec::from(ext.bytes()).encode());
let block_hash = block.hash();
let block_number = block.number().into();
let receipt = ReceiptInfo {
block_hash,
block_number,
contract_address,
from,
to: tx.transaction_legacy_unsigned.to,
effective_gas_price: gas_price,
gas_used: gas_used.into(),
status: Some(if success { U256::one() } else { U256::zero() }),
transaction_hash,
transaction_index: transaction_index.into(),
..Default::default()
};
Ok::<_, ClientError>((receipt.transaction_hash, (tx.into(), receipt)))
})
.buffer_unordered(10)
.collect::<Vec<Result<_, _>>>()
.await
.into_iter()
.collect::<Result<HashMap<_, _>, _>>()
}
}
/// Fetch the chain ID from the substrate chain.
async fn chain_id(api: &OnlineClient<SrcChainConfig>) -> Result<u64, ClientError> {
let query = subxt_client::constants().revive().chain_id();
api.constants().at(&query).map_err(|err| err.into())
}
/// Fetch the max block weight from the substrate chain.
async fn max_block_weight(api: &OnlineClient<SrcChainConfig>) -> Result<Weight, ClientError> {
let query = subxt_client::constants().system().block_weights();
let weights = api.constants().at(&query)?;
let max_block = weights.per_class.normal.max_extrinsic.unwrap_or(weights.max_block);
Ok(max_block.0)
}
/// Fetch the native to EVM ratio from the substrate chain.
async fn native_to_evm_ratio(api: &OnlineClient<SrcChainConfig>) -> Result<U256, ClientError> {
let query = subxt_client::constants().revive().native_to_eth_ratio();
let ratio = api.constants().at(&query)?;
Ok(U256::from(ratio))
}
/// Extract the block timestamp.
async fn extract_block_timestamp(block: &SubstrateBlock) -> Option<u64> {
let extrinsics = block.extrinsics().await.ok()?;
let ext = extrinsics
.find_first::<crate::subxt_client::timestamp::calls::types::Set>()
.ok()??;
Some(ext.value.now / 1000)
}
impl Client {
/// Create a new client instance.
/// The client will subscribe to new blocks and maintain a cache of [`CACHE_SIZE`] blocks.
pub async fn from_url(
url: &str,
spawn_handle: &sc_service::SpawnEssentialTaskHandle,
) -> Result<Self, ClientError> {
log::info!(target: LOG_TARGET, "Connecting to node at: {url} ...");
let inner: Arc<ClientInner> = Arc::new(ClientInner::from_url(url).await?);
log::info!(target: LOG_TARGET, "Connected to node at: {url}");
let (tx, mut updates) = tokio::sync::watch::channel(());
spawn_handle.spawn("subscribe-blocks", None, Self::subscribe_blocks(inner.clone(), tx));
spawn_handle.spawn("subscribe-reconnect", None, Self::subscribe_reconnect(inner.clone()));
updates.changed().await.expect("tx is not dropped");
Ok(Self { inner, updates })
}
/// Expose the storage API.
async fn storage_api(
&self,
at: &BlockNumberOrTagOrHash,
) -> Result<Storage<SrcChainConfig, OnlineClient<SrcChainConfig>>, ClientError> {
match at {
BlockNumberOrTagOrHash::U256(block_number) => {
let n: SubstrateBlockNumber =
(*block_number).try_into().map_err(|_| ClientError::ConversionFailed)?;
let hash = self.get_block_hash(n).await?.ok_or(ClientError::BlockNotFound)?;
Ok(self.inner.api.storage().at(hash))
},
BlockNumberOrTagOrHash::H256(hash) => Ok(self.inner.api.storage().at(*hash)),
BlockNumberOrTagOrHash::BlockTag(_) => {
if let Some(block) = self.latest_block().await {
return Ok(self.inner.api.storage().at(block.hash()));
}
let storage = self.inner.api.storage().at_latest().await?;
Ok(storage)
},
}
}
/// Expose the runtime API.
async fn runtime_api(
&self,
at: &BlockNumberOrTagOrHash,
) -> Result<
subxt::runtime_api::RuntimeApi<SrcChainConfig, OnlineClient<SrcChainConfig>>,
ClientError,
> {
match at {
BlockNumberOrTagOrHash::U256(block_number) => {
let n: SubstrateBlockNumber =
(*block_number).try_into().map_err(|_| ClientError::ConversionFailed)?;
let hash = self.get_block_hash(n).await?.ok_or(ClientError::BlockNotFound)?;
Ok(self.inner.api.runtime_api().at(hash))
},
BlockNumberOrTagOrHash::H256(hash) => Ok(self.inner.api.runtime_api().at(*hash)),
BlockNumberOrTagOrHash::BlockTag(_) => {
if let Some(block) = self.latest_block().await {
return Ok(self.inner.api.runtime_api().at(block.hash()));
}
let api = self.inner.api.runtime_api().at_latest().await?;
Ok(api)
},
}
}
/// Subscribe and log reconnection events.
async fn subscribe_reconnect(inner: Arc<ClientInner>) {
let rpc = inner.as_ref().rpc_client.clone();
loop {
let reconnected = rpc.reconnect_initiated().await;
log::info!(target: LOG_TARGET, "RPC client connection lost");
let now = std::time::Instant::now();
reconnected.await;
log::info!(target: LOG_TARGET, "RPC client reconnection took `{}s`", now.elapsed().as_secs());
}
}
/// Subscribe to new blocks and update the cache.
async fn subscribe_blocks(inner: Arc<ClientInner>, tx: Sender<()>) {
log::info!(target: LOG_TARGET, "Subscribing to new blocks");
let mut block_stream = match inner.as_ref().api.blocks().subscribe_best().await {
Ok(s) => s,
Err(err) => {
log::error!(target: LOG_TARGET, "Failed to subscribe to blocks: {err:?}");
return
},
};
while let Some(block) = block_stream.next().await {
let block = match block {
Ok(block) => block,
Err(err) => {
if err.is_disconnected_will_reconnect() {
log::warn!(
target: LOG_TARGET,
"The RPC connection was lost and we may have missed a few blocks"
);
continue;
}
log::error!(target: LOG_TARGET, "Failed to fetch block: {err:?}");
return
},
};
log::trace!(target: LOG_TARGET, "Pushing block: {}", block.number());
let mut cache = inner.cache.write().await;
let receipts = inner
.receipt_infos(&block)
.await
.inspect_err(|err| {
log::error!(target: LOG_TARGET, "Failed to get receipts: {err:?}");
})
.unwrap_or_default();
if !receipts.is_empty() {
log::debug!(target: LOG_TARGET, "Adding {} receipts", receipts.len());
let values = receipts
.iter()
.map(|(hash, (_, receipt))| (receipt.transaction_index, *hash))
.collect::<HashMap<_, _>>();
cache.tx_hashes_by_block_and_index.insert(block.hash(), values);
cache
.receipts_by_hash
.extend(receipts.iter().map(|(hash, (_, receipt))| (*hash, receipt.clone())));
cache.signed_tx_by_hash.extend(
receipts.iter().map(|(hash, (signed_tx, _))| (*hash, signed_tx.clone())),
)
}
cache.insert(block);
tx.send_replace(());
}
log::info!(target: LOG_TARGET, "Block subscription ended");
}
}
impl Client {
/// Get the most recent block stored in the cache.
pub async fn latest_block(&self) -> Option<Arc<SubstrateBlock>> {
let cache = self.inner.cache.read().await;
let block = cache.latest_block()?;
Some(block.clone())
}
/// Expose the transaction API.
pub async fn submit(
&self,
call: subxt::tx::DefaultPayload<EthTransact>,
) -> Result<H256, ClientError> {
let ext = self.inner.api.tx().create_unsigned(&call).map_err(ClientError::from)?;
let hash = ext.submit().await?;
Ok(hash)
}
/// Get an EVM transaction receipt by hash.
pub async fn receipt(&self, tx_hash: &H256) -> Option<ReceiptInfo> {
let cache = self.inner.cache.read().await;
cache.receipts_by_hash.get(tx_hash).cloned()
}
/// Get the syncing status of the chain.
pub async fn syncing(&self) -> Result<SyncingStatus, ClientError> {
let health = self.inner.rpc.system_health().await?;
let status = if health.is_syncing {
let client = RpcClient::new(self.inner.rpc_client.clone());
let sync_state: sc_rpc::system::SyncState<SubstrateBlockNumber> =
client.request("system_syncState", Default::default()).await?;
SyncingProgress {
current_block: Some(sync_state.current_block.into()),
highest_block: Some(sync_state.highest_block.into()),
starting_block: Some(sync_state.starting_block.into()),
}
.into()
} else {
SyncingStatus::Bool(false)
};
Ok(status)
}
/// Get an EVM transaction receipt by hash.
pub async fn receipt_by_hash_and_index(
&self,
block_hash: &H256,
transaction_index: &U256,
) -> Option<ReceiptInfo> {
let cache = self.inner.cache.read().await;
let receipt_hash =
cache.tx_hashes_by_block_and_index.get(block_hash)?.get(transaction_index)?;
let receipt = cache.receipts_by_hash.get(receipt_hash)?;
Some(receipt.clone())
}
pub async fn signed_tx_by_hash(&self, tx_hash: &H256) -> Option<TransactionSigned> {
let cache = self.inner.cache.read().await;
cache.signed_tx_by_hash.get(tx_hash).cloned()
}
/// Get receipts count per block.
pub async fn receipts_count_per_block(&self, block_hash: &SubstrateBlockHash) -> Option<usize> {
let cache = self.inner.cache.read().await;
cache.tx_hashes_by_block_and_index.get(block_hash).map(|v| v.len())
}
/// Get the system health.
pub async fn system_health(&self) -> Result<SystemHealth, ClientError> {
let health = self.inner.rpc.system_health().await?;
Ok(health)
}
/// Get the balance of the given address.
pub async fn balance(
&self,
address: H160,
at: &BlockNumberOrTagOrHash,
) -> Result<U256, ClientError> {
// TODO: remove once subxt is updated
let address = address.0.into();
let runtime_api = self.runtime_api(at).await?;
let payload = subxt_client::apis().revive_api().balance(address);
let balance = runtime_api.call(payload).await?.into();
Ok(self.inner.native_to_evm_decimals(balance))
}
/// Get the contract storage for the given contract address and key.
pub async fn get_contract_storage(
&self,
contract_address: H160,
key: U256,
block: BlockNumberOrTagOrHash,
) -> Result<Vec<u8>, ClientError> {
let runtime_api = self.runtime_api(&block).await?;
// TODO: remove once subxt is updated
let contract_address = contract_address.0.into();
let payload = subxt_client::apis()
.revive_api()
.get_storage(contract_address, key.to_big_endian());
let result = runtime_api.call(payload).await?.unwrap_or_default().unwrap_or_default();
Ok(result)
}
/// Get the contract code for the given contract address.
pub async fn get_contract_code(
&self,
contract_address: &H160,
block: BlockNumberOrTagOrHash,
) -> Result<Vec<u8>, ClientError> {
let storage_api = self.storage_api(&block).await?;
// TODO: remove once subxt is updated
let contract_address: subxt::utils::H160 = contract_address.0.into();
let query = subxt_client::storage().revive().contract_info_of(contract_address);
let Some(ContractInfo { code_hash, .. }) = storage_api.fetch(&query).await? else {
return Ok(Vec::new());
};
let query = subxt_client::storage().revive().pristine_code(code_hash);
let result = storage_api.fetch(&query).await?.map(|v| v.0).unwrap_or_default();
Ok(result)
}
/// Dry run a transaction and returns the [`EthContractResult`] for the transaction.
pub async fn dry_run(
&self,
tx: &GenericTransaction,
block: BlockNumberOrTagOrHash,
) -> Result<EthContractResult<Balance>, ClientError> {
let runtime_api = self.runtime_api(&block).await?;
let value = self
.inner
.evm_to_native_decimals(tx.value.unwrap_or_default())
.try_into()
.map_err(|_| ClientError::ConversionFailed)?;
// TODO: remove once subxt is updated
let from = tx.from.map(|v| v.0.into());
let to = tx.to.map(|v| v.0.into());
let payload = subxt_client::apis().revive_api().eth_transact(
from.unwrap_or_default(),
to,
value,
tx.input.clone().unwrap_or_default().0,
None,
None,
);
let res = runtime_api.call(payload).await?.0;
Ok(res)
}
/// Dry run a transaction and returns the gas estimate for the transaction.
pub async fn estimate_gas(
&self,
tx: &GenericTransaction,
block: BlockNumberOrTagOrHash,
) -> Result<U256, ClientError> {
let dry_run = self.dry_run(tx, block).await?;
Ok(U256::from(dry_run.fee / GAS_PRICE as u128) + GAS_PRICE)
}
/// Get the nonce of the given address.
pub async fn nonce(
&self,
address: H160,
at: BlockNumberOrTagOrHash,
) -> Result<U256, ClientError> {
let address = address.0.into();
let runtime_api = self.runtime_api(&at).await?;
let payload = subxt_client::apis().revive_api().nonce(address);
let nonce = runtime_api.call(payload).await?;
Ok(nonce.into())
}
/// Get the block number of the latest block.
pub async fn block_number(&self) -> Result<SubstrateBlockNumber, ClientError> {
let cache = self.inner.cache.read().await;
let latest_block = cache.buffer.back().ok_or(ClientError::CacheEmpty)?;
Ok(latest_block.number())
}
/// Get a block hash for the given block number.
pub async fn get_block_hash(
&self,
block_number: SubstrateBlockNumber,
) -> Result<Option<SubstrateBlockHash>, ClientError> {
let cache = self.inner.cache.read().await;
if let Some(block) = cache.blocks_by_number.get(&block_number) {
return Ok(Some(block.hash()));
}
let hash = self.inner.rpc.chain_get_block_hash(Some(block_number.into())).await?;
Ok(hash)
}
/// Get a block for the specified hash or number.
pub async fn block_by_number_or_tag(
&self,
block: &BlockNumberOrTag,
) -> Result<Option<Arc<SubstrateBlock>>, ClientError> {
match block {
BlockNumberOrTag::U256(n) => {
let n = (*n).try_into().map_err(|_| ClientError::ConversionFailed)?;
self.block_by_number(n).await
},
BlockNumberOrTag::BlockTag(_) => {
let cache = self.inner.cache.read().await;
Ok(cache.buffer.back().cloned())
},
}
}
/// Get a block by hash
pub async fn block_by_hash(
&self,
hash: &SubstrateBlockHash,
) -> Result<Option<Arc<SubstrateBlock>>, ClientError> {
let cache = self.inner.cache.read().await;
if let Some(block) = cache.blocks_by_hash.get(hash) {
return Ok(Some(block.clone()));
}
match self.inner.api.blocks().at(*hash).await {
Ok(block) => Ok(Some(Arc::new(block))),
Err(subxt::Error::Block(subxt::error::BlockError::NotFound(_))) => Ok(None),
Err(err) => Err(err.into()),
}
}
/// Get a block by number
pub async fn block_by_number(
&self,
block_number: SubstrateBlockNumber,
) -> Result<Option<Arc<SubstrateBlock>>, ClientError> {
let cache = self.inner.cache.read().await;
if let Some(block) = cache.blocks_by_number.get(&block_number) {
return Ok(Some(block.clone()));
}
let Some(hash) = self.get_block_hash(block_number).await? else {
return Ok(None);
};
self.block_by_hash(&hash).await
}
/// Get the EVM block for the given hash.
pub async fn evm_block(&self, block: Arc<SubstrateBlock>) -> Result<Block, ClientError> {
let runtime_api = self.inner.api.runtime_api().at(block.hash());
let max_fee = Self::weight_to_fee(&runtime_api, self.max_block_weight()).await?;
let gas_limit = U256::from(max_fee / GAS_PRICE as u128);
let header = block.header();
let timestamp = extract_block_timestamp(&block).await.unwrap_or_default();
// TODO: remove once subxt is updated
let parent_hash = header.parent_hash.0.into();
let state_root = header.state_root.0.into();
let extrinsics_root = header.extrinsics_root.0.into();
Ok(Block {
hash: block.hash(),
parent_hash,
state_root,
transactions_root: extrinsics_root,
number: header.number.into(),
timestamp: timestamp.into(),
difficulty: Some(0u32.into()),
gas_limit,
logs_bloom: Bytes256([0u8; 256]),
receipts_root: extrinsics_root,
..Default::default()
})
}
/// Convert a weight to a fee.
async fn weight_to_fee(
runtime_api: &subxt::runtime_api::RuntimeApi<SrcChainConfig, OnlineClient<SrcChainConfig>>,
weight: Weight,
) -> Result<Balance, ClientError> {
let payload = subxt_client::apis()
.transaction_payment_api()
.query_weight_to_fee(weight.into());
let fee = runtime_api.call(payload).await?;
Ok(fee)
}
/// Get the chain ID.
pub fn chain_id(&self) -> u64 {
self.inner.chain_id
}
/// Get the Max Block Weight.
pub fn max_block_weight(&self) -> Weight {
self.inner.max_block_weight
}
}