-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added dac module with fulfillment tracking (#62)
* 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
Showing
5 changed files
with
521 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ use ( | |
./core | ||
./model | ||
./test | ||
dac | ||
) |
Oops, something went wrong.