Skip to content

Commit

Permalink
swarm/storage/mru: Cosmetic changes as per ethereum#743 comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jpeletier committed Jul 21, 2018
1 parent 3abce1a commit 722a604
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 102 deletions.
8 changes: 4 additions & 4 deletions cmd/swarm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ var (
Usage: "Number of recent chunks cached in memory (default 5000)",
EnvVar: SWARM_ENV_STORE_CACHE_CAPACITY,
}
SwarmResourceRawFlag = cli.BoolFlag{
Name: "rawmru",
SwarmResourceMultihashFlag = cli.BoolTFlag{
Name: "multihash",
Usage: "Determines how to interpret data for a resource update. If not present, data will be interpreted as a multihash",
}
SwarmResourceNameFlag = cli.StringFlag{
Expand Down Expand Up @@ -265,7 +265,7 @@ func init() {
Usage: "creates a new Mutable Resource",
ArgsUsage: "<frequency>",
Description: "creates a new Mutable Resource",
Flags: []cli.Flag{SwarmResourceNameFlag, SwarmResourceDataOnCreateFlag, SwarmResourceRawFlag},
Flags: []cli.Flag{SwarmResourceNameFlag, SwarmResourceDataOnCreateFlag, SwarmResourceMultihashFlag},
},
{
Action: resourceUpdate,
Expand All @@ -274,7 +274,7 @@ func init() {
Usage: "updates the content of an existing Mutable Resource",
ArgsUsage: "<Manifest Address or ENS domain> <0x Hex data>",
Description: "updates the content of an existing Mutable Resource",
Flags: []cli.Flag{SwarmResourceRawFlag},
Flags: []cli.Flag{SwarmResourceMultihashFlag},
},
{
Action: resourceInfo,
Expand Down
18 changes: 9 additions & 9 deletions cmd/swarm/mru.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
"gopkg.in/urfave/cli.v1"
)

// swarm resource create <frequency> [--name <name>] [--data <0x Hexdata> [--rawmru]]
// swarm resource update <Manifest Address or ENS domain> <0x Hexdata> [--rawmru]
// swarm resource create <frequency> [--name <name>] [--data <0x Hexdata> [--multihash=false]]
// swarm resource update <Manifest Address or ENS domain> <0x Hexdata> [--multihash=false]
// swarm resource info <Manifest Address or ENS domain>

func resourceCreate(ctx *cli.Context) {
Expand All @@ -40,7 +40,7 @@ func resourceCreate(ctx *cli.Context) {
var (
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
client = swarm.NewClient(bzzapi)
rawResource = ctx.Bool(SwarmResourceRawFlag.Name)
multihash = ctx.Bool(SwarmResourceMultihashFlag.Name)
initialData = ctx.String(SwarmResourceDataOnCreateFlag.Name)
name = ctx.String(SwarmResourceNameFlag.Name)
)
Expand All @@ -62,7 +62,7 @@ func resourceCreate(ctx *cli.Context) {
newResourceRequest, err := mru.NewCreateRequest(&mru.ResourceMetadata{
Name: name,
Frequency: frequency,
OwnerAddr: signer.Address(),
Owner: signer.Address(),
})

if err != nil {
Expand All @@ -77,7 +77,7 @@ func resourceCreate(ctx *cli.Context) {
return
}

newResourceRequest.SetData(initialDataBytes, !rawResource)
newResourceRequest.SetData(initialDataBytes, multihash)
if err = newResourceRequest.Sign(signer); err != nil {
utils.Fatalf("Error signing resource update: %s", err.Error())
}
Expand All @@ -96,9 +96,9 @@ func resourceUpdate(ctx *cli.Context) {
args := ctx.Args()

var (
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
client = swarm.NewClient(bzzapi)
rawResource = ctx.Bool(SwarmResourceRawFlag.Name)
bzzapi = strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/")
client = swarm.NewClient(bzzapi)
multihash = ctx.Bool(SwarmResourceMultihashFlag.Name)
)

if len(args) < 2 {
Expand All @@ -121,7 +121,7 @@ func resourceUpdate(ctx *cli.Context) {
}

// set the new data
updateRequest.SetData(data, !rawResource)
updateRequest.SetData(data, multihash)

// sign update
if err = updateRequest.Sign(signer); err != nil {
Expand Down
18 changes: 9 additions & 9 deletions swarm/api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,8 @@ func (c *Client) MultipartUpload(hash string, uploader Uploader) (string, error)
// startTime=0 means "now"
// Returns the resulting Mutable Resource manifest address that you can use to include in an ENS Resolver (setContent)
// or reference future updates (Client.UpdateResource)
func (c *Client) CreateResource(createRequest *mru.Request) (string, error) {
responseStream, err := c.updateResource(createRequest)
func (c *Client) CreateResource(request *mru.Request) (string, error) {
responseStream, err := c.updateResource(request)
if err != nil {
return "", err
}
Expand All @@ -581,21 +581,21 @@ func (c *Client) CreateResource(createRequest *mru.Request) (string, error) {
return "", err
}

var manifestKey string
if err = json.Unmarshal(body, &manifestKey); err != nil {
var manifestAddress string
if err = json.Unmarshal(body, &manifestAddress); err != nil {
return "", err
}
return manifestKey, nil
return manifestAddress, nil
}

// UpdateResource allows you to send a new version of your content
func (c *Client) UpdateResource(updateRequest *mru.Request) error {
_, err := c.updateResource(updateRequest)
func (c *Client) UpdateResource(request *mru.Request) error {
_, err := c.updateResource(request)
return err
}

func (c *Client) updateResource(updateRequest *mru.Request) (io.ReadCloser, error) {
body, err := mru.EncodeUpdateRequest(updateRequest)
func (c *Client) updateResource(request *mru.Request) (io.ReadCloser, error) {
body, err := mru.EncodeUpdateRequest(request)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions swarm/api/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func TestClientCreateResourceMultihash(t *testing.T) {
Name: resourceName,
Frequency: 13,
StartTime: srv.GetCurrentTime(),
OwnerAddr: signer.Address(),
Owner: signer.Address(),
})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -453,7 +453,7 @@ func TestClientCreateUpdateResource(t *testing.T) {
Name: resourceName,
Frequency: 13,
StartTime: srv.GetCurrentTime(),
OwnerAddr: signer.Address(),
Owner: signer.Address(),
})
if err != nil {
t.Fatal(err)
Expand Down
10 changes: 5 additions & 5 deletions swarm/api/http/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,13 @@ func TestBzzResourceMultihash(t *testing.T) {
Name: keybytes,
Frequency: 13,
StartTime: srv.GetCurrentTime(),
OwnerAddr: signer.Address(),
Owner: signer.Address(),
})
if err != nil {
t.Fatal(err)
}

updateRequest.SetData(mh,true)
updateRequest.SetData(mh, true)

if err := updateRequest.Sign(signer); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -226,13 +226,13 @@ func TestBzzResource(t *testing.T) {
Name: keybytes,
Frequency: 13,
StartTime: srv.GetCurrentTime(),
OwnerAddr: signer.Address(),
Owner: signer.Address(),
})
if err != nil {
t.Fatal(err)
}

updateRequest.SetData(databytes,false)
updateRequest.SetData(databytes, false)

if err := updateRequest.Sign(signer); err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -364,7 +364,7 @@ func TestBzzResource(t *testing.T) {
t.Fatalf("Error decoding resource metadata: %s", err)
}
data := []byte("foo")
updateRequest.SetData(data,false)
updateRequest.SetData(data, false)
if err = updateRequest.Sign(signer); err != nil {
t.Fatal(err)
}
Expand Down
22 changes: 11 additions & 11 deletions swarm/storage/mru/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (h *Handler) Validate(chunkAddr storage.Address, data []byte) bool {
// to update

var r SignedResourceUpdate
if err := r.parseUpdateChunk(chunkAddr, data); err != nil {
if err := r.fromChunk(chunkAddr, data); err != nil {
log.Warn(fmt.Sprintf("Invalid resource chunk with address %s: %s ", chunkAddr.Hex(), err.Error()))
return false
}
Expand Down Expand Up @@ -174,7 +174,7 @@ func (h *Handler) New(ctx context.Context, request *Request) error {
}

// make sure owner is set to something
if request.metadata.OwnerAddr == zeroAddr {
if request.metadata.Owner == zeroAddr {
return NewError(ErrInvalidValue, "ownerAddr must be set to create a new metadata chunk")
}

Expand All @@ -192,7 +192,7 @@ func (h *Handler) New(ctx context.Context, request *Request) error {
request.rootAddr = chunk.Addr

h.chunkStore.Put(ctx, chunk)
log.Debug("new resource", "name", request.metadata.Name, "startTime", request.metadata.StartTime, "frequency", request.metadata.Frequency, "owner", request.metadata.OwnerAddr)
log.Debug("new resource", "name", request.metadata.Name, "startTime", request.metadata.StartTime, "frequency", request.metadata.Frequency, "owner", request.metadata.Owner)

// create the internal index for the resource and populate it with the data of the first version
rsrc := &resource{
Expand Down Expand Up @@ -226,7 +226,7 @@ func (h *Handler) NewUpdateRequest(ctx context.Context, rootAddr storage.Address
return nil, err
}

now := h.getCurrentTime(ctx)
now := h.Now(ctx)

updateRequest = new(Request)
updateRequest.period, err = getNextPeriod(rsrc.StartTime.Time, now.Time, rsrc.Frequency)
Expand Down Expand Up @@ -268,7 +268,7 @@ func (h *Handler) Lookup(ctx context.Context, params *LookupParams) (*resource,
}
if params.period == 0 {
// get the current time and the next period
now := h.getCurrentTime(ctx)
now := h.Now(ctx)

var period uint32
period, err := getNextPeriod(rsrc.StartTime.Time, now.Time, rsrc.Frequency)
Expand Down Expand Up @@ -337,7 +337,7 @@ func (h *Handler) lookup(rsrc *resource, params *LookupParams) (*resource, error
if lp.Limit != 0 && hops > lp.Limit {
return nil, NewErrorf(ErrPeriodDepth, "Lookup exceeded max period hops (%d)", lp.Limit)
}
updateAddr := lp.Addr()
updateAddr := lp.UpdateAddr()
chunk, err := h.chunkStore.GetWithTimeout(context.TODO(), updateAddr, defaultRetrieveTimeout)
if err == nil {
if specificversion {
Expand All @@ -347,7 +347,7 @@ func (h *Handler) lookup(rsrc *resource, params *LookupParams) (*resource, error
log.Trace("rsrc update version 1 found, checking for version updates", "period", lp.period, "updateAddr", updateAddr)
for {
newversion := lp.version + 1
updateAddr := lp.Addr()
updateAddr := lp.UpdateAddr()
newchunk, err := h.chunkStore.GetWithTimeout(context.TODO(), updateAddr, defaultRetrieveTimeout)
if err != nil {
return h.updateIndex(rsrc, chunk)
Expand Down Expand Up @@ -393,7 +393,7 @@ func (h *Handler) updateIndex(rsrc *resource, chunk *storage.Chunk) (*resource,

// retrieve metadata from chunk data and check that it matches this mutable resource
var r SignedResourceUpdate
if err := r.parseUpdateChunk(chunk.Addr, chunk.SData); err != nil {
if err := r.fromChunk(chunk.Addr, chunk.SData); err != nil {
return nil, err
}
log.Trace("resource index update", "name", rsrc.ResourceMetadata.Name, "updatekey", chunk.Addr, "period", r.period, "version", r.version)
Expand Down Expand Up @@ -441,7 +441,7 @@ func (h *Handler) update(ctx context.Context, r *SignedResourceUpdate) (updateAd
}
}

chunk, err := r.newUpdateChunk() // Serialize the update into a chunk. Fails if data is too big
chunk, err := r.toChunk() // Serialize the update into a chunk. Fails if data is too big
if err != nil {
return nil, err
}
Expand All @@ -466,8 +466,8 @@ func (h *Handler) update(ctx context.Context, r *SignedResourceUpdate) (updateAd
}

// gets the current time
func (h *Handler) getCurrentTime(ctx context.Context) Timestamp {
return h.timestampProvider.GetCurrentTimestamp()
func (h *Handler) Now(ctx context.Context) Timestamp {
return h.timestampProvider.Now()
}

// Retrieves the resource index value for the given nameHash
Expand Down
4 changes: 2 additions & 2 deletions swarm/storage/mru/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ type UpdateLookup struct {
// storage.Keylength for rootAddr
const updateLookupLength = 4 + 4 + storage.KeyLength

// Addr calculates the resource update chunk address corresponding to this lookup key
func (u *UpdateLookup) Addr() (updateAddr storage.Address) {
// UpdateAddr calculates the resource update chunk address corresponding to this lookup key
func (u *UpdateLookup) UpdateAddr() (updateAddr storage.Address) {
serializedData := make([]byte, updateLookupLength)
u.binaryPut(serializedData)
hasher := hashPool.Get().(hash.Hash)
Expand Down
6 changes: 3 additions & 3 deletions swarm/storage/mru/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type ResourceMetadata struct {
StartTime Timestamp // time at which the resource starts to be valid
Frequency uint64 // expected update frequency for the resource
Name string // name of the resource, for the reference of the user
OwnerAddr common.Address // public address of the resource owner
Owner common.Address // public address of the resource owner
}

// Resource metadata chunk layout:
Expand Down Expand Up @@ -77,7 +77,7 @@ func (r *ResourceMetadata) binaryGet(serializedData []byte) error {
r.Name = string(serializedData[cursor : cursor+nameLength])
cursor += nameLength

copy(r.OwnerAddr[:], serializedData[cursor:])
copy(r.Owner[:], serializedData[cursor:])
return nil
}

Expand Down Expand Up @@ -111,7 +111,7 @@ func (r *ResourceMetadata) binaryPut(serializedData []byte) error {
copy(serializedData[cursor:cursor+nameLength], []byte(r.Name[:nameLength]))
cursor += nameLength

copy(serializedData[cursor:cursor+common.AddressLength], r.OwnerAddr[:])
copy(serializedData[cursor:cursor+common.AddressLength], r.Owner[:])
cursor += common.AddressLength

return nil
Expand Down
2 changes: 1 addition & 1 deletion swarm/storage/mru/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestMarshallingAndUnmarshalling(t *testing.T) {
Time: 1528880400,
},
Frequency: 3600,
OwnerAddr: ownerAddr,
Owner: ownerAddr,
}

rootAddr, metaHash, chunkData, err := metadata.hashAndSerialize() // creates hashes and marshals, in one go
Expand Down
Loading

0 comments on commit 722a604

Please sign in to comment.