Skip to content

Commit

Permalink
feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
oncilla committed Jul 22, 2019
1 parent 21eb9c3 commit d01d2a8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 42 deletions.
19 changes: 6 additions & 13 deletions go/lib/scrypto/trc/v2/keychanges.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ func (c *KeyChanges) Sensitive() bool {
return len(c.Fresh[OfflineKey]) != 0 || len(c.Modified[OfflineKey]) != 0
}

func (c *KeyChanges) insertAllFresh(as addr.AS, next PrimaryAS) {
for keyType, meta := range next.Keys {
c.Fresh[keyType][as] = meta
}
}

func (c *KeyChanges) insertModifications(as addr.AS, prev, next PrimaryAS) error {
for keyType, meta := range next.Keys {
prevMeta, ok := prev.Keys[keyType]
Expand All @@ -89,14 +83,13 @@ func (c *KeyChanges) insertModifications(as addr.AS, prev, next PrimaryAS) error
// return value indicates, whether the update is a modification.
func ValidateKeyUpdate(prev, next KeyMeta) (bool, error) {
modified := next.Algorithm != prev.Algorithm || !bytes.Equal(next.Key, prev.Key)
// If the meta data has changed, expect a key version change.
expectedVersion := prev.KeyVersion
if modified {
expectedVersion = prev.KeyVersion + 1
}
if next.KeyVersion != expectedVersion {
switch {
case modified && next.KeyVersion != prev.KeyVersion+1:
return modified, common.NewBasicError(InvalidKeyVersion, nil, "modified", modified,
"expected", prev.KeyVersion+1, "actual", next.KeyVersion)
case !modified && next.KeyVersion != prev.KeyVersion:
return modified, common.NewBasicError(InvalidKeyVersion, nil, "modified", modified,
"expected", expectedVersion, "actual", next.KeyVersion)
"expected", prev.KeyVersion, "actual", next.KeyVersion)
}
return modified, nil
}
39 changes: 13 additions & 26 deletions go/lib/scrypto/trc/v2/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,7 @@ func (v *UpdateValidator) sanity() error {
func (v *UpdateValidator) keyChanges() (*KeyChanges, error) {
c := newKeyChanges()
for as, primary := range v.Next.PrimaryASes {
prev, ok := v.Prev.PrimaryASes[as]
if !ok {
c.insertAllFresh(as, primary)
continue
}
if err := c.insertModifications(as, prev, primary); err != nil {
if err := c.insertModifications(as, v.Prev.PrimaryASes[as], primary); err != nil {
return nil, err
}
}
Expand All @@ -153,17 +148,12 @@ func (v *UpdateValidator) attrChanges() AttributeChanges {
c := make(AttributeChanges)
// Check all attributes of all ASes that persist or are added.
for as, primary := range v.Next.PrimaryASes {
prev, ok := v.Prev.PrimaryASes[as]
if !ok {
c.insertAll(as, primary.Attributes, AttributeAdded)
continue
}
c.insertModifications(as, prev, primary)
c.insertModifications(as, v.Prev.PrimaryASes[as], primary)
}
// Check all attributes of all ASes that are removed.
for as, prev := range v.Prev.PrimaryASes {
if _, ok := v.Next.PrimaryASes[as]; !ok {
c.insertAll(as, prev.Attributes, AttributeRemoved)
if empty, ok := v.Next.PrimaryASes[as]; !ok {
c.insertModifications(as, prev, empty)
}
}
return c
Expand All @@ -185,12 +175,15 @@ func (v *UpdateValidator) checkProofOfPossesion(keyChanges *KeyChanges) error {
}

func (v *UpdateValidator) checkVotes(info UpdateInfo) error {
check := v.checkVotesSensitive
if info.Type == RegularUpdate {
check = v.checkVotesRegular
}
if err := check(info); err != nil {
return err
switch info.Type {
case RegularUpdate:
if err := v.checkVotesRegular(info); err != nil {
return err
}
default:
if err := v.checkVotesSensitive(info); err != nil {
return err
}
}
if len(v.Next.Votes) < v.Prev.VotingQuorum() {
return common.NewBasicError(QuorumUnmet, nil,
Expand Down Expand Up @@ -266,12 +259,6 @@ func (c AttributeChanges) Sensitive() bool {
return len(c) != 0
}

func (c AttributeChanges) insertAll(as addr.AS, attrs Attributes, change AttributeChange) {
for _, attr := range attrs {
c.insert(as, attr, change)
}
}

func (c AttributeChanges) insertModifications(as addr.AS, prev, next PrimaryAS) {
for _, attr := range next.Attributes {
if !prev.Is(attr) {
Expand Down
5 changes: 2 additions & 3 deletions go/lib/scrypto/trc/v2/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ func TestCommonUpdate(t *testing.T) {
"Trust reset": {
Modify: func(updated, _ *trc.TRC) {
*updated = *newBaseTRC()
updated.Version = 2
updated.BaseVersion = 2
updated.BaseVersion = updated.Version
},
ExpectedErrMsg: trc.ErrBaseNotUpdate.Error(),
},
Expand All @@ -48,7 +47,7 @@ func TestCommonUpdate(t *testing.T) {
},
"Wrong ISD": {
Modify: func(updated, _ *trc.TRC) {
updated.ISD = 2
updated.ISD = updated.ISD + 1
},
ExpectedErrMsg: trc.ImmutableISD,
},
Expand Down

0 comments on commit d01d2a8

Please sign in to comment.