Skip to content

Commit

Permalink
Added dac module with fulfillment tracking (#62)
Browse files Browse the repository at this point in the history
* Added dac module with fulfillment tracking

* Add workerAddress to Fulfillment struct

* Change POST to PUT to save fulfillment in DAC collection endpoint

* Add SignFulfillment func

* Upgrade go version in dac package

* Revert dac go upgrade

* Change URL formatting method in dac module

* Change URL formatting method in dac module

* Change URL formatting method in dac module

* Fail on non ok status codes in dac fulfillemnt post

* Fail on non ok status codes in dac fulfillemnt post

* Fail on non ok status codes in dac fulfillemnt post

* Fail on non ok status codes in dac fulfillemnt post

* Fail on non ok status codes in dac fulfillemnt post

* Fail on non ok status codes in dac fulfillemnt post
  • Loading branch information
idmitriev authored Sep 18, 2023
1 parent dd06e88 commit 2122780
Show file tree
Hide file tree
Showing 5 changed files with 521 additions and 1 deletion.
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
90 changes: 90 additions & 0 deletions dac/pkg/collection_point.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package dac

import (
"bytes"
"context"
"encoding/base64"
"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 []byte `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 {
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 + base64.StdEncoding.EncodeToString(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
Submodule ddc-schemas updated 0 files
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
)
Loading

0 comments on commit 2122780

Please sign in to comment.