@@ -59,12 +59,12 @@ func (s *DefaultStore) SaveBlock(block *types.Block, commit *types.Commit) error
59
59
hash := block .Header .Hash ()
60
60
blockBlob , err := block .MarshalBinary ()
61
61
if err != nil {
62
- return err
62
+ return fmt . Errorf ( "failed to marshal Block to binary: %w" , err )
63
63
}
64
64
65
65
commitBlob , err := commit .MarshalBinary ()
66
66
if err != nil {
67
- return err
67
+ return fmt . Errorf ( "failed to marshal Commit to binary: %w" , err )
68
68
}
69
69
70
70
bb := s .db .NewBatch ()
@@ -78,7 +78,7 @@ func (s *DefaultStore) SaveBlock(block *types.Block, commit *types.Commit) error
78
78
}
79
79
80
80
if err = bb .Commit (); err != nil {
81
- return err
81
+ return fmt . Errorf ( "failed to commit transaction: %w" , err )
82
82
}
83
83
84
84
return nil
@@ -91,7 +91,7 @@ func (s *DefaultStore) SaveBlock(block *types.Block, commit *types.Commit) error
91
91
func (s * DefaultStore ) LoadBlock (height uint64 ) (* types.Block , error ) {
92
92
h , err := s .loadHashFromIndex (height )
93
93
if err != nil {
94
- return nil , err
94
+ return nil , fmt . Errorf ( "failed to load hash from index: %w" , err )
95
95
}
96
96
return s .LoadBlockByHash (h )
97
97
}
@@ -115,7 +115,7 @@ func (s *DefaultStore) LoadBlockByHash(hash [32]byte) (*types.Block, error) {
115
115
func (s * DefaultStore ) SaveBlockResponses (height uint64 , responses * tmstate.ABCIResponses ) error {
116
116
data , err := responses .Marshal ()
117
117
if err != nil {
118
- return err
118
+ return fmt . Errorf ( "failed to marshal response: %w" , err )
119
119
}
120
120
return s .db .Set (getResponsesKey (height ), data )
121
121
}
@@ -124,18 +124,21 @@ func (s *DefaultStore) SaveBlockResponses(height uint64, responses *tmstate.ABCI
124
124
func (s * DefaultStore ) LoadBlockResponses (height uint64 ) (* tmstate.ABCIResponses , error ) {
125
125
data , err := s .db .Get (getResponsesKey (height ))
126
126
if err != nil {
127
- return nil , err
127
+ return nil , fmt . Errorf ( "failed to retrieve block results from height %v: %w" , height , err )
128
128
}
129
129
var responses tmstate.ABCIResponses
130
130
err = responses .Unmarshal (data )
131
- return & responses , err
131
+ if err != nil {
132
+ return & responses , fmt .Errorf ("failed to unmarshal data: %w" , err )
133
+ }
134
+ return & responses , nil
132
135
}
133
136
134
137
// LoadCommit returns commit for a block at given height, or error if it's not found in Store.
135
138
func (s * DefaultStore ) LoadCommit (height uint64 ) (* types.Commit , error ) {
136
139
hash , err := s .loadHashFromIndex (height )
137
140
if err != nil {
138
- return nil , err
141
+ return nil , fmt . Errorf ( "failed to load hash from index: %w" , err )
139
142
}
140
143
return s .LoadCommitByHash (hash )
141
144
}
@@ -144,19 +147,22 @@ func (s *DefaultStore) LoadCommit(height uint64) (*types.Commit, error) {
144
147
func (s * DefaultStore ) LoadCommitByHash (hash [32 ]byte ) (* types.Commit , error ) {
145
148
commitData , err := s .db .Get (getCommitKey (hash ))
146
149
if err != nil {
147
- return nil , err
150
+ return nil , fmt . Errorf ( "failed to retrieve commit from hash %v: %w" , hash , err )
148
151
}
149
152
commit := new (types.Commit )
150
153
err = commit .UnmarshalBinary (commitData )
151
- return commit , err
154
+ if err != nil {
155
+ return nil , fmt .Errorf ("failed to marshal Commit into object: %w" , err )
156
+ }
157
+ return commit , nil
152
158
}
153
159
154
160
// UpdateState updates state saved in Store. Only one State is stored.
155
161
// If there is no State in Store, state will be saved.
156
162
func (s * DefaultStore ) UpdateState (state types.State ) error {
157
163
pbState , err := state .ToProto ()
158
164
if err != nil {
159
- return err
165
+ return fmt . Errorf ( "failed to marshal state to JSON: %w" , err )
160
166
}
161
167
data , err := pbState .Marshal ()
162
168
if err != nil {
@@ -169,12 +175,12 @@ func (s *DefaultStore) UpdateState(state types.State) error {
169
175
func (s * DefaultStore ) LoadState () (types.State , error ) {
170
176
blob , err := s .db .Get (getStateKey ())
171
177
if err != nil {
172
- return types.State {}, err
178
+ return types.State {}, fmt . Errorf ( "failed to retrieve state: %w" , err )
173
179
}
174
180
var pbState pb.State
175
181
err = pbState .Unmarshal (blob )
176
182
if err != nil {
177
- return types.State {}, err
183
+ return types.State {}, fmt . Errorf ( "failed to unmarshal state from JSON: %w" , err )
178
184
}
179
185
180
186
var state types.State
@@ -186,11 +192,11 @@ func (s *DefaultStore) LoadState() (types.State, error) {
186
192
func (s * DefaultStore ) SaveValidators (height uint64 , validatorSet * tmtypes.ValidatorSet ) error {
187
193
pbValSet , err := validatorSet .ToProto ()
188
194
if err != nil {
189
- return err
195
+ return fmt . Errorf ( "failed to marshal ValidatorSet to protobuf: %w" , err )
190
196
}
191
197
blob , err := pbValSet .Marshal ()
192
198
if err != nil {
193
- return err
199
+ return fmt . Errorf ( "failed to marshal ValidatorSet: %w" , err )
194
200
}
195
201
196
202
return s .db .Set (getValidatorsKey (height ), blob )
@@ -199,12 +205,12 @@ func (s *DefaultStore) SaveValidators(height uint64, validatorSet *tmtypes.Valid
199
205
func (s * DefaultStore ) LoadValidators (height uint64 ) (* tmtypes.ValidatorSet , error ) {
200
206
blob , err := s .db .Get (getValidatorsKey (height ))
201
207
if err != nil {
202
- return nil , err
208
+ return nil , fmt . Errorf ( "failed to load Validators for height %v: %w" , height , err )
203
209
}
204
210
var pbValSet tmproto.ValidatorSet
205
211
err = pbValSet .Unmarshal (blob )
206
212
if err != nil {
207
- return nil , err
213
+ return nil , fmt . Errorf ( "failed to unmarshal to protobuf: %w" , err )
208
214
}
209
215
210
216
return tmtypes .ValidatorSetFromProto (& pbValSet )
@@ -215,7 +221,7 @@ func (s *DefaultStore) loadHashFromIndex(height uint64) ([32]byte, error) {
215
221
216
222
var hash [32 ]byte
217
223
if err != nil {
218
- return hash , err
224
+ return hash , fmt . Errorf ( "failed to load block hash for height %v: %w" , height , err )
219
225
}
220
226
if len (blob ) != len (hash ) {
221
227
return hash , errors .New ("invalid hash length" )
0 commit comments