anza-xyz / agave

Web-Scale Blockchain for fast, secure, scalable, decentralized apps and marketplaces.
https://www.anza.xyz/
Apache License 2.0
392 stars 189 forks source link

CostModel: estimating execution cost for CPIs #2175

Open tao-stones opened 3 months ago

tao-stones commented 3 months ago

Example: https://explorer.solana.com/tx/n59ZQqdXmE4ixZ98BUhMY6U6Sh1gQXicboP2EMUZXftEujQZyT1rt9BnsaZm3ioSCjKc4G59A9Etg7t1egBFWaA

ALT in this transaction makes several CPIs calls, that makes actual execution cost higher than estimated cost, leads to cost tracker to "adjust up" reservation. It is not an issue by itself, but we would like to avoid "adjust up" that could lead to over packing.

actual_cost 2250 estimated_cost 1650 tx_cost Transaction(UsageCostDetails { writable_accounts: [GHak4BpDSLV9wxhDtZaKr82bHEtr7h5EJB6VPCaBLumg CQJ1w4Q6YRQjSK8zvYy8BGF9Yi5CxVrrQkAm1KMpkE3c] signature_cost: 1440 write_lock_cost: 600 data_bytes_cost: 200 builtins_execution_cost: 1650 bpf_execution_cost: 0 loaded_accounts_data_size_cost: 0 account_data_size: 0 }) tx SanitizedTransaction { message: Legacy(LegacyMessage { message: Message { header: MessageHeader { num_required_signatures: 2 num_readonly_signed_accounts: 1 num_readonly_unsigned_accounts: 3 } account_keys: [GHak4BpDSLV9wxhDtZaKr82bHEtr7h5EJB6VPCaBLumg 5tWBhYVisggz2YQC4FXVB6PKWPTocszsnThQkPjfihrB CQJ1w4Q6YRQjSK8zvYy8BGF9Yi5CxVrrQkAm1KMpkE3c 11111111111111111111111111111111 ComputeBudget111111111111111111111111111111 AddressLookupTab1e1111111111111111111111111] recent_blockhash: 6z464pgW2bFmCugPWAkAVHNEAc5zg9FpWVVYs4y524vM instructions: [CompiledInstruction { program_id_index: 4 accounts: [] data: [3 144 208 3 0 0 0 0 0] } CompiledInstruction { program_id_index: 5 accounts: [2 1 0 3] data: [0 0 0 0 178 198 158 14 0 0 0 0 255] } CompiledInstruction { program_id_index: 5 accounts: [2 1 0 3] data: [2 0 0 0 24 0 0 0 0 0 0 0 4 124 196 4 220 89 26 157 147 160 17 64 109 192 97 119 56 100 234 230 133 251 45 244 85 68 70 90 72 194 57 82 65 87 176 88 15 49 197 252 228 74 98 88 45 188 249 215 142 231 89 67 160 132 163 147 179 80 54 141 34 137 147 8 43 47 132 61 122 79 18 99 177 184 39 123 89 6 157 3 194 21 42 26 197 70 234 217 93 91 60 193 80 221 86 15 133 15 45 110 2 164 122 248 36 208 154 182 157 196 45 112 203 40 203 250 36 159 183 238 87 185 210 86 193 39 98 239 13 7 81 168 40 45 166 19 5 254 41 156 55 185 152 229 132 113 219 17 53 3 115 16 248 190 16 69 166 10 246 238 6 221 246 225 215 101 161 147 217 203 225 70 206 235 121 172 28 180 133 237 95 91 55 145 58 140 245 133 126 255 0 169 34 70 113 88 61 249 202 76 71 197 184 34 244 95 83 197 138 138 182 173 108 52 165 19 241 17 55 64 229 139 120 6 138 199 131 214 230 158 234 250 221 16 175 22 133 175 77 15 246 143 241 81 99 91 96 215 212 136 202 214 233 158 114 176 244 72 81 73 140 130 18 226 224 227 9 203 0 211 165 51 163 248 39 233 227 216 180 237 95 6 192 226 131 48 67 237 16 74 114 56 239 75 30 109 228 63 42 20 67 61 231 211 157 28 200 243 161 19 126 153 48 51 122 82 112 104 79 207 94 173 152 104 42 20 123 154 128 75 135 134 93 145 8 55 88 184 217 90 145 6 8 198 166 112 178 180 91 5 146 184 121 222 25 85 203 128 70 17 6 134 5 70 238 194 30 55 63 127 91 251 212 58 80 50 238 97 135 3 255 216 218 102 4 157 66 80 204 135 104 34 179 243 64 112 74 79 103 129 165 74 70 244 142 13 93 123 20 155 12 244 150 242 40 152 138 84 87 118 51 128 169 248 44 53 99 90 108 108 145 242 152 250 195 165 3 112 227 44 213 76 105 71 163 35 209 87 122 97 127 79 168 29 116 9 37 52 83 55 148 49 225 192 211 19 87 93 91 113 24 2 65 93 158 150 98 247 238 101 20 31 193 183 69 5 42 234 171 21 177 52 175 9 65 182 162 2 203 151 156 40 131 202 122 105 39 26 121 189 3 3 206 85 59 249 130 83 24 142 190 232 99 27 117 105 64 170 76 165 227 234 253 77 12 230 111 251 37 194 47 149 120 219 246 95 251 187 216 117 253 81 246 130 127 48 15 230 56 184 191 4 155 17 8 241 33 17 52 83 4 62 131 180 126 114 252 24 20 53 234 232 56 89 123 124 18 209 110 138 225 112 206 27 28 114 244 95 110 225 175 253 194 160 61 237 41 123 231 249 54 246 10 166 118 4 178 75 150 74 203 114 224 196 193 186 193 197 92 26 165 246 0 72 122 176 238 212 142 239 145 170 17 117 199 142 251 138 206 217 53 183 147 173 248 12 107 39 74 16 115 68 55 145 52 134 43 154 234 31 33 113 46 43 101 210 46 106 241 205 132 162 38 185 249 172 174 40 21 185 43 55 170 40 108 243 89 193 106 13 117 188 150 80 34 27 99 69 90 143 37 125 33 4 225 35 72 75 1 22 32 5 3 222 199 210 223 69 90 152 46 161 112 50 204 170 152 222 38 219 99 52 209 14 20 165 47 207 137 230 215 171 114 211 114 34 26 145 95 163 153 204 135 147 133 131 13 228] }] } is_writable_account_cache: [true false true false false false] }) message_hash: 7r7LCFFoBHyMWpSLvK7NsdWemKq7VA13Xy6MwYc3d6cT is_simple_vote_tx: false signatures: [n59ZQqdXmE4ixZ98BUhMY6U6Sh1gQXicboP2EMUZXftEujQZyT1rt9BnsaZm3ioSCjKc4G59A9Etg7t1egBFWaA 4AosNtwNtU5GSb1YDckFRHEtXGYHULJzvdeKACPtCbiuuh6CHQvv7CEyi8fSAifdkZQdv3W3JCCKtGC3kcBg4VWT] }
* cost mode estimate: 150 + 750 + 750 = 1650
* compute-budget-processor set budget = 200,000 (alt) + 200,000 (alt) = 400,000
* actual: 150 (compute-budget) + 750 (alt) + 150 (transfer) + 150 (allocate account) + 150 (assign account) + 750 (alt) + 150 (transfer) = 2250.
tao-stones commented 3 months ago

2174 can potentially be a solution