-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathutils.rs
More file actions
170 lines (151 loc) · 5.87 KB
/
utils.rs
File metadata and controls
170 lines (151 loc) · 5.87 KB
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
use std::str::FromStr;
use alloy_consensus::constants::KECCAK_EMPTY;
use alloy_consensus::{TxEip1559, TypedTransaction};
use alloy_eips::{eip1559::MIN_PROTOCOL_BASE_FEE, eip2718::Encodable2718};
use alloy_primitives::{Address, Bytes, TxKind};
use reth_primitives::TransactionSigned;
use secp256k1::rand::SeedableRng as _;
use secp256k1::{PublicKey, SecretKey};
use sov_address::MultiAddress;
use sov_address::{EthereumAddress, FromVmAddress};
use sov_eth_dev_signer::Signer;
use sov_evm::{
AccountData, EthereumAuthenticator, EvmChainSpec, EvmGenesisConfig, RlpEvmTransaction, SpecId,
};
use sov_evm_test_utils::LegacySimpleStorage;
use sov_modules_api::capabilities::{config_chain_id, TransactionAuthenticator, UniquenessData};
use sov_modules_api::macros::config_value;
use sov_modules_api::transaction::{Transaction, UnsignedTransaction};
use sov_modules_api::{EncodeCall, RawTx};
use sov_test_utils::runtime::genesis::optimistic::HighLevelOptimisticGenesisConfig;
use sov_test_utils::runtime::{Runtime, TestRunner, ValueSetter, ValueSetterConfig};
use sov_test_utils::{
TestUser, TransactionType, TEST_DEFAULT_MAX_FEE, TEST_DEFAULT_MAX_PRIORITY_FEE,
TEST_DEFAULT_USER_BALANCE,
};
use crate::runtime::{GenesisConfig, TestNonceRuntime, RT, S};
pub(crate) struct EvmAccount(SecretKey);
impl EvmAccount {
pub fn generate() -> Self {
let mut rng = secp256k1::rand::rngs::StdRng::from_entropy();
let secret_key = SecretKey::new(&mut rng);
Self(secret_key)
}
pub fn public_key(&self) -> PublicKey {
PublicKey::from_secret_key(secp256k1::SECP256K1, &self.0)
}
pub fn address(&self) -> Address {
reth_primitives::public_key_to_address(self.public_key())
}
pub fn sign(&self, tx: TypedTransaction) -> (RlpEvmTransaction, TransactionSigned) {
let signer = Signer::new(self.0);
let signed_tx = signer.sign_transaction(tx).unwrap();
let rlp = signed_tx.encoded_2718();
(RlpEvmTransaction { rlp }, signed_tx)
}
}
pub(crate) fn generate_default_tx(
uniqueness: UniquenessData,
admin: &TestUser<S>,
evm_account: &EvmAccount,
) -> TransactionType<RT, S> {
match uniqueness {
UniquenessData::Nonce(nonce) => {
let contract = LegacySimpleStorage::default();
let create_contract_tx_request = TypedTransaction::Eip1559(TxEip1559 {
chain_id: config_value!("CHAIN_ID"),
nonce,
max_priority_fee_per_gas: Default::default(),
max_fee_per_gas: MIN_PROTOCOL_BASE_FEE as u128 * 2,
gas_limit: 1_000_000,
to: TxKind::Create,
value: Default::default(),
input: Bytes::from(contract.byte_code().to_vec()),
access_list: Default::default(),
});
let (signed_eth_tx, _) = evm_account.sign(create_contract_tx_request);
let create_contract_tx = RawTx {
data: borsh::to_vec(&signed_eth_tx).unwrap(),
};
TransactionType::<RT, S>::PreAuthenticated(RT::encode_with_ethereum_auth(
create_contract_tx,
))
}
x => generate_value_setter_tx(x, 10, admin),
}
}
pub(crate) fn generate_value_setter_tx(
nonce: UniquenessData,
value: u32,
admin: &TestUser<S>,
) -> TransactionType<RT, S> {
let runtime_msg =
<RT as EncodeCall<ValueSetter<S>>>::to_decodable(sov_value_setter::CallMessage::SetValue {
value,
gas: None,
});
let transaction = UnsignedTransaction::new(
runtime_msg,
config_chain_id(),
TEST_DEFAULT_MAX_PRIORITY_FEE,
TEST_DEFAULT_MAX_FEE,
nonce,
None,
);
let transaction = Transaction::<RT, S>::new_signed_tx(
admin.private_key(),
&<TestNonceRuntime<S> as Runtime<S>>::CHAIN_HASH,
transaction,
);
TransactionType::PreAuthenticated(<RT as Runtime<S>>::Auth::encode_with_standard_auth(RawTx {
data: borsh::to_vec(&transaction).unwrap(),
}))
}
pub(crate) fn setup() -> (TestUser<S>, TestRunner<TestNonceRuntime<S>, S>, EvmAccount) {
// Generate a genesis config, then overwrite the attester key/address with ones that
// we know. We leave the other values untouched.
let genesis_config =
HighLevelOptimisticGenesisConfig::generate().add_accounts_with_default_balance(1);
let admin = genesis_config
.additional_accounts()
.first()
.unwrap()
.clone();
let evm_account = EvmAccount::generate();
let evm_config = EvmGenesisConfig {
accounts: vec![AccountData {
address: evm_account.address(),
code_hash: KECCAK_EMPTY,
code: Default::default(),
}],
chain_spec: EvmChainSpec {
// CANCUN instead of LATEST
// https://github.com/Sovereign-Labs/sovereign-sdk/issues/912
hardforks: vec![(0, SpecId::CANCUN)].into_iter().collect(),
..Default::default()
},
contract_creation_policy: Default::default(),
initial_base_fee: 0,
genesis_timestamp: 0,
admin: MultiAddress::from_vm_address(
EthereumAddress::from_str("0x0123456789012345678901234567890123456789").unwrap(),
),
};
// Run genesis registering the attester and sequencer we've generated.
let mut genesis = GenesisConfig::from_minimal_config(
genesis_config.into(),
ValueSetterConfig {
admin: admin.address(),
},
evm_config,
);
if let Some(c) = genesis.bank.gas_token_config.as_mut() {
c.address_and_balances.push((
MultiAddress::Vm(EthereumAddress::from(evm_account.address())),
TEST_DEFAULT_USER_BALANCE,
));
}
let runner =
TestRunner::new_with_genesis(genesis.into_genesis_params(), TestNonceRuntime::default());
(admin, runner, evm_account)
}