Skip to content

Commit

Permalink
Merging with 'master'
Browse files Browse the repository at this point in the history
  • Loading branch information
yahortsaryk committed Oct 17, 2023
2 parents 1e6982a + 18f9a99 commit 59ccce9
Show file tree
Hide file tree
Showing 7 changed files with 537 additions and 4 deletions.
3 changes: 3 additions & 0 deletions dac/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/cerebellum-network/cere-ddc-sdk-go/dac

go 1.18
89 changes: 89 additions & 0 deletions dac/pkg/collection_point.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package dac

import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/cerebellum-network/cere-ddc-sdk-go/core/pkg/crypto"
"io"
"net/http"
"net/url"
"strconv"
"time"
)

type (
CollectionPoint interface {
SaveFulfillment(fulfillment Fulfillment) error
}

dacCollectionPoint struct {
url url.URL
httpClient http.Client
}

Fulfillment struct {
SessionId string `json:"sessionId"`
RequestId string `json:"requestId"`
Cid string `json:"cid"`
OpCode uint8 `json:"opCode"`
BytesSent uint32 `json:"bytesSent"`
FulfilledTimestamp uint64 `json:"fulfilledTimestamp"`
WorkerSignature []byte `json:"workerSignature"`
WorkerAddress string `json:"workerAddress"`
}
)

const (
dacTimeout = 10 * time.Second
fulfillmentPath = "/fulfillment"
)

func CreateCollectionPoint(url url.URL, httpClient http.Client) CollectionPoint {
return &dacCollectionPoint{
url,
httpClient,
}
}

func (d dacCollectionPoint) SaveFulfillment(fulfillment Fulfillment) error {
json, err := json.Marshal(fulfillment)
if err != nil {
return err
}

ctx, cancel := context.WithTimeout(context.Background(), dacTimeout)
defer cancel()

req, err := http.NewRequestWithContext(
ctx,
"POST",
d.url.String()+fulfillmentPath,
bytes.NewBuffer(json),
)

req.Header.Set("Content-Type", "application/json")

if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}

if response, err := d.httpClient.Do(req); err != nil {
return err
} else if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
body, _ := io.ReadAll(response.Body)
return fmt.Errorf("DAC collection point post: %d %s", response.StatusCode, string(body))
}

return nil
}

func SignFulfillment(fulfillment *Fulfillment, scheme crypto.Scheme) error {
signature, err := scheme.Sign([]byte(fulfillment.Cid + fulfillment.SessionId + fulfillment.RequestId + strconv.FormatUint(fulfillment.FulfilledTimestamp, 10)))
if err != nil {
return err
}
fulfillment.WorkerSignature = signature
return nil
}
2 changes: 1 addition & 1 deletion ddc-schemas
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ use (
./core
./model
./test
dac
)
426 changes: 426 additions & 0 deletions go.work.sum

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions model/domain/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Request struct {
Scheme string
MultiHashType uint64
SessionId []byte
RequestId string
}

var _ Protobufable = (*Request)(nil)
Expand All @@ -38,6 +39,7 @@ func (r *Request) ToProto() *pb.Request {
Scheme: r.Scheme,
MultiHashType: r.MultiHashType,
SessionId: r.SessionId,
RequestId: r.RequestId,
}
}

Expand All @@ -48,4 +50,5 @@ func (r *Request) ToDomain(pbRequest *pb.Request) {
r.Scheme = pbRequest.Scheme
r.MultiHashType = pbRequest.MultiHashType
r.SessionId = pbRequest.SessionId
r.RequestId = pbRequest.RequestId
}
17 changes: 14 additions & 3 deletions model/pb/request.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 59ccce9

Please sign in to comment.