Skip to content

Commit

Permalink
Add full path to lookup error.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed Mar 27, 2024
1 parent 8436dc4 commit 6ad578f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 29 deletions.
30 changes: 19 additions & 11 deletions certification/hashtree/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,45 @@ func pathToString(path []Label) string {
return sb.String()
}

// LookupError is an error that occurs during a lookup.
type LookupError struct {
// Type is the type of the lookup result.
Type LookupResultType
Path string
// Path is the path that was looked up.
Path []Label
// Index is the index in the path where the error occurred.
Index int
}

// NewLookupAbsentError returns a new LookupError with type LookupResultAbsent.
func NewLookupAbsentError(path ...Label) LookupError {
func NewLookupAbsentError(path []Label, index int) LookupError {
return LookupError{
Type: LookupResultAbsent,
Path: pathToString(path),
Type: LookupResultAbsent,
Path: path,
Index: index,
}
}

// NewLookupError returns a new LookupError with type LookupResultError.
func NewLookupError(path ...Label) LookupError {
func NewLookupError(path []Label, index int) LookupError {
return LookupError{
Type: LookupResultError,
Path: pathToString(path),
Type: LookupResultError,
Path: path,
Index: index,
}
}

// NewLookupUnknownError returns a new LookupError with type LookupResultUnknown.
func NewLookupUnknownError(path ...Label) LookupError {
func NewLookupUnknownError(path []Label, index int) LookupError {
return LookupError{
Type: LookupResultUnknown,
Path: pathToString(path),
Type: LookupResultUnknown,
Path: path,
Index: index,
}
}

func (l LookupError) Error() string {
return fmt.Sprintf("lookup error (path: %q): %s", l.Path, l.error())
return fmt.Sprintf("lookup error (path: %q) at %q: %s", pathToString(l.Path), l.Path[l.Index], l.error())
}

func (l LookupError) error() string {
Expand Down
4 changes: 2 additions & 2 deletions certification/hashtree/hashtree.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ func (t HashTree) Digest() [32]byte {

// Lookup looks up a path in the hash tree.
func (t HashTree) Lookup(path ...Label) ([]byte, error) {
return lookupPath(t.Root, path...)
return lookupPath(t.Root, path, 0)
}

// LookupSubTree looks up a path in the hash tree and returns the sub-tree.
func (t HashTree) LookupSubTree(path ...Label) (Node, error) {
return lookupSubTree(t.Root, path...)
return lookupSubTree(t.Root, path, 0)
}

// MarshalCBOR marshals a hash tree.
Expand Down
12 changes: 12 additions & 0 deletions certification/hashtree/hashtree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ func TestHashTree_Lookup(t *testing.T) {
for _, i := range []int{0, 1} {
if _, err := tree.Lookup(Label(fmt.Sprintf("label %d", i))); !errors.As(err, &lookupError) || lookupError.Type != LookupResultAbsent {
t.Fatalf("unexpected lookup result")
} else if e := lookupError.Error(); e != fmt.Sprintf(`lookup error (path: "label %d") at "label %d": not found, not present in the tree`, i, i) {
t.Fatalf("unexpected error message: %s", e)
}
}
if _, err := tree.Lookup(Label("label 2")); !errors.As(err, &lookupError) || lookupError.Type != LookupResultUnknown {
t.Fatalf("unexpected lookup result")
} else if e := lookupError.Error(); e != `lookup error (path: "label 2") at "label 2": not found, could be pruned` {
t.Fatalf("unexpected error message: %s", e)
}
if v, err := tree.Lookup(Label("label 3")); err != nil {
t.Fatalf("unexpected lookup result")
Expand All @@ -49,6 +53,8 @@ func TestHashTree_Lookup(t *testing.T) {
for _, i := range []int{4, 5, 6} {
if _, err := tree.Lookup(Label(fmt.Sprintf("label %d", i))); !errors.As(err, &lookupError) || lookupError.Type != LookupResultAbsent {
t.Fatalf("unexpected lookup result")
} else if e := lookupError.Error(); e != fmt.Sprintf(`lookup error (path: "label %d") at "label %d": not found, not present in the tree`, i, i) {
t.Fatalf("unexpected error message: %s", e)
}
}
})
Expand Down Expand Up @@ -84,6 +90,8 @@ func TestHashTree_Lookup(t *testing.T) {
for _, i := range []int{0, 1, 2} {
if _, err := tree.Lookup(Label(fmt.Sprintf("label %d", i))); !errors.As(err, &lookupError) || lookupError.Type != LookupResultAbsent {
t.Fatalf("unexpected lookup result")
} else if e := lookupError.Error(); e != fmt.Sprintf(`lookup error (path: "label %d") at "label %d": not found, not present in the tree`, i, i) {
t.Fatalf("unexpected error message: %s", e)
}
}
if v, err := tree.Lookup(Label("label 3")); err != nil {
Expand All @@ -96,10 +104,14 @@ func TestHashTree_Lookup(t *testing.T) {
for _, i := range []int{4, 5} {
if _, err := tree.Lookup(Label(fmt.Sprintf("label %d", i))); !errors.As(err, &lookupError) || lookupError.Type != LookupResultAbsent {
t.Fatalf("unexpected lookup result")
} else if e := lookupError.Error(); e != fmt.Sprintf(`lookup error (path: "label %d") at "label %d": not found, not present in the tree`, i, i) {
t.Fatalf("unexpected error message: %s", e)
}
}
if _, err := tree.Lookup(Label("label 6")); !errors.As(err, &lookupError) || lookupError.Type != LookupResultUnknown {
t.Fatalf("unexpected lookup result")
} else if e := lookupError.Error(); e != `lookup error (path: "label 6") at "label 6": not found, could be pruned` {
t.Fatalf("unexpected error message: %s", e)
}
})
}
Expand Down
30 changes: 15 additions & 15 deletions certification/hashtree/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,44 @@ import (
"bytes"
)

func lookupPath(n Node, path ...Label) ([]byte, error) {
func lookupPath(n Node, path []Label, idx int) ([]byte, error) {
switch {
case len(path) == 0:
case len(path) == 0 || len(path) == idx:
switch n := n.(type) {
case Leaf:
return n, nil
case nil, Empty:
return nil, NewLookupAbsentError()
return nil, NewLookupAbsentError(path, idx-1)
case Pruned:
return nil, NewLookupUnknownError()
return nil, NewLookupUnknownError(path, idx-1)
default:
// Labeled, Fork
return nil, NewLookupError()
return nil, NewLookupError(path, idx-1)
}
default:
switch l := lookupLabel(n, path[0]); l.Type {
switch l := lookupLabel(n, path[idx]); l.Type {
case lookupLabelResultFound:
return lookupPath(l.Node, path[1:]...)
return lookupPath(l.Node, path, idx+1)
case lookupLabelResultUnknown:
return nil, NewLookupUnknownError(path...)
return nil, NewLookupUnknownError(path, idx)
default:
return nil, NewLookupAbsentError(path...)
return nil, NewLookupAbsentError(path, idx)
}
}
}

func lookupSubTree(n Node, path ...Label) (Node, error) {
func lookupSubTree(n Node, path []Label, idx int) (Node, error) {
switch {
case len(path) == 0:
case len(path) == 0 || len(path) == idx:
return n, nil
default:
switch l := lookupLabel(n, path[0]); l.Type {
switch l := lookupLabel(n, path[idx]); l.Type {
case lookupLabelResultFound:
return lookupSubTree(l.Node, path[1:]...)
return lookupSubTree(l.Node, path, idx+1)
case lookupLabelResultUnknown:
return nil, NewLookupUnknownError(path...)
return nil, NewLookupUnknownError(path, idx)
default:
return nil, NewLookupAbsentError(path...)
return nil, NewLookupAbsentError(path, idx)
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions ic/ic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func TestModules(t *testing.T) {
if _, err := a.ApiVersion(); err != nil {
t.Error(err)
}

if err := a.Authorize(principal.AnonymousID); err != nil {
t.Fatal(err)
}
})

t.Run("management canister", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func NewRequestID(req Request) RequestID {
typeHash := sha256.Sum256([]byte(req.Type))
hashes = append(hashes, append(typeKey[:], typeHash[:]...))
}
if len(req.CanisterID.Raw) != 0 {
if req.CanisterID.Raw != nil {
canisterIDHash := sha256.Sum256(req.CanisterID.Raw)
hashes = append(hashes, append(canisterIDKey[:], canisterIDHash[:]...))
}
Expand Down

0 comments on commit 6ad578f

Please sign in to comment.