01_noupgrade.md
1 # Non-upgradable Programs 2 In many cases, developers will want to deploy a program that is not upgradable. 3 This is the default mode supported by Leo. 4 Let's take a look at a simple example. 5 6 ## Initializing the Project 7 You may either use the existing `noupgrade` project 8 ``` 9 > cd noupgrade 10 ``` 11 or create a new Leo project with the following command: 12 ``` 13 > leo new noupgrade 14 ``` 15 16 ## The Program 17 ```leo 18 program noupgrade_example.alpha { 19 @noupgrade 20 async constructor() {} 21 22 transition main(public a: u32, b: u32) -> u32 { 23 let c: u32 = a + b; 24 return c; 25 } 26 } 27 ``` 28 Upgrades are defined by the logic of the `constructor`. 29 The constructor is an **immutable** asynchronous function that is strictly on-chain. 30 This means that once the program is deployed, the constructor cannot be changed. 31 It is executed every time the program is deployed or upgraded. 32 33 34 The `@noupgrade` annotation indicates that this program is not upgradable. 35 If the `@noupgrade` annotation is specified, the body of the constructor must be left blank. 36 The Leo compiler will automatically generate the appropriate code. 37 The constructor checks that the program's edition (version) is `0`. 38 39 Each program is automatically assigned a version when the network processes the deployment. 40 If the program is already deployed on the network, the assigned version will be non-zero, the constructor will fail to execute, and the deployment will be rejected. 41 42 ## Deploying the Program 43 ``` 44 > leo deploy --broadcast 45 Leo 3 statements before dead code elimination. 46 Leo 3 statements after dead code elimination. 47 Leo The program checksum is: '[12u8, 65u8, 184u8, 236u8, 12u8, 123u8, 129u8, 53u8, 156u8, 105u8, 181u8, 154u8, 185u8, 201u8, 147u8, 232u8, 5u8, 12u8, 127u8, 88u8, 130u8, 105u8, 56u8, 198u8, 194u8, 9u8, 51u8, 107u8, 11u8, 148u8, 96u8, 114u8]'. 48 Leo โ Compiled 'noupgrade_example.alpha' into Aleo instructions. 49 Attempting to determine the consensus version from the latest block height at http://localhost:3030... 50 51 ๐ ๏ธ Deployment Plan Summary 52 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 53 ๐ง Configuration: 54 Private Key: APrivateKey1zkp8CZNn3yeC... 55 Address: aleo1rhgdu77hgyqd3xjj8uc... 56 Endpoint: http://localhost:3030 57 Network: testnet 58 Consensus Version: 9 59 60 ๐ฆ Deployment Tasks: 61 โข noupgrade_example.alpha โ priority fee: 0 โ fee record: no (public fee) 62 63 โ๏ธ Actions: 64 โข Transaction(s) will NOT be printed to the console. 65 โข Transaction(s) will NOT be saved to a file. 66 โข Transaction(s) will be broadcast to http://localhost:3030 67 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 68 69 โ Do you want to proceed with deployment? ยท yes 70 71 72 ๐ง You program 'noupgrade_example.alpha' has the following constructor. 73 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 74 constructor: 75 assert.eq edition 0u16; 76 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 77 Once it is deployed, it CANNOT be changed. 78 79 โ Would you like to proceed? ยท yes 80 81 ๐ฆ Creating deployment transaction for 'noupgrade_example.alpha'... 82 83 84 ๐ Deployment Summary for noupgrade_example.alpha 85 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 86 Total Variables: 17,276 87 Total Constraints: 12,927 88 Max Variables: 2,097,152 89 Max Constraints: 2,097,152 90 91 ๐ฐ Cost Breakdown (credits) 92 Transaction Storage: 0.886000 93 Program Synthesis: 0.755075 94 Namespace: 1.000000 95 Constructor: 0.050000 96 Priority Fee: 0.000000 97 Total Fee: 2.691075 98 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 99 100 ๐ก Broadcasting deployment for noupgrade_example.alpha... 101 ๐ฐYour current public balance is 93749987.434492 credits. 102 103 โ This transaction will cost you 2.691075 credits. Do you want to proceed? ยท yes 104 105 โ๏ธ Broadcasted transaction with: 106 - transaction ID: 'at1gk55y5asypvckqnszf83de8ktkte4f0k5m7cqe4pplccrvnhnszs53mnmu' 107 - fee ID: 'au1s7xwundv875gtj599l9v7wk8xdr6tncq8w8l4h65hqvj55k4svfqe0tc9k' 108 ๐ Searching up to 12 blocks to confirm transaction (this may take several seconds)... 109 Explored 1 blocks. 110 Transaction accepted. 111 โ Deployment confirmed! 112 ``` 113 114 We can query the network to see that the deployment transaction has been accepted: 115 ``` 116 leo query transaction at1gk55y5asypvckqnszf83de8ktkte4f0k5m7cqe4pplccrvnhnszs53mnmu 117 ``` 118 119 ## Attempting to Upgrade 120 To test the upgrade functionality, we can try to deploy the same program again. 121 You may also try to modify the program to add a new function. 122 Please refer to the [documentation](https://docs.leo-lang.org/guides/upgradability) for more details on what constitutes a valid upgrade. 123 124 Now we will run 125 ``` 126 > leo upgrade --broadcast 127 Leo 3 statements after dead code elimination. 128 Leo The program checksum is: '[12u8, 65u8, 184u8, 236u8, 12u8, 123u8, 129u8, 53u8, 156u8, 105u8, 181u8, 154u8, 185u8, 201u8, 147u8, 232u8, 5u8, 12u8, 127u8, 88u8, 130u8, 105u8, 56u8, 198u8, 194u8, 9u8, 51u8, 107u8, 11u8, 148u8, 96u8, 114u8]'. 129 Leo โ Compiled 'noupgrade_example.alpha' into Aleo instructions. 130 Attempting to determine the consensus version from the latest block height at http://localhost:3030... 131 132 ๐ ๏ธ Deployment Plan Summary 133 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 134 ๐ง Configuration: 135 Private Key: APrivateKey1zkp8CZNn3yeC... 136 Address: aleo1rhgdu77hgyqd3xjj8uc... 137 Endpoint: http://localhost:3030 138 Network: testnet 139 Consensus Version: 9 140 141 ๐ฆ Deployment Tasks: 142 โข noupgrade_example.alpha โ priority fee: 0 โ fee record: no (public fee) 143 144 โ๏ธ Actions: 145 โข Transaction(s) will NOT be printed to the console. 146 โข Transaction(s) will NOT be saved to a file. 147 โข Transaction(s) will be broadcast to http://localhost:3030 148 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 149 150 โ Do you want to proceed with upgrade? ยท yes 151 152 ๐ฆ Creating deployment transaction for 'noupgrade_example.alpha'... 153 154 155 ๐ Deployment Summary for noupgrade_example.alpha 156 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 157 Total Variables: 17,276 158 Total Constraints: 12,927 159 Max Variables: 2,097,152 160 Max Constraints: 2,097,152 161 162 ๐ฐ Cost Breakdown (credits) 163 Transaction Storage: 0.886000 164 Program Synthesis: 0.755075 165 Namespace: 1.000000 166 Constructor: 0.050000 167 Priority Fee: 0.000000 168 Total Fee: 2.691075 169 โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ 170 ๐ก Broadcasting upgrade for noupgrade_example.alpha... 171 ๐ฐYour current public balance is 93749961.436972 credits. 172 173 โ This transaction will cost you 2.691075 credits. Do you want to proceed? ยท yes 174 175 โ๏ธ Broadcasted transaction with: 176 - transaction ID: 'at1geum6h7f7ym97d8z6k4zgnj7ycl0kegqygenquxmd999rys0wvgsxms7jg' 177 - fee ID: 'au1wgjj8e2pa4swpt4myr99znypuag2w7c35ds3druuze5fm5tga5xspvjjq8' 178 ๐ Searching up to 12 blocks to confirm transaction (this may take several seconds)... 179 Explored 2 blocks. 180 Transaction rejected. 181 โ Failed to upgrade program noupgrade_example.alpha: Transaction apparently not accepted. 182 ```