-
Notifications
You must be signed in to change notification settings - Fork 78
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
Add high watermark to slashing database #896
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
69b62c6
Add high_watermark data to metadata db table
siladu 24f0dc6
Add PL/pgSQL triggers as constraints for checking high_watermarks are…
siladu bab8bb2
Add PL/pgSQL trigger as constraints for checking low_watermarks are l…
siladu cd69f99
Merge remote-tracking branch 'upstream/master' into add-high-watermar…
siladu 00bed32
Merge branch 'master' into add-high-watermark-metadata
siladu 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
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
60 changes: 60 additions & 0 deletions
60
...rotection/src/main/java/tech/pegasys/web3signer/slashingprotection/dao/HighWatermark.java
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2023 ConsenSys AG. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
package tech.pegasys.web3signer.slashingprotection.dao; | ||
|
||
import java.util.Objects; | ||
|
||
import org.apache.tuweni.units.bigints.UInt64; | ||
|
||
public class HighWatermark { | ||
|
||
private UInt64 slot; | ||
private UInt64 epoch; | ||
|
||
// needed for JDBI | ||
public HighWatermark() {} | ||
|
||
public HighWatermark(final UInt64 slot, final UInt64 epoch) { | ||
this.slot = slot; | ||
this.epoch = epoch; | ||
} | ||
|
||
public UInt64 getSlot() { | ||
return slot; | ||
} | ||
|
||
public UInt64 getEpoch() { | ||
return epoch; | ||
} | ||
|
||
public void setSlot(final UInt64 slot) { | ||
this.slot = slot; | ||
} | ||
|
||
public void setEpoch(final UInt64 epoch) { | ||
this.epoch = epoch; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
HighWatermark that = (HighWatermark) o; | ||
return Objects.equals(slot, that.slot) && Objects.equals(epoch, that.epoch); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(slot, epoch); | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
...rotection/src/main/resources/migrations/postgresql/V00012__add_highwatermark_metadata.sql
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
ALTER TABLE metadata | ||
ADD COLUMN high_watermark_epoch NUMERIC(20), | ||
ADD COLUMN high_watermark_slot NUMERIC(20); | ||
jframe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
-- inserted high watermark should be above low watermark | ||
|
||
CREATE OR REPLACE FUNCTION check_high_watermarks() RETURNS TRIGGER AS $$ | ||
DECLARE | ||
max_slot NUMERIC(20); | ||
max_epoch NUMERIC(20); | ||
BEGIN | ||
SELECT MAX(slot) INTO max_slot FROM low_watermarks; | ||
jframe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SELECT GREATEST(MAX(target_epoch), MAX(source_epoch)) INTO max_epoch FROM low_watermarks; | ||
|
||
IF NEW.high_watermark_slot <= max_slot THEN | ||
RAISE EXCEPTION 'Insert/Update violates constraint: high_watermark_slot must be greater than max slot in low_watermarks table'; | ||
END IF; | ||
|
||
IF NEW.high_watermark_epoch <= max_epoch THEN | ||
RAISE EXCEPTION 'Insert/Update violates constraint: high_watermark_epoch must be greater than max epoch in low_watermarks table'; | ||
END IF; | ||
|
||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER check_before_insert_or_update_high_watermarks | ||
jframe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
BEFORE INSERT OR UPDATE ON metadata | ||
FOR EACH ROW EXECUTE PROCEDURE check_high_watermarks(); | ||
|
||
|
||
-- inserted low watermark should be below or the same as high watermark | ||
|
||
CREATE OR REPLACE FUNCTION check_low_watermarks() RETURNS TRIGGER AS $$ | ||
DECLARE | ||
high_slot NUMERIC(20); | ||
high_epoch NUMERIC(20); | ||
BEGIN | ||
SELECT MIN(high_watermark_slot) INTO high_slot FROM metadata; | ||
SELECT MIN(high_watermark_epoch) INTO high_epoch FROM metadata; | ||
|
||
IF NEW.slot > high_slot THEN | ||
RAISE EXCEPTION 'Insert/Update violates constraint: low_watermark slot must be less than or equal to high_watermark_slot in the metadata table'; | ||
END IF; | ||
|
||
IF NEW.source_epoch > high_epoch THEN | ||
RAISE EXCEPTION 'Insert/Update violates constraint: low_watermark source epoch must be less than or equal to high_watermark_epoch in the metadata table'; | ||
END IF; | ||
|
||
IF NEW.target_epoch > high_epoch THEN | ||
RAISE EXCEPTION 'Insert/Update violates constraint: low_watermark target epoch must be less than or equal to high_watermark_epoch in the metadata table'; | ||
END IF; | ||
|
||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER check_before_insert_or_update_low_watermarks | ||
BEFORE INSERT OR UPDATE ON low_watermarks | ||
FOR EACH ROW EXECUTE PROCEDURE check_low_watermarks(); | ||
|
||
|
||
UPDATE database_version SET version = 12 WHERE id = 1; |
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
Oops, something went wrong.
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.
Does this need a constraint on the high watermark being height then all of the low watermarks?
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.
I didn't add anything around that because this AC simplifies it:
...but: even though you could still set a HWM such that LWM < HWM < latestSignedData, it's probably not a bad (and cheap?) check to have in place. I will look into it.