From 64872aefe875f43c5365fdec4fe6cee429abe2f8 Mon Sep 17 00:00:00 2001 From: Devon Bear Date: Thu, 15 Aug 2024 17:11:27 -0400 Subject: [PATCH] chore(errors): Cleanup (#1905) --- examples/berad/pkg/state-transition/errors.go | 8 +++ .../state_processor_payload.go | 3 +- .../state_processor_staking.go | 6 +- mod/beacon/validator/block_builder.go | 11 +--- mod/cli/pkg/commands/genesis/deposit.go | 4 +- mod/cli/pkg/commands/genesis/payload.go | 15 ++--- mod/cli/pkg/config/errors.go | 27 ++++++++ mod/cli/pkg/config/server.go | 2 +- mod/config/config.go | 7 +- mod/errors/go.mod | 19 +----- mod/errors/go.sum | 66 ------------------- mod/errors/mod.go | 20 ++++-- .../handlers/proof/merkle/block_proposer.go | 4 +- .../proof/merkle/execution_fee_recipient.go | 5 +- .../handlers/proof/merkle/execution_number.go | 5 +- mod/primitives/pkg/bytes/utils.go | 10 +-- mod/primitives/pkg/encoding/hex/bytes.go | 3 +- .../pkg/encoding/ssz/merkle/errors.go | 32 +++++++++ .../pkg/encoding/ssz/merkle/proof.go | 16 +++-- .../pkg/encoding/ssz/merkle/tree.go | 11 +++- mod/primitives/pkg/merkle/tree.go | 5 +- mod/primitives/pkg/net/jwt/jwt_test.go | 28 ++++++-- mod/runtime/pkg/service/errors.go | 27 ++++---- mod/runtime/pkg/service/registry.go | 8 +-- mod/state-transition/pkg/core/errors.go | 8 +++ .../pkg/core/state_processor_payload.go | 3 +- .../pkg/core/state_processor_staking.go | 6 +- mod/storage/pkg/filedb/range_db.go | 2 +- mod/storage/pkg/filedb/range_db_test.go | 2 +- 29 files changed, 190 insertions(+), 173 deletions(-) create mode 100644 mod/cli/pkg/config/errors.go create mode 100644 mod/primitives/pkg/encoding/ssz/merkle/errors.go diff --git a/examples/berad/pkg/state-transition/errors.go b/examples/berad/pkg/state-transition/errors.go index 7b8c5a7f93..d26398bfd8 100644 --- a/examples/berad/pkg/state-transition/errors.go +++ b/examples/berad/pkg/state-transition/errors.go @@ -66,4 +66,12 @@ var ( // ErrStateRootMismatch is returned when the state root in a block header // does not match the expected value. ErrStateRootMismatch = errors.New("state root mismatch") + + // ErrExceedMaximumWithdrawals is returned when the number of withdrawals + // in a block exceeds the maximum allowed. + ErrExceedMaximumWithdrawals = errors.New("exceeds maximum withdrawals") + + // ErrNumWithdrawalsMismatch is returned when the number of withdrawals + // in a block does not match the expected value. + ErrNumWithdrawalsMismatch = errors.New("number of withdrawals mismatch") ) diff --git a/examples/berad/pkg/state-transition/state_processor_payload.go b/examples/berad/pkg/state-transition/state_processor_payload.go index e13072e6a7..0671b3628f 100644 --- a/examples/berad/pkg/state-transition/state_processor_payload.go +++ b/examples/berad/pkg/state-transition/state_processor_payload.go @@ -172,7 +172,8 @@ func (sp *StateProcessor[ if withdrawals := payload.GetWithdrawals(); uint64( len(payload.GetWithdrawals()), ) > sp.cs.MaxWithdrawalsPerPayload() { - return errors.Newf( + return errors.Wrapf( + ErrExceedMaximumWithdrawals, "too many withdrawals, expected: %d, got: %d", sp.cs.MaxWithdrawalsPerPayload(), len(withdrawals), ) diff --git a/examples/berad/pkg/state-transition/state_processor_staking.go b/examples/berad/pkg/state-transition/state_processor_staking.go index 1f8b96aa94..6aba003cf4 100644 --- a/examples/berad/pkg/state-transition/state_processor_staking.go +++ b/examples/berad/pkg/state-transition/state_processor_staking.go @@ -205,7 +205,8 @@ func (sp *StateProcessor[ // Ensure the withdrawals have the same length if numWithdrawals != len(payloadWithdrawals) { - return errors.Newf( + return errors.Wrapf( + ErrNumWithdrawalsMismatch, "withdrawals do not match expected length %d, got %d", len(expectedWithdrawals), len(payloadWithdrawals), ) @@ -215,7 +216,8 @@ func (sp *StateProcessor[ for i, wd := range expectedWithdrawals { // Ensure the withdrawals match the local state. if !wd.Equals(payloadWithdrawals[i]) { - return errors.Newf( + return errors.Wrapf( + ErrNumWithdrawalsMismatch, "withdrawals do not match expected %s, got %s", spew.Sdump(wd), spew.Sdump(payloadWithdrawals[i]), ) diff --git a/mod/beacon/validator/block_builder.go b/mod/beacon/validator/block_builder.go index cc57349d9c..303a66c6df 100644 --- a/mod/beacon/validator/block_builder.go +++ b/mod/beacon/validator/block_builder.go @@ -25,7 +25,6 @@ import ( "time" engineprimitives "github.com/berachain/beacon-kit/mod/engine-primitives/pkg/engine-primitives" - "github.com/berachain/beacon-kit/mod/errors" "github.com/berachain/beacon-kit/mod/primitives/pkg/bytes" "github.com/berachain/beacon-kit/mod/primitives/pkg/common" "github.com/berachain/beacon-kit/mod/primitives/pkg/crypto" @@ -145,10 +144,7 @@ func (s *Service[ ) if err != nil { - return blk, errors.Newf( - "failed to get block root at index: %w", - err, - ) + return blk, err } // Get the proposer index for the slot. @@ -156,10 +152,7 @@ func (s *Service[ s.signer.PublicKey(), ) if err != nil { - return blk, errors.Newf( - "failed to get validator by pubkey: %w", - err, - ) + return blk, err } return blk.NewWithVersion( diff --git a/mod/cli/pkg/commands/genesis/deposit.go b/mod/cli/pkg/commands/genesis/deposit.go index 5878ddf809..0373fc6ff6 100644 --- a/mod/cli/pkg/commands/genesis/deposit.go +++ b/mod/cli/pkg/commands/genesis/deposit.go @@ -150,8 +150,8 @@ func AddGenesisDepositCmd(cs common.ChainSpec) *cobra.Command { func makeOutputFilepath(rootDir, pubkey string) (string, error) { writePath := filepath.Join(rootDir, "config", "premined-deposits") if err := afero.NewOsFs().MkdirAll(writePath, os.ModePerm); err != nil { - return "", errors.Newf( - "could not create directory %q: %w", + return "", errors.Wrapf( + errors.New("could not create directory"), "%q: %w", writePath, err, ) diff --git a/mod/cli/pkg/commands/genesis/payload.go b/mod/cli/pkg/commands/genesis/payload.go index 09d7e2f7b1..2e2fb5d7ac 100644 --- a/mod/cli/pkg/commands/genesis/payload.go +++ b/mod/cli/pkg/commands/genesis/payload.go @@ -93,18 +93,11 @@ func AddExecutionPayloadCmd(chainSpec common.ChainSpec) *cobra.Command { } // Inject the execution payload. - header, err := executableDataToExecutionPayloadHeader( + genesisInfo.ExecutionPayloadHeader = executableDataToExecutionPayloadHeader( version.ToUint32(genesisInfo.ForkVersion), payload, chainSpec.MaxWithdrawalsPerPayload(), ) - if err != nil { - return errors.Wrap( - err, - "failed to convert executable data to execution payload header", - ) - } - genesisInfo.ExecutionPayloadHeader = header appGenesisState["beacon"], err = json.Marshal(genesisInfo) if err != nil { @@ -131,7 +124,7 @@ func executableDataToExecutionPayloadHeader( data *gethprimitives.ExecutableData, // todo: re-enable when codec supports. _ uint64, -) (*types.ExecutionPayloadHeader, error) { +) *types.ExecutionPayloadHeader { var executionPayloadHeader *types.ExecutionPayloadHeader switch forkVersion { case version.Deneb, version.DenebPlus: @@ -187,8 +180,8 @@ func executableDataToExecutionPayloadHeader( ExcessBlobGas: math.U64(excessBlobGas), } default: - return nil, errors.Newf("unsupported fork version %d", forkVersion) + panic("unsupported fork version") } - return executionPayloadHeader, nil + return executionPayloadHeader } diff --git a/mod/cli/pkg/config/errors.go b/mod/cli/pkg/config/errors.go new file mode 100644 index 0000000000..aa665b1576 --- /dev/null +++ b/mod/cli/pkg/config/errors.go @@ -0,0 +1,27 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package config + +import "errors" + +// ErrFlagBind is returned when there is an error binding a flag to the viper +// instance. +var ErrFlagBind = errors.New("failed to bind flag") diff --git a/mod/cli/pkg/config/server.go b/mod/cli/pkg/config/server.go index 60bd832dd7..dfb7c6433c 100644 --- a/mod/cli/pkg/config/server.go +++ b/mod/cli/pkg/config/server.go @@ -97,7 +97,7 @@ func InitializeCmd[ // bind cobra flags to the viper instance if err = bindFlags(baseName, cmd, viper); err != nil { - return errors.Newf("error binding flags: %w", err) + return errors.Wrapf(ErrFlagBind, "error binding flags: %w", err) } ctx := cmd.Context() diff --git a/mod/config/config.go b/mod/config/config.go index ed797db7b5..ca1a91998c 100644 --- a/mod/config/config.go +++ b/mod/config/config.go @@ -101,7 +101,7 @@ func MustReadConfigFromAppOpts(opts AppOptions) *Config { func ReadConfigFromAppOpts(opts AppOptions) (*Config, error) { v, ok := opts.(*viper.Viper) if !ok { - return nil, errors.Newf("invalid application options type: %T", opts) + return nil, errors.New("invalid application options type") } type cfgUnmarshaller struct { @@ -116,10 +116,7 @@ func ReadConfigFromAppOpts(opts AppOptions) (*Config, error) { viperlib.StringToDialURLFunc(), viperlib.StringToConnectionURLFunc(), ))); err != nil { - return nil, errors.Newf( - "failed to decode beacon-kit configuration: %w", - err, - ) + return nil, err } return &cfg.BeaconKit, nil diff --git a/mod/errors/go.mod b/mod/errors/go.mod index 7da8c03443..3a1c845c1a 100644 --- a/mod/errors/go.mod +++ b/mod/errors/go.mod @@ -2,21 +2,4 @@ module github.com/berachain/beacon-kit/mod/errors go 1.23.0 -require github.com/cockroachdb/errors v1.11.3 - -require ( - github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b // indirect - github.com/cockroachdb/redact v1.1.5 // indirect - github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect - github.com/getsentry/sentry-go v0.28.1 // indirect - github.com/gogo/protobuf v1.3.2 // indirect - github.com/google/go-cmp v0.6.0 // indirect - github.com/kr/pretty v0.3.1 // indirect - github.com/kr/text v0.2.0 // indirect - github.com/pkg/errors v0.9.1 // indirect - github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect - github.com/rogpeppe/go-internal v1.12.0 // indirect - github.com/stretchr/testify v1.9.0 // indirect - golang.org/x/sys v0.23.0 // indirect - golang.org/x/text v0.17.0 // indirect -) +require github.com/pkg/errors v0.9.1 diff --git a/mod/errors/go.sum b/mod/errors/go.sum index bf69517c93..7c401c3f58 100644 --- a/mod/errors/go.sum +++ b/mod/errors/go.sum @@ -1,68 +1,2 @@ -github.com/cockroachdb/errors v1.11.3 h1:5bA+k2Y6r+oz/6Z/RFlNeVCesGARKuC6YymtcDrbC/I= -github.com/cockroachdb/errors v1.11.3/go.mod h1:m4UIW4CDjx+R5cybPsNrRbreomiFqt8o1h1wUVazSd8= -github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b h1:r6VH0faHjZeQy818SGhaone5OnYfxFR/+AzdY3sf5aE= -github.com/cockroachdb/logtags v0.0.0-20230118201751-21c54148d20b/go.mod h1:Vz9DsVWQQhf3vs21MhPMZpMGSht7O/2vFW2xusFUVOs= -github.com/cockroachdb/redact v1.1.5 h1:u1PMllDkdFfPWaNGMyLD1+so+aq3uUItthCFqzwPJ30= -github.com/cockroachdb/redact v1.1.5/go.mod h1:BVNblN9mBWFyMyqK1k3AAiSxhvhfK2oOZZ2lK+dpvRg= -github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= -github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/getsentry/sentry-go v0.28.1 h1:zzaSm/vHmGllRM6Tpx1492r0YDzauArdBfkJRtY6P5k= -github.com/getsentry/sentry-go v0.28.1/go.mod h1:1fQZ+7l7eeJ3wYi82q5Hg8GqAPgefRq+FP/QhafYVgg= -github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA= -github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og= -github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= -github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= -github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= -github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8= -github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= -github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= -github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= -github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= -github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= -github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= -github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= -github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= -github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= -github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= -github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= -github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= -github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= -github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.23.0 h1:YfKFowiIMvtgl1UERQoTPPToxltDeZfbj4H7dVUCwmM= -golang.org/x/sys v0.23.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= -golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/mod/errors/mod.go b/mod/errors/mod.go index 92a753bf73..303dba36e1 100644 --- a/mod/errors/mod.go +++ b/mod/errors/mod.go @@ -21,7 +21,9 @@ package errors import ( - "github.com/cockroachdb/errors" + stderrors "errors" + + "github.com/pkg/errors" ) // TODO: eventually swap out via build flags if we believe there is value @@ -30,15 +32,23 @@ import ( //nolint:gochecknoglobals // used an alias. var ( New = errors.New - Newf = errors.Newf Wrap = errors.Wrap Wrapf = errors.Wrapf Is = errors.Is - IsAny = errors.IsAny As = errors.As - Join = errors.Join + Join = stderrors.Join ) +// IsAny checks if the provided error is any of the provided errors. +func IsAny(err error, errs ...error) bool { + for _, e := range errs { + if errors.Is(err, e) { + return true + } + } + return false +} + // DetailedError is a custom error type that includes a message and a flag // indicating if the error is fatal. type DetailedError struct { @@ -104,7 +114,7 @@ func JoinFatal(errs ...error) error { break } } - retErr := errors.Join(errs...) + retErr := stderrors.Join(errs...) if fatal { return WrapFatal(retErr) } diff --git a/mod/node-api/handlers/proof/merkle/block_proposer.go b/mod/node-api/handlers/proof/merkle/block_proposer.go index 856c93e37b..3c2828b134 100644 --- a/mod/node-api/handlers/proof/merkle/block_proposer.go +++ b/mod/node-api/handlers/proof/merkle/block_proposer.go @@ -124,8 +124,8 @@ func verifyProposerInBlock( ); err != nil { return common.Root{}, err } else if !beaconRootVerified { - return common.Root{}, errors.Newf( - "proof failed to verify against beacon root: 0x%x", beaconRoot[:], + return common.Root{}, errors.New( + "proof failed to verify against beacon root", ) } diff --git a/mod/node-api/handlers/proof/merkle/execution_fee_recipient.go b/mod/node-api/handlers/proof/merkle/execution_fee_recipient.go index d10fd075d8..a3e1303514 100644 --- a/mod/node-api/handlers/proof/merkle/execution_fee_recipient.go +++ b/mod/node-api/handlers/proof/merkle/execution_fee_recipient.go @@ -121,8 +121,9 @@ func verifyExecutionFeeRecipientInBlock( ); err != nil { return common.Root{}, err } else if !beaconRootVerified { - return common.Root{}, errors.Newf( - "proof failed to verify against beacon root: 0x%x", beaconRoot[:], + return common.Root{}, errors.Wrapf( + errors.New("proof failed to verify against beacon root"), + "beacon root: 0x%x", beaconRoot[:], ) } diff --git a/mod/node-api/handlers/proof/merkle/execution_number.go b/mod/node-api/handlers/proof/merkle/execution_number.go index db7aae33f7..ffbb0c92ce 100644 --- a/mod/node-api/handlers/proof/merkle/execution_number.go +++ b/mod/node-api/handlers/proof/merkle/execution_number.go @@ -117,8 +117,9 @@ func verifyExecutionNumberInBlock( ); err != nil { return common.Root{}, err } else if !beaconRootVerified { - return common.Root{}, errors.Newf( - "proof failed to verify against beacon root: 0x%x", beaconRoot[:], + return common.Root{}, errors.Wrapf( + errors.New("proof failed to verify against beacon root"), + "beacon root: 0x%x", beaconRoot[:], ) } diff --git a/mod/primitives/pkg/bytes/utils.go b/mod/primitives/pkg/bytes/utils.go index 7cf7f9c206..4a3828c75c 100644 --- a/mod/primitives/pkg/bytes/utils.go +++ b/mod/primitives/pkg/bytes/utils.go @@ -34,10 +34,7 @@ func UnmarshalJSONHelper(target []byte, input []byte) error { return err } if len(bz) != len(target) { - return errors.Newf( - "incorrect length, expected %d bytes but got %d", - len(target), len(bz), - ) + return errors.New("incorrect length") } copy(target, bz) return nil @@ -50,10 +47,7 @@ func UnmarshalTextHelper(target []byte, text []byte) error { return err } if len(bz) != len(target) { - return errors.Newf( - "incorrect length, expected %d bytes but got %d", - len(target), len(bz), - ) + return errors.New("incorrect length") } copy(target, bz) return nil diff --git a/mod/primitives/pkg/encoding/hex/bytes.go b/mod/primitives/pkg/encoding/hex/bytes.go index a4376a04bd..355eb5a6e4 100644 --- a/mod/primitives/pkg/encoding/hex/bytes.go +++ b/mod/primitives/pkg/encoding/hex/bytes.go @@ -63,7 +63,8 @@ func DecodeFixedText(input, out []byte) error { return err } if len(raw)/encDecRatio != len(out) { - return errors.Newf( + return errors.Wrapf( + errors.New("invalid hex string length"), "hex string has length %d, want %d", len(raw), len(out)*encDecRatio, ) diff --git a/mod/primitives/pkg/encoding/ssz/merkle/errors.go b/mod/primitives/pkg/encoding/ssz/merkle/errors.go new file mode 100644 index 0000000000..0d23c96546 --- /dev/null +++ b/mod/primitives/pkg/encoding/ssz/merkle/errors.go @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: BUSL-1.1 +// +// Copyright (C) 2024, Berachain Foundation. All rights reserved. +// Use of this software is governed by the Business Source License included +// in the LICENSE file of this repository and at www.mariadb.com/bsl11. +// +// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY +// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER +// VERSIONS OF THE LICENSED WORK. +// +// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF +// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF +// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). +// +// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +// TITLE. + +package merkle + +import "errors" + +// ErrUnexpectedProofLength is returned when the proof length is unexpected. +var ErrUnexpectedProofLength = errors.New("unexpected proof length") + +// ErrMismatchLeavesIndicesLength is returned when the leaves and indices +// length mismatch. +var ErrMismatchLeavesIndicesLength = errors.New( + "mismatched leaves and indices length", +) diff --git a/mod/primitives/pkg/encoding/ssz/merkle/proof.go b/mod/primitives/pkg/encoding/ssz/merkle/proof.go index b06a4c04ab..18afa2fa53 100644 --- a/mod/primitives/pkg/encoding/ssz/merkle/proof.go +++ b/mod/primitives/pkg/encoding/ssz/merkle/proof.go @@ -80,19 +80,21 @@ func buildSingleProofFromTree[RootT ~[32]byte]( //#nosec:G701 // len(tree) cannot be greater than max uint64. treeLen := GeneralizedIndex(len(tree)) if pow.PrevPowerOfTwo(treeLen) != treeLen { - return nil, errors.Newf( - "invalid tree length (%d), must be power of 2", treeLen, + return nil, errors.Wrapf( + errors.New("invalid tree length"), + "tree length: %d, must be power of 2", treeLen, ) } if index >= treeLen { - return nil, errors.Newf( - "generalized index (%d) must be less than tree length (%d)", - index, treeLen, + return nil, errors.Wrapf( + errors.New("generalized index out of range"), + "index: %d, tree length: %d", index, treeLen, ) } if index < pow.PrevPowerOfTwo(treeLen-1) { - return nil, errors.Newf( - "generalized index (%d) must be of a leaf in the tree", index, + return nil, errors.Wrapf( + errors.New("generalized index not a leaf"), + "index: %d", index, ) } diff --git a/mod/primitives/pkg/encoding/ssz/merkle/tree.go b/mod/primitives/pkg/encoding/ssz/merkle/tree.go index 314cda1633..6b50df6c59 100644 --- a/mod/primitives/pkg/encoding/ssz/merkle/tree.go +++ b/mod/primitives/pkg/encoding/ssz/merkle/tree.go @@ -40,8 +40,12 @@ func CalculateRoot[RootT ~[32]byte]( ) (RootT, error) { if len(proof) != index.Length() { return RootT{}, - errors.Newf("expected proof length %d, received %d", index.Length(), - len(proof)) + errors.Wrapf( + ErrUnexpectedProofLength, + "expected proof length %d, received %d", + index.Length(), + len(proof), + ) } for i, h := range proof { if index.IndexBit(i) { @@ -73,7 +77,8 @@ func CalculateMultiRoot[RootT ~[32]byte]( proof []RootT, ) (RootT, error) { if len(leaves) != len(indices) { - return RootT{}, errors.Newf( + return RootT{}, errors.Wrapf( + ErrMismatchLeavesIndicesLength, "mismatched leaves and indices length: %d != %d", len(leaves), len(indices), ) diff --git a/mod/primitives/pkg/merkle/tree.go b/mod/primitives/pkg/merkle/tree.go index 7d4c6e6ff1..4a2073deed 100644 --- a/mod/primitives/pkg/merkle/tree.go +++ b/mod/primitives/pkg/merkle/tree.go @@ -190,8 +190,9 @@ func (m *Tree[RootT]) HashTreeRoot() common.Root { func (m *Tree[RootT]) MerkleProof(leafIndex uint64) ([]RootT, error) { numLeaves := uint64(len(m.branches[0])) if leafIndex >= numLeaves { - return nil, errors.Newf( - "merkle index out of range in tree, max range: %d, received: %d", + return nil, errors.Wrapf( + errors.New("merkle index out of range in tree"), + "max range: %d, received: %d", numLeaves, leafIndex, ) diff --git a/mod/primitives/pkg/net/jwt/jwt_test.go b/mod/primitives/pkg/net/jwt/jwt_test.go index 67e5f47782..3afec5203b 100644 --- a/mod/primitives/pkg/net/jwt/jwt_test.go +++ b/mod/primitives/pkg/net/jwt/jwt_test.go @@ -251,11 +251,21 @@ func TestHexRegexp(t *testing.T) { } for _, validHex := range validHexStrings { - require.True(t, jwt.HexRegexp.MatchString(validHex), "Valid hex string not matched: %s", validHex) + require.True( + t, + jwt.HexRegexp.MatchString(validHex), + "Valid hex string not matched: %s", + validHex, + ) } for _, invalidHex := range invalidHexStrings { - require.False(t, jwt.HexRegexp.MatchString(invalidHex), "Invalid hex string matched: %s", invalidHex) + require.False( + t, + jwt.HexRegexp.MatchString(invalidHex), + "Invalid hex string matched: %s", + invalidHex, + ) } } @@ -266,8 +276,18 @@ func TestSecretComparison(t *testing.T) { secret2, err := jwt.NewRandom() require.NoError(t, err, "NewRandom() error for secret2") - require.NotEqual(t, secret1, secret2, "Two random secrets should not be equal") + require.NotEqual( + t, + secret1, + secret2, + "Two random secrets should not be equal", + ) secret3 := *secret1 - require.Equal(t, secret1, &secret3, "Copied secret should be equal to original") + require.Equal( + t, + secret1, + &secret3, + "Copied secret should be equal to original", + ) } diff --git a/mod/runtime/pkg/service/errors.go b/mod/runtime/pkg/service/errors.go index b512379675..051aef3e99 100644 --- a/mod/runtime/pkg/service/errors.go +++ b/mod/runtime/pkg/service/errors.go @@ -18,7 +18,6 @@ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND // TITLE. -//nolint:gochecknoglobals // this file contains functions for use as errors. package service import "github.com/berachain/beacon-kit/mod/errors" @@ -26,21 +25,23 @@ import "github.com/berachain/beacon-kit/mod/errors" var ( // errServiceAlreadyExists defines an error for when a service already // exists. - errServiceAlreadyExists = func(serviceName string) error { - return errors.Newf("service already exists: %v", serviceName) - } + errServiceAlreadyExists = errors.Wrapf( + errors.New("service already exists"), + "%v", + ) // errInputIsNotPointer defines an error for when the input must // be of pointer type. - errInputIsNotPointer = func(valueType interface{}) error { - return errors.Newf( - "input must be of pointer type, received value type instead: %T", - valueType, - ) - } + errInputIsNotPointer = errors.Wrapf( + errors.New( + "input must be of pointer type, received value type instead", + ), + "%T", + ) // errUnknownService defines is returned when an unknown service is seen. - errUnknownService = func(serviceType interface{}) error { - return errors.Newf("unknown service: %T", serviceType) - } + errUnknownService = errors.Wrapf( + errors.New("unknown service"), + "%T", + ) ) diff --git a/mod/runtime/pkg/service/registry.go b/mod/runtime/pkg/service/registry.go index 358b400817..31556d5914 100644 --- a/mod/runtime/pkg/service/registry.go +++ b/mod/runtime/pkg/service/registry.go @@ -84,7 +84,7 @@ func (s *Registry) StartAll(ctx context.Context) error { func (s *Registry) RegisterService(service Basic) error { typeName := service.Name() if _, exists := s.services[typeName]; exists { - return errServiceAlreadyExists(typeName) + return errServiceAlreadyExists } s.services[typeName] = service s.serviceTypes = append(s.serviceTypes, typeName) @@ -99,7 +99,7 @@ func (s *Registry) FetchService(service interface{}) error { serviceType := reflect.TypeOf(service) if serviceType.Kind() != reflect.Ptr || serviceType.Elem().Kind() != reflect.Ptr { - return errInputIsNotPointer(serviceType) + return errInputIsNotPointer } element := reflect.ValueOf(service).Elem() @@ -114,12 +114,12 @@ func (s *Registry) FetchService(service interface{}) error { } if typeName == "" { - return errUnknownService(serviceType) + return errUnknownService } if running, ok := s.services[typeName]; ok { element.Set(reflect.ValueOf(running)) return nil } - return errUnknownService(serviceType) + return errUnknownService } diff --git a/mod/state-transition/pkg/core/errors.go b/mod/state-transition/pkg/core/errors.go index 25e92986f5..a29026a795 100644 --- a/mod/state-transition/pkg/core/errors.go +++ b/mod/state-transition/pkg/core/errors.go @@ -66,4 +66,12 @@ var ( // ErrStateRootMismatch is returned when the state root in a block header // does not match the expected value. ErrStateRootMismatch = errors.New("state root mismatch") + + // ErrExceedMaximumWithdrawals is returned when the number of withdrawals + // in a block exceeds the maximum allowed. + ErrExceedMaximumWithdrawals = errors.New("exceeds maximum withdrawals") + + // ErrNumWithdrawalsMismatch is returned when the number of withdrawals + // in a block does not match the expected value. + ErrNumWithdrawalsMismatch = errors.New("number of withdrawals mismatch") ) diff --git a/mod/state-transition/pkg/core/state_processor_payload.go b/mod/state-transition/pkg/core/state_processor_payload.go index 3b3708597a..23d8efe184 100644 --- a/mod/state-transition/pkg/core/state_processor_payload.go +++ b/mod/state-transition/pkg/core/state_processor_payload.go @@ -172,7 +172,8 @@ func (sp *StateProcessor[ if withdrawals := payload.GetWithdrawals(); uint64( len(payload.GetWithdrawals()), ) > sp.cs.MaxWithdrawalsPerPayload() { - return errors.Newf( + return errors.Wrapf( + ErrExceedMaximumWithdrawals, "too many withdrawals, expected: %d, got: %d", sp.cs.MaxWithdrawalsPerPayload(), len(withdrawals), ) diff --git a/mod/state-transition/pkg/core/state_processor_staking.go b/mod/state-transition/pkg/core/state_processor_staking.go index 8a995621fb..97dcbf95a9 100644 --- a/mod/state-transition/pkg/core/state_processor_staking.go +++ b/mod/state-transition/pkg/core/state_processor_staking.go @@ -246,7 +246,8 @@ func (sp *StateProcessor[ // Ensure the withdrawals have the same length if numWithdrawals != len(payloadWithdrawals) { - return errors.Newf( + return errors.Wrapf( + ErrNumWithdrawalsMismatch, "withdrawals do not match expected length %d, got %d", len(expectedWithdrawals), len(payloadWithdrawals), ) @@ -256,7 +257,8 @@ func (sp *StateProcessor[ for i, wd := range expectedWithdrawals { // Ensure the withdrawals match the local state. if !wd.Equals(payloadWithdrawals[i]) { - return errors.Newf( + return errors.Wrapf( + ErrNumWithdrawalsMismatch, "withdrawals do not match expected %s, got %s", spew.Sdump(wd), spew.Sdump(payloadWithdrawals[i]), ) diff --git a/mod/storage/pkg/filedb/range_db.go b/mod/storage/pkg/filedb/range_db.go index cf8228617f..707034cd46 100644 --- a/mod/storage/pkg/filedb/range_db.go +++ b/mod/storage/pkg/filedb/range_db.go @@ -132,7 +132,7 @@ func ExtractIndex(prefixedKey []byte) (uint64, error) { indexStr := string(parts[0]) index, err := strconv.ParseUint(indexStr, 10, 64) if err != nil { - return 0, errors.Newf("invalid index: %w", err) + return 0, err } //#nosec:g diff --git a/mod/storage/pkg/filedb/range_db_test.go b/mod/storage/pkg/filedb/range_db_test.go index 3668397816..51334ce806 100644 --- a/mod/storage/pkg/filedb/range_db_test.go +++ b/mod/storage/pkg/filedb/range_db_test.go @@ -175,7 +175,7 @@ func TestExtractIndex(t *testing.T) { prefixedKey: []byte("abc/testKey"), expectedIdx: 0, expectedErr: errors.New( - "invalid index: strconv.ParseUint: parsing \"abc\": invalid syntax", + "strconv.ParseUint: parsing \"abc\": invalid syntax", ), }, }