|
| 1 | +use serde_json::json; |
| 2 | +use tracing::info; |
| 3 | + |
| 4 | +mod common; |
| 5 | +use common::{panic::PanicFinder, prepare::sweat_wasm_bytes, prepare::Context}; |
| 6 | + |
| 7 | +/// `up_stage_code` is restricted to `StagingManager` (the `code_stagers` role) |
| 8 | +/// and `up_deploy_code` to `UpgradeManager` (`code_deployers`). The two roles |
| 9 | +/// are distinct: holding the stager role does not grant the deployer one. |
| 10 | +#[tokio::test] |
| 11 | +#[tracing::instrument] |
| 12 | +async fn test_upgrade_access_control() -> anyhow::Result<()> { |
| 13 | + let context = Context::builder().build().await?; |
| 14 | + let code = sweat_wasm_bytes()?; |
| 15 | + |
| 16 | + info!("call up_stage_code [signer=alice, unauthorized]"); |
| 17 | + let result = context |
| 18 | + .alice |
| 19 | + .call(context.sweat.id(), "up_stage_code") |
| 20 | + .args(code.clone()) |
| 21 | + .max_gas() |
| 22 | + .transact() |
| 23 | + .await? |
| 24 | + .into_result(); |
| 25 | + assert!(result.has_panic("Insufficient permissions for method up_stage_code restricted by access control.")); |
| 26 | + |
| 27 | + info!("view up_staged_code_hash — nothing should be staged yet"); |
| 28 | + let staged: Option<String> = context.sweat.view("up_staged_code_hash").await?.json()?; |
| 29 | + assert_eq!(staged, None, "unauthorized staging must not store any code"); |
| 30 | + |
| 31 | + info!("call up_deploy_code [signer=alice, unauthorized]"); |
| 32 | + let result = context |
| 33 | + .alice |
| 34 | + .call(context.sweat.id(), "up_deploy_code") |
| 35 | + .args_json(json!({ "hash": "ignored", "function_call_args": null })) |
| 36 | + .max_gas() |
| 37 | + .transact() |
| 38 | + .await? |
| 39 | + .into_result(); |
| 40 | + assert!(result.has_panic("Insufficient permissions for method up_deploy_code restricted by access control.")); |
| 41 | + |
| 42 | + info!("call acl_grant_role(StagingManager, alice) [signer=contract, super-admin]"); |
| 43 | + let granted: Option<bool> = context |
| 44 | + .sweat |
| 45 | + .call("acl_grant_role") |
| 46 | + .args_json(json!({ "role": "StagingManager", "account_id": context.alice.id() })) |
| 47 | + .transact() |
| 48 | + .await? |
| 49 | + .json()?; |
| 50 | + assert_eq!(granted, Some(true)); |
| 51 | + |
| 52 | + info!("call up_stage_code [signer=alice, authorized as StagingManager]"); |
| 53 | + let result = context |
| 54 | + .alice |
| 55 | + .call(context.sweat.id(), "up_stage_code") |
| 56 | + .args(code.clone()) |
| 57 | + .max_gas() |
| 58 | + .transact() |
| 59 | + .await? |
| 60 | + .into_result()?; |
| 61 | + assert!(result.outcome().is_success()); |
| 62 | + |
| 63 | + info!("view up_staged_code_hash — code is now staged"); |
| 64 | + let staged: Option<String> = context.sweat.view("up_staged_code_hash").await?.json()?; |
| 65 | + assert!(staged.is_some(), "staged code hash should be set after staging"); |
| 66 | + |
| 67 | + info!("call up_deploy_code [signer=alice, StagingManager but not UpgradeManager]"); |
| 68 | + let result = context |
| 69 | + .alice |
| 70 | + .call(context.sweat.id(), "up_deploy_code") |
| 71 | + .args_json(json!({ "hash": staged.unwrap(), "function_call_args": null })) |
| 72 | + .max_gas() |
| 73 | + .transact() |
| 74 | + .await? |
| 75 | + .into_result(); |
| 76 | + assert!( |
| 77 | + result.has_panic("Insufficient permissions for method up_deploy_code restricted by access control."), |
| 78 | + "the stager role must not grant deploy permission" |
| 79 | + ); |
| 80 | + |
| 81 | + Ok(()) |
| 82 | +} |
| 83 | + |
| 84 | +/// Full stage → deploy flow: an `UpgradeManager` re-deploys the contract over |
| 85 | +/// itself and the existing state survives the upgrade. |
| 86 | +#[tokio::test] |
| 87 | +#[tracing::instrument] |
| 88 | +async fn test_upgrade_deploy() -> anyhow::Result<()> { |
| 89 | + let context = Context::builder().with_oracle().with_claim().build().await?; |
| 90 | + let code = sweat_wasm_bytes()?; |
| 91 | + |
| 92 | + info!("record a batch so there is pre-upgrade state to preserve"); |
| 93 | + context |
| 94 | + .oracle() |
| 95 | + .call(context.sweat.id(), "defer_batch") |
| 96 | + .args_json(json!({ "steps_batch": [[context.alice.id(), 10_000]] })) |
| 97 | + .max_gas() |
| 98 | + .transact() |
| 99 | + .await? |
| 100 | + .into_result()?; |
| 101 | + let steps_before: String = context.sweat.view("get_steps_since_tge").await?.json()?; |
| 102 | + assert_ne!(steps_before, "0", "steps should be recorded before the upgrade"); |
| 103 | + |
| 104 | + info!("grant the upgrade roles to alice [signer=contract, super-admin]"); |
| 105 | + for role in ["StagingManager", "UpgradeManager"] { |
| 106 | + let granted: Option<bool> = context |
| 107 | + .sweat |
| 108 | + .call("acl_grant_role") |
| 109 | + .args_json(json!({ "role": role, "account_id": context.alice.id() })) |
| 110 | + .transact() |
| 111 | + .await? |
| 112 | + .json()?; |
| 113 | + assert_eq!(granted, Some(true), "{role} grant should succeed"); |
| 114 | + } |
| 115 | + |
| 116 | + info!("stage the contract code [signer=alice, StagingManager]"); |
| 117 | + context |
| 118 | + .alice |
| 119 | + .call(context.sweat.id(), "up_stage_code") |
| 120 | + .args(code) |
| 121 | + .max_gas() |
| 122 | + .transact() |
| 123 | + .await? |
| 124 | + .into_result()?; |
| 125 | + |
| 126 | + info!("read back the staged code hash to feed the deploy"); |
| 127 | + let staged_hash: Option<String> = context.sweat.view("up_staged_code_hash").await?.json()?; |
| 128 | + let staged_hash = staged_hash.expect("code must be staged before deploy"); |
| 129 | + |
| 130 | + info!("deploy the staged code [signer=alice, UpgradeManager]"); |
| 131 | + let result = context |
| 132 | + .alice |
| 133 | + .call(context.sweat.id(), "up_deploy_code") |
| 134 | + .args_json(json!({ "hash": staged_hash, "function_call_args": null })) |
| 135 | + .max_gas() |
| 136 | + .transact() |
| 137 | + .await? |
| 138 | + .into_result()?; |
| 139 | + assert!(result.outcome().is_success(), "deploy should succeed"); |
| 140 | + |
| 141 | + info!("verify state survived the upgrade"); |
| 142 | + let steps_after: String = context.sweat.view("get_steps_since_tge").await?.json()?; |
| 143 | + assert_eq!(steps_after, steps_before, "steps counter must be preserved across the upgrade"); |
| 144 | + |
| 145 | + info!("verify the upgraded contract still serves authorized calls"); |
| 146 | + context |
| 147 | + .oracle() |
| 148 | + .call(context.sweat.id(), "defer_batch") |
| 149 | + .args_json(json!({ "steps_batch": [[context.alice.id(), 10_000]] })) |
| 150 | + .max_gas() |
| 151 | + .transact() |
| 152 | + .await? |
| 153 | + .into_result()?; |
| 154 | + let steps_final: String = context.sweat.view("get_steps_since_tge").await?.json()?; |
| 155 | + assert_eq!(steps_final, "20000", "the upgraded contract should keep recording steps"); |
| 156 | + |
| 157 | + Ok(()) |
| 158 | +} |
0 commit comments