ethexe-common 2.0.0

Shared types and primitives for the ethexe execution layer
Documentation
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
// Copyright (C) Gear Technologies Inc.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

//! This is supposed to be an exact copy of Gear.sol library.

use crate::{Address, Digest, ToDigest, ValidatorsVec};
use alloc::vec::Vec;
use alloy_primitives::U256 as AlloyU256;
use gear_core::message::{ReplyCode, ReplyDetails, StoredMessage, SuccessReplyReason};
use gprimitives::{ActorId, CodeId, H256, MessageId, U256};
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
use sha3::Digest as _;

// TODO: support query from router.
pub const COMPUTATION_THRESHOLD: u64 = 2_500_000_000;
pub const WVARA_PER_SECOND: u128 = 10_000_000_000_000;

/// Gas limit for chunk processing.
pub const CHUNK_PROCESSING_GAS_LIMIT: u64 = 1_000_000_000_000;
/// Gas charge threshold for panicked injected messages.
pub const INJECTED_MESSAGE_PANIC_GAS_CHARGE_THRESHOLD: u64 = 1_000_000_000;

/// Max block gas limit for the node.
pub const MAX_BLOCK_GAS_LIMIT: u64 = 9_000_000_000_000;

/// [`CANONICAL_QUARANTINE`] defines the period of blocks to wait before applying canonical events.
pub const CANONICAL_QUARANTINE: u8 = 16;

#[derive(Clone, Debug, Default, Encode, Decode, PartialEq, Eq)]
pub struct AggregatedPublicKey {
    pub x: U256,
    pub y: U256,
}

#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq)]
#[repr(u8)]
pub enum SignatureType {
    FROST,
    ECDSA,
}

#[derive(Clone, Debug, Default, Encode, Decode, PartialEq, Eq)]
pub struct AddressBook {
    pub mirror: ActorId,
    pub mirror_proxy: ActorId,
    pub wrapped_vara: ActorId,
}

/// Squashed chain commitment with state transitions, MB head, and the latest
/// advanced Ethereum block hash, zero if no ethereum block has been advanced.
#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq)]
pub struct ChainCommitment {
    pub transitions: Vec<StateTransition>,
    pub head: H256,
    pub last_advanced_eth_block: H256,
}

impl ToDigest for ChainCommitment {
    fn update_hasher(&self, hasher: &mut sha3::Keccak256) {
        let ChainCommitment {
            transitions,
            head,
            last_advanced_eth_block,
        } = self;

        hasher.update(transitions.to_digest());
        hasher.update(head.0);
        hasher.update(last_advanced_eth_block.0);
    }
}

#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq)]
pub struct CodeCommitment {
    pub id: CodeId,
    pub valid: bool,
}

impl ToDigest for CodeCommitment {
    fn update_hasher(&self, hasher: &mut sha3::Keccak256) {
        // To avoid missing incorrect hashing while developing.
        let Self { id, valid } = self;

        hasher.update(id.into_bytes());
        hasher.update([*valid as u8]);
    }
}

#[derive(Clone, Debug, Default, Encode, Decode, PartialEq, Eq)]
pub struct OperatorRewardsCommitment {
    pub amount: U256,
    pub root: H256,
}

impl ToDigest for OperatorRewardsCommitment {
    fn update_hasher(&self, hasher: &mut sha3::Keccak256) {
        let OperatorRewardsCommitment { amount, root } = self;

        hasher.update(<[u8; 32]>::from(*amount));
        hasher.update(root);
    }
}

#[derive(Clone, Debug, Default, Encode, Decode, PartialEq, Eq)]
pub struct StakerRewards {
    pub vault: Address,
    pub amount: U256,
}

#[derive(Clone, Debug, Default, Encode, Decode, PartialEq, Eq)]
pub struct StakerRewardsCommitment {
    pub distribution: Vec<StakerRewards>,
    pub total_amount: U256,
    pub token: Address,
}

impl ToDigest for StakerRewardsCommitment {
    fn update_hasher(&self, hasher: &mut sha3::Keccak256) {
        let StakerRewardsCommitment {
            distribution,
            total_amount,
            token,
        } = &self;

        distribution
            .iter()
            .for_each(|StakerRewards { vault, amount }| {
                hasher.update(vault);
                hasher.update(<[u8; 32]>::from(*amount));
            });

        hasher.update(<[u8; 32]>::from(*total_amount));
        hasher.update(token);
    }
}

#[derive(Clone, Debug, Default, Encode, Decode, PartialEq, Eq)]
pub struct RewardsCommitment {
    pub operators: OperatorRewardsCommitment,
    pub stakers: StakerRewardsCommitment,
    /// Rewards for timestamp. Represented as u48 in router contract.
    pub timestamp: u64,
}

