From f8f9b8a581e8590a8cfd6a3113cc822d0eb39194 Mon Sep 17 00:00:00 2001 From: nk_ysg Date: Fri, 4 Apr 2025 09:07:05 +0800 Subject: [PATCH] optimize some code prealloc and early error returns --- chains/ethereum/proposal_data.go | 2 +- cmd/soltool/create_bridge_action.go | 6 +++++- cmd/soltool/set_fee_amount.go | 9 +++++---- cmd/soltool/set_fee_receiver.go | 9 +++++---- cmd/soltool/set_resource_id.go | 9 +++++---- cmd/soltool/set_support_chainId.go | 9 +++++---- cmd/solvault/vault_add.go | 2 +- shared/ethereum/erc20.go | 2 +- 8 files changed, 28 insertions(+), 20 deletions(-) diff --git a/chains/ethereum/proposal_data.go b/chains/ethereum/proposal_data.go index 6531e03..573a326 100644 --- a/chains/ethereum/proposal_data.go +++ b/chains/ethereum/proposal_data.go @@ -11,7 +11,7 @@ import ( // constructErc20ProposalData returns the bytes to construct a proposal suitable for Erc20 func ConstructErc20ProposalData(amount []byte, recipient []byte) []byte { - var data []byte + data := make([]byte, 0, 64+len(recipient)) data = append(data, common.LeftPadBytes(amount, 32)...) // amount (uint256) recipientLen := big.NewInt(int64(len(recipient))).Bytes() diff --git a/cmd/soltool/create_bridge_action.go b/cmd/soltool/create_bridge_action.go index ddfc80d..a1e8a4e 100644 --- a/cmd/soltool/create_bridge_action.go +++ b/cmd/soltool/create_bridge_action.go @@ -55,7 +55,11 @@ func createBridgeAccountAction(ctx *cli.Context) error { } fmt.Println("\nbridgePdaPubkey: ", PdaPubkey.ToBase58()) - owners := make([]solCommon.PublicKey, 0) + ownersLen := 1 + len(pc.OtherFeeAccountPubkey) + if ownersLen < int(pc.Threshold) { + return fmt.Errorf("owner len < threshold") + } + owners := make([]solCommon.PublicKey, 0, ownersLen) owners = append(owners, FeeAccount.PublicKey) for _, account := range pc.OtherFeeAccountPubkey { a := solTypes.AccountFromPrivateKeyBytes(privKeyMap[account]) diff --git a/cmd/soltool/set_fee_amount.go b/cmd/soltool/set_fee_amount.go index b9c82f5..0a751ca 100644 --- a/cmd/soltool/set_fee_amount.go +++ b/cmd/soltool/set_fee_amount.go @@ -48,15 +48,16 @@ func setFeeAmountAction(ctx *cli.Context) error { AdminAccount := solTypes.AccountFromPrivateKeyBytes(privKeyMap[pc.AdminAccountPubkey]) BridgeProgramId := solCommon.PublicKeyFromString(pc.BridgeProgramId) - owners := make([]solCommon.PublicKey, 0) + ownersLen := 1 + len(pc.OtherFeeAccountPubkey) + if ownersLen < int(pc.Threshold) { + return fmt.Errorf("owner len < threshold") + } + owners := make([]solCommon.PublicKey, 0, ownersLen) owners = append(owners, FeeAccount.PublicKey) for _, account := range pc.OtherFeeAccountPubkey { a := solTypes.AccountFromPrivateKeyBytes(privKeyMap[account]) owners = append(owners, a.PublicKey) } - if len(owners) < int(pc.Threshold) { - return fmt.Errorf("owner len < threshold") - } //start inter with solana chain c := solClient.NewClient([]string{pc.Endpoint}) diff --git a/cmd/soltool/set_fee_receiver.go b/cmd/soltool/set_fee_receiver.go index e4df64d..4be00cd 100644 --- a/cmd/soltool/set_fee_receiver.go +++ b/cmd/soltool/set_fee_receiver.go @@ -47,15 +47,16 @@ func setFeeReceiverAction(ctx *cli.Context) error { AdminAccount := solTypes.AccountFromPrivateKeyBytes(privKeyMap[pc.AdminAccountPubkey]) BridgeProgramId := solCommon.PublicKeyFromString(pc.BridgeProgramId) - owners := make([]solCommon.PublicKey, 0) + ownersLen := 1 + len(pc.FeeReceiverAccount) + if ownersLen < int(pc.Threshold) { + return fmt.Errorf("owner len < threshold") + } + owners := make([]solCommon.PublicKey, 0, ownersLen) owners = append(owners, FeeAccount.PublicKey) for _, account := range pc.OtherFeeAccountPubkey { a := solTypes.AccountFromPrivateKeyBytes(privKeyMap[account]) owners = append(owners, a.PublicKey) } - if len(owners) < int(pc.Threshold) { - return fmt.Errorf("owner len < threshold") - } //start inter with solana chain c := solClient.NewClient([]string{pc.Endpoint}) diff --git a/cmd/soltool/set_resource_id.go b/cmd/soltool/set_resource_id.go index a8ec90f..aefcd31 100644 --- a/cmd/soltool/set_resource_id.go +++ b/cmd/soltool/set_resource_id.go @@ -45,15 +45,16 @@ func mapResourceIdAction(ctx *cli.Context) error { AdminAccount := solTypes.AccountFromPrivateKeyBytes(privKeyMap[pc.AdminAccountPubkey]) BridgeProgramId := solCommon.PublicKeyFromString(pc.BridgeProgramId) - owners := make([]solCommon.PublicKey, 0) + ownersLen := 1 + len(pc.OtherFeeAccountPubkey) + if ownersLen < int(pc.Threshold) { + return fmt.Errorf("owner len < threshold") + } + owners := make([]solCommon.PublicKey, 0, ownersLen) owners = append(owners, FeeAccount.PublicKey) for _, account := range pc.OtherFeeAccountPubkey { a := solTypes.AccountFromPrivateKeyBytes(privKeyMap[account]) owners = append(owners, a.PublicKey) } - if len(owners) < int(pc.Threshold) { - return fmt.Errorf("owner len < threshold") - } resourceIdToMint := make(map[[32]byte]solCommon.PublicKey) for key, value := range pc.ResourceIdToMint { diff --git a/cmd/soltool/set_support_chainId.go b/cmd/soltool/set_support_chainId.go index fdb44f3..26394b4 100644 --- a/cmd/soltool/set_support_chainId.go +++ b/cmd/soltool/set_support_chainId.go @@ -44,15 +44,16 @@ func setSupportChainIdAction(ctx *cli.Context) error { AdminAccount := solTypes.AccountFromPrivateKeyBytes(privKeyMap[pc.AdminAccountPubkey]) BridgeProgramId := solCommon.PublicKeyFromString(pc.BridgeProgramId) - owners := make([]solCommon.PublicKey, 0) + ownersLen := 1 + len(pc.OtherFeeAccountPubkey) + if ownersLen < int(pc.Threshold) { + return fmt.Errorf("owner len < threshold") + } + owners := make([]solCommon.PublicKey, 0, ownersLen) owners = append(owners, FeeAccount.PublicKey) for _, account := range pc.OtherFeeAccountPubkey { a := solTypes.AccountFromPrivateKeyBytes(privKeyMap[account]) owners = append(owners, a.PublicKey) } - if len(owners) < int(pc.Threshold) { - return fmt.Errorf("owner len < threshold") - } //start inter with solana chain c := solClient.NewClient([]string{pc.Endpoint}) diff --git a/cmd/solvault/vault_add.go b/cmd/solvault/vault_add.go index 5aa3c03..8571fc4 100644 --- a/cmd/solvault/vault_add.go +++ b/cmd/solvault/vault_add.go @@ -56,7 +56,7 @@ var vaultAddCmd = &cobra.Command{ return } - var newKeys []vault.PublicKey + newKeys := make([]vault.PublicKey, 0, len(privateKeys)) for _, privateKey := range privateKeys { v.AddPrivateKey(privateKey) newKeys = append(newKeys, privateKey.PublicKey()) diff --git a/shared/ethereum/erc20.go b/shared/ethereum/erc20.go index e93f276..a2fd784 100644 --- a/shared/ethereum/erc20.go +++ b/shared/ethereum/erc20.go @@ -226,7 +226,7 @@ func Erc20GetResourceId(client *Client, handler common.Address, rId msg.Resource } func ConstructErc20DepositData(destRecipient []byte, amount *big.Int) []byte { - var data []byte + data := make([]byte, 64+len(destRecipient)) data = append(data, math.PaddedBigBytes(amount, 32)...) data = append(data, math.PaddedBigBytes(big.NewInt(int64(len(destRecipient))), 32)...) data = append(data, destRecipient...)