Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.

Commit 8dd0bef

Browse files
committed
swarm/storage/mru: refactor and documentation
1 parent 7781846 commit 8dd0bef

File tree

9 files changed

+365
-283
lines changed

9 files changed

+365
-283
lines changed

swarm/api/client/client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ func (c *Client) UpdateResource(manifestAddressOrDomain string, data []byte, sig
617617
}
618618

619619
func (c *Client) updateResource(mruRequest *mru.UpdateRequest, manifestAddressOrDomain string) (io.ReadCloser, error) {
620-
body, err := mru.EncodeMruRequest(mruRequest)
620+
body, err := mru.EncodeUpdateRequest(mruRequest)
621621
if err != nil {
622622
return nil, err
623623
}
@@ -665,7 +665,7 @@ func (c *Client) GetResourceMetadata(manifestAddressOrDomain string) (*mru.Updat
665665
return nil, err
666666
}
667667

668-
metadata, err := mru.DecodeMruRequest(body)
668+
metadata, err := mru.DecodeUpdateRequest(body)
669669
if err != nil {
670670
return nil, err
671671
}

swarm/api/http/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ func (s *Server) HandlePostResource(w http.ResponseWriter, r *Request) {
410410
Respond(w, r, err.Error(), http.StatusInternalServerError)
411411
return
412412
}
413-
mruRequest, err := mru.DecodeMruRequest(body) // decodes request JSON
413+
mruRequest, err := mru.DecodeUpdateRequest(body) // decodes request JSON
414414
if err != nil {
415415
Respond(w, r, err.Error(), http.StatusBadRequest) //TODO: send different status response depending on error
416416
return
@@ -550,7 +550,7 @@ func (s *Server) handleGetResource(w http.ResponseWriter, r *Request) {
550550
Respond(w, r, fmt.Sprintf("cannot retrieve resource metadata for rootAddr=%s: %s", rootAddr.Hex(), err), http.StatusNotFound)
551551
return
552552
}
553-
rawResponse, err := mru.EncodeMruRequest(unsignedMruRequest)
553+
rawResponse, err := mru.EncodeUpdateRequest(unsignedMruRequest)
554554
if err != nil {
555555
Respond(w, r, fmt.Sprintf("cannot encode unsigned MruRequest: %v", err), http.StatusInternalServerError)
556556
return

swarm/api/http/server_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func TestBzzResourceMultihash(t *testing.T) {
141141
updateRequest.Sign(signer)
142142
log.Info("added data", "manifest", string(b), "data", common.ToHex(mh))
143143

144-
body, err := mru.EncodeMruRequest(updateRequest)
144+
body, err := mru.EncodeUpdateRequest(updateRequest)
145145
if err != nil {
146146
t.Fatal(err)
147147
}
@@ -213,7 +213,7 @@ func TestBzzResource(t *testing.T) {
213213
}
214214
updateRequest.Sign(signer)
215215

216-
body, err := mru.EncodeMruRequest(updateRequest)
216+
body, err := mru.EncodeUpdateRequest(updateRequest)
217217
if err != nil {
218218
t.Fatal(err)
219219
}
@@ -334,7 +334,7 @@ func TestBzzResource(t *testing.T) {
334334
if err != nil {
335335
t.Fatal(err)
336336
}
337-
updateRequest, err = mru.DecodeMruRequest(b)
337+
updateRequest, err = mru.DecodeUpdateRequest(b)
338338
if err != nil {
339339
t.Fatalf("Error decoding resource metadata: %s", err)
340340
}
@@ -343,7 +343,7 @@ func TestBzzResource(t *testing.T) {
343343
if err = updateRequest.Sign(signer); err != nil {
344344
t.Fatal(err)
345345
}
346-
body, err = mru.EncodeMruRequest(updateRequest)
346+
body, err = mru.EncodeUpdateRequest(updateRequest)
347347
if err != nil {
348348
t.Fatal(err)
349349
}

swarm/storage/mru/doc.go

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,50 +3,57 @@
33
// The update scheme is built on swarm chunks with chunk keys following
44
// a predictable, versionable pattern.
55
//
6-
// Updates are defined to be periodic in nature, where periods are
7-
// expressed in terms of number of blocks.
8-
//
9-
// The root entry of a mutable resource is tied to a unique identifier,
10-
// typically - but not necessarily - an ens name. The identifier must be
11-
// an valid IDNA string. It also contains the block number
12-
// when the resource update was first registered, and
13-
// the block frequency with which the resource will be updated, both of
6+
// Updates are defined to be periodic in nature, where the update frequency
7+
// is expressed in seconds.
8+
//
9+
// The root entry of a mutable resource is tied to a unique identifier that
10+
// is deterministically generated out of the metadata content that describes
11+
// the resource. This metadata includes a user-defined resource name, a resource
12+
// start time that indicates when the resource becomes valid,
13+
// the frequency in seconds with which the resource is expected to be updated, both of
1414
// which are stored as little-endian uint64 values in the database (for a
15-
// total of 16 bytes). It also contains the unique identifier.
15+
// total of 16 bytes). It also contains the owner's address (ownerAddr)
1616
// This MRU info is stored in a separate content-addressed chunk
1717
// (call it the metadata chunk), with the following layout:
1818
//
19-
// (startblock|frequency|address|identifier)
19+
// (00|length|startTime|frequency|name|ownerAddr)
2020
//
2121
// (The two first zero-value bytes are used for disambiguation by the chunk validator,
2222
// and update chunk will always have a value > 0 there.)
2323
//
24+
// Each metadata chunk is identified by its rootAddr, calculated as follows:
25+
// metaHash=H(len(metadata), startTime, frequency,name)
26+
// rootAddr = H(metaHash, ownerAddr).
27+
// where H is the SHA3 hash function
28+
// This scheme effectively locks the root chunk so that only the owner of the private key
29+
// that ownerAddr was derived from can sign updates.
30+
//
2431
// The root entry tells the requester from when the mutable resource was
25-
// first added (block number) and in which block number to look for the
32+
// first added (Unix time in seconds) and in which moments to look for the
2633
// actual updates. Thus, a resource update for identifier "føø.bar"
27-
// starting at block 4200 with frequency 42 will have updates on block 4242,
28-
// 4284, 4326 and so on.
34+
// starting at unix time 1528800000 with frequency 300 (every 5 mins) will have updates on 1528800300,
35+
// 1528800600, 1528800900 and so on.
2936
//
3037
// Actual data updates are also made in the form of swarm chunks. The keys
3138
// of the updates are the hash of a concatenation of properties as follows:
3239
//
33-
// sha256(period|version|address|namehash)
34-
//
35-
// The period is (currentblock - startblock) / frequency
40+
// updateAddr = H(period, version, rootAddr)
41+
// where H is the SHA3 hash function
42+
// The period is (currentTime - startTime) / frequency
3643
//
37-
// Using our previous example, this means that a period 3 will have 4326 as
38-
// the block number.
44+
// Using our previous example, this means that a period 3 will happen when the
45+
// clock hits 1528800900
3946
//
40-
// If more than one update is made to the same block number, incremental
47+
// If more than one update is made in the same period, incremental
4148
// version numbers are used successively.
4249
//
43-
// A lookup agent need only know the identifier name in order to get the versions
50+
// A lookup agent need only know the rootAddr in order to get the versions
4451
//
45-
// the resourcedata is:
46-
// headerlength|period|version|identifier|data
52+
// the resource update data is:
53+
// resourcedata = headerlength|period|version|metaHash|rootAddr|data
4754
//
4855
// the full update data that goes in the chunk payload is:
4956
// resourcedata|sign(resourcedata)
5057
//
51-
// headerlength is a 16 bit value containing the byte length of period|version|name
58+
// headerlength is a 16 bit value containing the byte length of period|version|rootAddr|metaHash
5259
package mru

0 commit comments

Comments
 (0)