impl ToDigest for RewardsCommitment {
    fn update_hasher(&self, hasher: &mut sha3::Keccak256) {
        let RewardsCommitment {
            operators,
            stakers,
            timestamp,
        } = self;

        hasher.update(operators.to_digest());
        hasher.update(stakers.to_digest());
        hasher.update(crate::u64_into_uint48_be_bytes_lossy(*timestamp));
    }
}

/// Batch of different commitments that are created for a specific ethereum block.
#[derive(Clone, Debug, Default, Encode, Decode, PartialEq, Eq)]
pub struct BatchCommitment {
    // Hash of ethereum block for which this batch has been created
    // This is used to identify whether router have to apply this batch,
    // it can be a batch from another branch and after reorg it's not actual anymore (currently we have predecessorBlock for this)
    pub block_hash: H256,

    /// Timestamp of ethereum block for which this batch has been created
    /// This timestamp is used to identify validator set to verify commitment (current or previous era)
    pub timestamp: u64,

    /// Digest of the previous committed batch.
    /// This is used to verify that the batch is committed in the correct order.
    pub previous_batch: Digest,

    /// How long the batch is valid (in blocks since `block_hash`).
    /// if 1 - then valid only in child block
    /// if 2 - then valid in child and grandchild blocks
    /// ... etc.
    pub expiry: u8,

    pub chain_commitment: Option<ChainCommitment>,
    pub code_commitments: Vec<CodeCommitment>,
    pub validators_commitment: Option<ValidatorsCommitment>,
    pub rewards_commitment: Option<RewardsCommitment>,
}

impl ToDigest for BatchCommitment {
    fn update_hasher(&self, hasher: &mut sha3::Keccak256) {
        // To avoid missing incorrect hashing while developing.
        let Self {
            block_hash,
            timestamp,
            previous_batch,
            expiry,
            chain_commitment,
            code_commitments,
            validators_commitment,
            rewards_commitment,
        } = self;

        hasher.update(block_hash);
        hasher.update(crate::u64_into_uint48_be_bytes_lossy(*timestamp));
        hasher.update(previous_batch);
        hasher.update(expiry.to_be_bytes());
        hasher.update(chain_commitment.to_digest());
        hasher.update(code_commitments.to_digest());
        hasher.update(rewards_commitment.to_digest());
        hasher.update(validators_commitment.to_digest());
    }
}

#[derive(Clone, Debug, Default, Encode, Decode, PartialEq, Eq)]
pub struct Timelines {
    pub era: u64,
    pub election: u64,
    pub validation_delay: u64,
}

#[derive(Clone, Debug, Encode, Decode, PartialEq, Eq)]
pub struct ValidatorsCommitment {
    /// Does the batch have aggregated public key in validators commitment.
    pub has_aggregated_public_key: bool,
    pub aggregated_public_key: AggregatedPublicKey,
    pub verifiable_secret_sharing_commitment: Vec<u8>,
    pub validators: ValidatorsVec,
    pub era_index: u64,
}

impl ToDigest for ValidatorsCommitment {
    fn update_hasher(&self, hasher: &mut sha3::Keccak256) {
        let ValidatorsCommitment {
            has_aggregated_public_key,
            aggregated_public_key,
            verifiable_secret_sharing_commitment: _, // TODO: add to digest
            validators,
            era_index,
        } = self;

        hasher.update([*has_aggregated_public_key as u8]);
        hasher.update(<[u8; 32]>::from(aggregated_public_key.x));
        hasher.update(<[u8; 32]>::from(aggregated_public_key.y));
        hasher.update(
            validators
                .iter()
                .flat_map(|v| {
                    // Adjust to 32 bytes, because of `encodePacked` in Gear.validatorCommitmentHash
                    let mut bytes = [0u8; 32];
                    bytes[12..32].copy_from_slice(&v.0);
                    bytes.into_iter()
                })
                .collect::<Vec<u8>>(),
        );

        let bytes = AlloyU256::from(*era_index).to_be_bytes::<32>();
        hasher.update(bytes);
    }
}

#[derive(Clone, Copy, Debug, Default, Encode, Decode, PartialEq, Eq)]
pub enum CodeState {
    #[default]
    Unknown,
    ValidationRequested,
    Validated,
}

impl From<u8> for CodeState {
    fn from(value: u8) -> Self {
        match value {
            0 => Self::Unknown,
            1 => Self::ValidationRequested,
            2 => Self::Validated,
            // NOTE: newly added variants should be updated accordingly
            _ => Self::Unknown,
        }
    }
}

#[derive(Clone, Debug, Default, Encode, Decode, PartialEq, Eq)]
pub struct CommittedBlockInfo {
    pub hash: H256,
    /// represented as u48 in router contract.
    pub timestamp: u64,
}

#[derive(Clone, Debug, Default, Encode, Decode, PartialEq, Eq)]
pub struct ComputationSettings {
    pub threshold: u64,
    pub wvara_per_second: u128,
}

#[derive(Clone, Debug, Default, Encode, Decode, TypeInfo, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct Message {
    pub id: MessageId,
    pub destination: ActorId,
    pub payload: Vec<u8>,
    pub value: u128,
    pub reply_details: Option<ReplyDetails>,
    pub call: bool,
}

impl ToDigest for Message {
    fn update_hasher(&self, hasher: &mut sha3::Keccak256) {
        // To avoid missing incorrect hashing while developing.
        let Self {
            id,
            destination,
            payload,
            value,
            reply_details,
            call,
        } = self;

        let (reply_details_to, reply_details_code) =
            reply_details.map(|d| d.into_parts()).unwrap_or((
                MessageId::default(),
                ReplyCode::Success(SuccessReplyReason::Auto),
            ));

        hasher.update(id);
        hasher.update(destination.to_address_lossy());
        hasher.update(payload);
        hasher.update(value.to_be_bytes());
        hasher.update(reply_details_to);
        hasher.update(reply_details_code.to_bytes());
        hasher.update([*call as u8]);
    }
}

impl Message {
    pub fn from_stored(value: StoredMessage, call: bool) -> Self {
        let (id, _source, destination, payload, value, details) = value.into_parts();
        Self {
            id,
            destination,
            payload: payload.into_vec(),
            value,
            reply_details: details.and_then(|v| v.to_reply_details()),
            call,
        }
    }
}

#[derive(Clone, Debug, Default, Encode, Decode, PartialEq, Eq)]
pub struct ProtocolData {
    // flatten mapping of codes CodeId => CodeState
    // flatten mapping of program to codes ActorId => CodeId
    pub programs_count: U256,
    pub validated_codes_count: U256,
}

#[derive(Clone, Debug, Default, Encode, Decode, TypeInfo, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct StateTransition {
    pub actor_id: ActorId,
    pub new_state_hash: H256,
    pub exited: bool,
    pub inheritor: ActorId,
    /// We represent `value_to_receive` as `u128` and `bool` because each non-zero byte costs 16 gas,
    /// and each zero byte costs 4 gas (see <https://evm.codes/about#gascosts>).
    ///
    /// Negative numbers will be stored like this:
    /// ```bash
    /// $ cast
    /// > -1 ether
    /// Type: int256
    /// Hex: 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffff21f494c589c0000
    /// ```
    ///
    /// This is optimization on EVM side to reduce gas costs for storing and processing values.
    pub value_to_receive: u128,
    pub value_to_receive_negative_sign: bool,
    pub value_claims: Vec<ValueClaim>,
    pub messages: Vec<Message>,
}

impl ToDigest for StateTransition {
    fn update_hasher(&self, hasher: &mut sha3::Keccak256) {
        // To avoid missing incorrect hashing while developing.
        let Self {
            actor_id,
            new_state_hash,
            exited,
            inheritor,
            value_to_receive,
            value_to_receive_negative_sign,
            value_claims,
            messages,
        } = self;

        hasher.update(actor_id.to_address_lossy());
        hasher.update(new_state_hash);
        hasher.update([*exited as u8]);
        hasher.update(inheritor.to_address_lossy());
        hasher.update(value_to_receive.to_be_bytes());
        hasher.update([*value_to_receive_negative_sign as u8]);
        hasher.update(value_claims.to_digest());
        hasher.update(messages.to_digest());
    }
}

#[derive(Clone, Debug, Default, Encode, Decode, TypeInfo, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub struct ValueClaim {
    pub message_id: MessageId,
    pub destination: ActorId,
    pub value: u128,
}

impl ToDigest for ValueClaim {
    fn update_hasher(&self, hasher: &mut sha3::Keccak256) {
        let ValueClaim {
            message_id,
            destination,
            value,
        } = self;

        hasher.update(message_id);
        hasher.update(destination.to_address_lossy());
        hasher.update(value.to_be_bytes());
    }
}

#[derive(
    Clone,
    Copy,
    Debug,
    Encode,
    Decode,
    PartialEq,
    Eq,
    Default,
    PartialOrd,
    Ord,
    Hash,
    derive_more::IsVariant,
)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
pub enum MessageType {
    #[default]
    Canonical,
    Injected,
}

#[derive(Debug)]
pub struct GenesisBlockInfo {
    pub hash: H256,
    pub number: u32,
    pub timestamp: u64,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn validators_commitment_accepts_raw_vss_commitment_bytes() {
        let commitment = ValidatorsCommitment {
            has_aggregated_public_key: false,
            aggregated_public_key: AggregatedPublicKey::default(),
            verifiable_secret_sharing_commitment: vec![],
            validators: nonempty::nonempty![crate::Address::default()].into(),
            era_index: 0,
        };

        assert!(commitment.verifiable_secret_sharing_commitment.is_empty());
    }
}