···4747 return stacktrace.Propagate(err, "operation failed validation")
4848 }
49495050- err = store.Tree.StoreOperation(ctx, tx, effects.NewLogEntry, effects.NullifiedEntriesStartingSeq)
5050+ err = store.Consensus.StoreOperation(ctx, tx, effects.NewLogEntry, effects.NullifiedEntriesStartingSeq)
5151 if err != nil {
5252 return stacktrace.Propagate(err, "failed to commit operation")
5353 }
···6363 nullifiedEntriesStartingSeq := mo.None[uint64]()
64646565 var iteratorErr error
6666- for entry := range store.Tree.AuditLogReverseIterator(ctx, tx.Downgrade(), newEntry.DID, &iteratorErr) {
6666+ for entry := range store.Consensus.AuditLogReverseIterator(ctx, tx.Downgrade(), newEntry.DID, &iteratorErr) {
6767 entryCID := entry.CID.String()
6868 if !hasExistingOps {
6969 hasExistingOps = true
···8484 }
85858686 return stacktrace.Propagate(
8787- store.Tree.SetOperationCreatedAt(tx, entry.Seq, newCreatedAtDT.Time()),
8787+ store.Consensus.SetOperationCreatedAt(tx, entry.Seq, newCreatedAtDT.Time()),
8888 "")
8989 }
9090···110110 // there's nothing to do but store the operation, no nullification involved
111111 newEntry.Nullified = false
112112113113- err := store.Tree.StoreOperation(ctx, tx, newEntry, nullifiedEntriesStartingSeq)
113113+ err := store.Consensus.StoreOperation(ctx, tx, newEntry, nullifiedEntriesStartingSeq)
114114 return stacktrace.Propagate(err, "failed to commit operation")
115115 }
116116···123123 }
124124125125 newEntry.Nullified = false
126126- err := store.Tree.StoreOperation(ctx, tx, newEntry, nullifiedEntriesStartingSeq)
126126+ err := store.Consensus.StoreOperation(ctx, tx, newEntry, nullifiedEntriesStartingSeq)
127127 return stacktrace.Propagate(err, "failed to commit operation")
128128}
129129130130func (plc *plcImpl) Resolve(ctx context.Context, tx transaction.Read, did string) (didplc.Doc, error) {
131131 var iteratorErr error
132132- for entry := range store.Tree.AuditLogReverseIterator(ctx, tx, did, &iteratorErr) {
132132+ for entry := range store.Consensus.AuditLogReverseIterator(ctx, tx, did, &iteratorErr) {
133133 if entry.Operation.Tombstone != nil {
134134 return didplc.Doc{}, stacktrace.Propagate(ErrDIDGone, "")
135135 }
···147147 // if missing -> returns ErrDIDNotFound
148148 // if tombstone -> returns log as normal
149149150150- l, _, err := store.Tree.AuditLog(ctx, tx, did, false)
150150+ l, _, err := store.Consensus.AuditLog(ctx, tx, did, false)
151151 if err != nil {
152152 return nil, stacktrace.Propagate(err, "")
153153 }
···169169 // GetPlcAuditLog - /:did/log/audit - full audit log, with nullified
170170 // if missing -> returns ErrDIDNotFound
171171 // if tombstone -> returns log as normal
172172- l, _, err := store.Tree.AuditLog(ctx, tx, did, false)
172172+ l, _, err := store.Consensus.AuditLog(ctx, tx, did, false)
173173 if err != nil {
174174 return nil, stacktrace.Propagate(err, "")
175175 }
···189189 // if tombstone -> returns tombstone op
190190191191 var iteratorErr error
192192- for entry := range store.Tree.AuditLogReverseIterator(ctx, tx, did, &iteratorErr) {
192192+ for entry := range store.Consensus.AuditLogReverseIterator(ctx, tx, did, &iteratorErr) {
193193 return entry.Operation, nil
194194 }
195195 if iteratorErr != nil {
···205205 // if tombstone -> returns ErrDIDGone
206206207207 var iteratorErr error
208208- for entry := range store.Tree.AuditLogReverseIterator(ctx, tx, did, &iteratorErr) {
208208+ for entry := range store.Consensus.AuditLogReverseIterator(ctx, tx, did, &iteratorErr) {
209209 opEnum := entry.Operation
210210 if opEnum.Tombstone != nil {
211211 return didplc.RegularOp{}, stacktrace.Propagate(ErrDIDGone, "")
···223223}
224224225225func (plc *plcImpl) Export(ctx context.Context, tx transaction.Read, after uint64, count int) ([]types.SequencedLogEntry, error) {
226226- entries, err := store.Tree.ExportOperations(ctx, tx, after, count)
226226+ entries, err := store.Consensus.ExportOperations(ctx, tx, after, count)
227227 return entries, stacktrace.Propagate(err, "")
228228}
+2-2
plc/operation_validator.go
···7878 nullifiedEntriesStartingSeq := mo.None[uint64]()
79798080 var iteratorErr error
8181- for entry := range store.Tree.AuditLogReverseIterator(ctx, readTx, expectedDid, &iteratorErr) {
8181+ for entry := range store.Consensus.AuditLogReverseIterator(ctx, readTx, expectedDid, &iteratorErr) {
8282 relevantExistingEntries = append(relevantExistingEntries, entry)
8383 if !hasExistingOps {
8484 hasExistingOps = true
···218218219219 var withinHour, withinDay, withinWeek int
220220 var err error
221221- for entry := range store.Tree.AuditLogReverseIterator(ctx, tx, did, &err) {
221221+ for entry := range store.Consensus.AuditLogReverseIterator(ctx, tx, did, &err) {
222222 if entry.Nullified {
223223 // The typescript implementation operates over a `ops` array which doesn't include nullified ops
224224 // (With recovery ops also skipping rate limits, doesn't this leave the PLC vulnerable to the spam of constant recovery operations? TODO investigate)
+1-1
store/did_bloom.go
···72727373 // update bloom filter with DIDs it may have missed since the time it was serialized
7474 var iterErr error
7575- for entry := range Tree.OperationsIterator(tx, max(1, seq)-1, &iterErr) {
7575+ for entry := range Consensus.OperationsIterator(tx, max(1, seq)-1, &iterErr) {
7676 didBytes, err := DIDToBytes(entry.DID)
7777 if err != nil {
7878 return nil, 0, stacktrace.Propagate(err, "")
+18-18
store/tree.go
store/consensus.go
···2424 "tangled.org/gbl08ma.com/didplcbft/types"
2525)
26262727-// TODO rename to something more appropriate, now that this touches both the tree and the index
2828-var Tree *TreeStore = &TreeStore{}
2727+var Consensus ConsensusStore = &consensusStore{}
29283030-type PLCTreeStore interface {
2929+// ConsensusStore manages all information that is directly or indirectly protected by consensus
3030+type ConsensusStore interface {
3131 AuditLog(ctx context.Context, tx transaction.Read, did string, withProof bool) ([]types.SequencedLogEntry, *ics23.CommitmentProof, error)
3232 AuditLogReverseIterator(ctx context.Context, tx transaction.Read, did string, err *error) iter.Seq[types.SequencedLogEntry]
3333···4646 SetAuthoritativeImportProgress(tx transaction.Write, nextCursor uint64) error
4747}
48484949-var _ PLCTreeStore = (*TreeStore)(nil)
4949+var _ ConsensusStore = (*consensusStore)(nil)
50505151-// TreeStore exists just to groups methods nicely
5252-type TreeStore struct{}
5151+// consensusStore exists just to groups methods nicely
5252+type consensusStore struct{}
53535454-func (t *TreeStore) AuditLog(ctx context.Context, tx transaction.Read, did string, withProof bool) ([]types.SequencedLogEntry, *ics23.CommitmentProof, error) {
5454+func (t *consensusStore) AuditLog(ctx context.Context, tx transaction.Read, did string, withProof bool) ([]types.SequencedLogEntry, *ics23.CommitmentProof, error) {
5555 didBytes, err := DIDToBytes(did)
5656 if err != nil {
5757 return nil, nil, stacktrace.Propagate(err, "")
···121121 return logEntries, combinedProof, nil
122122}
123123124124-func (t *TreeStore) AuditLogReverseIterator(ctx context.Context, tx transaction.Read, did string, retErr *error) iter.Seq[types.SequencedLogEntry] {
124124+func (t *consensusStore) AuditLogReverseIterator(ctx context.Context, tx transaction.Read, did string, retErr *error) iter.Seq[types.SequencedLogEntry] {
125125 return func(yield func(types.SequencedLogEntry) bool) {
126126 didBytes, err := DIDToBytes(did)
127127 if err != nil {
···192192 }
193193}
194194195195-func (t *TreeStore) ExportOperations(ctx context.Context, tx transaction.Read, after uint64, count int) ([]types.SequencedLogEntry, error) {
195195+func (t *consensusStore) ExportOperations(ctx context.Context, tx transaction.Read, after uint64, count int) ([]types.SequencedLogEntry, error) {
196196 entries := make([]types.SequencedLogEntry, 0, count)
197197 var iterErr error
198198 for logEntry := range t.OperationsIterator(tx, after, &iterErr) {
···209209 return entries, nil
210210}
211211212212-func (t *TreeStore) OperationsIterator(tx transaction.Read, after uint64, retErr *error) iter.Seq[types.SequencedLogEntry] {
212212+func (t *consensusStore) OperationsIterator(tx transaction.Read, after uint64, retErr *error) iter.Seq[types.SequencedLogEntry] {
213213 return func(yield func(types.SequencedLogEntry) bool) {
214214 // as the name suggests, after is an exclusive lower bound, but our iterators use inclusive lower bounds
215215 start := after + 1
···249249//
250250// Even though this function is not meant to overwrite operations (it will error) we ask the caller to provide the sequence
251251// The caller is responsible for managing the sequence and invalidating it when needed (e.g. after a rollback) using
252252-// [[TreeStore.NextOperationSequence]].
252252+// [[consensusStore.NextOperationSequence]].
253253// Pushing the responsibility to the caller is preferable in terms of performance, even if it leads to less safe code,
254254// because getting the sequence from the tree within this function every time has a significant performance hit
255255-func (t *TreeStore) StoreOperation(ctx context.Context, tx transaction.Write, entry didplc.LogEntry, nullifyWithSequenceEqualOrGreaterThan mo.Option[uint64]) error {
255255+func (t *consensusStore) StoreOperation(ctx context.Context, tx transaction.Write, entry didplc.LogEntry, nullifyWithSequenceEqualOrGreaterThan mo.Option[uint64]) error {
256256 didBytes, err := DIDToBytes(entry.DID)
257257 if err != nil {
258258 return stacktrace.Propagate(err, "")
···354354 return nil
355355}
356356357357-func (t *TreeStore) SetOperationCreatedAt(tx transaction.Write, seqID uint64, createdAt time.Time) error {
357357+func (t *consensusStore) SetOperationCreatedAt(tx transaction.Write, seqID uint64, createdAt time.Time) error {
358358 opKey := marshalOperationKey(seqID)
359359360360 opValue, err := tx.Tree().Get(opKey)
···382382var minOperationKey = marshalOperationKey(0)
383383var maxOperationKey = marshalOperationKey(math.MaxInt64)
384384385385-func (t *TreeStore) NextOperationSequence(tx transaction.Read) (uint64, error) {
385385+func (t *consensusStore) NextOperationSequence(tx transaction.Read) (uint64, error) {
386386 seq := uint64(0)
387387388388 itr, err := tx.Tree().Iterator(minOperationKey, maxOperationKey, false)
···585585 Complete())
586586}
587587588588-func (t *TreeStore) AuthoritativePLC(tx transaction.Read) (string, error) {
588588+func (t *consensusStore) AuthoritativePLC(tx transaction.Read) (string, error) {
589589 url, err := tx.Tree().Get([]byte("aPLCURL"))
590590 if err != nil {
591591 return "", stacktrace.Propagate(err, "")
···596596 return string(url), nil
597597}
598598599599-func (t *TreeStore) SetAuthoritativePLC(tx transaction.Write, url string) error {
599599+func (t *consensusStore) SetAuthoritativePLC(tx transaction.Write, url string) error {
600600 _, err := tx.Tree().Set([]byte("aPLCURL"), []byte(url))
601601 return stacktrace.Propagate(err, "")
602602}
603603604604-func (t *TreeStore) AuthoritativeImportProgress(tx transaction.Read) (uint64, error) {
604604+func (t *consensusStore) AuthoritativeImportProgress(tx transaction.Read) (uint64, error) {
605605 progBytes, err := tx.Tree().Get([]byte("aImportProgress"))
606606 if err != nil {
607607 return 0, stacktrace.Propagate(err, "")
···612612 return binary.BigEndian.Uint64(progBytes), nil
613613}
614614615615-func (t *TreeStore) SetAuthoritativeImportProgress(tx transaction.Write, nextCursor uint64) error {
615615+func (t *consensusStore) SetAuthoritativeImportProgress(tx transaction.Write, nextCursor uint64) error {
616616 value := make([]byte, 8)
617617 binary.BigEndian.PutUint64(value, nextCursor)
618618