|
| 1 | +// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package firewood |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "context" |
| 9 | + "errors" |
| 10 | + "sync" |
| 11 | + |
| 12 | + "github.com/ava-labs/firewood-go-ethhash/ffi" |
| 13 | + |
| 14 | + "github.com/ava-labs/avalanchego/ids" |
| 15 | + "github.com/ava-labs/avalanchego/utils/maybe" |
| 16 | + |
| 17 | + xsync "github.com/ava-labs/avalanchego/x/sync" |
| 18 | +) |
| 19 | + |
| 20 | +var ( |
| 21 | + _ xsync.DB[*RangeProof, *ChangeProof] = (*syncDB)(nil) |
| 22 | + |
| 23 | + errNilProof = errors.New("nil proof") |
| 24 | +) |
| 25 | + |
| 26 | +type syncDB struct { |
| 27 | + fw *ffi.Database |
| 28 | + lock sync.Mutex |
| 29 | +} |
| 30 | + |
| 31 | +func New(db *ffi.Database) *syncDB { |
| 32 | + return &syncDB{fw: db} |
| 33 | +} |
| 34 | + |
| 35 | +func (db *syncDB) GetMerkleRoot(context.Context) (ids.ID, error) { |
| 36 | + root, err := db.fw.Root() |
| 37 | + if err != nil { |
| 38 | + return ids.ID{}, err |
| 39 | + } |
| 40 | + return ids.ID(root), nil |
| 41 | +} |
| 42 | + |
| 43 | +// TODO: implement |
| 44 | +func (db *syncDB) CommitChangeProof(_ context.Context, end maybe.Maybe[[]byte], proof *ChangeProof) (nextKey maybe.Maybe[[]byte], err error) { |
| 45 | + // Set up cleanup. |
| 46 | + var nextKeyRange *ffi.NextKeyRange |
| 47 | + defer func() { |
| 48 | + // If we got a nextKeyRange, free it too. |
| 49 | + if nextKeyRange != nil { |
| 50 | + err = errors.Join(err, nextKeyRange.Free()) |
| 51 | + } |
| 52 | + }() |
| 53 | + |
| 54 | + // TODO: Remove copy. Currently necessary to avoid passing pointer to a stack variable? |
| 55 | + startRootBytes := make([]byte, ids.IDLen) |
| 56 | + copy(startRootBytes, proof.startRoot[:]) |
| 57 | + endRootBytes := make([]byte, ids.IDLen) |
| 58 | + copy(endRootBytes, proof.endRoot[:]) |
| 59 | + |
| 60 | + // Verify and commit the proof in a single step (TODO: separate these steps). |
| 61 | + // CommitRangeProof will verify the proof as part of committing it. |
| 62 | + db.lock.Lock() |
| 63 | + defer db.lock.Unlock() |
| 64 | + newRoot, err := db.fw.VerifyAndCommitChangeProof(proof.proof, startRootBytes, endRootBytes, proof.startKey, end, uint32(proof.maxLength)) |
| 65 | + if err != nil { |
| 66 | + return maybe.Nothing[[]byte](), err |
| 67 | + } |
| 68 | + |
| 69 | + // TODO: This case should be handled by `FindNextKey`. |
| 70 | + if bytes.Equal(newRoot, endRootBytes) { |
| 71 | + return maybe.Nothing[[]byte](), nil |
| 72 | + } |
| 73 | + |
| 74 | + return proof.FindNextKey() |
| 75 | +} |
| 76 | + |
| 77 | +// Commit the range proof to the database. |
| 78 | +// TODO: This should only commit the range proof, not verify it. |
| 79 | +// This will be resolved once the Firewood supports that. |
| 80 | +// This is the last call to the proof, so it and any resources should be freed. |
| 81 | +func (db *syncDB) CommitRangeProof(_ context.Context, start, end maybe.Maybe[[]byte], proof *RangeProof) (nextKey maybe.Maybe[[]byte], err error) { |
| 82 | + // Set up cleanup. |
| 83 | + // TODO: Remove copy. Currently necessary to avoid passing pointer to a stack variable? |
| 84 | + rootBytes := make([]byte, ids.IDLen) |
| 85 | + copy(rootBytes, proof.root[:]) |
| 86 | + |
| 87 | + // Verify and commit the proof in a single step (TODO: separate these steps). |
| 88 | + // CommitRangeProof will verify the proof as part of committing it. |
| 89 | + db.lock.Lock() |
| 90 | + defer db.lock.Unlock() |
| 91 | + newRoot, err := db.fw.VerifyAndCommitRangeProof(proof.proof, start, end, rootBytes, uint32(proof.maxLength)) |
| 92 | + if err != nil { |
| 93 | + return maybe.Nothing[[]byte](), err |
| 94 | + } |
| 95 | + |
| 96 | + // TODO: This case should be handled by `FindNextKey`. |
| 97 | + if bytes.Equal(newRoot, rootBytes) { |
| 98 | + return maybe.Nothing[[]byte](), nil |
| 99 | + } |
| 100 | + |
| 101 | + return proof.FindNextKey() |
| 102 | +} |
| 103 | + |
| 104 | +// TODO: implement |
| 105 | +func (db *syncDB) GetChangeProof(_ context.Context, startRootID ids.ID, endRootID ids.ID, start maybe.Maybe[[]byte], end maybe.Maybe[[]byte], maxLength int) (*ChangeProof, error) { |
| 106 | + proof, err := db.fw.ChangeProof(startRootID[:], endRootID[:], start, end, uint32(maxLength)) |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + |
| 111 | + return newChangeProof(proof), nil |
| 112 | +} |
| 113 | + |
| 114 | +// Get the range proof between [start, end]. |
| 115 | +// The returned proof must be freed when no longer needed. |
| 116 | +// Since this method is only called prior to marshalling the proof for sending over the |
| 117 | +// network, the proof will be freed when marshalled. |
| 118 | +func (db *syncDB) GetRangeProofAtRoot(_ context.Context, rootID ids.ID, start maybe.Maybe[[]byte], end maybe.Maybe[[]byte], maxLength int) (*RangeProof, error) { |
| 119 | + proof, err := db.fw.RangeProof(maybe.Some(rootID[:]), start, end, uint32(maxLength)) |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + |
| 124 | + return newRangeProof(proof), nil |
| 125 | +} |
| 126 | + |
| 127 | +// TODO: implement |
| 128 | +// Right now, we verify the proof as part of committing it, making this function a no-op. |
| 129 | +// We must only pass the necessary data to CommitChangeProof so it can verify the proof. |
| 130 | +// |
| 131 | +//nolint:revive |
| 132 | +func (db *syncDB) VerifyChangeProof(_ context.Context, proof *ChangeProof, start maybe.Maybe[[]byte], end maybe.Maybe[[]byte], expectedEndRootID ids.ID, maxLength int) error { |
| 133 | + if proof.proof == nil { |
| 134 | + return errNilProof |
| 135 | + } |
| 136 | + |
| 137 | + // TODO: once firewood can verify separately from committing, do that here. |
| 138 | + // For now, pass any necessary data to be done in CommitChangeProof. |
| 139 | + // Namely, the start root, end root, and max length. |
| 140 | + proof.startRoot = expectedEndRootID |
| 141 | + proof.endRoot = expectedEndRootID |
| 142 | + proof.maxLength = maxLength |
| 143 | + return nil |
| 144 | +} |
| 145 | + |
| 146 | +// TODO: implement |
| 147 | +// Right now, we verify the proof as part of committing it, making this function a no-op. |
| 148 | +// We must only pass the necessary data to CommitRangeProof so it can verify the proof. |
| 149 | +// |
| 150 | +//nolint:revive |
| 151 | +func (db *syncDB) VerifyRangeProof(_ context.Context, proof *RangeProof, start maybe.Maybe[[]byte], end maybe.Maybe[[]byte], expectedEndRootID ids.ID, maxLength int) error { |
| 152 | + if proof.proof == nil { |
| 153 | + return errNilProof |
| 154 | + } |
| 155 | + |
| 156 | + // TODO: once firewood can verify separately from committing, do that here. |
| 157 | + // For now, pass any necessary data to be done in CommitRangeProof. |
| 158 | + // Namely, the max length and root. |
| 159 | + proof.root = expectedEndRootID |
| 160 | + proof.maxLength = maxLength |
| 161 | + return nil |
| 162 | +} |
| 163 | + |
| 164 | +// TODO: implement |
| 165 | +// No error is returned to ensure some tests pass. |
| 166 | +// |
| 167 | +//nolint:revive |
| 168 | +func (db *syncDB) Clear() error { |
| 169 | + return nil |
| 170 | +} |
0 commit comments