Skip to content

Commit 392ce20

Browse files
authored
Merge pull request #1150 from mintlayer/change_magic_bytes_func_test
Change magic bytes functional test
2 parents 8b0038d + 7ba26b5 commit 392ce20

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

node-lib/src/regtest_options.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ pub struct RegtestOptions {
2828

2929
#[derive(Args, Clone, Debug)]
3030
pub struct ChainConfigOptions {
31+
/// Magic bytes.
32+
#[clap(long)]
33+
pub chain_magic_bytes: Option<String>,
34+
3135
/// The maximum future block offset in seconds.
3236
#[clap(long)]
3337
pub chain_max_future_block_time_offset: Option<u64>,

node-lib/src/runner.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::{
2323
time::Duration,
2424
};
2525

26-
use anyhow::{anyhow, Context, Result};
26+
use anyhow::{anyhow, ensure, Context, Result};
2727
use blockprod::rpc::BlockProductionRpcServer;
2828
use chainstate_launcher::StorageBackendConfig;
2929
use paste::paste;
@@ -331,6 +331,7 @@ fn regtest_chain_config(command: &Command, options: &ChainConfigOptions) -> Resu
331331
};
332332

333333
let ChainConfigOptions {
334+
chain_magic_bytes,
334335
chain_max_future_block_time_offset,
335336
chain_version,
336337
chain_target_block_spacing,
@@ -365,6 +366,15 @@ fn regtest_chain_config(command: &Command, options: &ChainConfigOptions) -> Resu
365366
};
366367
}
367368

369+
let magic_bytes_from_string = |magic_string: String| -> Result<[u8; 4]> {
370+
ensure!(magic_string.len() == 4, "Invalid size of magic_bytes");
371+
let mut result: [u8; 4] = [0; 4];
372+
for (i, byte) in magic_string.bytes().enumerate() {
373+
result[i] = byte;
374+
}
375+
Ok(result)
376+
};
377+
update_builder!(magic_bytes, magic_bytes_from_string, map_err);
368378
update_builder!(max_future_block_time_offset, Duration::from_secs);
369379
update_builder!(version, SemVer::try_from, map_err);
370380
update_builder!(target_block_spacing, Duration::from_secs);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2023 RBB S.r.l
3+
4+
# SPDX-License-Identifier: MIT
5+
# Licensed under the MIT License;
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://github.com/mintlayer/mintlayer-core/blob/master/LICENSE
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
from scalecodec.base import RuntimeConfiguration
18+
from test_framework.test_framework import BitcoinTestFramework
19+
from test_framework.util import (
20+
assert_equal,
21+
)
22+
23+
block_input_data_obj = RuntimeConfiguration().create_scale_object('GenerateBlockInputData')
24+
25+
26+
class RestartWithDifferentMagicBytes(BitcoinTestFramework):
27+
def set_test_params(self):
28+
self.setup_clean_chain = True
29+
self.num_nodes = 1
30+
self.extra_args = [[
31+
"--blockprod-min-peers-to-produce-blocks=0"
32+
]]
33+
34+
def run_test(self):
35+
# generate and process block to update chainstate db
36+
block_input_data = block_input_data_obj.encode({"PoW": {"reward_destination": "AnyoneCanSpend"}}
37+
).to_hex()[2:]
38+
39+
block = self.nodes[0].blockprod_generate_block(block_input_data, [])
40+
self.nodes[0].chainstate_submit_block(block)
41+
42+
tip_height = self.nodes[0].chainstate_best_block_height()
43+
assert_equal(1, tip_height)
44+
45+
# restart the node and check that the db hasn't changed
46+
self.restart_node(0)
47+
tip_height = self.nodes[0].chainstate_best_block_height()
48+
assert_equal(1, tip_height)
49+
50+
# restart the node with different magic bytes and check that db was cleaned up
51+
self.restart_node(0, extra_args=["--chain-magic-bytes=ffff"])
52+
tip_height = self.nodes[0].chainstate_best_block_height()
53+
assert_equal(0, tip_height)
54+
55+
56+
if __name__ == '__main__':
57+
RestartWithDifferentMagicBytes().main()

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class UnicodeOnWindowsError(ValueError):
109109
'p2p_submit_orphan.py',
110110
'p2p_syncing_test.py',
111111
'p2p_relay_transactions.py',
112+
'feature_db_reinit.py',
112113
'feature_lmdb_backend_test.py',
113114
'mempool_basic_reorg.py',
114115
'mempool_eviction.py',

0 commit comments

Comments
 (0)