Skip to content

Commit

Permalink
Act refactor api and ctrl (#36)
Browse files Browse the repository at this point in the history
* Refactor: newreference pattern for history, kvs, grantee

* CHG: history metadata to store granteelist reference

* History test with metadata

* Remove putter from grantee
  • Loading branch information
bosi95 authored and aranyia committed May 17, 2024
1 parent 94b69e4 commit 37fbb68
Show file tree
Hide file tree
Showing 20 changed files with 256 additions and 198 deletions.
6 changes: 3 additions & 3 deletions pkg/api/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (s *Service) bytesUploadHandler(w http.ResponseWriter, r *http.Request) {
Encrypt bool `map:"Swarm-Encrypt"`
RLevel redundancy.Level `map:"Swarm-Redundancy-Level"`
Act bool `map:"Swarm-Act"`
HistoryAddress *swarm.Address `map:"Swarm-Act-History-Address"`
HistoryAddress swarm.Address `map:"Swarm-Act-History-Address"`
}{}
if response := s.mapStructure(r.Header, &headers); response != nil {
response("invalid header params", logger, w)
Expand Down Expand Up @@ -161,7 +161,7 @@ func (s *Service) bytesGetHandler(w http.ResponseWriter, r *http.Request) {
}

address := paths.Address
if v := getAddressFromContext(r.Context()); !v.Equal(swarm.ZeroAddress) {
if v := getAddressFromContext(r.Context()); !v.IsZero() {
address = v
}

Expand All @@ -184,7 +184,7 @@ func (s *Service) bytesHeadHandler(w http.ResponseWriter, r *http.Request) {
}

address := paths.Address
if v := getAddressFromContext(r.Context()); !v.Equal(swarm.ZeroAddress) {
if v := getAddressFromContext(r.Context()); !v.IsZero() {
address = v
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/api/bzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s *Service) bzzUploadHandler(w http.ResponseWriter, r *http.Request) {
IsDir bool `map:"Swarm-Collection"`
RLevel redundancy.Level `map:"Swarm-Redundancy-Level"`
Act bool `map:"Swarm-Act"`
HistoryAddress *swarm.Address `map:"Swarm-Act-History-Address"`
HistoryAddress swarm.Address `map:"Swarm-Act-History-Address"`
}{}
if response := s.mapStructure(r.Header, &headers); response != nil {
response("invalid header params", logger, w)
Expand Down Expand Up @@ -158,7 +158,7 @@ func (s *Service) fileUploadHandler(
tagID uint64,
rLevel redundancy.Level,
act bool,
historyAddress *swarm.Address,
historyAddress swarm.Address,
) {
queries := struct {
FileName string `map:"name" validate:"startsnotwith=/"`
Expand Down Expand Up @@ -310,7 +310,7 @@ func (s *Service) bzzDownloadHandler(w http.ResponseWriter, r *http.Request) {
}

address := paths.Address
if v := getAddressFromContext(r.Context()); !v.Equal(swarm.ZeroAddress) {
if v := getAddressFromContext(r.Context()); !v.IsZero() {
address = v
}

Expand All @@ -334,7 +334,7 @@ func (s *Service) bzzHeadHandler(w http.ResponseWriter, r *http.Request) {
}

address := paths.Address
if v := getAddressFromContext(r.Context()); !v.Equal(swarm.ZeroAddress) {
if v := getAddressFromContext(r.Context()); !v.IsZero() {
address = v
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/api/chunk.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ func (s *Service) chunkUploadHandler(w http.ResponseWriter, r *http.Request) {
logger := s.logger.WithName("post_chunk").Build()

headers := struct {
BatchID []byte `map:"Swarm-Postage-Batch-Id" validate:"required"`
SwarmTag uint64 `map:"Swarm-Tag"`
Act bool `map:"Swarm-Act"`
HistoryAddress *swarm.Address `map:"Swarm-Act-History-Address"`
BatchID []byte `map:"Swarm-Postage-Batch-Id" validate:"required"`
SwarmTag uint64 `map:"Swarm-Tag"`
Act bool `map:"Swarm-Act"`
HistoryAddress swarm.Address `map:"Swarm-Act-History-Address"`
}{}
if response := s.mapStructure(r.Header, &headers); response != nil {
response("invalid header params", logger, w)
Expand Down Expand Up @@ -204,7 +204,7 @@ func (s *Service) chunkGetHandler(w http.ResponseWriter, r *http.Request) {
}

address := paths.Address
if v := getAddressFromContext(r.Context()); !v.Equal(swarm.ZeroAddress) {
if v := getAddressFromContext(r.Context()); !v.IsZero() {
address = v
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/api/chunk_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (s *Service) hasChunkHandler(w http.ResponseWriter, r *http.Request) {
}

address := paths.Address
if v := getAddressFromContext(r.Context()); !v.Equal(swarm.ZeroAddress) {
if v := getAddressFromContext(r.Context()); !v.IsZero() {
address = v
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/api/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (s *Service) dirUploadHandler(
tag uint64,
rLevel redundancy.Level,
act bool,
historyAddress *swarm.Address,
historyAddress swarm.Address,
) {
if r.Body == http.NoBody {
logger.Error(nil, "request has no body")
Expand Down
37 changes: 22 additions & 15 deletions pkg/api/dynamicaccess.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func setAddressInContext(ctx context.Context, address swarm.Address) context.Con
return context.WithValue(ctx, addressKey{}, address)
}

// actDecryptionHandler is a middleware that looks up and decrypts the given address,
// if the act headers are present
func (s *Service) actDecryptionHandler() func(h http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -56,7 +58,7 @@ func (s *Service) actDecryptionHandler() func(h http.Handler) http.Handler {
return
}
ctx := r.Context()
reference, err := s.dac.DownloadHandler(ctx, *headers.Timestamp, paths.Address, headers.Publisher, *headers.HistoryAddress)
reference, err := s.dac.DownloadHandler(ctx, paths.Address, headers.Publisher, *headers.HistoryAddress, *headers.Timestamp)
if err != nil {
jsonhttp.InternalServerError(w, errActDownload)
return
Expand All @@ -67,40 +69,45 @@ func (s *Service) actDecryptionHandler() func(h http.Handler) http.Handler {

}

// TODO: is ctx needed in ctrl upload ?
// actEncryptionHandler is a middleware that encrypts the given address using the publisher's public key
// Uploads the encrypted reference, history and kvs to the store
func (s *Service) actEncryptionHandler(
ctx context.Context,
logger log.Logger,
w http.ResponseWriter,
putter storer.PutterSession,
reference swarm.Address,
historyAddress *swarm.Address,
historyRootHash swarm.Address,
) (swarm.Address, error) {
publisherPublicKey := &s.publicKey
kvsReference, historyReference, encryptedReference, err := s.dac.UploadHandler(ctx, reference, publisherPublicKey, historyAddress)
storageReference, historyReference, encryptedReference, err := s.dac.UploadHandler(ctx, reference, publisherPublicKey, historyRootHash)
if err != nil {
logger.Debug("act failed to encrypt reference", "error", err)
logger.Error(nil, "act failed to encrypt reference")
return swarm.ZeroAddress, err
}
err = putter.Done(historyReference)
if err != nil {
logger.Debug("done split history failed", "error", err)
logger.Error(nil, "done split history failed")
return swarm.ZeroAddress, err
// only need to upload history and kvs if a new history is created,
// meaning that the publsher uploaded to the history for the first time
if !historyReference.Equal(historyRootHash) {
err = putter.Done(storageReference)
if err != nil {
logger.Debug("done split keyvaluestore failed", "error", err)
logger.Error(nil, "done split keyvaluestore failed")
return swarm.ZeroAddress, err
}
err = putter.Done(historyReference)
if err != nil {
logger.Debug("done split history failed", "error", err)
logger.Error(nil, "done split history failed")
return swarm.ZeroAddress, err
}
}
err = putter.Done(encryptedReference)
if err != nil {
logger.Debug("done split encrypted reference failed", "error", err)
logger.Error(nil, "done split encrypted reference failed")
return swarm.ZeroAddress, err
}
err = putter.Done(kvsReference)
if err != nil {
logger.Debug("done split kvs reference failed", "error", err)
logger.Error(nil, "done split kvs reference failed")
return swarm.ZeroAddress, err
}

w.Header().Set(SwarmActHistoryAddressHeader, historyReference.String())

Expand Down
18 changes: 9 additions & 9 deletions pkg/api/dynamicaccess_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,27 @@ func prepareHistoryFixture(storer api.Storer) (dynamicaccess.History, swarm.Addr
ctx := context.Background()
ls := loadsave.New(storer.ChunkStore(), storer.Cache(), pipelineFactory(storer.Cache(), false, redundancy.NONE))

h, _ := dynamicaccess.NewHistory(ls, nil)
h, _ := dynamicaccess.NewHistory(ls)

testActRef1 := swarm.NewAddress([]byte("39a5ea87b141fe44aa609c3327ecd891"))
firstTime := time.Date(1994, time.April, 1, 0, 0, 0, 0, time.UTC).Unix()
h.Add(ctx, testActRef1, &firstTime)
h.Add(ctx, testActRef1, &firstTime, nil)

testActRef2 := swarm.NewAddress([]byte("39a5ea87b141fe44aa609c3327ecd892"))
secondTime := time.Date(2000, time.April, 1, 0, 0, 0, 0, time.UTC).Unix()
h.Add(ctx, testActRef2, &secondTime)
h.Add(ctx, testActRef2, &secondTime, nil)

testActRef3 := swarm.NewAddress([]byte("39a5ea87b141fe44aa609c3327ecd893"))
thirdTime := time.Date(2015, time.April, 1, 0, 0, 0, 0, time.UTC).Unix()
h.Add(ctx, testActRef3, &thirdTime)
h.Add(ctx, testActRef3, &thirdTime, nil)

testActRef4 := swarm.NewAddress([]byte("39a5ea87b141fe44aa609c3327ecd894"))
fourthTime := time.Date(2020, time.April, 1, 0, 0, 0, 0, time.UTC).Unix()
h.Add(ctx, testActRef4, &fourthTime)
h.Add(ctx, testActRef4, &fourthTime, nil)

testActRef5 := swarm.NewAddress([]byte("39a5ea87b141fe44aa609c3327ecd895"))
fifthTime := time.Date(2030, time.April, 1, 0, 0, 0, 0, time.UTC).Unix()
h.Add(ctx, testActRef5, &fifthTime)
h.Add(ctx, testActRef5, &fifthTime, nil)

ref, _ := h.Store(ctx)
return h, ref
Expand Down Expand Up @@ -371,7 +371,7 @@ func TestDacInvalidPath(t *testing.T) {
// [positive tests] 1., 2.: uploading a file w/ and w/o history address then downloading it and checking the data.
// [negative test] 3. uploading a file then downloading it with a wrong history address.
// [negative test] 4. uploading a file to a wrong history address.
// [negative test] 4. downloading a file to w/o history address.
// [negative test] 5. downloading a file to w/o history address.
func TestDacHistory(t *testing.T) {
t.Parallel()
var (
Expand Down Expand Up @@ -552,7 +552,7 @@ func TestDacHistory(t *testing.T) {
}

// nolint:paralleltest,tparallel
// TestDacTimestamp doc. comment
// TestDacTimestamp
// [positive test] 1.: uploading a file w/ ACT then download it w/ timestamp and check the data.
// [negative test] 2.: try to download a file w/o timestamp.
func TestDacTimestamp(t *testing.T) {
Expand Down Expand Up @@ -637,7 +637,7 @@ func TestDacTimestamp(t *testing.T) {
}

// nolint:paralleltest,tparallel
// TestDacPublisher doc. comment
// TestDacPublisher
// [positive test] 1.: uploading a file w/ ACT then download it w/ the publisher address and check the data.
// [negative test] 2.: expect Bad request when the public key is invalid.
// [negative test] 3.: try to download a file w/ an incorrect publisher address.
Expand Down
10 changes: 5 additions & 5 deletions pkg/api/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ func (s *Service) feedPostHandler(w http.ResponseWriter, r *http.Request) {
}

headers := struct {
BatchID []byte `map:"Swarm-Postage-Batch-Id" validate:"required"`
Pin bool `map:"Swarm-Pin"`
Deferred *bool `map:"Swarm-Deferred-Upload"`
Act bool `map:"Swarm-Act"`
HistoryAddress *swarm.Address `map:"Swarm-Act-History-Address"`
BatchID []byte `map:"Swarm-Postage-Batch-Id" validate:"required"`
Pin bool `map:"Swarm-Pin"`
Deferred *bool `map:"Swarm-Deferred-Upload"`
Act bool `map:"Swarm-Act"`
HistoryAddress swarm.Address `map:"Swarm-Act-History-Address"`
}{}
if response := s.mapStructure(r.Header, &headers); response != nil {
response("invalid header params", logger, w)
Expand Down
8 changes: 4 additions & 4 deletions pkg/api/soc.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ func (s *Service) socUploadHandler(w http.ResponseWriter, r *http.Request) {
}

headers := struct {
BatchID []byte `map:"Swarm-Postage-Batch-Id" validate:"required"`
Pin bool `map:"Swarm-Pin"`
Act bool `map:"Swarm-Act"`
HistoryAddress *swarm.Address `map:"Swarm-Act-History-Address"`
BatchID []byte `map:"Swarm-Postage-Batch-Id" validate:"required"`
Pin bool `map:"Swarm-Pin"`
Act bool `map:"Swarm-Act"`
HistoryAddress swarm.Address `map:"Swarm-Act-History-Address"`
}{}
if response := s.mapStructure(r.Header, &headers); response != nil {
response("invalid header params", logger, w)
Expand Down
4 changes: 2 additions & 2 deletions pkg/dynamicaccess/accesslogic.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ func (al ActLogic) DecryptRef(ctx context.Context, storage kvs.KeyValueStore, en
return swarm.NewAddress(ref), nil
}

func NewLogic(S Session) ActLogic {
func NewLogic(s Session) ActLogic {
return ActLogic{
Session: S,
Session: s,
}
}
Loading

0 comments on commit 37fbb68

Please sign in to comment.