github.com/klaytn/klaytn@v1.12.1/tests/tx_gas_calculation_test.go (about) 1 // Copyright 2019 The klaytn Authors 2 // This file is part of the klaytn library. 3 // 4 // The klaytn library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The klaytn library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>. 16 17 package tests 18 19 import ( 20 "crypto/ecdsa" 21 "math/big" 22 "strings" 23 "testing" 24 "time" 25 26 "github.com/klaytn/klaytn/accounts/abi" 27 "github.com/klaytn/klaytn/blockchain/types" 28 "github.com/klaytn/klaytn/blockchain/types/accountkey" 29 "github.com/klaytn/klaytn/common" 30 "github.com/klaytn/klaytn/common/profile" 31 "github.com/klaytn/klaytn/crypto" 32 "github.com/klaytn/klaytn/log" 33 "github.com/klaytn/klaytn/params" 34 "github.com/stretchr/testify/assert" 35 ) 36 37 var code = "0x608060405234801561001057600080fd5b506101de806100206000396000f3006080604052600436106100615763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416631a39d8ef81146100805780636353586b146100a757806370a08231146100ca578063fd6b7ef8146100f8575b3360009081526001602052604081208054349081019091558154019055005b34801561008c57600080fd5b5061009561010d565b60408051918252519081900360200190f35b6100c873ffffffffffffffffffffffffffffffffffffffff60043516610113565b005b3480156100d657600080fd5b5061009573ffffffffffffffffffffffffffffffffffffffff60043516610147565b34801561010457600080fd5b506100c8610159565b60005481565b73ffffffffffffffffffffffffffffffffffffffff1660009081526001602052604081208054349081019091558154019055565b60016020526000908152604090205481565b336000908152600160205260408120805490829055908111156101af57604051339082156108fc029083906000818181858888f193505050501561019c576101af565b3360009081526001602052604090208190555b505600a165627a7a72305820627ca46bb09478a015762806cc00c431230501118c7c26c30ac58c4e09e51c4f0029" 38 39 type TestAccount interface { 40 GetAddr() common.Address 41 GetTxKeys() []*ecdsa.PrivateKey 42 GetUpdateKeys() []*ecdsa.PrivateKey 43 GetFeeKeys() []*ecdsa.PrivateKey 44 GetNonce() uint64 45 GetAccKey() accountkey.AccountKey 46 GetValidationGas(r accountkey.RoleType) uint64 47 AddNonce() 48 SetNonce(uint64) 49 SetAddr(common.Address) 50 } 51 52 type genTransaction func(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) 53 54 func TestGasCalculation(t *testing.T) { 55 testFunctions := []struct { 56 Name string 57 genTx genTransaction 58 }{ 59 {"LegacyTransaction", genLegacyTransaction}, 60 {"AccessListTransaction", genAccessListTransaction}, 61 {"DynamicFeeTransaction", genDynamicFeeTransaction}, 62 63 {"ValueTransfer", genValueTransfer}, 64 {"ValueTransferWithMemo", genValueTransferWithMemo}, 65 {"AccountUpdate", genAccountUpdate}, 66 {"SmartContractDeploy", genSmartContractDeploy}, 67 {"SmartContractExecution", genSmartContractExecution}, 68 {"Cancel", genCancel}, 69 {"ChainDataAnchoring", genChainDataAnchoring}, 70 71 {"FeeDelegatedValueTransfer", genFeeDelegatedValueTransfer}, 72 {"FeeDelegatedValueTransferWithMemo", genFeeDelegatedValueTransferWithMemo}, 73 {"FeeDelegatedAccountUpdate", genFeeDelegatedAccountUpdate}, 74 {"FeeDelegatedSmartContractDeploy", genFeeDelegatedSmartContractDeploy}, 75 {"FeeDelegatedSmartContractExecution", genFeeDelegatedSmartContractExecution}, 76 {"FeeDelegatedCancel", genFeeDelegatedCancel}, 77 {"FeeDelegatedChainDataAnchoring", genFeeDelegatedChainDataAnchoring}, 78 79 {"FeeDelegatedWithRatioValueTransfer", genFeeDelegatedWithRatioValueTransfer}, 80 {"FeeDelegatedWithRatioValueTransferWithMemo", genFeeDelegatedWithRatioValueTransferWithMemo}, 81 {"FeeDelegatedWithRatioAccountUpdate", genFeeDelegatedWithRatioAccountUpdate}, 82 {"FeeDelegatedWithRatioSmartContractDeploy", genFeeDelegatedWithRatioSmartContractDeploy}, 83 {"FeeDelegatedWithRatioSmartContractExecution", genFeeDelegatedWithRatioSmartContractExecution}, 84 {"FeeDelegatedWithRatioCancel", genFeeDelegatedWithRatioCancel}, 85 {"FeeDelegatedWithRatioChainDataAnchoring", genFeeDelegatedWithRatioChainDataAnchoring}, 86 } 87 88 accountTypes := []struct { 89 Type string 90 account TestAccount 91 }{ 92 {"KlaytnLegacy", genKlaytnLegacyAccount(t)}, 93 {"Public", genPublicAccount(t)}, 94 {"MultiSig", genMultiSigAccount(t)}, 95 {"RoleBasedWithPublic", genRoleBasedWithPublicAccount(t)}, 96 {"RoleBasedWithMultiSig", genRoleBasedWithMultiSigAccount(t)}, 97 } 98 99 log.EnableLogForTest(log.LvlCrit, log.LvlTrace) 100 prof := profile.NewProfiler() 101 102 // Initialize blockchain 103 start := time.Now() 104 bcdata, err := NewBCData(6, 4) 105 assert.Equal(t, nil, err) 106 bcdata.bc.Config().IstanbulCompatibleBlock = big.NewInt(0) 107 bcdata.bc.Config().LondonCompatibleBlock = big.NewInt(0) 108 bcdata.bc.Config().EthTxTypeCompatibleBlock = big.NewInt(0) 109 bcdata.bc.Config().KoreCompatibleBlock = big.NewInt(0) 110 bcdata.bc.Config().ShanghaiCompatibleBlock = big.NewInt(0) 111 prof.Profile("main_init_blockchain", time.Now().Sub(start)) 112 113 defer bcdata.Shutdown() 114 115 // Initialize address-balance map for verification 116 start = time.Now() 117 accountMap := NewAccountMap() 118 if err := accountMap.Initialize(bcdata); err != nil { 119 t.Fatal(err) 120 } 121 prof.Profile("main_init_accountMap", time.Now().Sub(start)) 122 123 // reservoir account 124 var reservoir TestAccount 125 reservoir = &TestAccountType{ 126 Addr: *bcdata.addrs[0], 127 Keys: []*ecdsa.PrivateKey{bcdata.privKeys[0]}, 128 Nonce: uint64(0), 129 } 130 131 signer := types.LatestSignerForChainID(bcdata.bc.Config().ChainID) 132 gasPrice := new(big.Int).SetUint64(bcdata.bc.Config().UnitPrice) 133 134 // Preparing step. Send KLAY to a KlaytnAcount. 135 { 136 var txs types.Transactions 137 138 amount := new(big.Int).Mul(big.NewInt(3000), new(big.Int).SetUint64(params.KLAY)) 139 tx := types.NewTransaction(reservoir.GetNonce(), 140 accountTypes[0].account.GetAddr(), amount, gasLimit, gasPrice, []byte{}) 141 142 err := tx.SignWithKeys(signer, reservoir.GetTxKeys()) 143 assert.Equal(t, nil, err) 144 txs = append(txs, tx) 145 146 if err := bcdata.GenABlockWithTransactions(accountMap, txs, prof); err != nil { 147 t.Fatal(err) 148 } 149 reservoir.AddNonce() 150 } 151 152 // Preparing step. Send KLAY to KlaytnAcounts. 153 for i := 1; i < len(accountTypes); i++ { 154 // create an account which account key will be replaced to one of account key types. 155 anon, err := createAnonymousAccount(getRandomPrivateKeyString(t)) 156 assert.Equal(t, nil, err) 157 158 { 159 var txs types.Transactions 160 161 amount := new(big.Int).Mul(big.NewInt(3000), new(big.Int).SetUint64(params.KLAY)) 162 tx := types.NewTransaction(reservoir.GetNonce(), 163 anon.Addr, amount, gasLimit, gasPrice, []byte{}) 164 165 err := tx.SignWithKeys(signer, reservoir.GetTxKeys()) 166 assert.Equal(t, nil, err) 167 txs = append(txs, tx) 168 169 if err := bcdata.GenABlockWithTransactions(accountMap, txs, prof); err != nil { 170 t.Fatal(err) 171 } 172 reservoir.AddNonce() 173 } 174 175 // update the account's key 176 { 177 var txs types.Transactions 178 179 values := map[types.TxValueKeyType]interface{}{ 180 types.TxValueKeyNonce: anon.Nonce, 181 types.TxValueKeyFrom: anon.Addr, 182 types.TxValueKeyGasLimit: gasLimit, 183 types.TxValueKeyGasPrice: gasPrice, 184 types.TxValueKeyAccountKey: accountTypes[i].account.GetAccKey(), 185 } 186 tx, err := types.NewTransactionWithMap(types.TxTypeAccountUpdate, values) 187 assert.Equal(t, nil, err) 188 189 err = tx.SignWithKeys(signer, anon.Keys) 190 assert.Equal(t, nil, err) 191 192 txs = append(txs, tx) 193 194 if err := bcdata.GenABlockWithTransactions(accountMap, txs, prof); err != nil { 195 t.Fatal(err) 196 } 197 anon.AddNonce() 198 } 199 200 accountTypes[i].account.SetAddr(anon.Addr) 201 accountTypes[i].account.SetNonce(anon.Nonce) 202 } 203 204 // For smart contract 205 contract, err := createAnonymousAccount("a5c9a50938a089618167c9d67dbebc0deaffc3c76ddc6b40c2777ae59438e989") 206 contract.Addr = common.Address{} 207 208 { 209 var txs types.Transactions 210 211 amount := new(big.Int).SetUint64(0) 212 values := map[types.TxValueKeyType]interface{}{ 213 types.TxValueKeyNonce: reservoir.GetNonce(), 214 types.TxValueKeyFrom: reservoir.GetAddr(), 215 types.TxValueKeyTo: (*common.Address)(nil), 216 types.TxValueKeyAmount: amount, 217 types.TxValueKeyGasLimit: gasLimit, 218 types.TxValueKeyGasPrice: gasPrice, 219 types.TxValueKeyHumanReadable: false, 220 types.TxValueKeyData: common.FromHex(code), 221 types.TxValueKeyCodeFormat: params.CodeFormatEVM, 222 } 223 tx, err := types.NewTransactionWithMap(types.TxTypeSmartContractDeploy, values) 224 assert.Equal(t, nil, err) 225 226 err = tx.SignWithKeys(signer, reservoir.GetTxKeys()) 227 assert.Equal(t, nil, err) 228 229 txs = append(txs, tx) 230 231 if err := bcdata.GenABlockWithTransactions(accountMap, txs, prof); err != nil { 232 t.Fatal(err) 233 } 234 235 contract.Addr = crypto.CreateAddress(reservoir.GetAddr(), reservoir.GetNonce()) 236 237 reservoir.AddNonce() 238 } 239 240 for _, f := range testFunctions { 241 for _, sender := range accountTypes { 242 toAccount := reservoir 243 senderRole := accountkey.RoleTransaction 244 245 // LegacyTransaction can be used only by the KlaytnAccount with AccountKeyLegacy. 246 if sender.Type != "KlaytnLegacy" && (strings.Contains(f.Name, "Legacy") || strings.Contains(f.Name, "Access") || strings.Contains(f.Name, "Dynamic")) { 247 continue 248 } 249 250 if strings.Contains(f.Name, "AccountUpdate") { 251 senderRole = accountkey.RoleAccountUpdate 252 } 253 254 // Set contract's address with SmartContractExecution 255 if strings.Contains(f.Name, "SmartContractExecution") { 256 toAccount = contract 257 } 258 259 if !strings.Contains(f.Name, "FeeDelegated") { 260 // For NonFeeDelegated Transactions 261 Name := f.Name + "/" + sender.Type + "Sender" 262 t.Run(Name, func(t *testing.T) { 263 tx, intrinsic := f.genTx(t, signer, sender.account, toAccount, nil, gasPrice) 264 acocuntValidationGas := sender.account.GetValidationGas(senderRole) 265 testGasValidation(t, bcdata, tx, intrinsic+acocuntValidationGas) 266 }) 267 } else { 268 // For FeeDelegated(WithRatio) Transactions 269 for _, payer := range accountTypes { 270 Name := f.Name + "/" + sender.Type + "Sender/" + payer.Type + "Payer" 271 t.Run(Name, func(t *testing.T) { 272 tx, intrinsic := f.genTx(t, signer, sender.account, toAccount, payer.account, gasPrice) 273 acocuntsValidationGas := sender.account.GetValidationGas(senderRole) + payer.account.GetValidationGas(accountkey.RoleFeePayer) 274 testGasValidation(t, bcdata, tx, intrinsic+acocuntsValidationGas) 275 }) 276 } 277 } 278 279 } 280 } 281 } 282 283 func testGasValidation(t *testing.T, bcdata *BCData, tx *types.Transaction, validationGas uint64) { 284 receipt, err := applyTransaction(t, bcdata, tx) 285 assert.Equal(t, nil, err) 286 287 assert.Equal(t, receipt.Status, types.ReceiptStatusSuccessful) 288 289 assert.Equal(t, validationGas, receipt.GasUsed) 290 } 291 292 func genLegacyTransaction(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 293 intrinsic := getIntrinsicGas(types.TxTypeLegacyTransaction) 294 amount := big.NewInt(100000) 295 tx := types.NewTransaction(from.GetNonce(), to.GetAddr(), amount, gasLimit, gasPrice, []byte{}) 296 297 err := tx.SignWithKeys(signer, from.GetTxKeys()) 298 assert.Equal(t, nil, err) 299 300 return tx, intrinsic 301 } 302 303 func genAccessListTransaction(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 304 values, intrinsic := genMapForAccessListTransaction(from, to, gasPrice, types.TxTypeEthereumAccessList) 305 tx, err := types.NewTransactionWithMap(types.TxTypeEthereumAccessList, values) 306 assert.Equal(t, nil, err) 307 308 err = tx.SignWithKeys(signer, from.GetTxKeys()) 309 assert.Equal(t, nil, err) 310 311 return tx, intrinsic 312 } 313 314 func genDynamicFeeTransaction(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 315 values, intrinsic := genMapForDynamicFeeTransaction(from, to, gasPrice, types.TxTypeEthereumDynamicFee) 316 tx, err := types.NewTransactionWithMap(types.TxTypeEthereumDynamicFee, values) 317 assert.Equal(t, nil, err) 318 319 err = tx.SignWithKeys(signer, from.GetTxKeys()) 320 assert.Equal(t, nil, err) 321 322 return tx, intrinsic 323 } 324 325 func genValueTransfer(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 326 values, intrinsic := genMapForValueTransfer(from, to, gasPrice, types.TxTypeValueTransfer) 327 tx, err := types.NewTransactionWithMap(types.TxTypeValueTransfer, values) 328 assert.Equal(t, nil, err) 329 330 err = tx.SignWithKeys(signer, from.GetTxKeys()) 331 assert.Equal(t, nil, err) 332 333 return tx, intrinsic 334 } 335 336 func genFeeDelegatedValueTransfer(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 337 values, intrinsic := genMapForValueTransfer(from, to, gasPrice, types.TxTypeFeeDelegatedValueTransfer) 338 values[types.TxValueKeyFeePayer] = payer.GetAddr() 339 340 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedValueTransfer, values) 341 assert.Equal(t, nil, err) 342 343 err = tx.SignWithKeys(signer, from.GetTxKeys()) 344 assert.Equal(t, nil, err) 345 346 err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys()) 347 assert.Equal(t, nil, err) 348 349 return tx, intrinsic 350 } 351 352 func genFeeDelegatedWithRatioValueTransfer(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 353 values, intrinsic := genMapForValueTransfer(from, to, gasPrice, types.TxTypeFeeDelegatedValueTransferWithRatio) 354 values[types.TxValueKeyFeePayer] = payer.GetAddr() 355 values[types.TxValueKeyFeeRatioOfFeePayer] = types.FeeRatio(30) 356 357 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedValueTransferWithRatio, values) 358 assert.Equal(t, nil, err) 359 360 err = tx.SignWithKeys(signer, from.GetTxKeys()) 361 assert.Equal(t, nil, err) 362 363 err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys()) 364 assert.Equal(t, nil, err) 365 366 return tx, intrinsic 367 } 368 369 func genValueTransferWithMemo(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 370 values, gasPayloadWithGas := genMapForValueTransferWithMemo(from, to, gasPrice, types.TxTypeValueTransferMemo) 371 372 tx, err := types.NewTransactionWithMap(types.TxTypeValueTransferMemo, values) 373 assert.Equal(t, nil, err) 374 375 err = tx.SignWithKeys(signer, from.GetTxKeys()) 376 assert.Equal(t, nil, err) 377 378 return tx, gasPayloadWithGas 379 } 380 381 func genFeeDelegatedValueTransferWithMemo(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 382 values, gasPayloadWithGas := genMapForValueTransferWithMemo(from, to, gasPrice, types.TxTypeFeeDelegatedValueTransferMemo) 383 values[types.TxValueKeyFeePayer] = payer.GetAddr() 384 385 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedValueTransferMemo, values) 386 assert.Equal(t, nil, err) 387 388 err = tx.SignWithKeys(signer, from.GetTxKeys()) 389 assert.Equal(t, nil, err) 390 391 err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys()) 392 assert.Equal(t, nil, err) 393 394 return tx, gasPayloadWithGas 395 } 396 397 func genFeeDelegatedWithRatioValueTransferWithMemo(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 398 values, gasPayloadWithGas := genMapForValueTransferWithMemo(from, to, gasPrice, types.TxTypeFeeDelegatedValueTransferMemoWithRatio) 399 values[types.TxValueKeyFeePayer] = payer.GetAddr() 400 values[types.TxValueKeyFeeRatioOfFeePayer] = types.FeeRatio(30) 401 402 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedValueTransferMemoWithRatio, values) 403 assert.Equal(t, nil, err) 404 405 err = tx.SignWithKeys(signer, from.GetTxKeys()) 406 assert.Equal(t, nil, err) 407 408 err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys()) 409 assert.Equal(t, nil, err) 410 411 return tx, gasPayloadWithGas 412 } 413 414 func genAccountUpdate(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 415 newAccount, gasKey, _ := genNewAccountWithGas(t, from) 416 417 values, intrinsic := genMapForUpdate(from, to, gasPrice, newAccount.GetAccKey(), types.TxTypeAccountUpdate) 418 419 tx, err := types.NewTransactionWithMap(types.TxTypeAccountUpdate, values) 420 assert.Equal(t, nil, err) 421 422 err = tx.SignWithKeys(signer, from.GetUpdateKeys()) 423 assert.Equal(t, nil, err) 424 425 return tx, intrinsic + gasKey 426 } 427 428 func genFeeDelegatedAccountUpdate(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 429 newAccount, gasKey, _ := genNewAccountWithGas(t, from) 430 431 values, intrinsic := genMapForUpdate(from, to, gasPrice, newAccount.GetAccKey(), types.TxTypeFeeDelegatedAccountUpdate) 432 values[types.TxValueKeyFeePayer] = payer.GetAddr() 433 434 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedAccountUpdate, values) 435 assert.Equal(t, nil, err) 436 437 err = tx.SignWithKeys(signer, from.GetUpdateKeys()) 438 assert.Equal(t, nil, err) 439 440 err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys()) 441 assert.Equal(t, nil, err) 442 443 return tx, intrinsic + gasKey 444 } 445 446 func genFeeDelegatedWithRatioAccountUpdate(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 447 newAccount, gasKey, _ := genNewAccountWithGas(t, from) 448 449 values, intrinsic := genMapForUpdate(from, to, gasPrice, newAccount.GetAccKey(), types.TxTypeFeeDelegatedAccountUpdateWithRatio) 450 values[types.TxValueKeyFeePayer] = payer.GetAddr() 451 values[types.TxValueKeyFeeRatioOfFeePayer] = types.FeeRatio(30) 452 453 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedAccountUpdateWithRatio, values) 454 assert.Equal(t, nil, err) 455 456 err = tx.SignWithKeys(signer, from.GetUpdateKeys()) 457 assert.Equal(t, nil, err) 458 459 err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys()) 460 assert.Equal(t, nil, err) 461 462 return tx, intrinsic + gasKey 463 } 464 465 func genSmartContractDeploy(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 466 values, intrinsicGas := genMapForDeploy(from, to, gasPrice, types.TxTypeSmartContractDeploy) 467 if values == nil { 468 t.Fatalf("failed to genMapForDeploy") 469 } 470 471 tx, err := types.NewTransactionWithMap(types.TxTypeSmartContractDeploy, values) 472 assert.Equal(t, nil, err) 473 474 err = tx.SignWithKeys(signer, from.GetTxKeys()) 475 assert.Equal(t, nil, err) 476 477 return tx, intrinsicGas 478 } 479 480 func genFeeDelegatedSmartContractDeploy(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 481 values, intrinsicGas := genMapForDeploy(from, to, gasPrice, types.TxTypeFeeDelegatedSmartContractDeploy) 482 if values == nil { 483 t.Fatalf("failed to genMapForDeploy") 484 } 485 486 values[types.TxValueKeyFeePayer] = payer.GetAddr() 487 488 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedSmartContractDeploy, values) 489 assert.Equal(t, nil, err) 490 491 err = tx.SignWithKeys(signer, from.GetTxKeys()) 492 assert.Equal(t, nil, err) 493 494 err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys()) 495 assert.Equal(t, nil, err) 496 497 return tx, intrinsicGas 498 } 499 500 func genFeeDelegatedWithRatioSmartContractDeploy(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 501 values, intrinsicGas := genMapForDeploy(from, to, gasPrice, types.TxTypeFeeDelegatedSmartContractDeployWithRatio) 502 if values == nil { 503 t.Fatalf("failed to genMapForDeploy") 504 } 505 506 values[types.TxValueKeyFeePayer] = payer.GetAddr() 507 values[types.TxValueKeyFeeRatioOfFeePayer] = types.FeeRatio(30) 508 509 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedSmartContractDeployWithRatio, values) 510 assert.Equal(t, nil, err) 511 512 err = tx.SignWithKeys(signer, from.GetTxKeys()) 513 assert.Equal(t, nil, err) 514 515 err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys()) 516 assert.Equal(t, nil, err) 517 518 return tx, intrinsicGas 519 } 520 521 func genSmartContractExecution(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 522 values, intrinsicGas := genMapForExecution(from, to, gasPrice, types.TxTypeSmartContractExecution) 523 if values == nil { 524 t.Fatalf("failed to genMapForExecution") 525 } 526 527 tx, err := types.NewTransactionWithMap(types.TxTypeSmartContractExecution, values) 528 529 assert.Equal(t, nil, err) 530 531 err = tx.SignWithKeys(signer, from.GetTxKeys()) 532 assert.Equal(t, nil, err) 533 534 return tx, intrinsicGas 535 } 536 537 func genFeeDelegatedSmartContractExecution(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 538 values, intrinsicGas := genMapForExecution(from, to, gasPrice, types.TxTypeFeeDelegatedSmartContractExecution) 539 if values == nil { 540 t.Fatalf("failed to genMapForExecution") 541 } 542 543 values[types.TxValueKeyFeePayer] = payer.GetAddr() 544 545 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedSmartContractExecution, values) 546 547 assert.Equal(t, nil, err) 548 549 err = tx.SignWithKeys(signer, from.GetTxKeys()) 550 assert.Equal(t, nil, err) 551 552 err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys()) 553 assert.Equal(t, nil, err) 554 555 return tx, intrinsicGas 556 } 557 558 func genFeeDelegatedWithRatioSmartContractExecution(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 559 values, intrinsicGas := genMapForExecution(from, to, gasPrice, types.TxTypeFeeDelegatedSmartContractExecutionWithRatio) 560 if values == nil { 561 t.Fatalf("failed to genMapForExecution") 562 } 563 564 values[types.TxValueKeyFeePayer] = payer.GetAddr() 565 values[types.TxValueKeyFeeRatioOfFeePayer] = types.FeeRatio(30) 566 567 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedSmartContractExecutionWithRatio, values) 568 569 assert.Equal(t, nil, err) 570 571 err = tx.SignWithKeys(signer, from.GetTxKeys()) 572 assert.Equal(t, nil, err) 573 574 err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys()) 575 assert.Equal(t, nil, err) 576 577 return tx, intrinsicGas 578 } 579 580 func genCancel(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 581 values, intrinsic := genMapForCancel(from, gasPrice, types.TxTypeCancel) 582 583 tx, err := types.NewTransactionWithMap(types.TxTypeCancel, values) 584 assert.Equal(t, nil, err) 585 586 err = tx.SignWithKeys(signer, from.GetTxKeys()) 587 assert.Equal(t, nil, err) 588 589 return tx, intrinsic 590 } 591 592 func genFeeDelegatedCancel(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 593 values, intrinsic := genMapForCancel(from, gasPrice, types.TxTypeFeeDelegatedCancel) 594 values[types.TxValueKeyFeePayer] = payer.GetAddr() 595 596 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedCancel, values) 597 assert.Equal(t, nil, err) 598 599 err = tx.SignWithKeys(signer, from.GetTxKeys()) 600 assert.Equal(t, nil, err) 601 602 err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys()) 603 assert.Equal(t, nil, err) 604 605 return tx, intrinsic 606 } 607 608 func genFeeDelegatedWithRatioCancel(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 609 values, intrinsic := genMapForCancel(from, gasPrice, types.TxTypeFeeDelegatedCancelWithRatio) 610 values[types.TxValueKeyFeePayer] = payer.GetAddr() 611 values[types.TxValueKeyFeeRatioOfFeePayer] = types.FeeRatio(30) 612 613 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedCancelWithRatio, values) 614 assert.Equal(t, nil, err) 615 616 err = tx.SignWithKeys(signer, from.GetTxKeys()) 617 assert.Equal(t, nil, err) 618 619 err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys()) 620 assert.Equal(t, nil, err) 621 622 return tx, intrinsic 623 } 624 625 func genChainDataAnchoring(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 626 values, intrinsic := genMapForChainDataAnchoring(from, gasPrice, types.TxTypeChainDataAnchoring) 627 628 tx, err := types.NewTransactionWithMap(types.TxTypeChainDataAnchoring, values) 629 assert.Equal(t, nil, err) 630 631 err = tx.SignWithKeys(signer, from.GetTxKeys()) 632 assert.Equal(t, nil, err) 633 634 return tx, intrinsic 635 } 636 637 func genFeeDelegatedChainDataAnchoring(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 638 values, intrinsic := genMapForChainDataAnchoring(from, gasPrice, types.TxTypeFeeDelegatedChainDataAnchoring) 639 values[types.TxValueKeyFeePayer] = payer.GetAddr() 640 641 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedChainDataAnchoring, values) 642 assert.Equal(t, nil, err) 643 644 err = tx.SignWithKeys(signer, from.GetTxKeys()) 645 assert.Equal(t, nil, err) 646 647 err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys()) 648 assert.Equal(t, nil, err) 649 650 return tx, intrinsic 651 } 652 653 func genFeeDelegatedWithRatioChainDataAnchoring(t *testing.T, signer types.Signer, from TestAccount, to TestAccount, payer TestAccount, gasPrice *big.Int) (*types.Transaction, uint64) { 654 values, intrinsic := genMapForChainDataAnchoring(from, gasPrice, types.TxTypeFeeDelegatedChainDataAnchoringWithRatio) 655 values[types.TxValueKeyFeePayer] = payer.GetAddr() 656 values[types.TxValueKeyFeeRatioOfFeePayer] = types.FeeRatio(30) 657 658 tx, err := types.NewTransactionWithMap(types.TxTypeFeeDelegatedChainDataAnchoringWithRatio, values) 659 assert.Equal(t, nil, err) 660 661 err = tx.SignWithKeys(signer, from.GetTxKeys()) 662 assert.Equal(t, nil, err) 663 664 err = tx.SignFeePayerWithKeys(signer, payer.GetFeeKeys()) 665 assert.Equal(t, nil, err) 666 667 return tx, intrinsic 668 } 669 670 // Generate map functions 671 func genMapForLegacyTransaction(from TestAccount, to TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) { 672 intrinsic := getIntrinsicGas(txType) 673 amount := big.NewInt(100000) 674 data := []byte{0x11, 0x22} 675 gasPayload := uint64(len(data)) * params.TxDataGas 676 677 values := map[types.TxValueKeyType]interface{}{ 678 types.TxValueKeyNonce: from.GetNonce(), 679 types.TxValueKeyTo: to.GetAddr(), 680 types.TxValueKeyAmount: amount, 681 types.TxValueKeyData: data, 682 types.TxValueKeyGasLimit: gasLimit, 683 types.TxValueKeyGasPrice: gasPrice, 684 } 685 return values, intrinsic + gasPayload 686 } 687 688 func genMapForAccessListTransaction(from TestAccount, to TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) { 689 intrinsic := getIntrinsicGas(txType) 690 amount := big.NewInt(100000) 691 data := []byte{0x11, 0x22} 692 gasPayload := uint64(len(data)) * params.TxDataGas 693 accessList := types.AccessList{{Address: common.HexToAddress("0x0000000000000000000000000000000000000001"), StorageKeys: []common.Hash{{0}}}} 694 toAddress := to.GetAddr() 695 696 gasPayload += uint64(len(accessList)) * params.TxAccessListAddressGas 697 gasPayload += uint64(accessList.StorageKeys()) * params.TxAccessListStorageKeyGas 698 699 values := map[types.TxValueKeyType]interface{}{ 700 types.TxValueKeyNonce: from.GetNonce(), 701 types.TxValueKeyTo: &toAddress, 702 types.TxValueKeyAmount: amount, 703 types.TxValueKeyData: data, 704 types.TxValueKeyGasLimit: gasLimit, 705 types.TxValueKeyGasPrice: gasPrice, 706 types.TxValueKeyAccessList: accessList, 707 types.TxValueKeyChainID: big.NewInt(1), 708 } 709 return values, intrinsic + gasPayload 710 } 711 712 func genMapForDynamicFeeTransaction(from TestAccount, to TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) { 713 intrinsic := getIntrinsicGas(txType) 714 amount := big.NewInt(100000) 715 data := []byte{0x11, 0x22} 716 gasPayload := uint64(len(data)) * params.TxDataGas 717 accessList := types.AccessList{{Address: common.HexToAddress("0x0000000000000000000000000000000000000001"), StorageKeys: []common.Hash{{0}}}} 718 toAddress := to.GetAddr() 719 720 gasPayload += uint64(len(accessList)) * params.TxAccessListAddressGas 721 gasPayload += uint64(accessList.StorageKeys()) * params.TxAccessListStorageKeyGas 722 723 values := map[types.TxValueKeyType]interface{}{ 724 types.TxValueKeyNonce: from.GetNonce(), 725 types.TxValueKeyTo: &toAddress, 726 types.TxValueKeyAmount: amount, 727 types.TxValueKeyData: data, 728 types.TxValueKeyGasLimit: gasLimit, 729 types.TxValueKeyGasFeeCap: gasPrice, 730 types.TxValueKeyGasTipCap: gasPrice, 731 types.TxValueKeyAccessList: accessList, 732 types.TxValueKeyChainID: big.NewInt(1), 733 } 734 return values, intrinsic + gasPayload 735 } 736 737 func genMapForValueTransfer(from TestAccount, to TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) { 738 intrinsic := getIntrinsicGas(txType) 739 amount := big.NewInt(100000) 740 741 values := map[types.TxValueKeyType]interface{}{ 742 types.TxValueKeyNonce: from.GetNonce(), 743 types.TxValueKeyFrom: from.GetAddr(), 744 types.TxValueKeyTo: to.GetAddr(), 745 types.TxValueKeyAmount: amount, 746 types.TxValueKeyGasLimit: gasLimit, 747 types.TxValueKeyGasPrice: gasPrice, 748 } 749 return values, intrinsic 750 } 751 752 func genMapForValueTransferWithMemo(from TestAccount, to TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) { 753 intrinsic := getIntrinsicGas(txType) 754 755 nonZeroData := []byte{1, 2, 3, 4} 756 zeroData := []byte{0, 0, 0, 0} 757 758 data := append(nonZeroData, zeroData...) 759 760 amount := big.NewInt(100000) 761 762 values := map[types.TxValueKeyType]interface{}{ 763 types.TxValueKeyNonce: from.GetNonce(), 764 types.TxValueKeyFrom: from.GetAddr(), 765 types.TxValueKeyTo: to.GetAddr(), 766 types.TxValueKeyAmount: amount, 767 types.TxValueKeyGasLimit: gasLimit, 768 types.TxValueKeyGasPrice: gasPrice, 769 types.TxValueKeyData: data, 770 } 771 772 gasPayload := uint64(len(data)) * params.TxDataGas 773 774 return values, intrinsic + gasPayload 775 } 776 777 func genMapForCreate(from TestAccount, to TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) { 778 intrinsic := getIntrinsicGas(txType) 779 amount := big.NewInt(0) 780 781 values := map[types.TxValueKeyType]interface{}{ 782 types.TxValueKeyNonce: from.GetNonce(), 783 types.TxValueKeyFrom: from.GetAddr(), 784 types.TxValueKeyTo: to.GetAddr(), 785 types.TxValueKeyAmount: amount, 786 types.TxValueKeyGasLimit: gasLimit, 787 types.TxValueKeyGasPrice: gasPrice, 788 types.TxValueKeyHumanReadable: false, 789 types.TxValueKeyAccountKey: to.GetAccKey(), 790 } 791 return values, intrinsic + uint64(len(to.GetTxKeys()))*params.TxAccountCreationGasPerKey 792 } 793 794 func genMapForUpdate(from TestAccount, to TestAccount, gasPrice *big.Int, newKeys accountkey.AccountKey, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) { 795 intrinsic := getIntrinsicGas(txType) 796 797 values := map[types.TxValueKeyType]interface{}{ 798 types.TxValueKeyNonce: from.GetNonce(), 799 types.TxValueKeyFrom: from.GetAddr(), 800 types.TxValueKeyGasLimit: gasLimit, 801 types.TxValueKeyGasPrice: gasPrice, 802 types.TxValueKeyAccountKey: newKeys, 803 } 804 return values, intrinsic 805 } 806 807 func genMapForDeploy(from TestAccount, to TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) { 808 amount := new(big.Int).SetUint64(0) 809 values := map[types.TxValueKeyType]interface{}{ 810 types.TxValueKeyNonce: from.GetNonce(), 811 types.TxValueKeyAmount: amount, 812 types.TxValueKeyGasLimit: gasLimit, 813 types.TxValueKeyGasPrice: gasPrice, 814 types.TxValueKeyHumanReadable: false, 815 types.TxValueKeyFrom: from.GetAddr(), 816 types.TxValueKeyData: common.FromHex(code), 817 types.TxValueKeyCodeFormat: params.CodeFormatEVM, 818 types.TxValueKeyTo: (*common.Address)(nil), 819 } 820 821 intrinsicGas := getIntrinsicGas(txType) 822 intrinsicGas += uint64(0x175fd) 823 824 gasPayloadWithGas, err := types.IntrinsicGasPayload(intrinsicGas, common.FromHex(code), true, params.Rules{IsShanghai: true}) 825 if err != nil { 826 return nil, 0 827 } 828 829 return values, gasPayloadWithGas 830 } 831 832 func genMapForExecution(from TestAccount, to TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) { 833 abiStr := `[{"constant":true,"inputs":[],"name":"totalAmount","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"receiver","type":"address"}],"name":"reward","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[],"name":"safeWithdrawal","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"payable":true,"stateMutability":"payable","type":"fallback"}]` 834 abii, err := abi.JSON(strings.NewReader(string(abiStr))) 835 if err != nil { 836 return nil, 0 837 } 838 839 data, err := abii.Pack("reward", to.GetAddr()) 840 if err != nil { 841 return nil, 0 842 } 843 844 amount := new(big.Int).SetUint64(10) 845 846 values := map[types.TxValueKeyType]interface{}{ 847 types.TxValueKeyNonce: from.GetNonce(), 848 types.TxValueKeyFrom: from.GetAddr(), 849 types.TxValueKeyTo: to.GetAddr(), 850 types.TxValueKeyAmount: amount, 851 types.TxValueKeyGasLimit: gasLimit, 852 types.TxValueKeyGasPrice: gasPrice, 853 types.TxValueKeyData: data, 854 } 855 856 intrinsicGas := getIntrinsicGas(txType) 857 intrinsicGas += uint64(0x9ec4) 858 859 gasPayloadWithGas, err := types.IntrinsicGasPayload(intrinsicGas, data, false, params.Rules{IsShanghai: false}) 860 if err != nil { 861 return nil, 0 862 } 863 864 return values, gasPayloadWithGas 865 } 866 867 func genMapForCancel(from TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) { 868 intrinsic := getIntrinsicGas(txType) 869 870 values := map[types.TxValueKeyType]interface{}{ 871 types.TxValueKeyNonce: from.GetNonce(), 872 types.TxValueKeyFrom: from.GetAddr(), 873 types.TxValueKeyGasLimit: gasLimit, 874 types.TxValueKeyGasPrice: gasPrice, 875 } 876 return values, intrinsic 877 } 878 879 func genMapForChainDataAnchoring(from TestAccount, gasPrice *big.Int, txType types.TxType) (map[types.TxValueKeyType]interface{}, uint64) { 880 intrinsic := getIntrinsicGas(txType) 881 data := []byte{0x11, 0x22} 882 gasPayload := uint64(len(data)) * params.TxDataGas 883 884 values := map[types.TxValueKeyType]interface{}{ 885 types.TxValueKeyNonce: from.GetNonce(), 886 types.TxValueKeyFrom: from.GetAddr(), 887 types.TxValueKeyGasLimit: gasLimit, 888 types.TxValueKeyGasPrice: gasPrice, 889 types.TxValueKeyAnchoredData: data, 890 } 891 return values, intrinsic + gasPayload 892 } 893 894 func genKlaytnLegacyAccount(t *testing.T) TestAccount { 895 // For KlaytnLegacy 896 klaytnLegacy, err := createAnonymousAccount(getRandomPrivateKeyString(t)) 897 assert.Equal(t, nil, err) 898 899 return klaytnLegacy 900 } 901 902 func genPublicAccount(t *testing.T) TestAccount { 903 // For AccountKeyPublic 904 var newAddress common.Address 905 newAddress.SetBytesFromFront([]byte(getRandomPrivateKeyString(t))) 906 907 publicAccount, err := createDecoupledAccount(getRandomPrivateKeyString(t), newAddress) 908 assert.Equal(t, nil, err) 909 910 return publicAccount 911 } 912 913 func genMultiSigAccount(t *testing.T) TestAccount { 914 // For AccountKeyWeightedMultiSig 915 var newAddress common.Address 916 newAddress.SetBytesFromFront([]byte(getRandomPrivateKeyString(t))) 917 918 multisigAccount, err := createMultisigAccount(uint(2), 919 []uint{1, 1, 1}, 920 []string{getRandomPrivateKeyString(t), getRandomPrivateKeyString(t), getRandomPrivateKeyString(t)}, newAddress) 921 assert.Equal(t, nil, err) 922 923 return multisigAccount 924 } 925 926 func genRoleBasedWithPublicAccount(t *testing.T) TestAccount { 927 // For AccountKeyRoleBased With AccountKeyPublic 928 var roleBasedWithPublicAddr common.Address 929 roleBasedWithPublicAddr.SetBytesFromFront([]byte(getRandomPrivateKeyString(t))) 930 931 roleBasedWithPublic, err := createRoleBasedAccountWithAccountKeyPublic( 932 []string{getRandomPrivateKeyString(t), getRandomPrivateKeyString(t), getRandomPrivateKeyString(t)}, roleBasedWithPublicAddr) 933 assert.Equal(t, nil, err) 934 935 return roleBasedWithPublic 936 } 937 938 func genRoleBasedWithMultiSigAccount(t *testing.T) TestAccount { 939 // For AccountKeyRoleBased With AccountKeyWeightedMultiSig 940 p := genMultiSigParamForRoleBased(t) 941 942 var roleBasedWithMultiSigAddr common.Address 943 roleBasedWithMultiSigAddr.SetBytesFromFront([]byte(getRandomPrivateKeyString(t))) 944 945 roleBasedWithMultiSig, err := createRoleBasedAccountWithAccountKeyWeightedMultiSig( 946 []TestCreateMultisigAccountParam{p[0], p[1], p[2]}, roleBasedWithMultiSigAddr) 947 assert.Equal(t, nil, err) 948 949 return roleBasedWithMultiSig 950 } 951 952 // Generate new Account functions for testing AccountUpdate 953 func genNewAccountWithGas(t *testing.T, testAccount TestAccount) (TestAccount, uint64, bool) { 954 var newAccount TestAccount 955 gas := uint64(0) 956 readableGas := uint64(0) 957 readable := false 958 959 // AccountKeyLegacy 960 if testAccount.GetAccKey() == nil || testAccount.GetAccKey().Type() == accountkey.AccountKeyTypeLegacy { 961 anon, err := createAnonymousAccount(getRandomPrivateKeyString(t)) 962 assert.Equal(t, nil, err) 963 964 return anon, gas + readableGas, readable 965 } 966 967 // newAccount 968 newAccount, err := createAnonymousAccount(getRandomPrivateKeyString(t)) 969 assert.Equal(t, nil, err) 970 971 switch testAccount.GetAccKey().Type() { 972 case accountkey.AccountKeyTypePublic: 973 publicAccount, err := createDecoupledAccount(getRandomPrivateKeyString(t), newAccount.GetAddr()) 974 assert.Equal(t, nil, err) 975 976 newAccount = publicAccount 977 gas += uint64(len(newAccount.GetTxKeys())) * params.TxAccountCreationGasPerKey 978 case accountkey.AccountKeyTypeWeightedMultiSig: 979 multisigAccount, err := createMultisigAccount(uint(2), []uint{1, 1, 1}, 980 []string{getRandomPrivateKeyString(t), getRandomPrivateKeyString(t), getRandomPrivateKeyString(t)}, newAccount.GetAddr()) 981 assert.Equal(t, nil, err) 982 983 newAccount = multisigAccount 984 gas += uint64(len(newAccount.GetTxKeys())) * params.TxAccountCreationGasPerKey 985 case accountkey.AccountKeyTypeRoleBased: 986 p := genMultiSigParamForRoleBased(t) 987 988 newRoleBasedWithMultiSig, err := createRoleBasedAccountWithAccountKeyWeightedMultiSig( 989 []TestCreateMultisigAccountParam{p[0], p[1], p[2]}, newAccount.GetAddr()) 990 assert.Equal(t, nil, err) 991 992 newAccount = newRoleBasedWithMultiSig 993 gas = uint64(len(newAccount.GetTxKeys())) * params.TxAccountCreationGasPerKey 994 gas += uint64(len(newAccount.GetUpdateKeys())) * params.TxAccountCreationGasPerKey 995 gas += uint64(len(newAccount.GetFeeKeys())) * params.TxAccountCreationGasPerKey 996 } 997 998 return newAccount, gas + readableGas, readable 999 } 1000 1001 func getRandomPrivateKeyString(t *testing.T) string { 1002 key, err := crypto.GenerateKey() 1003 assert.Equal(t, nil, err) 1004 keyBytes := crypto.FromECDSA(key) 1005 1006 return common.Bytes2Hex(keyBytes) 1007 } 1008 1009 // Return multisig parameters for creating RoleBased with MultiSig 1010 func genMultiSigParamForRoleBased(t *testing.T) []TestCreateMultisigAccountParam { 1011 var params []TestCreateMultisigAccountParam 1012 param1 := TestCreateMultisigAccountParam{ 1013 Threshold: uint(2), 1014 Weights: []uint{1, 1, 1}, 1015 PrvKeys: []string{getRandomPrivateKeyString(t), getRandomPrivateKeyString(t), getRandomPrivateKeyString(t)}, 1016 } 1017 params = append(params, param1) 1018 1019 param2 := TestCreateMultisigAccountParam{ 1020 Threshold: uint(2), 1021 Weights: []uint{1, 1, 1}, 1022 PrvKeys: []string{getRandomPrivateKeyString(t), getRandomPrivateKeyString(t), getRandomPrivateKeyString(t)}, 1023 } 1024 params = append(params, param2) 1025 1026 param3 := TestCreateMultisigAccountParam{ 1027 Threshold: uint(2), 1028 Weights: []uint{1, 1, 1}, 1029 PrvKeys: []string{getRandomPrivateKeyString(t), getRandomPrivateKeyString(t), getRandomPrivateKeyString(t)}, 1030 } 1031 params = append(params, param3) 1032 1033 return params 1034 } 1035 1036 func getIntrinsicGas(txType types.TxType) uint64 { 1037 var intrinsic uint64 1038 1039 switch txType { 1040 case types.TxTypeLegacyTransaction: 1041 intrinsic = params.TxGas 1042 case types.TxTypeEthereumAccessList: 1043 intrinsic = params.TxGas 1044 case types.TxTypeEthereumDynamicFee: 1045 intrinsic = params.TxGas 1046 case types.TxTypeValueTransfer: 1047 intrinsic = params.TxGasValueTransfer 1048 case types.TxTypeFeeDelegatedValueTransfer: 1049 intrinsic = params.TxGasValueTransfer + params.TxGasFeeDelegated 1050 case types.TxTypeFeeDelegatedValueTransferWithRatio: 1051 intrinsic = params.TxGasValueTransfer + params.TxGasFeeDelegatedWithRatio 1052 case types.TxTypeValueTransferMemo: 1053 intrinsic = params.TxGasValueTransfer 1054 case types.TxTypeFeeDelegatedValueTransferMemo: 1055 intrinsic = params.TxGasValueTransfer + params.TxGasFeeDelegated 1056 case types.TxTypeFeeDelegatedValueTransferMemoWithRatio: 1057 intrinsic = params.TxGasValueTransfer + params.TxGasFeeDelegatedWithRatio 1058 case types.TxTypeAccountUpdate: 1059 intrinsic = params.TxGasAccountUpdate 1060 case types.TxTypeFeeDelegatedAccountUpdate: 1061 intrinsic = params.TxGasAccountUpdate + params.TxGasFeeDelegated 1062 case types.TxTypeFeeDelegatedAccountUpdateWithRatio: 1063 intrinsic = params.TxGasAccountUpdate + params.TxGasFeeDelegatedWithRatio 1064 case types.TxTypeSmartContractDeploy: 1065 intrinsic = params.TxGasContractCreation 1066 case types.TxTypeFeeDelegatedSmartContractDeploy: 1067 intrinsic = params.TxGasContractCreation + params.TxGasFeeDelegated 1068 case types.TxTypeFeeDelegatedSmartContractDeployWithRatio: 1069 intrinsic = params.TxGasContractCreation + params.TxGasFeeDelegatedWithRatio 1070 case types.TxTypeSmartContractExecution: 1071 intrinsic = params.TxGasContractExecution 1072 case types.TxTypeFeeDelegatedSmartContractExecution: 1073 intrinsic = params.TxGasContractExecution + params.TxGasFeeDelegated 1074 case types.TxTypeFeeDelegatedSmartContractExecutionWithRatio: 1075 intrinsic = params.TxGasContractExecution + params.TxGasFeeDelegatedWithRatio 1076 case types.TxTypeChainDataAnchoring: 1077 intrinsic = params.TxChainDataAnchoringGas 1078 case types.TxTypeFeeDelegatedChainDataAnchoring: 1079 intrinsic = params.TxChainDataAnchoringGas + params.TxGasFeeDelegated 1080 case types.TxTypeFeeDelegatedChainDataAnchoringWithRatio: 1081 intrinsic = params.TxChainDataAnchoringGas + params.TxGasFeeDelegatedWithRatio 1082 case types.TxTypeCancel: 1083 intrinsic = params.TxGasCancel 1084 case types.TxTypeFeeDelegatedCancel: 1085 intrinsic = params.TxGasCancel + params.TxGasFeeDelegated 1086 case types.TxTypeFeeDelegatedCancelWithRatio: 1087 intrinsic = params.TxGasCancel + params.TxGasFeeDelegatedWithRatio 1088 } 1089 1090 return intrinsic 1091 } 1092 1093 // Implement TestAccount interface for TestAccountType 1094 func (t *TestAccountType) GetAddr() common.Address { 1095 return t.Addr 1096 } 1097 1098 func (t *TestAccountType) GetTxKeys() []*ecdsa.PrivateKey { 1099 return t.Keys 1100 } 1101 1102 func (t *TestAccountType) GetUpdateKeys() []*ecdsa.PrivateKey { 1103 return t.Keys 1104 } 1105 1106 func (t *TestAccountType) GetFeeKeys() []*ecdsa.PrivateKey { 1107 return t.Keys 1108 } 1109 1110 func (t *TestAccountType) GetNonce() uint64 { 1111 return t.Nonce 1112 } 1113 1114 func (t *TestAccountType) GetAccKey() accountkey.AccountKey { 1115 return t.AccKey 1116 } 1117 1118 func (t *TestAccountType) SetNonce(nonce uint64) { 1119 t.Nonce = nonce 1120 } 1121 1122 func (t *TestAccountType) SetAddr(addr common.Address) { 1123 t.Addr = addr 1124 } 1125 1126 // Return SigValidationGas depends on AccountType 1127 func (t *TestAccountType) GetValidationGas(r accountkey.RoleType) uint64 { 1128 if t.GetAccKey() == nil { 1129 return 0 1130 } 1131 1132 var gas uint64 1133 1134 switch t.GetAccKey().Type() { 1135 case accountkey.AccountKeyTypeLegacy: 1136 gas = 0 1137 case accountkey.AccountKeyTypePublic: 1138 gas = (1 - 1) * params.TxValidationGasPerKey 1139 case accountkey.AccountKeyTypeWeightedMultiSig: 1140 gas = uint64(len(t.GetTxKeys())-1) * params.TxValidationGasPerKey 1141 } 1142 1143 return gas 1144 } 1145 1146 func (t *TestAccountType) AddNonce() { 1147 t.Nonce += 1 1148 } 1149 1150 // Implement TestAccount interface for TestRoleBasedAccountType 1151 func (t *TestRoleBasedAccountType) GetAddr() common.Address { 1152 return t.Addr 1153 } 1154 1155 func (t *TestRoleBasedAccountType) GetTxKeys() []*ecdsa.PrivateKey { 1156 return t.TxKeys 1157 } 1158 1159 func (t *TestRoleBasedAccountType) GetUpdateKeys() []*ecdsa.PrivateKey { 1160 return t.UpdateKeys 1161 } 1162 1163 func (t *TestRoleBasedAccountType) GetFeeKeys() []*ecdsa.PrivateKey { 1164 return t.FeeKeys 1165 } 1166 1167 func (t *TestRoleBasedAccountType) GetNonce() uint64 { 1168 return t.Nonce 1169 } 1170 1171 func (t *TestRoleBasedAccountType) GetAccKey() accountkey.AccountKey { 1172 return t.AccKey 1173 } 1174 1175 func (t *TestRoleBasedAccountType) SetNonce(nonce uint64) { 1176 t.Nonce = nonce 1177 } 1178 1179 func (t *TestRoleBasedAccountType) SetAddr(addr common.Address) { 1180 t.Addr = addr 1181 } 1182 1183 // Return SigValidationGas depends on AccountType 1184 func (t *TestRoleBasedAccountType) GetValidationGas(r accountkey.RoleType) uint64 { 1185 if t.GetAccKey() == nil { 1186 return 0 1187 } 1188 1189 var gas uint64 1190 1191 switch r { 1192 case accountkey.RoleTransaction: 1193 gas = uint64(len(t.GetTxKeys())-1) * params.TxValidationGasPerKey 1194 case accountkey.RoleAccountUpdate: 1195 gas = uint64(len(t.GetUpdateKeys())-1) * params.TxValidationGasPerKey 1196 case accountkey.RoleFeePayer: 1197 gas = uint64(len(t.GetFeeKeys())-1) * params.TxValidationGasPerKey 1198 } 1199 1200 return gas 1201 } 1202 1203 func (t *TestRoleBasedAccountType) AddNonce() { 1204 t.Nonce += 1 1205 }