Skip to content

Commit

Permalink
perf: use errors.New to replace fmt.Errorf with no parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Andi committed Apr 20, 2024
1 parent 89d7a55 commit b5f95c2
Show file tree
Hide file tree
Showing 25 changed files with 47 additions and 39 deletions.
3 changes: 2 additions & 1 deletion cmd/slnc/cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package cmd

import (
"errors"
"fmt"
"os"
"strings"
Expand All @@ -38,7 +39,7 @@ func getClient() *rpc.Client {
for _, header := range httpHeaders {
headerArray := strings.SplitN(header, ": ", 2)
if len(headerArray) != 2 || strings.Contains(headerArray[0], " ") {
errorCheck("validating http headers", fmt.Errorf("invalid HTTP Header format"))
errorCheck("validating http headers", errors.New("invalid HTTP Header format"))
}
headers[headerArray[0]] = headerArray[1]
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/slnc/cmd/get_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package cmd

import (
"errors"
"fmt"

"github.com/gagliardetto/solana-go"
Expand All @@ -41,7 +42,7 @@ var getBalanceCmd = &cobra.Command{
}

if resp.Value == 0 {
return fmt.Errorf("account not found")
return errors.New("account not found")
}

fmt.Println(resp.Value, "lamports")
Expand Down
3 changes: 2 additions & 1 deletion cmd/slnc/cmd/get_program_accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package cmd

import (
"encoding/json"
"errors"
"fmt"
"os"

Expand All @@ -45,7 +46,7 @@ var getProgramAccountsCmd = &cobra.Command{
}

if resp == nil {
return fmt.Errorf("program account not found")
return errors.New("program account not found")
}

for _, keyedAcct := range resp {
Expand Down
6 changes: 3 additions & 3 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ package solana

import (
"encoding/base64"
"errors"
"fmt"

bin "github.com/gagliardetto/binary"
"github.com/gagliardetto/treeout"

Expand Down Expand Up @@ -120,7 +120,7 @@ type Message struct {
// NOTE: you can call this once.
func (mx *Message) SetAddressTables(tables map[PublicKey]PublicKeySlice) error {
if mx.addressTables != nil {
return fmt.Errorf("address tables already set")
return errors.New("address tables already set")
}
mx.addressTables = tables
return nil
Expand Down Expand Up @@ -644,7 +644,7 @@ func (m Message) checkPreconditions() error {
// but the address table is empty,
// then we can't build the account meta list:
if m.IsVersioned() && m.AddressTableLookups.NumLookups() > 0 && (m.addressTables == nil || len(m.addressTables) == 0) {
return fmt.Errorf("cannot build account meta list without address tables")
return errors.New("cannot build account meta list without address tables")
}

return nil
Expand Down
7 changes: 4 additions & 3 deletions programs/bpf-loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bpfloader

import (
"encoding/binary"
"errors"
"fmt"

"github.com/gagliardetto/solana-go"
Expand Down Expand Up @@ -45,19 +46,19 @@ func completePartialProgramInit(
allowExcessiveBalance bool,
) (instructions []solana.Instruction, balanceNeeded uint64, err error) {
if account.Executable {
err = fmt.Errorf("buffer account is already executable")
err = errors.New("buffer account is already executable")
return
}
if !account.Owner.Equals(loaderId) &&
!account.Owner.Equals(solana.SystemProgramID) {
err = fmt.Errorf(
err = errors.New(
"buffer account passed is already in use by another program",
)
return
}
if len(account.Data.GetBinary()) > 0 &&
len(account.Data.GetBinary()) < accountDataLen {
err = fmt.Errorf(
err = errors.New(
"buffer account passed is not large enough, may have been for a " +
" different deploy?",
)
Expand Down
3 changes: 2 additions & 1 deletion programs/serum/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package serum
import (
"bytes"
"context"
"errors"
"fmt"

rice "github.com/GeertJohan/go.rice"
Expand All @@ -36,7 +37,7 @@ import (
func KnownMarket() ([]*MarketMeta, error) {
box := rice.MustFindBox("data").MustBytes("markets.json")
if box == nil {
return nil, fmt.Errorf("unable to retrieve known markets")
return nil, errors.New("unable to retrieve known markets")
}

dec := json.NewDecoder(bytes.NewReader(box))
Expand Down
5 changes: 2 additions & 3 deletions programs/system/CreateAccountWithSeed.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package system
import (
"encoding/binary"
"errors"
"fmt"

ag_binary "github.com/gagliardetto/binary"
ag_solanago "github.com/gagliardetto/solana-go"
Expand Down Expand Up @@ -166,10 +165,10 @@ func (inst *CreateAccountWithSeed) Validate() error {
// Check whether all accounts are set:
{
if inst.AccountMetaSlice[0] == nil {
return fmt.Errorf("FundingAccount is not set")
return errors.New("FundingAccount is not set")
}
if inst.AccountMetaSlice[1] == nil {
return fmt.Errorf("CreatedAccount is not set")
return errors.New("CreatedAccount is not set")
}
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion programs/token/Approve.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (inst *Approve) Validate() error {
return errors.New("accounts.Owner is not set")
}
if !inst.Accounts[2].IsSigner && len(inst.Signers) == 0 {
return fmt.Errorf("accounts.Signers is not set")
return errors.New("accounts.Signers is not set")
}
if len(inst.Signers) > MAX_SIGNERS {
return fmt.Errorf("too many signers; got %v, but max is 11", len(inst.Signers))
Expand Down
2 changes: 1 addition & 1 deletion programs/token/ApproveChecked.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ func (inst *ApproveChecked) Validate() error {
return errors.New("accounts.Owner is not set")
}
if !inst.Accounts[3].IsSigner && len(inst.Signers) == 0 {
return fmt.Errorf("accounts.Signers is not set")
return errors.New("accounts.Signers is not set")
}
if len(inst.Signers) > MAX_SIGNERS {
return fmt.Errorf("too many signers; got %v, but max is 11", len(inst.Signers))
Expand Down
2 changes: 1 addition & 1 deletion programs/token/Burn.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (inst *Burn) Validate() error {
return errors.New("accounts.Owner is not set")
}
if !inst.Accounts[2].IsSigner && len(inst.Signers) == 0 {
return fmt.Errorf("accounts.Signers is not set")
return errors.New("accounts.Signers is not set")
}
if len(inst.Signers) > MAX_SIGNERS {
return fmt.Errorf("too many signers; got %v, but max is 11", len(inst.Signers))
Expand Down
2 changes: 1 addition & 1 deletion programs/token/BurnChecked.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (inst *BurnChecked) Validate() error {
return errors.New("accounts.Owner is not set")
}
if !inst.Accounts[2].IsSigner && len(inst.Signers) == 0 {
return fmt.Errorf("accounts.Signers is not set")
return errors.New("accounts.Signers is not set")
}
if len(inst.Signers) > MAX_SIGNERS {
return fmt.Errorf("too many signers; got %v, but max is 11", len(inst.Signers))
Expand Down
2 changes: 1 addition & 1 deletion programs/token/CloseAccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (inst *CloseAccount) Validate() error {
return errors.New("accounts.Owner is not set")
}
if !inst.Accounts[2].IsSigner && len(inst.Signers) == 0 {
return fmt.Errorf("accounts.Signers is not set")
return errors.New("accounts.Signers is not set")
}
if len(inst.Signers) > MAX_SIGNERS {
return fmt.Errorf("too many signers; got %v, but max is 11", len(inst.Signers))
Expand Down
6 changes: 3 additions & 3 deletions programs/token/InitializeMultisig.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ func (inst *InitializeMultisig) Validate() error {
// Check whether all (required) accounts are set:
{
if inst.Accounts[0] == nil {
return fmt.Errorf("accounts.Account is not set")
return errors.New("accounts.Account is not set")
}
if inst.Accounts[1] == nil {
return fmt.Errorf("accounts.SysVarRentPubkey is not set")
return errors.New("accounts.SysVarRentPubkey is not set")
}
if len(inst.Signers) == 0 {
return fmt.Errorf("accounts.Signers is not set")
return errors.New("accounts.Signers is not set")
}
if len(inst.Signers) > MAX_SIGNERS {
return fmt.Errorf("too many signers; got %v, but max is 11", len(inst.Signers))
Expand Down
2 changes: 1 addition & 1 deletion programs/token/InitializeMultisig2.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (inst *InitializeMultisig2) Validate() error {
return errors.New("accounts.Account is not set")
}
if len(inst.Signers) == 0 {
return fmt.Errorf("accounts.Signers is not set")
return errors.New("accounts.Signers is not set")
}
if len(inst.Signers) > MAX_SIGNERS {
return fmt.Errorf("too many signers; got %v, but max is 11", len(inst.Signers))
Expand Down
2 changes: 1 addition & 1 deletion programs/token/MintTo.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func (inst *MintTo) Validate() error {
return errors.New("accounts.Authority is not set")
}
if !inst.Accounts[2].IsSigner && len(inst.Signers) == 0 {
return fmt.Errorf("accounts.Signers is not set")
return errors.New("accounts.Signers is not set")
}
if len(inst.Signers) > MAX_SIGNERS {
return fmt.Errorf("too many signers; got %v, but max is 11", len(inst.Signers))
Expand Down
2 changes: 1 addition & 1 deletion programs/token/MintToChecked.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (inst *MintToChecked) Validate() error {
return errors.New("accounts.Authority is not set")
}
if !inst.Accounts[2].IsSigner && len(inst.Signers) == 0 {
return fmt.Errorf("accounts.Signers is not set")
return errors.New("accounts.Signers is not set")
}
if len(inst.Signers) > MAX_SIGNERS {
return fmt.Errorf("too many signers; got %v, but max is 11", len(inst.Signers))
Expand Down
2 changes: 1 addition & 1 deletion programs/token/Revoke.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (inst *Revoke) Validate() error {
return errors.New("accounts.Owner is not set")
}
if !inst.Accounts[1].IsSigner && len(inst.Signers) == 0 {
return fmt.Errorf("accounts.Signers is not set")
return errors.New("accounts.Signers is not set")
}
if len(inst.Signers) > MAX_SIGNERS {
return fmt.Errorf("too many signers; got %v, but max is 11", len(inst.Signers))
Expand Down
2 changes: 1 addition & 1 deletion programs/token/SetAuthority.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (inst *SetAuthority) Validate() error {
return errors.New("accounts.Authority is not set")
}
if !inst.Accounts[1].IsSigner && len(inst.Signers) == 0 {
return fmt.Errorf("accounts.Signers is not set")
return errors.New("accounts.Signers is not set")
}
if len(inst.Signers) > MAX_SIGNERS {
return fmt.Errorf("too many signers; got %v, but max is 11", len(inst.Signers))
Expand Down
2 changes: 1 addition & 1 deletion programs/token/ThawAccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (inst *ThawAccount) Validate() error {
return errors.New("accounts.Authority is not set")
}
if !inst.Accounts[2].IsSigner && len(inst.Signers) == 0 {
return fmt.Errorf("accounts.Signers is not set")
return errors.New("accounts.Signers is not set")
}
if len(inst.Signers) > MAX_SIGNERS {
return fmt.Errorf("too many signers; got %v, but max is 11", len(inst.Signers))
Expand Down
8 changes: 4 additions & 4 deletions programs/token/Transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,16 @@ func (inst *Transfer) Validate() error {
// Check whether all (required) accounts are set:
{
if inst.Accounts[0] == nil {
return fmt.Errorf("accounts.Source is not set")
return errors.New("accounts.Source is not set")
}
if inst.Accounts[1] == nil {
return fmt.Errorf("accounts.Destination is not set")
return errors.New("accounts.Destination is not set")
}
if inst.Accounts[2] == nil {
return fmt.Errorf("accounts.Owner is not set")
return errors.New("accounts.Owner is not set")
}
if !inst.Accounts[2].IsSigner && len(inst.Signers) == 0 {
return fmt.Errorf("accounts.Signers is not set")
return errors.New("accounts.Signers is not set")
}
if len(inst.Signers) > MAX_SIGNERS {
return fmt.Errorf("too many signers; got %v, but max is 11", len(inst.Signers))
Expand Down
2 changes: 1 addition & 1 deletion programs/token/TransferChecked.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (inst *TransferChecked) Validate() error {
return errors.New("accounts.Owner is not set")
}
if !inst.Accounts[3].IsSigner && len(inst.Signers) == 0 {
return fmt.Errorf("accounts.Signers is not set")
return errors.New("accounts.Signers is not set")
}
if len(inst.Signers) > MAX_SIGNERS {
return fmt.Errorf("too many signers; got %v, but max is 11", len(inst.Signers))
Expand Down
3 changes: 2 additions & 1 deletion programs/token/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package token

import (
"context"
"errors"
"fmt"

bin "github.com/gagliardetto/binary"
Expand Down Expand Up @@ -52,7 +53,7 @@ func FetchMints(ctx context.Context, rpcCli *rpc.Client) (out []*Mint, err error
return nil, err
}
if resp == nil {
return nil, fmt.Errorf("resp empty... program account not found")
return nil, errors.New("resp empty... program account not found")
}

for _, keyedAcct := range resp {
Expand Down
3 changes: 2 additions & 1 deletion programs/tokenregistry/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package tokenregistry
import (
"bytes"
"encoding/binary"
"errors"
"fmt"

"github.com/gagliardetto/solana-go/text"
Expand Down Expand Up @@ -138,7 +139,7 @@ type RegisterToken struct {

func (i *RegisterToken) SetAccounts(accounts []*solana.AccountMeta) error {
if len(accounts) < 9 {
return fmt.Errorf("insufficient account")
return errors.New("insufficient account")
}
i.Accounts = &RegisterTokenAccounts{
TokenMeta: accounts[0],
Expand Down
5 changes: 3 additions & 2 deletions programs/tokenregistry/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package tokenregistry

import (
"context"
"errors"
"fmt"

"github.com/gagliardetto/solana-go"
Expand All @@ -44,7 +45,7 @@ func GetTokenRegistryEntry(ctx context.Context, rpcCli *rpc.Client, mintAddress
return nil, err
}
if resp == nil {
return nil, fmt.Errorf("resp empty... cannot find account")
return nil, errors.New("resp empty... cannot find account")
}

for _, keyedAcct := range resp {
Expand Down Expand Up @@ -74,7 +75,7 @@ func GetEntries(ctx context.Context, rpcCli *rpc.Client) (out []*TokenMeta, err
return nil, err
}
if resp == nil {
return nil, fmt.Errorf("resp empty... cannot find accounts")
return nil, errors.New("resp empty... cannot find accounts")
}

for _, keyedAcct := range resp {
Expand Down
7 changes: 4 additions & 3 deletions transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package solana
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"sort"

Expand Down Expand Up @@ -230,7 +231,7 @@ type addressTablePubkeyWithIndex struct {

func NewTransaction(instructions []Instruction, recentBlockHash Hash, opts ...TransactionOption) (*Transaction, error) {
if len(instructions) == 0 {
return nil, fmt.Errorf("requires at-least one instruction to create a transaction")
return nil, errors.New("requires at-least one instruction to create a transaction")
}

options := transactionOptions{}
Expand All @@ -249,7 +250,7 @@ func NewTransaction(instructions []Instruction, recentBlockHash Hash, opts ...Tr
}
}
if !found {
return nil, fmt.Errorf("cannot determine fee payer. You can ether pass the fee payer via the 'TransactionWithInstructions' option parameter or it falls back to the first instruction's first signer")
return nil, errors.New("cannot determine fee payer. You can ether pass the fee payer via the 'TransactionWithInstructions' option parameter or it falls back to the first instruction's first signer")
}
}

Expand Down Expand Up @@ -506,7 +507,7 @@ func (tx *Transaction) UnmarshalWithDecoder(decoder *bin.Decoder) (err error) {
return fmt.Errorf("unable to read numSignatures: %w", err)
}
if numSignatures < 0 {
return fmt.Errorf("numSignatures is negative")
return errors.New("numSignatures is negative")
}
if numSignatures > decoder.Remaining()/64 {
return fmt.Errorf("numSignatures %d is too large for remaining bytes %d", numSignatures, decoder.Remaining())
Expand Down

0 comments on commit b5f95c2

Please sign in to comment.