Skip to content

Commit b0d0faf

Browse files
lmarsfjl
authored andcommitted
swarm/api: fix error reporting in api.Resolve (#14464)
Previously, resolve errors were being swallowed and the returned error was a generic "not a content hash" which isn't helpful. This updates the Resolve function to fail fast rather than only returning an error at the end, and also adds test coverage.
1 parent 90c7155 commit b0d0faf

3 files changed

Lines changed: 148 additions & 21 deletions

File tree

‎swarm/api/api.go‎

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ import (
2525
"sync"
2626

2727
"bytes"
28-
"github.com/ethereum/go-ethereum/common"
29-
"github.com/ethereum/go-ethereum/log"
30-
"github.com/ethereum/go-ethereum/swarm/storage"
3128
"mime"
3229
"path/filepath"
3330
"time"
31+
32+
"github.com/ethereum/go-ethereum/common"
33+
"github.com/ethereum/go-ethereum/log"
34+
"github.com/ethereum/go-ethereum/swarm/storage"
3435
)
3536

3637
var (
@@ -84,27 +85,33 @@ type ErrResolve error
8485
func (self *Api) Resolve(uri *URI) (storage.Key, error) {
8586
log.Trace(fmt.Sprintf("Resolving : %v", uri.Addr))
8687

87-
var err error
88-
if !uri.Immutable() {
89-
if self.dns != nil {
90-
resolved, err := self.dns.Resolve(uri.Addr)
91-
if err == nil {
92-
return resolved[:], nil
93-
}
94-
} else {
95-
err = fmt.Errorf("no DNS to resolve name")
88+
// if the URI is immutable, check if the address is a hash
89+
isHash := hashMatcher.MatchString(uri.Addr)
90+
if uri.Immutable() {
91+
if !isHash {
92+
return nil, fmt.Errorf("immutable address not a content hash: %q", uri.Addr)
9693
}
94+
return common.Hex2Bytes(uri.Addr), nil
9795
}
98-
if hashMatcher.MatchString(uri.Addr) {
99-
return storage.Key(common.Hex2Bytes(uri.Addr)), nil
96+
97+
// if DNS is not configured, check if the address is a hash
98+
if self.dns == nil {
99+
if !isHash {
100+
return nil, fmt.Errorf("no DNS to resolve name: %q", uri.Addr)
101+
}
102+
return common.Hex2Bytes(uri.Addr), nil
100103
}
101-
if err != nil {
102-
return nil, fmt.Errorf("'%s' does not resolve: %v but is not a content hash", uri.Addr, err)
104+
105+
// try and resolve the address
106+
resolved, err := self.dns.Resolve(uri.Addr)
107+
if err == nil {
108+
return resolved[:], nil
109+
} else if !isHash {
110+
return nil, err
103111
}
104-
return nil, fmt.Errorf("'%s' is not a content hash", uri.Addr)
112+
return common.Hex2Bytes(uri.Addr), nil
105113
}
106114

107-
108115
// Put provides singleton manifest creation on top of dpa store
109116
func (self *Api) Put(content, contentType string) (storage.Key, error) {
110117
r := strings.NewReader(content)

‎swarm/api/api_test.go‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package api
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"io"
2223
"io/ioutil"
@@ -117,3 +118,122 @@ func TestApiPut(t *testing.T) {
117118
checkResponse(t, resp, exp)
118119
})
119120
}
121+
122+
// testResolver implements the Resolver interface and either returns the given
123+
// hash if it is set, or returns a "name not found" error
124+
type testResolver struct {
125+
hash *common.Hash
126+
}
127+
128+
func newTestResolver(addr string) *testResolver {
129+
r := &testResolver{}
130+
if addr != "" {
131+
hash := common.HexToHash(addr)
132+
r.hash = &hash
133+
}
134+
return r
135+
}
136+
137+
func (t *testResolver) Resolve(addr string) (common.Hash, error) {
138+
if t.hash == nil {
139+
return common.Hash{}, fmt.Errorf("DNS name not found: %q", addr)
140+
}
141+
return *t.hash, nil
142+
}
143+
144+
// TestAPIResolve tests resolving URIs which can either contain content hashes
145+
// or ENS names
146+
func TestAPIResolve(t *testing.T) {
147+
ensAddr := "swarm.eth"
148+
hashAddr := "1111111111111111111111111111111111111111111111111111111111111111"
149+
resolvedAddr := "2222222222222222222222222222222222222222222222222222222222222222"
150+
doesResolve := newTestResolver(resolvedAddr)
151+
doesntResolve := newTestResolver("")
152+
153+
type test struct {
154+
desc string
155+
dns Resolver
156+
addr string
157+
immutable bool
158+
result string
159+
expectErr error
160+
}
161+
162+
tests := []*test{
163+
{
164+
desc: "DNS not configured, hash address, returns hash address",
165+
dns: nil,
166+
addr: hashAddr,
167+
result: hashAddr,
168+
},
169+
{
170+
desc: "DNS not configured, ENS address, returns error",
171+
dns: nil,
172+
addr: ensAddr,
173+
expectErr: errors.New(`no DNS to resolve name: "swarm.eth"`),
174+
},
175+
{
176+
desc: "DNS configured, hash address, hash resolves, returns resolved address",
177+
dns: doesResolve,
178+
addr: hashAddr,
179+
result: resolvedAddr,
180+
},
181+
{
182+
desc: "DNS configured, immutable hash address, hash resolves, returns hash address",
183+
dns: doesResolve,
184+
addr: hashAddr,
185+
immutable: true,
186+
result: hashAddr,
187+
},
188+
{
189+
desc: "DNS configured, hash address, hash doesn't resolve, returns hash address",
190+
dns: doesntResolve,
191+
addr: hashAddr,
192+
result: hashAddr,
193+
},
194+
{
195+
desc: "DNS configured, ENS address, name resolves, returns resolved address",
196+
dns: doesResolve,
197+
addr: ensAddr,
198+
result: resolvedAddr,
199+
},
200+
{
201+
desc: "DNS configured, immutable ENS address, name resolves, returns error",
202+
dns: doesResolve,
203+
addr: ensAddr,
204+
immutable: true,
205+
expectErr: errors.New(`immutable address not a content hash: "swarm.eth"`),
206+
},
207+
{
208+
desc: "DNS configured, ENS address, name doesn't resolve, returns error",
209+
dns: doesntResolve,
210+
addr: ensAddr,
211+
expectErr: errors.New(`DNS name not found: "swarm.eth"`),
212+
},
213+
}
214+
for _, x := range tests {
215+
t.Run(x.desc, func(t *testing.T) {
216+
api := &Api{dns: x.dns}
217+
uri := &URI{Addr: x.addr, Scheme: "bzz"}
218+
if x.immutable {
219+
uri.Scheme = "bzzi"
220+
}
221+
res, err := api.Resolve(uri)
222+
if err == nil {
223+
if x.expectErr != nil {
224+
t.Fatalf("expected error %q, got result %q", x.expectErr, res)
225+
}
226+
if res.String() != x.result {
227+
t.Fatalf("expected result %q, got %q", x.result, res)
228+
}
229+
} else {
230+
if x.expectErr == nil {
231+
t.Fatalf("expected no error, got %q", err)
232+
}
233+
if err.Error() != x.expectErr.Error() {
234+
t.Fatalf("expected error %q, got %q", x.expectErr, err)
235+
}
236+
}
237+
})
238+
}
239+
}

‎swarm/api/http/server_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ func TestBzzrGetPath(t *testing.T) {
106106
}
107107

108108
nonhashresponses := []string{
109-
"error resolving name: 'name' does not resolve: no DNS to resolve name but is not a content hash\n",
110-
"error resolving nonhash: 'nonhash' is not a content hash\n",
111-
"error resolving nonhash: 'nonhash' does not resolve: no DNS to resolve name but is not a content hash\n",
109+
"error resolving name: no DNS to resolve name: \"name\"\n",
110+
"error resolving nonhash: immutable address not a content hash: \"nonhash\"\n",
111+
"error resolving nonhash: no DNS to resolve name: \"nonhash\"\n",
112112
}
113113

114114
for i, url := range nonhashtests {

0 commit comments

Comments
 (0)