Skip to content
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 Maybe to the end bound of proofs (Part 2) #1813

Merged
merged 49 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
1d62cda
Add Maybe to end For some Methods
dboehm-avalabs Aug 2, 2023
aa84659
update sync messages
dboehm-avalabs Aug 2, 2023
4124fcd
Update client_test.go
dboehm-avalabs Aug 2, 2023
48100d4
more
dboehm-avalabs Aug 2, 2023
15bbbed
more
dboehm-avalabs Aug 2, 2023
02a6ec7
Update db.go
dboehm-avalabs Aug 2, 2023
4a7d224
fix network client and server
dboehm-avalabs Aug 2, 2023
3d44e2d
lint
dboehm-avalabs Aug 2, 2023
f931491
Merge branch 'dev' into MaybeBounds1
dboehm-avalabs Aug 2, 2023
716d27b
Merge branch 'dev' into MaybeBounds1
dboehm-avalabs Aug 2, 2023
06f7433
improve
dboehm-avalabs Aug 2, 2023
d2c0e6e
Merge branch 'MaybeBounds1' of https://github.com/ava-labs/avalancheg…
dboehm-avalabs Aug 2, 2023
869249c
Update proof_test.go
dboehm-avalabs Aug 2, 2023
c4d3100
Update db.go
dboehm-avalabs Aug 2, 2023
67df793
Update sync_test.go
dboehm-avalabs Aug 3, 2023
a005025
Update sync_test.go
dboehm-avalabs Aug 3, 2023
48f05c0
Update manager.go
dboehm-avalabs Aug 3, 2023
b5ec872
Update manager.go
dboehm-avalabs Aug 3, 2023
2a00683
Update manager.go
dboehm-avalabs Aug 3, 2023
2439323
Update manager.go
dboehm-avalabs Aug 3, 2023
20426dd
Merge branch 'dev' into MaybeBounds1
dboehm-avalabs Aug 3, 2023
9d5290b
merge
dboehm-avalabs Aug 3, 2023
f5be4fc
fix
dboehm-avalabs Aug 3, 2023
8d88e88
remove
dboehm-avalabs Aug 3, 2023
c6f57da
Merge branch 'dev' into MaybeBounds1
Aug 3, 2023
9cefc79
add maybe compare
dboehm-avalabs Aug 4, 2023
f174079
Merge branch 'MaybeBounds1' of https://github.com/ava-labs/avalancheg…
dboehm-avalabs Aug 4, 2023
b2a3bd8
Update maybe.go
dboehm-avalabs Aug 4, 2023
fc4dcc7
Update End Bounds
dboehm-avalabs Aug 4, 2023
1885715
Merge branch 'dev' into MaybeBounds1
dboehm-avalabs Aug 4, 2023
1838bdc
comment
dboehm-avalabs Aug 7, 2023
a29cf20
Merge branch 'MaybeBounds1' into MaybeBounds2
dboehm-avalabs Aug 7, 2023
dcd8be0
Update manager.go
dboehm-avalabs Aug 7, 2023
be4f02e
Update proof_test.go
dboehm-avalabs Aug 7, 2023
e6634cb
unexport MaybeBytesToMaybe
dboehm-avalabs Aug 7, 2023
0b301e7
move maybeBytesToMaybe closer
dboehm-avalabs Aug 7, 2023
58ef8b4
comment
Aug 7, 2023
52506bd
comment
Aug 7, 2023
282a14f
Merge branch 'dev' into MaybeBounds2
dboehm-avalabs Aug 7, 2023
7e5dc08
Merge branch 'MaybeBounds2' of https://github.com/ava-labs/avalancheg…
dboehm-avalabs Aug 7, 2023
b1e21f1
Update db.go
dboehm-avalabs Aug 7, 2023
0b31339
Update db_test.go
dboehm-avalabs Aug 7, 2023
fc5e07b
Update manager.go
dboehm-avalabs Aug 7, 2023
9815760
remove convert function
dboehm-avalabs Aug 8, 2023
27d5cf0
cleanup
dboehm-avalabs Aug 8, 2023
2148711
rename
dboehm-avalabs Aug 8, 2023
bd1c538
Update maybe_test.go
dboehm-avalabs Aug 8, 2023
974b4ca
Merge branch 'dev' into MaybeBounds2
dboehm-avalabs Aug 8, 2023
26b8c7d
Merge branch 'dev' into MaybeBounds2
Aug 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
374 changes: 189 additions & 185 deletions proto/pb/sync/sync.pb.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions proto/sync/sync.proto
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ message GetChangeProofRequest {
bytes start_root_hash = 1;
bytes end_root_hash = 2;
bytes start_key = 3;
bytes end_key = 4;
MaybeBytes end_key = 4;
uint32 key_limit = 5;
}

