Add rollback after adding invalid changes

This commit is contained in:
mcrakhman 2022-08-16 00:57:38 +02:00 committed by Mikhail Iudin
parent 1ea9f03f57
commit 38209fc1fb
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
2 changed files with 14 additions and 1 deletions

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

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{