Skip to content

Commit

Permalink
Add 'snapshotHash' field to '/debug/stateHash' HTTP responses (#1330)
Browse files Browse the repository at this point in the history
* Add 'snapshotHash' field to '/debug/stateHash' HTTP responses.

* Small fix in 'NodeApi.snapshotStateHash'.
  • Loading branch information
nickeskov authored Feb 13, 2024
1 parent 9538513 commit 24ad13d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
25 changes: 23 additions & 2 deletions pkg/api/node_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,12 @@ func (a *NodeApi) stateHashDebug(height proto.Height) (*proto.StateHashDebug, er
if err != nil {
return nil, errors.Wrapf(err, "failed to get state hash at height %d", height)
}
stateHashDebug := proto.NewStateHashJSDebug(*stateHash, height, a.app.version().Version)
snapshotStateHash, err := a.state.SnapshotStateHashAtHeight(height)
if err != nil {
return nil, errors.Wrapf(err, "failed to get snapshot state hash at height %d", height)
}
version := a.app.version().Version
stateHashDebug := proto.NewStateHashJSDebug(*stateHash, height, version, snapshotStateHash)
return &stateHashDebug, nil
}

Expand All @@ -804,8 +809,14 @@ func (a *NodeApi) stateHash(w http.ResponseWriter, r *http.Request) error {
// TODO(nickeskov): which error it should send?
return &BadRequestError{err}
}
if height < 1 {
return apiErrs.BlockDoesNotExist
}
stateHashDebug, err := a.stateHashDebug(height)
if err != nil {
if state.IsNotFound(err) {
return apiErrs.BlockDoesNotExist
}
return errors.Wrap(err, "failed to get state hash debug")
}

Expand All @@ -820,8 +831,15 @@ func (a *NodeApi) stateHashLast(w http.ResponseWriter, _ *http.Request) error {
if err != nil {
return errors.Wrap(err, "failed to get last height")
}
stateHashDebug, err := a.stateHashDebug(height - 1)
h := height - 1
if h < 1 {
return apiErrs.BlockDoesNotExist
}
stateHashDebug, err := a.stateHashDebug(h)
if err != nil {
if state.IsNotFound(err) {
return apiErrs.BlockDoesNotExist
}
return errors.Wrap(err, "failed to get last state hash")
}
if err := trySendJson(w, stateHashDebug); err != nil {
Expand All @@ -837,6 +855,9 @@ func (a *NodeApi) snapshotStateHash(w http.ResponseWriter, r *http.Request) erro
// TODO(nickeskov): which error it should send?
return &BadRequestError{err}
}
if height < 1 {
return apiErrs.BlockDoesNotExist
}
sh, err := a.state.SnapshotStateHashAtHeight(height)
if err != nil {
if state.IsNotFound(err) {
Expand Down
9 changes: 5 additions & 4 deletions pkg/proto/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4370,12 +4370,13 @@ func (s *StateHash) toStateHashJS() stateHashJS {

type StateHashDebug struct {
stateHashJS
Height uint64 `json:"height,omitempty"`
Version string `json:"version,omitempty"`
Height uint64 `json:"height,omitempty"`
Version string `json:"version,omitempty"`
SnapshotHash crypto.Digest `json:"snapshotHash"`
}

func NewStateHashJSDebug(s StateHash, h uint64, v string) StateHashDebug {
return StateHashDebug{s.toStateHashJS(), h, v}
func NewStateHashJSDebug(s StateHash, h uint64, v string, snapshotStateHash crypto.Digest) StateHashDebug {
return StateHashDebug{s.toStateHashJS(), h, v, snapshotStateHash}
}

func (s StateHashDebug) GetStateHash() *StateHash {
Expand Down

0 comments on commit 24ad13d

Please sign in to comment.