Skip to content

Commit

Permalink
Wrap errors returned from Store (cosmos#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriceLC92 committed Jul 7, 2022
1 parent aaaa20d commit 8cd8832
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
4 changes: 3 additions & 1 deletion rpc/json/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"crypto/rand"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -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), "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(),
Expand All @@ -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))
Expand Down
42 changes: 24 additions & 18 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,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("failed to marshal Block to binary: %w", err)
}

commitBlob, err := commit.MarshalBinary()
if err != nil {
return err
return fmt.Errorf("failed to marshal Commit to binary: %w", err)
}

bb := s.db.NewBatch()
Expand All @@ -78,7 +78,7 @@ func (s *DefaultStore) SaveBlock(block *types.Block, commit *types.Commit) error
}

if err = bb.Commit(); err != nil {
return err
return fmt.Errorf("failed to commit transaction: %w", err)
}

return nil
Expand All @@ -91,7 +91,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("failed to load hash from index: %w", err)
}
return s.LoadBlockByHash(h)
}
Expand All @@ -115,7 +115,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("failed to marshal response: %w", err)
}
return s.db.Set(getResponsesKey(height), data)
}
Expand All @@ -124,18 +124,21 @@ 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("failed to retrieve block results from height %v: %w", height, err)
}
var responses tmstate.ABCIResponses
err = responses.Unmarshal(data)
return &responses, err
if err != nil {
return &responses, fmt.Errorf("failed to unmarshal data: %w", err)
}
return &responses, nil
}

// 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("failed to load hash from index: %w", err)
}
return s.LoadCommitByHash(hash)
}
Expand All @@ -144,19 +147,22 @@ 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("failed to retrieve commit from hash %v: %w", hash, err)
}
commit := new(types.Commit)
err = commit.UnmarshalBinary(commitData)
return commit, err
if err != nil {
return nil, fmt.Errorf("failed to marshal Commit into object: %w", err)
}
return commit, nil
}

// UpdateState updates state saved in Store. Only one State is stored.
// If there is no State in Store, state will be saved.
func (s *DefaultStore) UpdateState(state types.State) error {
pbState, err := state.ToProto()
if err != nil {
return err
return fmt.Errorf("failed to marshal state to JSON: %w", err)
}
data, err := pbState.Marshal()
if err != nil {
Expand All @@ -169,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 state: %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
Expand All @@ -186,11 +192,11 @@ func (s *DefaultStore) LoadState() (types.State, error) {
func (s *DefaultStore) SaveValidators(height uint64, validatorSet *tmtypes.ValidatorSet) error {
pbValSet, err := validatorSet.ToProto()
if err != nil {
return err
return fmt.Errorf("failed to marshal ValidatorSet to protobuf: %w", err)
}
blob, err := pbValSet.Marshal()
if err != nil {
return err
return fmt.Errorf("failed to marshal ValidatorSet: %w", err)
}

return s.db.Set(getValidatorsKey(height), blob)
Expand All @@ -199,12 +205,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("failed to load Validators for height %v: %w", height, err)
}
var pbValSet tmproto.ValidatorSet
err = pbValSet.Unmarshal(blob)
if err != nil {
return nil, err
return nil, fmt.Errorf("failed to unmarshal to protobuf: %w", err)
}

return tmtypes.ValidatorSetFromProto(&pbValSet)
Expand All @@ -215,7 +221,7 @@ func (s *DefaultStore) loadHashFromIndex(height uint64) ([32]byte, error) {

var hash [32]byte
if err != nil {
return hash, 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")
Expand Down

0 comments on commit 8cd8832

Please sign in to comment.