@mainsail/evm 0.0.1-evm.52 → 0.0.1-evm.53
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +5922 -0
- package/Cargo.toml +34 -0
- package/bindings/Cargo.toml +33 -0
- package/bindings/build.rs +6 -0
- package/bindings/src/ctx.rs +667 -0
- package/bindings/src/lib.rs +2231 -0
- package/bindings/src/logger.rs +110 -0
- package/bindings/src/result.rs +542 -0
- package/bindings/src/utils.rs +71 -0
- package/core/Cargo.toml +36 -0
- package/core/src/account.rs +112 -0
- package/core/src/bytecode.rs +39 -0
- package/core/src/compression.rs +158 -0
- package/core/src/db.rs +3311 -0
- package/core/src/events.rs +9 -0
- package/core/src/historical.rs +544 -0
- package/core/src/legacy.rs +153 -0
- package/core/src/lib.rs +14 -0
- package/core/src/logger.rs +98 -0
- package/core/src/logs_bloom.rs +96 -0
- package/core/src/precompiles.rs +450 -0
- package/core/src/receipt.rs +153 -0
- package/core/src/state_changes.rs +122 -0
- package/core/src/state_commit.rs +615 -0
- package/core/src/state_root.rs +266 -0
- package/index.d.ts +1 -0
- package/index.js +52 -52
- package/package.json +5 -4
- package/scripts/postinstall.mjs +73 -0
|
@@ -0,0 +1,667 @@
|
|
|
1
|
+
use std::{path::PathBuf, str::FromStr};
|
|
2
|
+
|
|
3
|
+
use mainsail_evm_core::{
|
|
4
|
+
db::{BlockHeaderData, CommitData, CommitKey, ProofData, TransactionData},
|
|
5
|
+
legacy::LegacyAddress,
|
|
6
|
+
};
|
|
7
|
+
use napi::bindgen_prelude::{BigInt, Buffer, Function};
|
|
8
|
+
use napi_derive::napi;
|
|
9
|
+
use revm::primitives::{Address, B256, Bytes, U256, alloy_primitives::Bloom, hardfork::SpecId};
|
|
10
|
+
|
|
11
|
+
use crate::{logger::JsLogMessage, utils};
|
|
12
|
+
|
|
13
|
+
#[napi(object)]
|
|
14
|
+
pub struct JsEvmOptions {
|
|
15
|
+
pub path: String,
|
|
16
|
+
pub logger: Option<Function<'static, JsLogMessage, ()>>,
|
|
17
|
+
pub history_size: Option<BigInt>,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
#[napi(object)]
|
|
21
|
+
pub struct JsTransactionContext {
|
|
22
|
+
pub from: String,
|
|
23
|
+
pub legacy_address: Option<String>,
|
|
24
|
+
/// Omit recipient when deploying a contract
|
|
25
|
+
pub to: Option<String>,
|
|
26
|
+
pub gas_limit: BigInt,
|
|
27
|
+
pub gas_price: BigInt,
|
|
28
|
+
pub value: BigInt,
|
|
29
|
+
pub nonce: BigInt,
|
|
30
|
+
pub data: Buffer,
|
|
31
|
+
pub tx_hash: String,
|
|
32
|
+
pub index: Option<u32>,
|
|
33
|
+
pub block_context: JsBlockContext,
|
|
34
|
+
pub spec_id: String,
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
#[napi(object)]
|
|
38
|
+
pub struct JsTransactionSimulateContext {
|
|
39
|
+
pub from: String,
|
|
40
|
+
/// Omit recipient when deploying a contract
|
|
41
|
+
pub to: Option<String>,
|
|
42
|
+
pub gas_limit: BigInt,
|
|
43
|
+
pub gas_price: BigInt,
|
|
44
|
+
pub value: BigInt,
|
|
45
|
+
pub nonce: BigInt,
|
|
46
|
+
pub data: Buffer,
|
|
47
|
+
pub block_context: JsBlockContext,
|
|
48
|
+
pub spec_id: String,
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
#[napi(object)]
|
|
52
|
+
pub struct JsPreverifyTransactionContext {
|
|
53
|
+
pub from: String,
|
|
54
|
+
pub legacy_address: Option<String>,
|
|
55
|
+
/// Omit recipient when deploying a contract
|
|
56
|
+
pub to: Option<String>,
|
|
57
|
+
pub gas_limit: BigInt,
|
|
58
|
+
pub gas_price: BigInt,
|
|
59
|
+
pub value: BigInt,
|
|
60
|
+
pub nonce: BigInt,
|
|
61
|
+
pub data: Buffer,
|
|
62
|
+
pub tx_hash: String,
|
|
63
|
+
pub spec_id: String,
|
|
64
|
+
pub block_gas_limit: BigInt,
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
#[napi(object)]
|
|
68
|
+
pub struct JsTransactionViewContext {
|
|
69
|
+
pub from: String,
|
|
70
|
+
pub to: String,
|
|
71
|
+
pub data: Buffer,
|
|
72
|
+
pub spec_id: String,
|
|
73
|
+
pub gas_limit: Option<BigInt>,
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
#[napi(object)]
|
|
77
|
+
pub struct JsBlockContext {
|
|
78
|
+
pub commit_key: JsCommitKey,
|
|
79
|
+
pub gas_limit: BigInt,
|
|
80
|
+
pub timestamp: BigInt,
|
|
81
|
+
pub validator_address: String,
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
#[napi(object)]
|
|
85
|
+
pub struct JsGenesisContext {
|
|
86
|
+
pub account: String,
|
|
87
|
+
pub deployer_account: String,
|
|
88
|
+
pub validator_contract: String,
|
|
89
|
+
pub username_contract: String,
|
|
90
|
+
pub initial_block_number: BigInt,
|
|
91
|
+
pub initial_supply: BigInt,
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
#[napi(object)]
|
|
95
|
+
pub struct JsCalculateRoundValidatorsContext {
|
|
96
|
+
pub commit_key: JsCommitKey,
|
|
97
|
+
pub timestamp: BigInt,
|
|
98
|
+
pub round_validators: BigInt,
|
|
99
|
+
pub validator_address: String,
|
|
100
|
+
pub spec_id: String,
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
#[napi(object)]
|
|
104
|
+
pub struct JsUpdateRewardsAndVotesContext {
|
|
105
|
+
pub commit_key: JsCommitKey,
|
|
106
|
+
pub timestamp: BigInt,
|
|
107
|
+
pub block_reward: BigInt,
|
|
108
|
+
pub validator_address: String,
|
|
109
|
+
pub spec_id: String,
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
#[napi(object)]
|
|
113
|
+
pub struct JsProofData {
|
|
114
|
+
pub round: u32,
|
|
115
|
+
pub signature: String,
|
|
116
|
+
pub validator_set: BigInt,
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
#[napi(object)]
|
|
120
|
+
pub struct JsBlockHeaderData {
|
|
121
|
+
pub version: u8,
|
|
122
|
+
pub timestamp: BigInt,
|
|
123
|
+
pub number: u32,
|
|
124
|
+
pub round: u32,
|
|
125
|
+
pub hash: String,
|
|
126
|
+
pub parent_hash: String,
|
|
127
|
+
pub state_root: String,
|
|
128
|
+
pub logs_bloom: String,
|
|
129
|
+
pub transactions_root: String,
|
|
130
|
+
pub transactions_count: u16,
|
|
131
|
+
pub gas_used: u32,
|
|
132
|
+
pub fee: BigInt,
|
|
133
|
+
pub reward: BigInt,
|
|
134
|
+
pub payload_size: u32,
|
|
135
|
+
pub proposer: String,
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
#[napi(object)]
|
|
139
|
+
pub struct JsTransactionData {
|
|
140
|
+
pub from: String,
|
|
141
|
+
pub sender_public_key: String,
|
|
142
|
+
pub legacy_address: Option<String>,
|
|
143
|
+
pub to: Option<String>,
|
|
144
|
+
pub gas_limit: BigInt,
|
|
145
|
+
pub gas_price: BigInt,
|
|
146
|
+
pub value: BigInt,
|
|
147
|
+
pub nonce: BigInt,
|
|
148
|
+
pub data: Buffer,
|
|
149
|
+
pub v: u32,
|
|
150
|
+
pub r: String,
|
|
151
|
+
pub s: String,
|
|
152
|
+
pub legacy_second_signature: Option<String>,
|
|
153
|
+
pub tx_hash: String,
|
|
154
|
+
pub block_number: u32,
|
|
155
|
+
pub index: u32,
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
#[napi(object)]
|
|
159
|
+
pub struct JsCommitKey {
|
|
160
|
+
pub block_number: BigInt,
|
|
161
|
+
pub round: BigInt,
|
|
162
|
+
pub block_hash: Option<String>,
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
#[napi(object)]
|
|
166
|
+
pub struct JsCommitData {
|
|
167
|
+
pub proof: JsProofData,
|
|
168
|
+
pub header: JsBlockHeaderData,
|
|
169
|
+
pub transactions: Vec<JsTransactionData>,
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
#[napi(object)]
|
|
173
|
+
pub struct JsPrepareNextCommitContext {
|
|
174
|
+
pub commit_key: JsCommitKey,
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
#[derive(Debug)]
|
|
178
|
+
pub struct PrepareNextCommitContext {
|
|
179
|
+
pub commit_key: CommitKey,
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
#[derive(Debug)]
|
|
183
|
+
pub struct PreverifyTxContext {
|
|
184
|
+
pub from: Address,
|
|
185
|
+
pub legacy_address: Option<LegacyAddress>,
|
|
186
|
+
/// Omit recipient when deploying a contract
|
|
187
|
+
pub to: Option<Address>,
|
|
188
|
+
pub gas_limit: u64,
|
|
189
|
+
pub gas_price: u128,
|
|
190
|
+
pub value: U256,
|
|
191
|
+
pub nonce: u64,
|
|
192
|
+
pub data: Bytes,
|
|
193
|
+
pub tx_hash: B256,
|
|
194
|
+
pub spec_id: SpecId,
|
|
195
|
+
pub block_gas_limit: u64,
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
#[derive(Debug)]
|
|
199
|
+
pub struct TxContext {
|
|
200
|
+
pub from: Address,
|
|
201
|
+
pub legacy_address: Option<LegacyAddress>,
|
|
202
|
+
/// Omit recipient when deploying a contract
|
|
203
|
+
pub to: Option<Address>,
|
|
204
|
+
pub gas_limit: u64,
|
|
205
|
+
pub gas_price: u128,
|
|
206
|
+
pub value: U256,
|
|
207
|
+
pub nonce: u64,
|
|
208
|
+
pub data: Bytes,
|
|
209
|
+
pub tx_hash: B256,
|
|
210
|
+
pub block_context: BlockContext,
|
|
211
|
+
pub spec_id: SpecId,
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
#[derive(Debug)]
|
|
215
|
+
pub struct TxViewContext {
|
|
216
|
+
pub from: Address,
|
|
217
|
+
pub to: Address,
|
|
218
|
+
pub data: Bytes,
|
|
219
|
+
pub spec_id: SpecId,
|
|
220
|
+
pub gas_limit: Option<u64>,
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
#[derive(Debug)]
|
|
224
|
+
pub struct TxSimulateContext {
|
|
225
|
+
pub from: Address,
|
|
226
|
+
pub to: Option<Address>,
|
|
227
|
+
pub gas_limit: u64,
|
|
228
|
+
pub gas_price: u128,
|
|
229
|
+
pub value: U256,
|
|
230
|
+
pub nonce: u64,
|
|
231
|
+
pub data: Bytes,
|
|
232
|
+
pub block_context: BlockContext,
|
|
233
|
+
pub spec_id: SpecId,
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
#[derive(Debug)]
|
|
237
|
+
pub struct BlockContext {
|
|
238
|
+
pub commit_key: CommitKey,
|
|
239
|
+
pub gas_limit: u64,
|
|
240
|
+
pub timestamp: u64,
|
|
241
|
+
pub validator_address: Address,
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
#[derive(Debug)]
|
|
245
|
+
pub struct GenesisContext {
|
|
246
|
+
pub account: Address,
|
|
247
|
+
pub deployer_account: Address,
|
|
248
|
+
pub validator_contract: Address,
|
|
249
|
+
pub username_contract: Address,
|
|
250
|
+
pub initial_block_number: u64,
|
|
251
|
+
pub initial_supply: U256,
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
#[derive(Debug)]
|
|
255
|
+
pub struct CalculateRoundValidatorsContext {
|
|
256
|
+
pub commit_key: CommitKey,
|
|
257
|
+
pub timestamp: u64,
|
|
258
|
+
pub round_validators: u8,
|
|
259
|
+
pub validator_address: Address,
|
|
260
|
+
pub spec_id: SpecId,
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
#[derive(Debug)]
|
|
264
|
+
pub struct UpdateRewardsAndVotesContext {
|
|
265
|
+
pub commit_key: CommitKey,
|
|
266
|
+
pub timestamp: u64,
|
|
267
|
+
pub block_reward: u128,
|
|
268
|
+
pub validator_address: Address,
|
|
269
|
+
pub spec_id: SpecId,
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
pub struct EvmOptions {
|
|
273
|
+
pub path: PathBuf,
|
|
274
|
+
pub logger_callback: Option<Function<'static, JsLogMessage, ()>>,
|
|
275
|
+
pub history_size: Option<u64>,
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
#[derive(Debug)]
|
|
279
|
+
pub struct ExecutionContext {
|
|
280
|
+
pub from: Address,
|
|
281
|
+
pub to: Option<Address>,
|
|
282
|
+
pub gas_limit: Option<u64>,
|
|
283
|
+
pub gas_price: u128,
|
|
284
|
+
pub value: U256,
|
|
285
|
+
pub nonce: Option<u64>,
|
|
286
|
+
pub data: Bytes,
|
|
287
|
+
pub tx_hash: Option<B256>,
|
|
288
|
+
pub block_context: Option<BlockContext>,
|
|
289
|
+
pub spec_id: SpecId,
|
|
290
|
+
pub stateful: bool,
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
impl From<TxViewContext> for ExecutionContext {
|
|
294
|
+
fn from(value: TxViewContext) -> Self {
|
|
295
|
+
Self {
|
|
296
|
+
from: value.from,
|
|
297
|
+
to: Some(value.to),
|
|
298
|
+
gas_limit: value.gas_limit,
|
|
299
|
+
gas_price: 0,
|
|
300
|
+
value: U256::ZERO,
|
|
301
|
+
nonce: None,
|
|
302
|
+
data: value.data,
|
|
303
|
+
tx_hash: None,
|
|
304
|
+
block_context: None,
|
|
305
|
+
spec_id: value.spec_id,
|
|
306
|
+
stateful: false,
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
impl From<TxContext> for ExecutionContext {
|
|
312
|
+
fn from(value: TxContext) -> Self {
|
|
313
|
+
Self {
|
|
314
|
+
from: value.from,
|
|
315
|
+
to: value.to,
|
|
316
|
+
gas_limit: Some(value.gas_limit),
|
|
317
|
+
gas_price: value.gas_price,
|
|
318
|
+
value: value.value,
|
|
319
|
+
nonce: Some(value.nonce),
|
|
320
|
+
data: value.data,
|
|
321
|
+
tx_hash: Some(value.tx_hash),
|
|
322
|
+
block_context: Some(value.block_context),
|
|
323
|
+
spec_id: value.spec_id,
|
|
324
|
+
stateful: true,
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
impl From<TxSimulateContext> for ExecutionContext {
|
|
330
|
+
fn from(value: TxSimulateContext) -> Self {
|
|
331
|
+
Self {
|
|
332
|
+
from: value.from,
|
|
333
|
+
to: value.to,
|
|
334
|
+
gas_limit: Some(value.gas_limit),
|
|
335
|
+
gas_price: value.gas_price,
|
|
336
|
+
value: value.value,
|
|
337
|
+
nonce: Some(value.nonce),
|
|
338
|
+
data: value.data,
|
|
339
|
+
tx_hash: None,
|
|
340
|
+
block_context: Some(value.block_context),
|
|
341
|
+
spec_id: value.spec_id,
|
|
342
|
+
stateful: false,
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
impl TryFrom<JsCommitKey> for CommitKey {
|
|
348
|
+
type Error = anyhow::Error;
|
|
349
|
+
|
|
350
|
+
fn try_from(value: JsCommitKey) -> Result<Self, Self::Error> {
|
|
351
|
+
let block_hash = if let Some(block_hash) = value.block_hash {
|
|
352
|
+
utils::convert_string_to_b256(block_hash)?
|
|
353
|
+
} else {
|
|
354
|
+
B256::ZERO
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
Ok(CommitKey(
|
|
358
|
+
value.block_number.get_u64().1,
|
|
359
|
+
value.round.get_u64().1,
|
|
360
|
+
block_hash,
|
|
361
|
+
))
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
impl TryFrom<JsProofData> for ProofData {
|
|
366
|
+
type Error = anyhow::Error;
|
|
367
|
+
|
|
368
|
+
fn try_from(value: JsProofData) -> Result<Self, Self::Error> {
|
|
369
|
+
Ok(ProofData {
|
|
370
|
+
round: value.round,
|
|
371
|
+
signature: utils::convert_string_to_bls_sig(value.signature)?,
|
|
372
|
+
validator_set: value.validator_set.get_u128().1,
|
|
373
|
+
})
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
impl TryFrom<JsBlockHeaderData> for BlockHeaderData {
|
|
378
|
+
type Error = anyhow::Error;
|
|
379
|
+
|
|
380
|
+
fn try_from(value: JsBlockHeaderData) -> Result<Self, Self::Error> {
|
|
381
|
+
Ok(BlockHeaderData {
|
|
382
|
+
proposer: utils::create_address_from_string(&value.proposer)?,
|
|
383
|
+
version: value.version,
|
|
384
|
+
timestamp: value.timestamp.get_u64().1,
|
|
385
|
+
number: value.number,
|
|
386
|
+
round: value.round,
|
|
387
|
+
hash: utils::convert_string_to_b256(value.hash)?,
|
|
388
|
+
parent_hash: utils::convert_string_to_b256(value.parent_hash)?,
|
|
389
|
+
state_root: utils::convert_string_to_b256(value.state_root)?,
|
|
390
|
+
logs_bloom: Bloom::from_str(&value.logs_bloom)?,
|
|
391
|
+
transactions_root: utils::convert_string_to_b256(value.transactions_root)?,
|
|
392
|
+
transactions_count: value.transactions_count,
|
|
393
|
+
gas_used: value.gas_used,
|
|
394
|
+
fee: utils::convert_bigint_to_u256(value.fee)?,
|
|
395
|
+
reward: utils::convert_bigint_to_u256(value.reward)?,
|
|
396
|
+
payload_size: value.payload_size,
|
|
397
|
+
})
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
impl TryFrom<JsTransactionData> for TransactionData {
|
|
402
|
+
type Error = anyhow::Error;
|
|
403
|
+
|
|
404
|
+
fn try_from(value: JsTransactionData) -> Result<Self, Self::Error> {
|
|
405
|
+
let to = if let Some(to) = value.to {
|
|
406
|
+
Some(utils::create_address_from_string(&to)?)
|
|
407
|
+
} else {
|
|
408
|
+
None
|
|
409
|
+
};
|
|
410
|
+
|
|
411
|
+
let legacy_address = if let Some(legacy_address) = value.legacy_address {
|
|
412
|
+
Some(utils::create_legacy_address_from_string(&legacy_address)?)
|
|
413
|
+
} else {
|
|
414
|
+
None
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
Ok(TransactionData {
|
|
418
|
+
from: utils::create_address_from_string(&value.from)?,
|
|
419
|
+
sender_public_key: value.sender_public_key,
|
|
420
|
+
legacy_address,
|
|
421
|
+
to,
|
|
422
|
+
gas_limit: value.gas_limit.get_u64().1,
|
|
423
|
+
gas_price: value.gas_price.get_u128().1,
|
|
424
|
+
value: utils::convert_bigint_to_u256(value.value)?,
|
|
425
|
+
nonce: value.nonce.get_u64().1,
|
|
426
|
+
data: utils::convert_js_buffer_to_bytes(value.data),
|
|
427
|
+
tx_hash: utils::convert_string_to_b256(value.tx_hash)?,
|
|
428
|
+
block_number: value.block_number,
|
|
429
|
+
index: value.index,
|
|
430
|
+
legacy_second_signature: value.legacy_second_signature,
|
|
431
|
+
v: value.v,
|
|
432
|
+
r: utils::convert_hex_to_u256(&value.r),
|
|
433
|
+
s: utils::convert_hex_to_u256(&value.s),
|
|
434
|
+
})
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
impl TryFrom<JsCommitData> for CommitData {
|
|
439
|
+
type Error = anyhow::Error;
|
|
440
|
+
|
|
441
|
+
fn try_from(value: JsCommitData) -> Result<Self, Self::Error> {
|
|
442
|
+
let proof: ProofData = value.proof.try_into()?;
|
|
443
|
+
let header: BlockHeaderData = value.header.try_into()?;
|
|
444
|
+
|
|
445
|
+
let mut transactions = Vec::with_capacity(value.transactions.len());
|
|
446
|
+
for data in value.transactions {
|
|
447
|
+
transactions.push(data.try_into()?);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
assert_eq!(header.transactions_count, transactions.len() as u16);
|
|
451
|
+
|
|
452
|
+
Ok(CommitData {
|
|
453
|
+
proof,
|
|
454
|
+
header,
|
|
455
|
+
transactions,
|
|
456
|
+
})
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
impl TryFrom<JsPrepareNextCommitContext> for PrepareNextCommitContext {
|
|
461
|
+
type Error = anyhow::Error;
|
|
462
|
+
|
|
463
|
+
fn try_from(value: JsPrepareNextCommitContext) -> Result<Self, Self::Error> {
|
|
464
|
+
Ok(PrepareNextCommitContext {
|
|
465
|
+
commit_key: value.commit_key.try_into()?,
|
|
466
|
+
})
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
impl TryFrom<JsBlockContext> for BlockContext {
|
|
471
|
+
type Error = anyhow::Error;
|
|
472
|
+
|
|
473
|
+
fn try_from(value: JsBlockContext) -> Result<Self, Self::Error> {
|
|
474
|
+
Ok(BlockContext {
|
|
475
|
+
commit_key: value.commit_key.try_into()?,
|
|
476
|
+
gas_limit: value.gas_limit.get_u64().1,
|
|
477
|
+
timestamp: value.timestamp.get_u64().1,
|
|
478
|
+
validator_address: utils::create_address_from_string(&value.validator_address)?,
|
|
479
|
+
})
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
impl TryFrom<JsTransactionContext> for TxContext {
|
|
484
|
+
type Error = anyhow::Error;
|
|
485
|
+
|
|
486
|
+
fn try_from(value: JsTransactionContext) -> std::result::Result<Self, Self::Error> {
|
|
487
|
+
let to = if let Some(to) = value.to {
|
|
488
|
+
Some(utils::create_address_from_string(&to)?)
|
|
489
|
+
} else {
|
|
490
|
+
None
|
|
491
|
+
};
|
|
492
|
+
|
|
493
|
+
let legacy_address = if let Some(legacy_address) = value.legacy_address {
|
|
494
|
+
Some(utils::create_legacy_address_from_string(&legacy_address)?)
|
|
495
|
+
} else {
|
|
496
|
+
None
|
|
497
|
+
};
|
|
498
|
+
|
|
499
|
+
let tx_ctx = TxContext {
|
|
500
|
+
to,
|
|
501
|
+
gas_limit: value.gas_limit.get_u64().1,
|
|
502
|
+
gas_price: value.gas_price.get_u128().1,
|
|
503
|
+
from: utils::create_address_from_string(&value.from)?,
|
|
504
|
+
legacy_address,
|
|
505
|
+
value: utils::convert_bigint_to_u256(value.value)?,
|
|
506
|
+
nonce: value.nonce.get_u64().1,
|
|
507
|
+
data: utils::convert_js_buffer_to_bytes(value.data),
|
|
508
|
+
tx_hash: utils::convert_string_to_b256(value.tx_hash)?,
|
|
509
|
+
block_context: value.block_context.try_into()?,
|
|
510
|
+
spec_id: parse_spec_id(value.spec_id)?,
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
Ok(tx_ctx)
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
impl TryFrom<JsTransactionSimulateContext> for TxSimulateContext {
|
|
518
|
+
type Error = anyhow::Error;
|
|
519
|
+
|
|
520
|
+
fn try_from(value: JsTransactionSimulateContext) -> std::result::Result<Self, Self::Error> {
|
|
521
|
+
let to = if let Some(to) = value.to {
|
|
522
|
+
Some(utils::create_address_from_string(&to)?)
|
|
523
|
+
} else {
|
|
524
|
+
None
|
|
525
|
+
};
|
|
526
|
+
|
|
527
|
+
Ok(TxSimulateContext {
|
|
528
|
+
to,
|
|
529
|
+
gas_limit: value.gas_limit.get_u64().1,
|
|
530
|
+
gas_price: value.gas_price.get_u128().1,
|
|
531
|
+
from: utils::create_address_from_string(&value.from)?,
|
|
532
|
+
value: utils::convert_bigint_to_u256(value.value)?,
|
|
533
|
+
nonce: value.nonce.get_u64().1,
|
|
534
|
+
data: utils::convert_js_buffer_to_bytes(value.data),
|
|
535
|
+
block_context: value.block_context.try_into()?,
|
|
536
|
+
spec_id: parse_spec_id(value.spec_id)?,
|
|
537
|
+
})
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
impl TryFrom<JsPreverifyTransactionContext> for PreverifyTxContext {
|
|
542
|
+
type Error = anyhow::Error;
|
|
543
|
+
|
|
544
|
+
fn try_from(value: JsPreverifyTransactionContext) -> std::result::Result<Self, Self::Error> {
|
|
545
|
+
let to = if let Some(to) = value.to {
|
|
546
|
+
Some(utils::create_address_from_string(&to)?)
|
|
547
|
+
} else {
|
|
548
|
+
None
|
|
549
|
+
};
|
|
550
|
+
|
|
551
|
+
let legacy_address = if let Some(legacy_address) = value.legacy_address {
|
|
552
|
+
Some(utils::create_legacy_address_from_string(&legacy_address)?)
|
|
553
|
+
} else {
|
|
554
|
+
None
|
|
555
|
+
};
|
|
556
|
+
|
|
557
|
+
let tx_ctx = PreverifyTxContext {
|
|
558
|
+
to,
|
|
559
|
+
gas_limit: value.gas_limit.get_u64().1,
|
|
560
|
+
gas_price: value.gas_price.get_u128().1,
|
|
561
|
+
from: utils::create_address_from_string(&value.from)?,
|
|
562
|
+
legacy_address,
|
|
563
|
+
value: utils::convert_bigint_to_u256(value.value)?,
|
|
564
|
+
nonce: value.nonce.get_u64().1,
|
|
565
|
+
data: utils::convert_js_buffer_to_bytes(value.data),
|
|
566
|
+
tx_hash: utils::convert_string_to_b256(value.tx_hash)?,
|
|
567
|
+
block_gas_limit: value.block_gas_limit.get_u64().1,
|
|
568
|
+
spec_id: parse_spec_id(value.spec_id)?,
|
|
569
|
+
};
|
|
570
|
+
|
|
571
|
+
Ok(tx_ctx)
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
impl TryFrom<JsTransactionViewContext> for TxViewContext {
|
|
576
|
+
type Error = anyhow::Error;
|
|
577
|
+
|
|
578
|
+
fn try_from(value: JsTransactionViewContext) -> std::result::Result<Self, Self::Error> {
|
|
579
|
+
let gas_limit = value.gas_limit.map(|gas_limit| gas_limit.get_u64().1);
|
|
580
|
+
|
|
581
|
+
let tx_ctx = TxViewContext {
|
|
582
|
+
from: utils::create_address_from_string(&value.from)?,
|
|
583
|
+
to: utils::create_address_from_string(&value.to)?,
|
|
584
|
+
data: utils::convert_js_buffer_to_bytes(value.data),
|
|
585
|
+
spec_id: parse_spec_id(value.spec_id)?,
|
|
586
|
+
gas_limit,
|
|
587
|
+
};
|
|
588
|
+
|
|
589
|
+
Ok(tx_ctx)
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
impl TryFrom<JsGenesisContext> for GenesisContext {
|
|
594
|
+
type Error = anyhow::Error;
|
|
595
|
+
|
|
596
|
+
fn try_from(value: JsGenesisContext) -> Result<Self, Self::Error> {
|
|
597
|
+
Ok(GenesisContext {
|
|
598
|
+
account: utils::create_address_from_string(&value.account)?,
|
|
599
|
+
validator_contract: utils::create_address_from_string(&value.validator_contract)?,
|
|
600
|
+
username_contract: utils::create_address_from_string(&value.username_contract)?,
|
|
601
|
+
deployer_account: utils::create_address_from_string(&value.deployer_account)?,
|
|
602
|
+
initial_block_number: value.initial_block_number.get_u64().1,
|
|
603
|
+
initial_supply: utils::convert_bigint_to_u256(value.initial_supply)?,
|
|
604
|
+
})
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
impl TryFrom<JsEvmOptions> for EvmOptions {
|
|
609
|
+
type Error = anyhow::Error;
|
|
610
|
+
|
|
611
|
+
fn try_from(value: JsEvmOptions) -> Result<Self, Self::Error> {
|
|
612
|
+
let history_size = value
|
|
613
|
+
.history_size
|
|
614
|
+
.map(|history_size| history_size.get_u64().1);
|
|
615
|
+
|
|
616
|
+
Ok(EvmOptions {
|
|
617
|
+
path: value.path.into(),
|
|
618
|
+
logger_callback: value.logger,
|
|
619
|
+
history_size,
|
|
620
|
+
})
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
impl TryFrom<JsCalculateRoundValidatorsContext> for CalculateRoundValidatorsContext {
|
|
625
|
+
type Error = anyhow::Error;
|
|
626
|
+
|
|
627
|
+
fn try_from(value: JsCalculateRoundValidatorsContext) -> Result<Self, Self::Error> {
|
|
628
|
+
Ok(CalculateRoundValidatorsContext {
|
|
629
|
+
commit_key: value.commit_key.try_into()?,
|
|
630
|
+
timestamp: value.timestamp.get_u64().1,
|
|
631
|
+
validator_address: utils::create_address_from_string(&value.validator_address)?,
|
|
632
|
+
round_validators: u8::try_from(value.round_validators.get_u64().1)?,
|
|
633
|
+
spec_id: parse_spec_id(value.spec_id)?,
|
|
634
|
+
})
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
impl TryFrom<JsUpdateRewardsAndVotesContext> for UpdateRewardsAndVotesContext {
|
|
639
|
+
type Error = anyhow::Error;
|
|
640
|
+
|
|
641
|
+
fn try_from(value: JsUpdateRewardsAndVotesContext) -> Result<Self, Self::Error> {
|
|
642
|
+
Ok(UpdateRewardsAndVotesContext {
|
|
643
|
+
commit_key: value.commit_key.try_into()?,
|
|
644
|
+
timestamp: value.timestamp.get_u64().1,
|
|
645
|
+
validator_address: utils::create_address_from_string(&value.validator_address)?,
|
|
646
|
+
block_reward: value.block_reward.get_u128().1,
|
|
647
|
+
spec_id: parse_spec_id(value.spec_id)?,
|
|
648
|
+
})
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
fn parse_spec_id(spec_id: String) -> Result<SpecId, anyhow::Error> {
|
|
653
|
+
// By default "Latest" also includes unreleased specs, hence pin it to a specific spec which we
|
|
654
|
+
// can change manually as needed.
|
|
655
|
+
if spec_id == "Latest" {
|
|
656
|
+
return Ok(SpecId::SHANGHAI);
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
// Any supported spec is listed in the first match arm
|
|
660
|
+
match SpecId::from_str(spec_id.as_str()) {
|
|
661
|
+
Ok(spec_id) => match spec_id {
|
|
662
|
+
SpecId::SHANGHAI => Ok(spec_id),
|
|
663
|
+
_ => Err(anyhow::anyhow!("unsupported spec_id")),
|
|
664
|
+
},
|
|
665
|
+
_ => Err(anyhow::anyhow!("invalid spec_id")),
|
|
666
|
+
}
|
|
667
|
+
}
|