Skip to content

Commit 2e40b5d

Browse files
committed
feat: add admin address for registering contracts
1 parent 800fd79 commit 2e40b5d

File tree

3 files changed

+77
-26
lines changed

3 files changed

+77
-26
lines changed

core-contracts/contracts/subnet.clar

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
;; Map recording processed withdrawal leaves
3232
(define-map processed-withdrawal-leaves-map { withdrawal-leaf-hash: (buff 32), withdrawal-root-hash: (buff 32) } bool)
3333

34-
;; List of miners
34+
;; principal that can commit blocks
3535
(define-data-var miner principal tx-sender)
36+
;; principal that can register contracts
37+
(define-data-var admin principal tx-sender)
3638

3739
;; Map of allowed contracts for asset transfers - maps L1 contract principal to L2 contract principal
3840
(define-map allowed-contracts principal principal)
@@ -53,8 +55,8 @@
5355
;; Register a new FT contract to be supported by this subnet.
5456
(define-public (register-new-ft-contract (ft-contract <ft-trait>) (l2-contract principal))
5557
(begin
56-
;; Verify that tx-sender is an authorized miner
57-
(asserts! (is-miner tx-sender) (err ERR_INVALID_MINER))
58+
;; Verify that tx-sender is an authorized admin
59+
(asserts! (is-admin tx-sender) (err ERR_INVALID_MINER))
5860

5961
;; Set up the assets that the contract is allowed to transfer
6062
(asserts! (map-insert allowed-contracts (contract-of ft-contract) l2-contract)
@@ -67,8 +69,8 @@
6769
;; Register a new NFT contract to be supported by this subnet.
6870
(define-public (register-new-nft-contract (nft-contract <nft-trait>) (l2-contract principal))
6971
(begin
70-
;; Verify that tx-sender is an authorized miner
71-
(asserts! (is-miner tx-sender) (err ERR_INVALID_MINER))
72+
;; Verify that tx-sender is an authorized admin
73+
(asserts! (is-admin tx-sender) (err ERR_INVALID_MINER))
7274

7375
;; Set up the assets that the contract is allowed to transfer
7476
(asserts! (map-insert allowed-contracts (contract-of nft-contract) l2-contract)
@@ -84,6 +86,12 @@
8486
(is-eq miner-to-check (var-get miner))
8587
)
8688

89+
;; Helper function: returns a boolean indicating whether the given principal is an admin
90+
;; Returns bool
91+
(define-private (is-admin (addr-to-check principal))
92+
(is-eq addr-to-check (var-get admin))
93+
)
94+
8795
;; Helper function: determines whether the commit-block operation satisfies pre-conditions
8896
;; listed in `commit-block`.
8997
;; Returns response<bool, int>

testnet/stacks-node/src/tests/l1_multiparty.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,12 @@ fn l1_multiparty_1_of_n_integration_test() {
136136

137137
wait_for_target_l1_block(&sortition_db, MOCKNET_EPOCH_2_1);
138138

139-
let l1_nonce = publish_subnet_contracts_to_l1(0, &config, multi_party_contract.clone().into());
139+
let l1_nonce = publish_subnet_contracts_to_l1(
140+
0,
141+
&config,
142+
multi_party_contract.clone().into(),
143+
multi_party_contract.clone().into(),
144+
);
140145
publish_multiparty_contract_to_l1(l1_nonce, &config, &[miner_account.clone().into()]);
141146

142147
// Wait for exactly two stacks blocks.
@@ -264,8 +269,12 @@ fn l1_multiparty_2_of_2_integration_test() {
264269
thread::sleep(Duration::from_millis(10_000));
265270
wait_for_target_l1_block(&sortition_db, MOCKNET_EPOCH_2_1);
266271

267-
let l1_nonce =
268-
publish_subnet_contracts_to_l1(0, &leader_config, multi_party_contract.clone().into());
272+
let l1_nonce = publish_subnet_contracts_to_l1(
273+
0,
274+
&leader_config,
275+
multi_party_contract.clone().into(),
276+
multi_party_contract.clone().into(),
277+
);
269278
publish_multiparty_contract_to_l1(
270279
l1_nonce,
271280
&leader_config,

testnet/stacks-node/src/tests/l1_observer_test.rs

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ pub fn publish_subnet_contracts_to_l1(
260260
mut l1_nonce: u64,
261261
config: &Config,
262262
miner: PrincipalData,
263+
admin: PrincipalData,
263264
) -> u64 {
264265
// Publish the subnet traits contract
265266
let trait_standard_contract_name = "subnet-traits";
@@ -298,9 +299,15 @@ pub fn publish_subnet_contracts_to_l1(
298299
&format!(
299300
"(define-data-var miner principal '{})",
300301
&miner
302+
),
303+
).replace(
304+
"(define-data-var admin principal tx-sender)",
305+
&format!(
306+
"(define-data-var admin principal '{})",
307+
&admin
301308
),
302-
).replace(
303-
"(use-trait nft-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait)",
309+
).replace(
310+
"(use-trait nft-trait 'SP2PABAF9FTAJYNFZH93XENAJ8FVY99RRM50D2JG9.nft-trait.nft-trait)",
304311
"(use-trait nft-trait .sip-traits.nft-trait)"
305312
).replace(
306313
"(use-trait ft-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait)",
@@ -411,7 +418,12 @@ fn l1_integration_test() {
411418
thread::sleep(Duration::from_millis(10_000));
412419

413420
wait_for_target_l1_block(&sortition_db, MOCKNET_EPOCH_2_1);
414-
publish_subnet_contracts_to_l1(0, &config, miner_account.clone().into());
421+
publish_subnet_contracts_to_l1(
422+
0,
423+
&config,
424+
miner_account.clone().into(),
425+
miner_account.clone().into(),
426+
);
415427

416428
// Wait for exactly two stacks blocks.
417429
wait_for_next_stacks_block(&sortition_db);
@@ -493,7 +505,12 @@ fn l1_deposit_and_withdraw_asset_integration_test() {
493505
thread::sleep(Duration::from_millis(10_000));
494506
wait_for_target_l1_block(&sortition_db, MOCKNET_EPOCH_2_1);
495507

496-
l1_nonce = publish_subnet_contracts_to_l1(l1_nonce, &config, miner_account.clone().into());
508+
l1_nonce = publish_subnet_contracts_to_l1(
509+
l1_nonce,
510+
&config,
511+
miner_account.clone().into(),
512+
user_addr.clone().into(),
513+
);
497514

498515
// Publish a simple FT and NFT
499516
let ft_content = include_str!("../../../../core-contracts/contracts/helper/simple-ft.clar");
@@ -606,12 +623,11 @@ fn l1_deposit_and_withdraw_asset_integration_test() {
606623
submit_tx(l1_rpc_origin, &l1_mint_ft_tx);
607624
submit_tx(l1_rpc_origin, &l1_mint_nft_tx);
608625

609-
// Register the contract (submitted by miner)
610-
let account = get_account(&l1_rpc_origin, &miner_account);
626+
// Register the contract
611627
let subnet_setup_ft_tx = make_contract_call(
612-
&MOCKNET_PRIVATE_KEY_2,
628+
&MOCKNET_PRIVATE_KEY_1,
613629
LAYER_1_CHAIN_ID_TESTNET,
614-
account.nonce,
630+
l1_nonce,
615631
1_000_000,
616632
&user_addr,
617633
config.burnchain.contract_identifier.name.as_str(),
@@ -621,12 +637,12 @@ fn l1_deposit_and_withdraw_asset_integration_test() {
621637
Value::Principal(PrincipalData::Contract(subnet_ft_contract_id.clone())),
622638
],
623639
);
624-
submit_tx(l1_rpc_origin, &subnet_setup_ft_tx);
640+
l1_nonce += 1;
625641

626642
let subnet_setup_nft_tx = make_contract_call(
627-
&MOCKNET_PRIVATE_KEY_2,
643+
&MOCKNET_PRIVATE_KEY_1,
628644
LAYER_1_CHAIN_ID_TESTNET,
629-
account.nonce + 1,
645+
l1_nonce,
630646
1_000_000,
631647
&user_addr,
632648
config.burnchain.contract_identifier.name.as_str(),
@@ -636,6 +652,9 @@ fn l1_deposit_and_withdraw_asset_integration_test() {
636652
Value::Principal(PrincipalData::Contract(subnet_nft_contract_id.clone())),
637653
],
638654
);
655+
l1_nonce += 1;
656+
657+
submit_tx(l1_rpc_origin, &subnet_setup_ft_tx);
639658
submit_tx(l1_rpc_origin, &subnet_setup_nft_tx);
640659

641660
wait_for_next_stacks_block(&sortition_db);
@@ -1305,7 +1324,12 @@ fn l1_deposit_and_withdraw_stx_integration_test() {
13051324
thread::sleep(Duration::from_millis(10_000));
13061325
wait_for_target_l1_block(&sortition_db, MOCKNET_EPOCH_2_1);
13071326

1308-
l1_nonce = publish_subnet_contracts_to_l1(l1_nonce, &config, miner_account.clone().into());
1327+
l1_nonce = publish_subnet_contracts_to_l1(
1328+
l1_nonce,
1329+
&config,
1330+
miner_account.clone().into(),
1331+
user_addr.clone().into(),
1332+
);
13091333

13101334
// The burnchain should have registered what the listener recorded.
13111335
let tip = burndb
@@ -1686,7 +1710,12 @@ fn l2_simple_contract_calls() {
16861710
thread::sleep(Duration::from_millis(10_000));
16871711
wait_for_target_l1_block(&sortition_db, MOCKNET_EPOCH_2_1);
16881712

1689-
publish_subnet_contracts_to_l1(0, &config, miner_account.clone().into());
1713+
publish_subnet_contracts_to_l1(
1714+
0,
1715+
&config,
1716+
miner_account.clone().into(),
1717+
user_addr.clone().into(),
1718+
);
16901719

16911720
wait_for_next_stacks_block(&sortition_db);
16921721
wait_for_next_stacks_block(&sortition_db);
@@ -1813,7 +1842,12 @@ fn nft_deposit_and_withdraw_integration_test() {
18131842
thread::sleep(Duration::from_millis(10_000));
18141843
wait_for_target_l1_block(&sortition_db, MOCKNET_EPOCH_2_1);
18151844

1816-
l1_nonce = publish_subnet_contracts_to_l1(l1_nonce, &config, miner_account.clone().into());
1845+
l1_nonce = publish_subnet_contracts_to_l1(
1846+
l1_nonce,
1847+
&config,
1848+
miner_account.clone().into(),
1849+
user_addr.clone().into(),
1850+
);
18171851

18181852
// Publish a simple NFT onto L1
18191853
let nft_content = include_str!("../../../../core-contracts/contracts/helper/simple-nft.clar");
@@ -1869,12 +1903,11 @@ fn nft_deposit_and_withdraw_integration_test() {
18691903
let subnet_nft_contract_id =
18701904
QualifiedContractIdentifier::new(user_addr.into(), ContractName::from("simple-nft"));
18711905

1872-
// Setup subnet contract (submitted by miner)
1873-
let account = get_account(&l2_rpc_origin, &miner_account);
1906+
// Setup subnet contract
18741907
let subnet_setup_nft_tx = make_contract_call(
1875-
&MOCKNET_PRIVATE_KEY_2,
1908+
&MOCKNET_PRIVATE_KEY_1,
18761909
LAYER_1_CHAIN_ID_TESTNET,
1877-
account.nonce + 1,
1910+
l1_nonce,
18781911
1_000_000,
18791912
&user_addr,
18801913
config.burnchain.contract_identifier.name.as_str(),
@@ -1884,6 +1917,7 @@ fn nft_deposit_and_withdraw_integration_test() {
18841917
Value::Principal(PrincipalData::Contract(subnet_nft_contract_id.clone())),
18851918
],
18861919
);
1920+
l1_nonce += 1;
18871921

18881922
submit_tx(&l2_rpc_origin, &subnet_nft_publish);
18891923
submit_tx(l1_rpc_origin, &subnet_setup_nft_tx);

0 commit comments

Comments
 (0)