Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added dac module with fulfillment tracking #62

Merged
merged 15 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading