From 275becbad6711617a0bec13c21750a2d145efcea Mon Sep 17 00:00:00 2001 From: Maurice Le Cordier Date: Sat, 26 Mar 2022 09:59:08 +0200 Subject: [PATCH 1/6] wrapped errors in the store.go file --- store/store.go | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/store/store.go b/store/store.go index a8ac1968ca..f02ddc3752 100644 --- a/store/store.go +++ b/store/store.go @@ -57,12 +57,12 @@ func (s *DefaultStore) SaveBlock(block *types.Block, commit *types.Commit) error hash := block.Header.Hash() blockBlob, err := block.MarshalBinary() if err != nil { - return err + return fmt.Errorf("error encoding Block into binary form: %w", err) } commitBlob, err := commit.MarshalBinary() if err != nil { - return err + return fmt.Errorf("error encoding Commit into binary form: %w", err) } s.mtx.Lock() @@ -79,7 +79,7 @@ func (s *DefaultStore) SaveBlock(block *types.Block, commit *types.Commit) error } if err = bb.Commit(); err != nil { - return err + return fmt.Errorf("error committing a transaction: %w", err) } if block.Header.Height > s.height { @@ -96,7 +96,7 @@ func (s *DefaultStore) SaveBlock(block *types.Block, commit *types.Commit) error func (s *DefaultStore) LoadBlock(height uint64) (*types.Block, error) { h, err := s.loadHashFromIndex(height) if err != nil { - return nil, err + return nil, fmt.Errorf("error loading hash from index: %w", err) } return s.LoadBlockByHash(h) } @@ -120,7 +120,7 @@ func (s *DefaultStore) LoadBlockByHash(hash [32]byte) (*types.Block, error) { func (s *DefaultStore) SaveBlockResponses(height uint64, responses *tmstate.ABCIResponses) error { data, err := responses.Marshal() if err != nil { - return err + return fmt.Errorf("error marshalling response: %w", err) } return s.db.Set(getResponsesKey(height), data) } @@ -129,18 +129,18 @@ func (s *DefaultStore) SaveBlockResponses(height uint64, responses *tmstate.ABCI func (s *DefaultStore) LoadBlockResponses(height uint64) (*tmstate.ABCIResponses, error) { data, err := s.db.Get(getResponsesKey(height)) if err != nil { - return nil, err + return nil, fmt.Errorf("error retrieving block results from height %v: %w", height, err) } var responses tmstate.ABCIResponses err = responses.Unmarshal(data) - return &responses, err + return &responses, fmt.Errorf("error unmarshalling data: %w", err) } // LoadCommit returns commit for a block at given height, or error if it's not found in Store. func (s *DefaultStore) LoadCommit(height uint64) (*types.Commit, error) { hash, err := s.loadHashFromIndex(height) if err != nil { - return nil, err + return nil, fmt.Errorf("error loading hash from index: %w", err) } return s.LoadCommitByHash(hash) } @@ -149,11 +149,11 @@ func (s *DefaultStore) LoadCommit(height uint64) (*types.Commit, error) { func (s *DefaultStore) LoadCommitByHash(hash [32]byte) (*types.Commit, error) { commitData, err := s.db.Get(getCommitKey(hash)) if err != nil { - return nil, err + return nil, fmt.Errorf("error retrieving commit from hash %v: %w", hash, err) } commit := new(types.Commit) err = commit.UnmarshalBinary(commitData) - return commit, err + return commit, fmt.Errorf("error decoding binary data of Commit into object: %w", err) } // UpdateState updates state saved in Store. Only one State is stored. @@ -161,7 +161,7 @@ func (s *DefaultStore) LoadCommitByHash(hash [32]byte) (*types.Commit, error) { func (s *DefaultStore) UpdateState(state state.State) error { blob, err := json.Marshal(state) if err != nil { - return err + return fmt.Errorf("error marshalling state into JSON encoding: %w", err) } return s.db.Set(getStateKey(), blob) } @@ -172,24 +172,24 @@ func (s *DefaultStore) LoadState() (state.State, error) { blob, err := s.db.Get(getStateKey()) if err != nil { - return state, err + return state, fmt.Errorf("error marshalling state into JSON encoding: %w", err) } err = json.Unmarshal(blob, &state) s.mtx.Lock() s.height = uint64(state.LastBlockHeight) s.mtx.Unlock() - return state, err + return state, fmt.Errorf("error unmarshalling state from JSON encoding: %w", err) } func (s *DefaultStore) SaveValidators(height uint64, validatorSet *tmtypes.ValidatorSet) error { pbValSet, err := validatorSet.ToProto() if err != nil { - return err + return fmt.Errorf("error converting ValidatorSet to protobuf: %w", err) } blob, err := pbValSet.Marshal() if err != nil { - return err + return fmt.Errorf("error marshalling ValidatorSet: %w", err) } return s.db.Set(getValidatorsKey(height), blob) @@ -198,12 +198,12 @@ func (s *DefaultStore) SaveValidators(height uint64, validatorSet *tmtypes.Valid func (s *DefaultStore) LoadValidators(height uint64) (*tmtypes.ValidatorSet, error) { blob, err := s.db.Get(getValidatorsKey(height)) if err != nil { - return nil, err + return nil, fmt.Errorf("error loading Validators by height %v: %w", height, err) } var pbValSet tmproto.ValidatorSet err = pbValSet.Unmarshal(blob) if err != nil { - return nil, err + return nil, fmt.Errorf("error unmarshalling protobuf ValidatorSet: %w", err) } return tmtypes.ValidatorSetFromProto(&pbValSet) @@ -214,7 +214,7 @@ func (s *DefaultStore) loadHashFromIndex(height uint64) ([32]byte, error) { var hash [32]byte if err != nil { - return hash, err + return hash, fmt.Errorf("error loading hash by height %v: %w", height, err) } if len(blob) != len(hash) { return hash, errors.New("invalid hash length") From 858cef9f3bf09f954e355f8412eb7358a2fad57d Mon Sep 17 00:00:00 2001 From: Maurice Le Cordier Date: Sat, 14 May 2022 15:11:56 +0200 Subject: [PATCH 2/6] updates error to be descriptive for LoadState --- store/store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/store/store.go b/store/store.go index 19714cdcf5..ff9c142686 100644 --- a/store/store.go +++ b/store/store.go @@ -169,7 +169,7 @@ func (s *DefaultStore) LoadState() (state.State, error) { err = json.Unmarshal(blob, &state) atomic.StoreUint64(&s.height, uint64(state.LastBlockHeight)) - return state, err + return state, fmt.Errorf("error unmarshalling state from JSON encoding: %w", err) } func (s *DefaultStore) SaveValidators(height uint64, validatorSet *tmtypes.ValidatorSet) error { From a0307781316392aaa0b9d2352f9ca2f51dce5973 Mon Sep 17 00:00:00 2001 From: Maurice Le Cordier Date: Sun, 15 May 2022 20:23:12 +0200 Subject: [PATCH 3/6] Updated based on PR comments - fixes the failing tests --- rpc/json/service_test.go | 4 +++- store/store.go | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/rpc/json/service_test.go b/rpc/json/service_test.go index d901591f78..ef649fa2ad 100644 --- a/rpc/json/service_test.go +++ b/rpc/json/service_test.go @@ -5,6 +5,7 @@ import ( "context" "crypto/rand" "encoding/json" + "fmt" "net/http" "net/http/httptest" "net/url" @@ -70,7 +71,7 @@ func TestREST(t *testing.T) { {"invalid/missing param", "/block", http.StatusOK, int(json2.E_INVALID_REQ), `missing param 'height'`}, {"valid/no params", "/abci_info", http.StatusOK, -1, `"last_block_height":345`}, // to keep test simple, allow returning application error in following case - {"valid/int param", "/block?height=321", http.StatusOK, int(json2.E_INTERNAL), `"key not found"`}, + {"valid/int param", "/block?height=321", http.StatusOK, int(json2.E_INTERNAL), "error loading hash by height 321: key not found"}, {"invalid/int param", "/block?height=foo", http.StatusOK, int(json2.E_PARSE), "failed to parse param 'height'"}, {"valid/bool int string params", "/tx_search?" + txSearchParams.Encode(), @@ -95,6 +96,7 @@ func TestREST(t *testing.T) { assert.Equal(c.httpCode, resp.Code) s := resp.Body.String() assert.NotEmpty(s) + fmt.Print(s) assert.Contains(s, c.bodyContains) var jsonResp response assert.NoError(json.Unmarshal([]byte(s), &jsonResp)) diff --git a/store/store.go b/store/store.go index ff9c142686..caf43371a5 100644 --- a/store/store.go +++ b/store/store.go @@ -125,7 +125,10 @@ func (s *DefaultStore) LoadBlockResponses(height uint64) (*tmstate.ABCIResponses } var responses tmstate.ABCIResponses err = responses.Unmarshal(data) - return &responses, fmt.Errorf("error unmarshalling data: %w", err) + if err != nil { + return &responses, fmt.Errorf("error unmarshalling data: %w", err) + } + return &responses, nil } // LoadCommit returns commit for a block at given height, or error if it's not found in Store. @@ -145,7 +148,10 @@ func (s *DefaultStore) LoadCommitByHash(hash [32]byte) (*types.Commit, error) { } commit := new(types.Commit) err = commit.UnmarshalBinary(commitData) - return commit, fmt.Errorf("error decoding binary data of Commit into object: %w", err) + if err != nil { + return nil, fmt.Errorf("error decoding binary data of Commit into object: %w", err) + } + return commit, nil } // UpdateState updates state saved in Store. Only one State is stored. @@ -168,8 +174,11 @@ func (s *DefaultStore) LoadState() (state.State, error) { } err = json.Unmarshal(blob, &state) + if err != nil { + return state, fmt.Errorf("error unmarshalling state from JSON encoding: %w", err) + } atomic.StoreUint64(&s.height, uint64(state.LastBlockHeight)) - return state, fmt.Errorf("error unmarshalling state from JSON encoding: %w", err) + return state, nil } func (s *DefaultStore) SaveValidators(height uint64, validatorSet *tmtypes.ValidatorSet) error { From df85882f15e6ee96011c0b01846d0db5cf9d38d9 Mon Sep 17 00:00:00 2001 From: Maurice Le Cordier Date: Wed, 6 Jul 2022 06:58:09 +0200 Subject: [PATCH 4/6] updates error string based on PR comments --- rpc/json/service_test.go | 2 +- store/store.go | 36 ++++++++++++++++++------------------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/rpc/json/service_test.go b/rpc/json/service_test.go index ef649fa2ad..3bce4a91ea 100644 --- a/rpc/json/service_test.go +++ b/rpc/json/service_test.go @@ -71,7 +71,7 @@ func TestREST(t *testing.T) { {"invalid/missing param", "/block", http.StatusOK, int(json2.E_INVALID_REQ), `missing param 'height'`}, {"valid/no params", "/abci_info", http.StatusOK, -1, `"last_block_height":345`}, // to keep test simple, allow returning application error in following case - {"valid/int param", "/block?height=321", http.StatusOK, int(json2.E_INTERNAL), "error loading hash by height 321: key not found"}, + {"valid/int param", "/block?height=321", http.StatusOK, int(json2.E_INTERNAL), "error loading block hash for height"}, {"invalid/int param", "/block?height=foo", http.StatusOK, int(json2.E_PARSE), "failed to parse param 'height'"}, {"valid/bool int string params", "/tx_search?" + txSearchParams.Encode(), diff --git a/store/store.go b/store/store.go index caf43371a5..2ded8444d4 100644 --- a/store/store.go +++ b/store/store.go @@ -52,12 +52,12 @@ func (s *DefaultStore) SaveBlock(block *types.Block, commit *types.Commit) error hash := block.Header.Hash() blockBlob, err := block.MarshalBinary() if err != nil { - return fmt.Errorf("error encoding Block into binary form: %w", err) + return fmt.Errorf("failed to marshal Block to binary: %w", err) } commitBlob, err := commit.MarshalBinary() if err != nil { - return fmt.Errorf("error encoding Commit into binary form: %w", err) + return fmt.Errorf("failed to marshal Commit to binary: %w", err) } bb := s.db.NewBatch() @@ -71,7 +71,7 @@ func (s *DefaultStore) SaveBlock(block *types.Block, commit *types.Commit) error } if err = bb.Commit(); err != nil { - return fmt.Errorf("error committing a transaction: %w", err) + return fmt.Errorf("failed to commit transaction: %w", err) } if block.Header.Height > atomic.LoadUint64(&s.height) { @@ -88,7 +88,7 @@ func (s *DefaultStore) SaveBlock(block *types.Block, commit *types.Commit) error func (s *DefaultStore) LoadBlock(height uint64) (*types.Block, error) { h, err := s.loadHashFromIndex(height) if err != nil { - return nil, fmt.Errorf("error loading hash from index: %w", err) + return nil, fmt.Errorf("failed to load hash from index: %w", err) } return s.LoadBlockByHash(h) } @@ -112,7 +112,7 @@ func (s *DefaultStore) LoadBlockByHash(hash [32]byte) (*types.Block, error) { func (s *DefaultStore) SaveBlockResponses(height uint64, responses *tmstate.ABCIResponses) error { data, err := responses.Marshal() if err != nil { - return fmt.Errorf("error marshalling response: %w", err) + return fmt.Errorf("failed to marshal response: %w", err) } return s.db.Set(getResponsesKey(height), data) } @@ -121,12 +121,12 @@ func (s *DefaultStore) SaveBlockResponses(height uint64, responses *tmstate.ABCI func (s *DefaultStore) LoadBlockResponses(height uint64) (*tmstate.ABCIResponses, error) { data, err := s.db.Get(getResponsesKey(height)) if err != nil { - return nil, fmt.Errorf("error retrieving block results from height %v: %w", height, err) + return nil, fmt.Errorf("failed to retrieve block results from height %v: %w", height, err) } var responses tmstate.ABCIResponses err = responses.Unmarshal(data) if err != nil { - return &responses, fmt.Errorf("error unmarshalling data: %w", err) + return &responses, fmt.Errorf("failed to unmarshal data: %w", err) } return &responses, nil } @@ -135,7 +135,7 @@ func (s *DefaultStore) LoadBlockResponses(height uint64) (*tmstate.ABCIResponses func (s *DefaultStore) LoadCommit(height uint64) (*types.Commit, error) { hash, err := s.loadHashFromIndex(height) if err != nil { - return nil, fmt.Errorf("error loading hash from index: %w", err) + return nil, fmt.Errorf("failed to load hash from index: %w", err) } return s.LoadCommitByHash(hash) } @@ -144,12 +144,12 @@ func (s *DefaultStore) LoadCommit(height uint64) (*types.Commit, error) { func (s *DefaultStore) LoadCommitByHash(hash [32]byte) (*types.Commit, error) { commitData, err := s.db.Get(getCommitKey(hash)) if err != nil { - return nil, fmt.Errorf("error retrieving commit from hash %v: %w", hash, err) + return nil, fmt.Errorf("failed to retrieve commit from hash %v: %w", hash, err) } commit := new(types.Commit) err = commit.UnmarshalBinary(commitData) if err != nil { - return nil, fmt.Errorf("error decoding binary data of Commit into object: %w", err) + return nil, fmt.Errorf("failed to marshal Commit into object: %w", err) } return commit, nil } @@ -159,7 +159,7 @@ func (s *DefaultStore) LoadCommitByHash(hash [32]byte) (*types.Commit, error) { func (s *DefaultStore) UpdateState(state state.State) error { blob, err := json.Marshal(state) if err != nil { - return fmt.Errorf("error marshalling state into JSON encoding: %w", err) + return fmt.Errorf("failed to marshal state to JSON: %w", err) } return s.db.Set(getStateKey(), blob) } @@ -170,12 +170,12 @@ func (s *DefaultStore) LoadState() (state.State, error) { blob, err := s.db.Get(getStateKey()) if err != nil { - return state, fmt.Errorf("error marshalling state into JSON encoding: %w", err) + return state, fmt.Errorf("failed to retrieve key: %w", err) } err = json.Unmarshal(blob, &state) if err != nil { - return state, fmt.Errorf("error unmarshalling state from JSON encoding: %w", err) + return state, fmt.Errorf("failed to unmarshal state from JSON: %w", err) } atomic.StoreUint64(&s.height, uint64(state.LastBlockHeight)) return state, nil @@ -184,11 +184,11 @@ func (s *DefaultStore) LoadState() (state.State, error) { func (s *DefaultStore) SaveValidators(height uint64, validatorSet *tmtypes.ValidatorSet) error { pbValSet, err := validatorSet.ToProto() if err != nil { - return fmt.Errorf("error converting ValidatorSet to protobuf: %w", err) + return fmt.Errorf("failed to marshal ValidatorSet to protobuf: %w", err) } blob, err := pbValSet.Marshal() if err != nil { - return fmt.Errorf("error marshalling ValidatorSet: %w", err) + return fmt.Errorf("failed to marshal ValidatorSet: %w", err) } return s.db.Set(getValidatorsKey(height), blob) @@ -197,12 +197,12 @@ func (s *DefaultStore) SaveValidators(height uint64, validatorSet *tmtypes.Valid func (s *DefaultStore) LoadValidators(height uint64) (*tmtypes.ValidatorSet, error) { blob, err := s.db.Get(getValidatorsKey(height)) if err != nil { - return nil, fmt.Errorf("error loading Validators by height %v: %w", height, err) + return nil, fmt.Errorf("failed to load Validators for height %v: %w", height, err) } var pbValSet tmproto.ValidatorSet err = pbValSet.Unmarshal(blob) if err != nil { - return nil, fmt.Errorf("error unmarshalling protobuf ValidatorSet: %w", err) + return nil, fmt.Errorf("failed to unmarshal to protobuf: %w", err) } return tmtypes.ValidatorSetFromProto(&pbValSet) @@ -213,7 +213,7 @@ func (s *DefaultStore) loadHashFromIndex(height uint64) ([32]byte, error) { var hash [32]byte if err != nil { - return hash, fmt.Errorf("error loading hash by height %v: %w", height, err) + return hash, fmt.Errorf("failed to load block hash for height %v: %w", height, err) } if len(blob) != len(hash) { return hash, errors.New("invalid hash length") From da1acdff3262f5997ca64293d7f695865d5150a2 Mon Sep 17 00:00:00 2001 From: Maurice Le Cordier Date: Wed, 6 Jul 2022 07:03:51 +0200 Subject: [PATCH 5/6] add formatted error to LoadState --- store/store.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/store/store.go b/store/store.go index 6e1860404b..77fa78db5f 100644 --- a/store/store.go +++ b/store/store.go @@ -175,12 +175,12 @@ func (s *DefaultStore) UpdateState(state types.State) error { func (s *DefaultStore) LoadState() (types.State, error) { blob, err := s.db.Get(getStateKey()) if err != nil { - return types.State{}, err + return types.State{}, fmt.Errorf("failed to retrieve key: %w", err) } var pbState pb.State err = pbState.Unmarshal(blob) if err != nil { - return types.State{}, err + return types.State{}, fmt.Errorf("failed to unmarshal state from JSON: %w", err) } var state types.State From 678130f2483aa7567fe63894740e4dfe8d984478 Mon Sep 17 00:00:00 2001 From: Maurice Le Cordier Date: Thu, 7 Jul 2022 20:18:14 +0200 Subject: [PATCH 6/6] updated LoadState error string --- rpc/json/service_test.go | 2 +- store/store.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rpc/json/service_test.go b/rpc/json/service_test.go index d899d81d7f..91c591e6e3 100644 --- a/rpc/json/service_test.go +++ b/rpc/json/service_test.go @@ -71,7 +71,7 @@ func TestREST(t *testing.T) { {"invalid/missing param", "/block", http.StatusOK, int(json2.E_INVALID_REQ), `missing param 'height'`}, {"valid/no params", "/abci_info", http.StatusOK, -1, `"last_block_height":"345"`}, // to keep test simple, allow returning application error in following case - {"valid/int param", "/block?height=321", http.StatusOK, int(json2.E_INTERNAL), "error loading block hash for height"}, + {"valid/int param", "/block?height=321", http.StatusOK, int(json2.E_INTERNAL), "failed to load hash from index"}, {"invalid/int param", "/block?height=foo", http.StatusOK, int(json2.E_PARSE), "failed to parse param 'height'"}, {"valid/bool int string params", "/tx_search?" + txSearchParams.Encode(), diff --git a/store/store.go b/store/store.go index 77fa78db5f..3db9f72401 100644 --- a/store/store.go +++ b/store/store.go @@ -175,7 +175,7 @@ func (s *DefaultStore) UpdateState(state types.State) error { func (s *DefaultStore) LoadState() (types.State, error) { blob, err := s.db.Get(getStateKey()) if err != nil { - return types.State{}, fmt.Errorf("failed to retrieve key: %w", err) + return types.State{}, fmt.Errorf("failed to retrieve state: %w", err) } var pbState pb.State err = pbState.Unmarshal(blob)