aboutsummaryrefslogtreecommitdiffstats
path: root/swarm/api/api.go
diff options
context:
space:
mode:
Diffstat (limited to 'swarm/api/api.go')
-rw-r--r--swarm/api/api.go446
1 files changed, 347 insertions, 99 deletions
diff --git a/swarm/api/api.go b/swarm/api/api.go
index 0cf12fdbe..36f19998a 100644
--- a/swarm/api/api.go
+++ b/swarm/api/api.go
@@ -17,13 +17,13 @@
package api
import (
+ "context"
"fmt"
"io"
+ "math/big"
"net/http"
"path"
- "regexp"
"strings"
- "sync"
"bytes"
"mime"
@@ -31,14 +31,15 @@ import (
"time"
"github.com/ethereum/go-ethereum/common"
- "github.com/ethereum/go-ethereum/log"
+ "github.com/ethereum/go-ethereum/contracts/ens"
+ "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/metrics"
+ "github.com/ethereum/go-ethereum/swarm/log"
+ "github.com/ethereum/go-ethereum/swarm/multihash"
"github.com/ethereum/go-ethereum/swarm/storage"
+ "github.com/ethereum/go-ethereum/swarm/storage/mru"
)
-var hashMatcher = regexp.MustCompile("^[0-9A-Fa-f]{64}")
-
-//setup metrics
var (
apiResolveCount = metrics.NewRegisteredCounter("api.resolve.count", nil)
apiResolveFail = metrics.NewRegisteredCounter("api.resolve.fail", nil)
@@ -46,7 +47,7 @@ var (
apiPutFail = metrics.NewRegisteredCounter("api.put.fail", nil)
apiGetCount = metrics.NewRegisteredCounter("api.get.count", nil)
apiGetNotFound = metrics.NewRegisteredCounter("api.get.notfound", nil)
- apiGetHttp300 = metrics.NewRegisteredCounter("api.get.http.300", nil)
+ apiGetHTTP300 = metrics.NewRegisteredCounter("api.get.http.300", nil)
apiModifyCount = metrics.NewRegisteredCounter("api.modify.count", nil)
apiModifyFail = metrics.NewRegisteredCounter("api.modify.fail", nil)
apiAddFileCount = metrics.NewRegisteredCounter("api.addfile.count", nil)
@@ -55,22 +56,33 @@ var (
apiRmFileFail = metrics.NewRegisteredCounter("api.removefile.fail", nil)
apiAppendFileCount = metrics.NewRegisteredCounter("api.appendfile.count", nil)
apiAppendFileFail = metrics.NewRegisteredCounter("api.appendfile.fail", nil)
+ apiGetInvalid = metrics.NewRegisteredCounter("api.get.invalid", nil)
)
+// Resolver interface resolve a domain name to a hash using ENS
type Resolver interface {
Resolve(string) (common.Hash, error)
}
+// ResolveValidator is used to validate the contained Resolver
+type ResolveValidator interface {
+ Resolver
+ Owner(node [32]byte) (common.Address, error)
+ HeaderByNumber(context.Context, *big.Int) (*types.Header, error)
+}
+
// NoResolverError is returned by MultiResolver.Resolve if no resolver
// can be found for the address.
type NoResolverError struct {
TLD string
}
+// NewNoResolverError creates a NoResolverError for the given top level domain
func NewNoResolverError(tld string) *NoResolverError {
return &NoResolverError{TLD: tld}
}
+// Error NoResolverError implements error
func (e *NoResolverError) Error() string {
if e.TLD == "" {
return "no ENS resolver"
@@ -82,7 +94,8 @@ func (e *NoResolverError) Error() string {
// Each TLD can have multiple resolvers, and the resoluton from the
// first one in the sequence will be returned.
type MultiResolver struct {
- resolvers map[string][]Resolver
+ resolvers map[string][]ResolveValidator
+ nameHash func(string) common.Hash
}
// MultiResolverOption sets options for MultiResolver and is used as
@@ -93,16 +106,24 @@ type MultiResolverOption func(*MultiResolver)
// for a specific TLD. If TLD is an empty string, the resolver will be added
// to the list of default resolver, the ones that will be used for resolution
// of addresses which do not have their TLD resolver specified.
-func MultiResolverOptionWithResolver(r Resolver, tld string) MultiResolverOption {
+func MultiResolverOptionWithResolver(r ResolveValidator, tld string) MultiResolverOption {
return func(m *MultiResolver) {
m.resolvers[tld] = append(m.resolvers[tld], r)
}
}
+// MultiResolverOptionWithNameHash is unused at the time of this writing
+func MultiResolverOptionWithNameHash(nameHash func(string) common.Hash) MultiResolverOption {
+ return func(m *MultiResolver) {
+ m.nameHash = nameHash
+ }
+}
+
// NewMultiResolver creates a new instance of MultiResolver.
func NewMultiResolver(opts ...MultiResolverOption) (m *MultiResolver) {
m = &MultiResolver{
- resolvers: make(map[string][]Resolver),
+ resolvers: make(map[string][]ResolveValidator),
+ nameHash: ens.EnsNode,
}
for _, o := range opts {
o(m)
@@ -114,18 +135,10 @@ func NewMultiResolver(opts ...MultiResolverOption) (m *MultiResolver) {
// If there are more default Resolvers, or for a specific TLD,
// the Hash from the the first one which does not return error
// will be returned.
-func (m MultiResolver) Resolve(addr string) (h common.Hash, err error) {
- rs := m.resolvers[""]
- tld := path.Ext(addr)
- if tld != "" {
- tld = tld[1:]
- rstld, ok := m.resolvers[tld]
- if ok {
- rs = rstld
- }
- }
- if rs == nil {
- return h, NewNoResolverError(tld)
+func (m *MultiResolver) Resolve(addr string) (h common.Hash, err error) {
+ rs, err := m.getResolveValidator(addr)
+ if err != nil {
+ return h, err
}
for _, r := range rs {
h, err = r.Resolve(addr)
@@ -136,104 +149,171 @@ func (m MultiResolver) Resolve(addr string) (h common.Hash, err error) {
return
}
+// ValidateOwner checks the ENS to validate that the owner of the given domain is the given eth address
+func (m *MultiResolver) ValidateOwner(name string, address common.Address) (bool, error) {
+ rs, err := m.getResolveValidator(name)
+ if err != nil {
+ return false, err
+ }
+ var addr common.Address
+ for _, r := range rs {
+ addr, err = r.Owner(m.nameHash(name))
+ // we hide the error if it is not for the last resolver we check
+ if err == nil {
+ return addr == address, nil
+ }
+ }
+ return false, err
+}
+
+// HeaderByNumber uses the validator of the given domainname and retrieves the header for the given block number
+func (m *MultiResolver) HeaderByNumber(ctx context.Context, name string, blockNr *big.Int) (*types.Header, error) {
+ rs, err := m.getResolveValidator(name)
+ if err != nil {
+ return nil, err
+ }
+ for _, r := range rs {
+ var header *types.Header
+ header, err = r.HeaderByNumber(ctx, blockNr)
+ // we hide the error if it is not for the last resolver we check
+ if err == nil {
+ return header, nil
+ }
+ }
+ return nil, err
+}
+
+// getResolveValidator uses the hostname to retrieve the resolver associated with the top level domain
+func (m *MultiResolver) getResolveValidator(name string) ([]ResolveValidator, error) {
+ rs := m.resolvers[""]
+ tld := path.Ext(name)
+ if tld != "" {
+ tld = tld[1:]
+ rstld, ok := m.resolvers[tld]
+ if ok {
+ return rstld, nil
+ }
+ }
+ if len(rs) == 0 {
+ return rs, NewNoResolverError(tld)
+ }
+ return rs, nil
+}
+
+// SetNameHash sets the hasher function that hashes the domain into a name hash that ENS uses
+func (m *MultiResolver) SetNameHash(nameHash func(string) common.Hash) {
+ m.nameHash = nameHash
+}
+
/*
-Api implements webserver/file system related content storage and retrieval
-on top of the dpa
-it is the public interface of the dpa which is included in the ethereum stack
+API implements webserver/file system related content storage and retrieval
+on top of the FileStore
+it is the public interface of the FileStore which is included in the ethereum stack
*/
-type Api struct {
- dpa *storage.DPA
- dns Resolver
+type API struct {
+ resource *mru.Handler
+ fileStore *storage.FileStore
+ dns Resolver
}
-//the api constructor initialises
-func NewApi(dpa *storage.DPA, dns Resolver) (self *Api) {
- self = &Api{
- dpa: dpa,
- dns: dns,
+// NewAPI the api constructor initialises a new API instance.
+func NewAPI(fileStore *storage.FileStore, dns Resolver, resourceHandler *mru.Handler) (self *API) {
+ self = &API{
+ fileStore: fileStore,
+ dns: dns,
+ resource: resourceHandler,
}
return
}
-// to be used only in TEST
-func (self *Api) Upload(uploadDir, index string) (hash string, err error) {
- fs := NewFileSystem(self)
- hash, err = fs.Upload(uploadDir, index)
+// Upload to be used only in TEST
+func (a *API) Upload(uploadDir, index string, toEncrypt bool) (hash string, err error) {
+ fs := NewFileSystem(a)
+ hash, err = fs.Upload(uploadDir, index, toEncrypt)
return hash, err
}
-// DPA reader API
-func (self *Api) Retrieve(key storage.Key) storage.LazySectionReader {
- return self.dpa.Retrieve(key)
+// Retrieve FileStore reader API
+func (a *API) Retrieve(addr storage.Address) (reader storage.LazySectionReader, isEncrypted bool) {
+ return a.fileStore.Retrieve(addr)
}
-func (self *Api) Store(data io.Reader, size int64, wg *sync.WaitGroup) (key storage.Key, err error) {
- return self.dpa.Store(data, size, wg, nil)
+// Store wraps the Store API call of the embedded FileStore
+func (a *API) Store(data io.Reader, size int64, toEncrypt bool) (addr storage.Address, wait func(), err error) {
+ log.Debug("api.store", "size", size)
+ return a.fileStore.Store(data, size, toEncrypt)
}
+// ErrResolve is returned when an URI cannot be resolved from ENS.
type ErrResolve error
-// DNS Resolver
-func (self *Api) Resolve(uri *URI) (storage.Key, error) {
+// Resolve resolves a URI to an Address using the MultiResolver.
+func (a *API) Resolve(uri *URI) (storage.Address, error) {
apiResolveCount.Inc(1)
- log.Trace(fmt.Sprintf("Resolving : %v", uri.Addr))
+ log.Trace("resolving", "uri", uri.Addr)
- // if the URI is immutable, check if the address is a hash
- isHash := hashMatcher.MatchString(uri.Addr)
- if uri.Immutable() || uri.DeprecatedImmutable() {
- if !isHash {
+ // if the URI is immutable, check if the address looks like a hash
+ if uri.Immutable() {
+ key := uri.Address()
+ if key == nil {
return nil, fmt.Errorf("immutable address not a content hash: %q", uri.Addr)
}
- return common.Hex2Bytes(uri.Addr), nil
+ return key, nil
}
// if DNS is not configured, check if the address is a hash
- if self.dns == nil {
- if !isHash {
+ if a.dns == nil {
+ key := uri.Address()
+ if key == nil {
apiResolveFail.Inc(1)
return nil, fmt.Errorf("no DNS to resolve name: %q", uri.Addr)
}
- return common.Hex2Bytes(uri.Addr), nil
+ return key, nil
}
// try and resolve the address
- resolved, err := self.dns.Resolve(uri.Addr)
+ resolved, err := a.dns.Resolve(uri.Addr)
if err == nil {
return resolved[:], nil
- } else if !isHash {
+ }
+
+ key := uri.Address()
+ if key == nil {
apiResolveFail.Inc(1)
return nil, err
}
- return common.Hex2Bytes(uri.Addr), nil
+ return key, nil
}
-// Put provides singleton manifest creation on top of dpa store
-func (self *Api) Put(content, contentType string) (storage.Key, error) {
+// Put provides singleton manifest creation on top of FileStore store
+func (a *API) Put(content, contentType string, toEncrypt bool) (k storage.Address, wait func(), err error) {
apiPutCount.Inc(1)
r := strings.NewReader(content)
- wg := &sync.WaitGroup{}
- key, err := self.dpa.Store(r, int64(len(content)), wg, nil)
+ key, waitContent, err := a.fileStore.Store(r, int64(len(content)), toEncrypt)
if err != nil {
apiPutFail.Inc(1)
- return nil, err
+ return nil, nil, err
}
manifest := fmt.Sprintf(`{"entries":[{"hash":"%v","contentType":"%s"}]}`, key, contentType)
r = strings.NewReader(manifest)
- key, err = self.dpa.Store(r, int64(len(manifest)), wg, nil)
+ key, waitManifest, err := a.fileStore.Store(r, int64(len(manifest)), toEncrypt)
if err != nil {
apiPutFail.Inc(1)
- return nil, err
+ return nil, nil, err
}
- wg.Wait()
- return key, nil
+ return key, func() {
+ waitContent()
+ waitManifest()
+ }, nil
}
// Get uses iterative manifest retrieval and prefix matching
-// to resolve basePath to content using dpa retrieve
-// it returns a section reader, mimeType, status and an error
-func (self *Api) Get(key storage.Key, path string) (reader storage.LazySectionReader, mimeType string, status int, err error) {
+// to resolve basePath to content using FileStore retrieve
+// it returns a section reader, mimeType, status, the key of the actual content and an error
+func (a *API) Get(manifestAddr storage.Address, path string) (reader storage.LazySectionReader, mimeType string, status int, contentAddr storage.Address, err error) {
+ log.Debug("api.get", "key", manifestAddr, "path", path)
apiGetCount.Inc(1)
- trie, err := loadManifest(self.dpa, key, nil)
+ trie, err := loadManifest(a.fileStore, manifestAddr, nil)
if err != nil {
apiGetNotFound.Inc(1)
status = http.StatusNotFound
@@ -241,34 +321,111 @@ func (self *Api) Get(key storage.Key, path string) (reader storage.LazySectionRe
return
}
- log.Trace(fmt.Sprintf("getEntry(%s)", path))
-
+ log.Debug("trie getting entry", "key", manifestAddr, "path", path)
entry, _ := trie.getEntry(path)
if entry != nil {
- key = common.Hex2Bytes(entry.Hash)
+ log.Debug("trie got entry", "key", manifestAddr, "path", path, "entry.Hash", entry.Hash)
+ // we need to do some extra work if this is a mutable resource manifest
+ if entry.ContentType == ResourceContentType {
+
+ // get the resource root chunk key
+ log.Trace("resource type", "key", manifestAddr, "hash", entry.Hash)
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ rsrc, err := a.resource.Load(storage.Address(common.FromHex(entry.Hash)))
+ if err != nil {
+ apiGetNotFound.Inc(1)
+ status = http.StatusNotFound
+ log.Debug(fmt.Sprintf("get resource content error: %v", err))
+ return reader, mimeType, status, nil, err
+ }
+
+ // use this key to retrieve the latest update
+ rsrc, err = a.resource.LookupLatest(ctx, rsrc.NameHash(), true, &mru.LookupParams{})
+ if err != nil {
+ apiGetNotFound.Inc(1)
+ status = http.StatusNotFound
+ log.Debug(fmt.Sprintf("get resource content error: %v", err))
+ return reader, mimeType, status, nil, err
+ }
+
+ // if it's multihash, we will transparently serve the content this multihash points to
+ // \TODO this resolve is rather expensive all in all, review to see if it can be achieved cheaper
+ if rsrc.Multihash {
+
+ // get the data of the update
+ _, rsrcData, err := a.resource.GetContent(rsrc.NameHash().Hex())
+ if err != nil {
+ apiGetNotFound.Inc(1)
+ status = http.StatusNotFound
+ log.Warn(fmt.Sprintf("get resource content error: %v", err))
+ return reader, mimeType, status, nil, err
+ }
+
+ // validate that data as multihash
+ decodedMultihash, err := multihash.FromMultihash(rsrcData)
+ if err != nil {
+ apiGetInvalid.Inc(1)
+ status = http.StatusUnprocessableEntity
+ log.Warn("invalid resource multihash", "err", err)
+ return reader, mimeType, status, nil, err
+ }
+ manifestAddr = storage.Address(decodedMultihash)
+ log.Trace("resource is multihash", "key", manifestAddr)
+
+ // get the manifest the multihash digest points to
+ trie, err := loadManifest(a.fileStore, manifestAddr, nil)
+ if err != nil {
+ apiGetNotFound.Inc(1)
+ status = http.StatusNotFound
+ log.Warn(fmt.Sprintf("loadManifestTrie (resource multihash) error: %v", err))
+ return reader, mimeType, status, nil, err
+ }
+
+ // finally, get the manifest entry
+ // it will always be the entry on path ""
+ entry, _ = trie.getEntry(path)
+ if entry == nil {
+ status = http.StatusNotFound
+ apiGetNotFound.Inc(1)
+ err = fmt.Errorf("manifest (resource multihash) entry for '%s' not found", path)
+ log.Trace("manifest (resource multihash) entry not found", "key", manifestAddr, "path", path)
+ return reader, mimeType, status, nil, err
+ }
+
+ } else {
+ // data is returned verbatim since it's not a multihash
+ return rsrc, "application/octet-stream", http.StatusOK, nil, nil
+ }
+ }
+
+ // regardless of resource update manifests or normal manifests we will converge at this point
+ // get the key the manifest entry points to and serve it if it's unambiguous
+ contentAddr = common.Hex2Bytes(entry.Hash)
status = entry.Status
if status == http.StatusMultipleChoices {
- apiGetHttp300.Inc(1)
- return
- } else {
- mimeType = entry.ContentType
- log.Trace(fmt.Sprintf("content lookup key: '%v' (%v)", key, mimeType))
- reader = self.dpa.Retrieve(key)
+ apiGetHTTP300.Inc(1)
+ return nil, entry.ContentType, status, contentAddr, err
}
+ mimeType = entry.ContentType
+ log.Debug("content lookup key", "key", contentAddr, "mimetype", mimeType)
+ reader, _ = a.fileStore.Retrieve(contentAddr)
} else {
+ // no entry found
status = http.StatusNotFound
apiGetNotFound.Inc(1)
err = fmt.Errorf("manifest entry for '%s' not found", path)
- log.Warn(fmt.Sprintf("%v", err))
+ log.Trace("manifest entry not found", "key", contentAddr, "path", path)
}
return
}
-func (self *Api) Modify(key storage.Key, path, contentHash, contentType string) (storage.Key, error) {
+// Modify loads manifest and checks the content hash before recalculating and storing the manifest.
+func (a *API) Modify(addr storage.Address, path, contentHash, contentType string) (storage.Address, error) {
apiModifyCount.Inc(1)
quitC := make(chan bool)
- trie, err := loadManifest(self.dpa, key, quitC)
+ trie, err := loadManifest(a.fileStore, addr, quitC)
if err != nil {
apiModifyFail.Inc(1)
return nil, err
@@ -288,10 +445,11 @@ func (self *Api) Modify(key storage.Key, path, contentHash, contentType string)
apiModifyFail.Inc(1)
return nil, err
}
- return trie.hash, nil
+ return trie.ref, nil
}
-func (self *Api) AddFile(mhash, path, fname string, content []byte, nameresolver bool) (storage.Key, string, error) {
+// AddFile creates a new manifest entry, adds it to swarm, then adds a file to swarm.
+func (a *API) AddFile(mhash, path, fname string, content []byte, nameresolver bool) (storage.Address, string, error) {
apiAddFileCount.Inc(1)
uri, err := Parse("bzz:/" + mhash)
@@ -299,7 +457,7 @@ func (self *Api) AddFile(mhash, path, fname string, content []byte, nameresolver
apiAddFileFail.Inc(1)
return nil, "", err
}
- mkey, err := self.Resolve(uri)
+ mkey, err := a.Resolve(uri)
if err != nil {
apiAddFileFail.Inc(1)
return nil, "", err
@@ -318,7 +476,7 @@ func (self *Api) AddFile(mhash, path, fname string, content []byte, nameresolver
ModTime: time.Now(),
}
- mw, err := self.NewManifestWriter(mkey, nil)
+ mw, err := a.NewManifestWriter(mkey, nil)
if err != nil {
apiAddFileFail.Inc(1)
return nil, "", err
@@ -341,7 +499,8 @@ func (self *Api) AddFile(mhash, path, fname string, content []byte, nameresolver
}
-func (self *Api) RemoveFile(mhash, path, fname string, nameresolver bool) (string, error) {
+// RemoveFile removes a file entry in a manifest.
+func (a *API) RemoveFile(mhash, path, fname string, nameresolver bool) (string, error) {
apiRmFileCount.Inc(1)
uri, err := Parse("bzz:/" + mhash)
@@ -349,7 +508,7 @@ func (self *Api) RemoveFile(mhash, path, fname string, nameresolver bool) (strin
apiRmFileFail.Inc(1)
return "", err
}
- mkey, err := self.Resolve(uri)
+ mkey, err := a.Resolve(uri)
if err != nil {
apiRmFileFail.Inc(1)
return "", err
@@ -360,7 +519,7 @@ func (self *Api) RemoveFile(mhash, path, fname string, nameresolver bool) (strin
path = path[1:]
}
- mw, err := self.NewManifestWriter(mkey, nil)
+ mw, err := a.NewManifestWriter(mkey, nil)
if err != nil {
apiRmFileFail.Inc(1)
return "", err
@@ -382,7 +541,8 @@ func (self *Api) RemoveFile(mhash, path, fname string, nameresolver bool) (strin
return newMkey.String(), nil
}
-func (self *Api) AppendFile(mhash, path, fname string, existingSize int64, content []byte, oldKey storage.Key, offset int64, addSize int64, nameresolver bool) (storage.Key, string, error) {
+// AppendFile removes old manifest, appends file entry to new manifest and adds it to Swarm.
+func (a *API) AppendFile(mhash, path, fname string, existingSize int64, content []byte, oldAddr storage.Address, offset int64, addSize int64, nameresolver bool) (storage.Address, string, error) {
apiAppendFileCount.Inc(1)
buffSize := offset + addSize
@@ -392,7 +552,7 @@ func (self *Api) AppendFile(mhash, path, fname string, existingSize int64, conte
buf := make([]byte, buffSize)
- oldReader := self.Retrieve(oldKey)
+ oldReader, _ := a.Retrieve(oldAddr)
io.ReadAtLeast(oldReader, buf, int(offset))
newReader := bytes.NewReader(content)
@@ -406,7 +566,7 @@ func (self *Api) AppendFile(mhash, path, fname string, existingSize int64, conte
totalSize := int64(len(buf))
// TODO(jmozah): to append using pyramid chunker when it is ready
- //oldReader := self.Retrieve(oldKey)
+ //oldReader := a.Retrieve(oldKey)
//newReader := bytes.NewReader(content)
//combinedReader := io.MultiReader(oldReader, newReader)
@@ -415,7 +575,7 @@ func (self *Api) AppendFile(mhash, path, fname string, existingSize int64, conte
apiAppendFileFail.Inc(1)
return nil, "", err
}
- mkey, err := self.Resolve(uri)
+ mkey, err := a.Resolve(uri)
if err != nil {
apiAppendFileFail.Inc(1)
return nil, "", err
@@ -426,7 +586,7 @@ func (self *Api) AppendFile(mhash, path, fname string, existingSize int64, conte
path = path[1:]
}
- mw, err := self.NewManifestWriter(mkey, nil)
+ mw, err := a.NewManifestWriter(mkey, nil)
if err != nil {
apiAppendFileFail.Inc(1)
return nil, "", err
@@ -463,21 +623,22 @@ func (self *Api) AppendFile(mhash, path, fname string, existingSize int64, conte
}
-func (self *Api) BuildDirectoryTree(mhash string, nameresolver bool) (key storage.Key, manifestEntryMap map[string]*manifestTrieEntry, err error) {
+// BuildDirectoryTree used by swarmfs_unix
+func (a *API) BuildDirectoryTree(mhash string, nameresolver bool) (addr storage.Address, manifestEntryMap map[string]*manifestTrieEntry, err error) {
uri, err := Parse("bzz:/" + mhash)
if err != nil {
return nil, nil, err
}
- key, err = self.Resolve(uri)
+ addr, err = a.Resolve(uri)
if err != nil {
return nil, nil, err
}
quitC := make(chan bool)
- rootTrie, err := loadManifest(self.dpa, key, quitC)
+ rootTrie, err := loadManifest(a.fileStore, addr, quitC)
if err != nil {
- return nil, nil, fmt.Errorf("can't load manifest %v: %v", key.String(), err)
+ return nil, nil, fmt.Errorf("can't load manifest %v: %v", addr.String(), err)
}
manifestEntryMap = map[string]*manifestTrieEntry{}
@@ -486,7 +647,94 @@ func (self *Api) BuildDirectoryTree(mhash string, nameresolver bool) (key storag
})
if err != nil {
- return nil, nil, fmt.Errorf("list with prefix failed %v: %v", key.String(), err)
+ return nil, nil, fmt.Errorf("list with prefix failed %v: %v", addr.String(), err)
+ }
+ return addr, manifestEntryMap, nil
+}
+
+// ResourceLookup Looks up mutable resource updates at specific periods and versions
+func (a *API) ResourceLookup(ctx context.Context, addr storage.Address, period uint32, version uint32, maxLookup *mru.LookupParams) (string, []byte, error) {
+ var err error
+ rsrc, err := a.resource.Load(addr)
+ if err != nil {
+ return "", nil, err
+ }
+ if version != 0 {
+ if period == 0 {
+ return "", nil, mru.NewError(mru.ErrInvalidValue, "Period can't be 0")
+ }
+ _, err = a.resource.LookupVersion(ctx, rsrc.NameHash(), period, version, true, maxLookup)
+ } else if period != 0 {
+ _, err = a.resource.LookupHistorical(ctx, rsrc.NameHash(), period, true, maxLookup)
+ } else {
+ _, err = a.resource.LookupLatest(ctx, rsrc.NameHash(), true, maxLookup)
+ }
+ if err != nil {
+ return "", nil, err
+ }
+ var data []byte
+ _, data, err = a.resource.GetContent(rsrc.NameHash().Hex())
+ if err != nil {
+ return "", nil, err
+ }
+ return rsrc.Name(), data, nil
+}
+
+// ResourceCreate creates Resource and returns its key
+func (a *API) ResourceCreate(ctx context.Context, name string, frequency uint64) (storage.Address, error) {
+ key, _, err := a.resource.New(ctx, name, frequency)
+ if err != nil {
+ return nil, err
+ }
+ return key, nil
+}
+
+// ResourceUpdateMultihash updates a Mutable Resource and marks the update's content to be of multihash type, which will be recognized upon retrieval.
+// It will fail if the data is not a valid multihash.
+func (a *API) ResourceUpdateMultihash(ctx context.Context, name string, data []byte) (storage.Address, uint32, uint32, error) {
+ return a.resourceUpdate(ctx, name, data, true)
+}
+
+// ResourceUpdate updates a Mutable Resource with arbitrary data.
+// Upon retrieval the update will be retrieved verbatim as bytes.
+func (a *API) ResourceUpdate(ctx context.Context, name string, data []byte) (storage.Address, uint32, uint32, error) {
+ return a.resourceUpdate(ctx, name, data, false)
+}
+
+func (a *API) resourceUpdate(ctx context.Context, name string, data []byte, multihash bool) (storage.Address, uint32, uint32, error) {
+ var addr storage.Address
+ var err error
+ if multihash {
+ addr, err = a.resource.UpdateMultihash(ctx, name, data)
+ } else {
+ addr, err = a.resource.Update(ctx, name, data)
}
- return key, manifestEntryMap, nil
+ period, _ := a.resource.GetLastPeriod(name)
+ version, _ := a.resource.GetVersion(name)
+ return addr, period, version, err
+}
+
+// ResourceHashSize returned the size of the digest produced by the Mutable Resource hashing function
+func (a *API) ResourceHashSize() int {
+ return a.resource.HashSize
+}
+
+// ResourceIsValidated checks if the Mutable Resource has an active content validator.
+func (a *API) ResourceIsValidated() bool {
+ return a.resource.IsValidated()
+}
+
+// ResolveResourceManifest retrieves the Mutable Resource manifest for the given address, and returns the address of the metadata chunk.
+func (a *API) ResolveResourceManifest(addr storage.Address) (storage.Address, error) {
+ trie, err := loadManifest(a.fileStore, addr, nil)
+ if err != nil {
+ return nil, fmt.Errorf("cannot load resource manifest: %v", err)
+ }
+
+ entry, _ := trie.getEntry("")
+ if entry.ContentType != ResourceContentType {
+ return nil, fmt.Errorf("not a resource manifest: %s", addr)
+ }
+
+ return storage.Address(common.FromHex(entry.Hash)), nil
}