Expand Down Expand Up @@ -104,7 +104,7 @@ message SyncGetRangeProofRequest {
message GetRangeProofRequest {
bytes root_hash = 1;
bytes start_key = 2;
bytes end_key = 3;
MaybeBytes end_key = 3;
uint32 key_limit = 4;
}

Expand Down
34 changes: 15 additions & 19 deletions x/merkledb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type ChangeProofer interface {
startRootID ids.ID,
endRootID ids.ID,
start []byte,
end []byte,
end Maybe[[]byte],
maxLength int,
) (*ChangeProof, error)

Expand Down Expand Up @@ -97,8 +97,8 @@ type RangeProofer interface {
GetRangeProofAtRoot(
ctx context.Context,
rootID ids.ID,
start,
end []byte,
start []byte,
end Maybe[[]byte],
maxLength int,
) (*RangeProof, error)

Expand Down Expand Up @@ -480,8 +480,8 @@ func (db *merkleDB) getProof(ctx context.Context, key []byte) (*Proof, error) {
// [start, end].
func (db *merkleDB) GetRangeProof(
ctx context.Context,
start,
end []byte,
start []byte,
end Maybe[[]byte],
maxLength int,
) (*RangeProof, error) {
db.commitLock.RLock()
Expand All @@ -495,8 +495,8 @@ func (db *merkleDB) GetRangeProof(
func (db *merkleDB) GetRangeProofAtRoot(
ctx context.Context,
rootID ids.ID,
start,
end []byte,
start []byte,
end Maybe[[]byte],
maxLength int,
) (*RangeProof, error) {
db.commitLock.RLock()
Expand All @@ -509,8 +509,8 @@ func (db *merkleDB) GetRangeProofAtRoot(
func (db *merkleDB) getRangeProofAtRoot(
ctx context.Context,
rootID ids.ID,
start,
end []byte,
start []byte,
end Maybe[[]byte],
maxLength int,
) (*RangeProof, error) {
if db.closed {
Expand All @@ -532,10 +532,10 @@ func (db *merkleDB) GetChangeProof(
startRootID ids.ID,
endRootID ids.ID,
start []byte,
end []byte,
end Maybe[[]byte],
maxLength int,
) (*ChangeProof, error) {
if len(end) > 0 && bytes.Compare(start, end) == 1 {
if end.HasValue() && bytes.Compare(start, end.Value()) == 1 {
return nil, ErrStartAfterEnd
}
if startRootID == endRootID {
Expand Down Expand Up @@ -582,16 +582,14 @@ func (db *merkleDB) GetChangeProof(
})
}

largestKey := Nothing[[]byte]()
largestKey := end
if len(result.KeyChanges) > 0 {
largestKey = Some(result.KeyChanges[len(result.KeyChanges)-1].Key)
} else if len(end) > 0 {
largestKey = Some(end)
}

// Since we hold [db.commitlock] we must still have sufficient
// history to recreate the trie at [endRootID].
historicalView, err := db.getHistoricalViewForRange(endRootID, start, largestKey.Value())
historicalView, err := db.getHistoricalViewForRange(endRootID, start, largestKey)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1006,14 +1004,12 @@ func (db *merkleDB) VerifyChangeProof(
// Find the greatest key in [proof.KeyChanges]
// Note that [proof.EndProof] is a proof for this key.
// [largestPath] is also used when we add children of proof nodes to [trie] below.
largestPath := Nothing[path]()
largestPath := BindMaybe(end, newPath)
if len(proof.KeyChanges) > 0 {
// If [proof] has key-value pairs, we should insert children
// greater than [end] to ancestors of the node containing [end]
// so that we get the expected root ID.
largestPath = Some(newPath(proof.KeyChanges[len(proof.KeyChanges)-1].Key))
} else if end.HasValue() {
largestPath = Some(newPath(end.Value()))
}

// Make sure the end proof, if given, is well-formed.
Expand Down Expand Up @@ -1169,7 +1165,7 @@ func (db *merkleDB) initializeRootIfNeeded() (ids.ID, error) {
func (db *merkleDB) getHistoricalViewForRange(
rootID ids.ID,
start []byte,
end []byte,
end Maybe[[]byte],
) (*trieView, error) {
currentRootID := db.getMerkleRoot()

Expand Down
21 changes: 12 additions & 9 deletions x/merkledb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func Test_MerkleDB_Commit_Proof_To_Empty_Trie(t *testing.T) {
require.NoError(batch.Put([]byte("key3"), []byte("3")))
require.NoError(batch.Write())

proof, err := db.GetRangeProof(context.Background(), []byte("key1"), []byte("key3"), 10)
proof, err := db.GetRangeProof(context.Background(), []byte("key1"), Some([]byte("key3")), 10)
require.NoError(err)

freshDB, err := getBasicDB()
Expand Down Expand Up @@ -289,7 +289,7 @@ func Test_MerkleDB_Commit_Proof_To_Filled_Trie(t *testing.T) {
require.NoError(batch.Put([]byte("key3"), []byte("3")))
require.NoError(batch.Write())

proof, err := db.GetRangeProof(context.Background(), []byte("key1"), []byte("key3"), 10)
proof, err := db.GetRangeProof(context.Background(), []byte("key1"), Some([]byte("key3")), 10)
require.NoError(err)

freshDB, err := getBasicDB()
Expand Down Expand Up @@ -768,12 +768,13 @@ func runRandDBTest(require *require.Assertions, r *rand.Rand, rt randTest) {
if len(pastRoots) > 0 {
root = pastRoots[r.Intn(len(pastRoots))]
}
rangeProof, err := db.GetRangeProofAtRoot(context.Background(), root, step.key, step.value, 100)
require.NoError(err)
end := Nothing[[]byte]()
if len(step.value) > 0 {
end = Some(step.value)
}

rangeProof, err := db.GetRangeProofAtRoot(context.Background(), root, step.key, end, 100)
require.NoError(err)
require.NoError(rangeProof.Verify(
context.Background(),
step.key,
Expand All @@ -787,18 +788,20 @@ func runRandDBTest(require *require.Assertions, r *rand.Rand, rt randTest) {
if len(pastRoots) > 1 {
root = pastRoots[r.Intn(len(pastRoots))]
}
changeProof, err := db.GetChangeProof(context.Background(), startRoot, root, step.key, step.value, 100)
end := Nothing[[]byte]()
if len(step.value) > 0 {
end = Some(step.value)
}

changeProof, err := db.GetChangeProof(context.Background(), startRoot, root, step.key, end, 100)
if startRoot == root {
require.ErrorIs(err, errSameRoot)
continue
}
require.NoError(err)
changeProofDB, err := getBasicDB()
require.NoError(err)
end := Nothing[[]byte]()
if len(step.value) > 0 {
end = Some(step.value)
}

require.NoError(changeProofDB.VerifyChangeProof(
context.Background(),
changeProof,
Expand Down
12 changes: 6 additions & 6 deletions x/merkledb/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func newTrieHistory(maxHistoryLookback int) *trieHistory {

// Returns up to [maxLength] key-value pair changes with keys in [start, end] that
// occurred between [startRoot] and [endRoot].
func (th *trieHistory) getValueChanges(startRoot, endRoot ids.ID, start, end []byte, maxLength int) (*changeSummary, error) {
func (th *trieHistory) getValueChanges(startRoot, endRoot ids.ID, start []byte, end Maybe[[]byte], maxLength int) (*changeSummary, error) {
if maxLength <= 0 {
return nil, fmt.Errorf("%w but was %d", ErrInvalidMaxLength, maxLength)
}
Expand Down Expand Up @@ -132,7 +132,7 @@ func (th *trieHistory) getValueChanges(startRoot, endRoot ids.ID, start, end []b
)

startPath := newPath(start)
endPath := newPath(end)
endPath := BindMaybe(end, newPath)

// For each element in the history in the range between [startRoot]'s
// last appearance (exclusive) and [endRoot]'s last appearance (inclusive),
Expand All @@ -158,7 +158,7 @@ func (th *trieHistory) getValueChanges(startRoot, endRoot ids.ID, start, end []b
for key, valueChange := range item.values {
// The key is outside the range [start, end].
if (len(startPath) > 0 && key.Compare(startPath) < 0) ||
(len(endPath) > 0 && key.Compare(endPath) > 0) {
(end.HasValue() && key.Compare(endPath.Value()) > 0) {
continue
}

Expand Down Expand Up @@ -198,7 +198,7 @@ func (th *trieHistory) getValueChanges(startRoot, endRoot ids.ID, start, end []b
// for the keys in [start, end].
// If [start] is nil, all keys are considered > [start].
// If [end] is nil, all keys are considered < [end].
func (th *trieHistory) getChangesToGetToRoot(rootID ids.ID, start, end []byte) (*changeSummary, error) {
func (th *trieHistory) getChangesToGetToRoot(rootID ids.ID, start []byte, end Maybe[[]byte]) (*changeSummary, error) {
// [lastRootChange] is the last change in the history resulting in [rootID].
lastRootChange, ok := th.lastChanges[rootID]
if !ok {
Expand All @@ -207,7 +207,7 @@ func (th *trieHistory) getChangesToGetToRoot(rootID ids.ID, start, end []byte) (

var (
startPath = newPath(start)
endPath = newPath(end)
endPath = BindMaybe(end, newPath)
combinedChanges = newChangeSummary(defaultPreallocationSize)
)

Expand All @@ -227,7 +227,7 @@ func (th *trieHistory) getChangesToGetToRoot(rootID ids.ID, start, end []byte) (

for key, valueChange := range item.values {
if (len(startPath) == 0 || key.Compare(startPath) >= 0) &&
(len(endPath) == 0 || key.Compare(endPath) <= 0) {
(endPath.IsNothing() || key.Compare(endPath.Value()) <= 0) {
if existing, ok := combinedChanges.values[key]; ok {
existing.after = valueChange.before
} else {
Expand Down
Loading