-
Notifications
You must be signed in to change notification settings - Fork 648
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
02-client-refactor: rename update to UpdateState for 07-tendermint #1117
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8e1309b
rename update to UpdateState
colin-axner a8203b9
Merge branch '02-client-refactor' into colin/879-tm-updatestate
seantking 6658bea
fix: duplicate update check was performing incorrect logic
colin-axner 4863e98
update godoc
colin-axner 3e07080
add UpdateState tests
colin-axner 8c573de
update godoc
colin-axner 6e63509
chore: fix code spacing
colin-axner e1dbbd9
Merge branch '02-client-refactor' into colin/879-tm-updatestate
colin-axner ef300da
Merge branch '02-client-refactor' into colin/879-tm-updatestate
colin-axner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package types | |
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"reflect" | ||
"time" | ||
|
||
|
@@ -110,35 +111,10 @@ func (cs ClientState) CheckHeaderAndUpdateState( | |
return &cs, consState, nil | ||
} | ||
|
||
// Check the earliest consensus state to see if it is expired, if so then set the prune height | ||
// so that we can delete consensus state and all associated metadata. | ||
var ( | ||
pruneHeight exported.Height | ||
pruneError error | ||
) | ||
pruneCb := func(height exported.Height) bool { | ||
consState, err := GetConsensusState(clientStore, cdc, height) | ||
// this error should never occur | ||
if err != nil { | ||
pruneError = err | ||
return true | ||
} | ||
if cs.IsExpired(consState.Timestamp, ctx.BlockTime()) { | ||
pruneHeight = height | ||
} | ||
return true | ||
} | ||
IterateConsensusStateAscending(clientStore, pruneCb) | ||
if pruneError != nil { | ||
return nil, nil, pruneError | ||
} | ||
// if pruneHeight is set, delete consensus state and metadata | ||
if pruneHeight != nil { | ||
deleteConsensusState(clientStore, pruneHeight) | ||
deleteConsensusMetadata(clientStore, pruneHeight) | ||
newClientState, consensusState, err := cs.UpdateState(ctx, cdc, clientStore, tmHeader) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
newClientState, consensusState := update(ctx, clientStore, &cs, tmHeader) | ||
return newClientState, consensusState, nil | ||
} | ||
|
||
|
@@ -244,11 +220,24 @@ func checkValidity( | |
return nil | ||
} | ||
|
||
// update the consensus state from a new header and set processed time metadata | ||
func update(ctx sdk.Context, clientStore sdk.KVStore, clientState *ClientState, header *Header) (*ClientState, *ConsensusState) { | ||
// UpdateState sets the consensus state associated with the provided header and sets the consensus metadata. | ||
func (cs ClientState) UpdateState(ctx sdk.Context, cdc codec.BinaryCodec, clientStore sdk.KVStore, msg exported.ClientMessage) (*ClientState, *ConsensusState, error) { | ||
header, ok := msg.(*Header) | ||
if !ok { | ||
panic(fmt.Sprintf("client state can only be updated with a Header: expected %T, got %T)", &Header{}, msg)) | ||
} | ||
|
||
// check for duplicate update | ||
if consensusState, err := GetConsensusState(clientStore, cdc, header.GetHeight()); err != nil { | ||
// perform no-op | ||
return &cs, consensusState, nil | ||
} | ||
|
||
cs.pruneOldestConsensusState(ctx, cdc, clientStore) | ||
|
||
height := header.GetHeight().(clienttypes.Height) | ||
if height.GT(clientState.LatestHeight) { | ||
clientState.LatestHeight = height | ||
if height.GT(cs.LatestHeight) { | ||
cs.LatestHeight = height | ||
} | ||
consensusState := &ConsensusState{ | ||
Timestamp: header.GetTime(), | ||
|
@@ -259,5 +248,37 @@ func update(ctx sdk.Context, clientStore sdk.KVStore, clientState *ClientState, | |
// set metadata for this consensus state | ||
setConsensusMetadata(ctx, clientStore, header.GetHeight()) | ||
|
||
return clientState, consensusState | ||
return &cs, consensusState, nil | ||
} | ||
|
||
// pruneOldestConsensusState attempts to prune the oldest consensus state. The consensus state will only be pruned | ||
// if it is expired. | ||
func (cs ClientState) pruneOldestConsensusState(ctx sdk.Context, cdc codec.BinaryCodec, clientStore sdk.KVStore) { | ||
// Check the earliest consensus state to see if it is expired, if so then set the prune height | ||
// so that we can delete consensus state and all associated metadata. | ||
var ( | ||
pruneHeight exported.Height | ||
pruneError error | ||
) | ||
pruneCb := func(height exported.Height) bool { | ||
consState, err := GetConsensusState(clientStore, cdc, height) | ||
// this error should never occur | ||
if err != nil { | ||
pruneError = err | ||
return true | ||
} | ||
if cs.IsExpired(consState.Timestamp, ctx.BlockTime()) { | ||
pruneHeight = height | ||
} | ||
return true | ||
} | ||
IterateConsensusStateAscending(clientStore, pruneCb) | ||
if pruneError != nil { | ||
panic(pruneError) | ||
} | ||
Comment on lines
+276
to
+278
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should just panic within the iterate function, but I can fix in a followup pr |
||
// if pruneHeight is set, delete consensus state and metadata | ||
if pruneHeight != nil { | ||
deleteConsensusState(clientStore, pruneHeight) | ||
deleteConsensusMetadata(clientStore, pruneHeight) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: add some logical spacing between code in this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good. Copied from existing code, but agree it could use spacing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated!