Add rollback after adding invalid changes

This commit is contained in:
mcrakhman committed 2022-08-16 00:57:38 +02:00
1 parent 9142f28d55
commit f4aeecae84
2 files changed
+14 -1

No files matched your search

+1
View File
@@ -15,6 +15,7 @@ import (
type AddResultSummary int
var ErrTreeWithoutIdentity = errors.New("acl tree is created without identity")
var ErrHasInvalidChanges = errors.New("the change is invalid")
const (
AddResultSummaryNothing AddResultSummary = iota
+13 -1
View File
@@ -228,6 +228,10 @@ func (d *docTree) AddRawChanges(ctx context.Context, aclTree ACLTree, rawChanges
if d.HasChange(ch.Id) {
continue
}
// if we already added the change to invalid ones
if _, exists := d.tree.invalidChanges[ch.Id]; exists {
return AddResult{}, ErrHasInvalidChanges
}
var change *Change
change, err = NewFromVerifiedRawChange(ch, d.identityKeys, d.treeBuilder.signingPubKeyDecoder)
@@ -329,7 +333,15 @@ func (d *docTree) AddRawChanges(ctx context.Context, aclTree ACLTree, rawChanges
// as an optimization we could've started from current heads, but I didn't implement that
err = d.validator.ValidateTree(d.tree, aclTree)
if err != nil {
return AddResult{}, err
// rolling back
for _, ch := range d.tmpChangesBuf {
if _, exists := d.tree.attached[ch.Id]; exists {
delete(d.tree.attached, ch.Id)
} else if _, exists := d.tree.unAttached[ch.Id]; exists {
delete(d.tree.unAttached, ch.Id)
}
}
return AddResult{}, ErrHasInvalidChanges
}
addResult = AddResult{