Skip to content

Commit

Permalink
t: act
Browse files Browse the repository at this point in the history
  • Loading branch information
Ferenc Sárai committed Jul 31, 2024
1 parent 571cbcd commit 4362255
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 74 deletions.
5 changes: 3 additions & 2 deletions pkg/bee/api/act.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (a *ActService) Download(ctx context.Context, addr swarm.Address, opts *Dow
func (a *ActService) Upload(ctx context.Context, name string, data io.Reader, o UploadOptions) (ActUploadResponse, error) {
var resp ActUploadResponse
h := http.Header{}
h.Add("swarm-postage-batch-id", o.BatchID)
h.Add(postageStampBatchHeader, o.BatchID)
h.Add("swarm-deferred-upload", "true")
h.Add("content-type", "application/octet-stream")
h.Add("Swarm-Act", "true")
Expand All @@ -43,7 +43,8 @@ func (a *ActService) Upload(ctx context.Context, name string, data io.Reader, o
func (a *ActService) AddGrantees(ctx context.Context, name string, data io.Reader, o UploadOptions) (ActGranteesResponse, error) {
var resp ActGranteesResponse
h := http.Header{}
h.Add("swarm-postage-batch-id", o.BatchID)
h.Add(postageStampBatchHeader, o.BatchID)
h.Add(swarmActHistoryAddress, o.ActHistoryAddress.String())
err := a.client.requestWithHeader(ctx, http.MethodPost, "/"+apiVersion+"/grantee", h, data, &resp)
return resp, err
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/bee/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ func (c *Client) requestData(ctx context.Context, method, path string, body io.R
if opts != nil && opts.RedundancyFallbackMode != nil {
req.Header.Set(swarmRedundancyFallbackMode, strconv.FormatBool(*opts.RedundancyFallbackMode))
}

r, err := c.httpClient.Do(req)
if err != nil {
return nil, err
Expand Down Expand Up @@ -313,11 +312,12 @@ func (f roundTripperFunc) RoundTrip(r *http.Request) (*http.Response, error) {
}

type UploadOptions struct {
Act bool
Pin bool
Tag uint64
BatchID string
Direct bool
Act bool
Pin bool
Tag uint64
BatchID string
Direct bool
ActHistoryAddress swarm.Address
}

type DownloadOptions struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/bee/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ func (c *Client) PatchActGrantees(ctx context.Context, pf *File, addr swarm.Addr
}

pf.SetAddress(r.Reference)
pf.SetHistroryAddress(r.HistoryAddress)
return nil
}

Expand Down
163 changes: 97 additions & 66 deletions pkg/check/act/act.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewDefaultOptions() Options {
FileName: "act",
FileSize: 1 * 1024,
PostageAmount: 420000000,
PostageDepth: 17,
PostageDepth: 20,
PostageLabel: "act-label",
Seed: 0,
}
Expand Down Expand Up @@ -66,12 +66,43 @@ func (c *Check) Run(ctx context.Context, cluster orchestration.Cluster, opts int
return err
}

sortedNodes := cluster.NodeNames()
upNodeName := sortedNodes[0]
for name, node := range cluster.Nodes() {
c.logger.Infof("Node %s: %+v", name, node.Config())
c.logger.Info("-------------------")
cl := node.Client()
t, _ := cl.Topology(ctx)
c.logger.Infof("Node %s: %+v", name, t)
c.logger.Info("=====================================")
}

fullNodes := cluster.FullNodeNames()
lightNodes := cluster.LightNodeNames()

//sortedNodes := cluster.NodeNames()
//upNodeName := sortedNodes[0]
upNodeName := lightNodes[0]
upClient := clients[upNodeName]
addr, _ := upClient.Addresses(ctx)
publisher, _ := swarm.ParseHexAddress(addr.PublicKey)

//nodeName1 := sortedNodes[1]
nodeName1 := fullNodes[0]
client1 := clients[nodeName1]
addr1, _ := client1.Addresses(ctx)
pubk1, _ := swarm.ParseHexAddress(addr1.PublicKey)

//nodeName2 := sortedNodes[2]
nodeName2 := fullNodes[1]
client2 := clients[nodeName2]
addr2, _ := client2.Addresses(ctx)
pubk2, _ := swarm.ParseHexAddress(addr2.PublicKey)

//odeName3 := sortedNodes[3]
nodeName3 := fullNodes[2]
client3 := clients[nodeName3]
addr3, _ := client3.Addresses(ctx)
pubk3, _ := swarm.ParseHexAddress(addr3.PublicKey)

rnds := random.PseudoGenerators(o.Seed, 1)

fileName := fmt.Sprintf("%s-%s-%d", o.FileName, upNodeName, rnds[0].Int())
Expand All @@ -91,50 +122,52 @@ func (c *Check) Run(ctx context.Context, cluster orchestration.Cluster, opts int
}
c.logger.Info("ACT file uploaded")

time.Sleep(3 * time.Second)
time.Sleep(5 * time.Second)

act := true
fileAddress := file.Address()
history := file.HistroryAddress()
timestamp := uint64(2)

// download act file
size, hash, err := upClient.DownloadActFile(ctx, file.Address(), &api.DownloadOptions{Act: &act, ActPublicKey: &publisher, ActHistoryAddress: &history, ActTimestamp: &timestamp})
size, hash, err := upClient.DownloadActFile(ctx, fileAddress, &api.DownloadOptions{Act: &act, ActPublicKey: &publisher, ActHistoryAddress: &history})
if err != nil {
return fmt.Errorf("node %s: %w", upNodeName, err)
}

if !bytes.Equal(file.Hash(), hash) {
c.logger.Infof("Node %s. ACT file hash not equal. Uploaded size: %d Downloaded size: %d File: %s", upNodeName, file.Size(), size, file.Address().String())
c.logger.Infof("Node %s. ACT file hash not equal. Uploaded size: %d Downloaded size: %d File: %s", upNodeName, file.Size(), size, fileAddress.String())
return errors.New("ACT file retrieval - hash error")
}

c.logger.Info("ACT file downloaded")
time.Sleep(5 * time.Second)

// download act file with wrong public key
notPublisher, _ := swarm.ParseHexAddress("010101010101010101010101010101010101010101010101010101010101010101")
_, _, notPErr := upClient.DownloadActFile(ctx, file.Address(), &api.DownloadOptions{Act: &act, ActPublicKey: &notPublisher, ActHistoryAddress: &history, ActTimestamp: &timestamp})
notPublisher := pubk1
_, _, notPErr := upClient.DownloadActFile(ctx, file.Address(), &api.DownloadOptions{Act: &act, ActPublicKey: &notPublisher, ActHistoryAddress: &history})
if notPErr == nil {
return fmt.Errorf("node %s: File downloaded with wrong public key successfully - this is an error", upNodeName)
}

c.logger.Info("ACT Access denied with incorrect public key")
time.Sleep(5 * time.Second)

// add grantees list
gFile := bee.NewBufferFile("grantees.json", bytes.NewBuffer([]byte(`{ "grantees": [
"02ceff1422a7026ba54ad89967d81f2805a55eb3d05f64eb5c49ea6024212b12e8",
"02ceff1422a7026ba54ad89967d81f2805a55eb3d05f64eb5c49ea6024212b12e9",
"02ceff1422a7026ba54ad89967d81f2805a55eb3d05f64eb5c49ea6024212b12ee"
]
"`+pubk2.String()+`",
"`+pubk3.String()+`"
]
}`)))

err = upClient.AddActGrantees(ctx, &gFile, api.UploadOptions{BatchID: batchID})
fileHis := file.HistroryAddress()
err = upClient.AddActGrantees(ctx, &gFile, api.UploadOptions{BatchID: batchID, ActHistoryAddress: fileHis})
if err != nil {
return fmt.Errorf("node %s: add grantees error: %w", upNodeName, err)
}

c.logger.Info("ACT grantees added")
time.Sleep(5 * time.Second)

time.Sleep(3 * time.Second)
time.Sleep(1 * time.Second)

// list grantees
addresses, gErr := upClient.GetActGrantees(ctx, gFile.Address())
Expand All @@ -144,66 +177,64 @@ func (c *Check) Run(ctx context.Context, cluster orchestration.Cluster, opts int
if addresses == nil {
return fmt.Errorf("node %s: GetActGrantees: addresses is nil", upNodeName)
}
if len(addresses) != 3 {
return fmt.Errorf("node %s: GetActGrantees: addresses length is not 3", upNodeName)
if len(addresses) != 2 {
return fmt.Errorf("node %s: GetActGrantees: addresses length is not 2", upNodeName)
}

c.logger.Info("ACT grantees listed")
time.Sleep(5 * time.Second)

// download act file with added grantees
grantees := []string{"02ceff1422a7026ba54ad89967d81f2805a55eb3d05f64eb5c49ea6024212b12e8",
"02ceff1422a7026ba54ad89967d81f2805a55eb3d05f64eb5c49ea6024212b12e9",
"02ceff1422a7026ba54ad89967d81f2805a55eb3d05f64eb5c49ea6024212b12ee"}

for _, str := range grantees {
gPublisher, _ := swarm.ParseHexAddress(str)
history := file.HistroryAddress()
gSize, gHash, gPErr := upClient.DownloadActFile(ctx, file.Address(), &api.DownloadOptions{Act: &act, ActPublicKey: &gPublisher, ActHistoryAddress: &history, ActTimestamp: &timestamp})
if gPErr != nil {
return fmt.Errorf("node %s: %w", upNodeName, gPErr)
}

if !bytes.Equal(file.Hash(), gHash) {
c.logger.Infof("Node %s. ACT file with new grantee hash not equal. Uploaded size: %d Downloaded size: %d File: %s", upNodeName, file.Size(), gSize, file.Address().String())
return errors.New("ACT file retrieval with new grantee - hash error")
// patch grantees
/*
pFile := bee.NewBufferFile("grantees-patch.json", bytes.NewBuffer([]byte(`{
"add": [
"`+pubk1.String()+`"
],
"revoke": [
"`+pubk2.String()+`",
"`+pubk3.String()+`"
]
}`)))
pErr := upClient.PatchActGrantees(ctx, &pFile, gFile.Address(), gFile.HistroryAddress(), batchID)
if pErr != nil {
return fmt.Errorf("node %s: PatchActGrantees: %w", upNodeName, pErr)
}
}
c.logger.Info("ACT file downloaded with new grantees")
c.logger.Info("ACT grantees patched")
// patch grantees
pFile := bee.NewBufferFile("grantees-patch.json", bytes.NewBuffer([]byte(`{
"add": [
"02ceff1422a7026ba54ad89967d81f2805a55eb3d05f64eb5c49ea6024212b12e7"
],
"revoke": [
"02ceff1422a7026ba54ad89967d81f2805a55eb3d05f64eb5c49ea6024212b12e9",
"02ceff1422a7026ba54ad89967d81f2805a55eb3d05f64eb5c49ea6024212b12ee"
]
}`)))

pErr := upClient.PatchActGrantees(ctx, &pFile, gFile.Address(), gFile.HistroryAddress(), batchID)
if pErr != nil {
return fmt.Errorf("node %s: PatchActGrantees: %w", upNodeName, pErr)
// list grantees after patch
patchAddresses, patchErr := upClient.GetActGrantees(ctx, pFile.Address())
if patchErr != nil {
return fmt.Errorf("node %s: GetActGrantees after patch: %w", upNodeName, patchErr)
}
if patchAddresses == nil {
return fmt.Errorf("node %s: GetActGrantees after patch: addresses is nil", upNodeName)
}
if len(patchAddresses) != 1 {
return fmt.Errorf("node %s: GetActGrantees after patch: addresses length is not 1", upNodeName)
}
c.logger.Info("ACT grantees listed after patch")
*/
// download act file
his := gFile.HistroryAddress()
c.logger.Infof("ACT file download started: %s", fileAddress.String())
_, _, err1 := client2.DownloadActFile(ctx, fileAddress, &api.DownloadOptions{Act: &act, ActPublicKey: &publisher, ActHistoryAddress: &his})
if err1 != nil {
return fmt.Errorf("node %s: %w", nodeName1, err1)
}

c.logger.Info("ACT grantees patched")

time.Sleep(3 * time.Second)
return errors.New("ACT file retrieval - hash error")
/*
if !bytes.Equal(file.Hash(), hash1) {
c.logger.Infof("Node %s. ACT file hash not equal. Uploaded size: %d Downloaded size: %d File: %s", nodeName1, file.Size(), size1, file.Address().String())
return errors.New("ACT file retrieval - hash error")
}
// list grantees after patch
patchAddresses, patchErr := upClient.GetActGrantees(ctx, pFile.Address())
if patchErr != nil {
return fmt.Errorf("node %s: GetActGrantees after patch: %w", upNodeName, patchErr)
}
if patchAddresses == nil {
return fmt.Errorf("node %s: GetActGrantees after patch: addresses is nil", upNodeName)
}
if len(patchAddresses) != 2 {
return fmt.Errorf("node %s: GetActGrantees after patch: addresses length is not 2", upNodeName)
}
c.logger.Info("ACT file downloaded with grantee")
c.logger.Info("ACT test completed")
c.logger.Info("ACT test completed")
return
return
*/
}

0 comments on commit 4362255

Please sign in to comment.