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

adjust the fields included in the task structure #786

Merged
merged 1 commit into from
Dec 19, 2024
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
7 changes: 6 additions & 1 deletion datasource/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ func (p *Clickhouse) Retrieve(taskIDs []common.Hash) ([]*task.Task, error) {
if err != nil {
return nil, errors.Wrap(err, "failed to decode signature from hex format")
}
pubkey, err := hexutil.Decode(ts[i].DevicePubKey)
if err != nil {
return nil, errors.Wrap(err, "failed to decode device public key from hex format")
}

res = append(res, &task.Task{
ID: common.HexToHash(ts[i].TaskID),
ProjectID: pid,
ProjectVersion: ts[i].ProjectVersion,
Payload: []byte(ts[i].Payload),
DeviceID: common.HexToAddress(ts[i].DeviceID),
DevicePubKey: pubkey,
Signature: sig,
})
}
Expand Down
15 changes: 7 additions & 8 deletions service/apinode/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ var (
)

type CreateTaskReq struct {
Nonce uint64 `binding:"required"`
ProjectID string `binding:"required"`
ProjectVersion string
Payload []byte `binding:"required"`
Signature string `binding:"required"`
Nonce uint64 `json:"nonce" binding:"required"`
ProjectID string `json:"projectID" binding:"required"`
ProjectVersion string `json:"projectVersion,omitempty"`
Payload []byte `json:"payload" binding:"required"`
Signature string `json:"signature,omitempty" binding:"required"`
}

func (ct CreateTaskReq) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -211,13 +211,12 @@ func (s *httpServer) createTask(c *gin.Context) {
return
}

ioid := crypto.PubkeyToAddress(*matchedPubkey)
//ioid := crypto.PubkeyToAddress(*matchedPubkey)
taskID := crypto.Keccak256Hash(sig)

if err := s.db.CreateTask(
&db.Task{
DeviceID: ioid.Hex(),
// TODO: add a public key field to the task
DevicePubKey: hexutil.Encode(crypto.FromECDSAPub(matchedPubkey)),
TaskID: taskID.Hex(),
Nonce: req.Nonce,
ProjectID: pid.String(),
Expand Down
4 changes: 2 additions & 2 deletions service/apinode/db/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

type Task struct {
TaskID string `ch:"task_id" gorm:"primarykey"`
DeviceID string `ch:"device_id" gorm:"not null"`
DevicePubKey string `ch:"device_public_key" gorm:"not null"`
Nonce uint64 `ch:"nonce" gorm:"not null"`
ProjectID string `ch:"project_id" gorm:"not null"`
ProjectVersion string `ch:"project_version" gorm:"not null"`
Expand Down Expand Up @@ -51,7 +51,7 @@ func migrateCH(conn driver.Conn) error {
CREATE TABLE IF NOT EXISTS w3bstream_tasks
(
task_id String NOT NULL,
device_id String NOT NULL,
device_public_key String NOT NULL,
nonce UInt64 NOT NULL,
project_id String NOT NULL,
project_version String NOT NULL,
Expand Down
6 changes: 5 additions & 1 deletion task/processor/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func (r *processor) process(taskID common.Hash) error {
slog.Info("process task success", "project_id", t.ProjectID.String(), "task_id", t.ID, "process_time", processTime)
metrics.TaskDurationMtc.WithLabelValues(t.ProjectID.String(), t.ProjectVersion, t.ID.String()).Set(processTime.Seconds())

pubkey, err := crypto.UnmarshalPubkey(t.DevicePubKey)
if err != nil {
return errors.Wrap(err, "failed to unmarshal public key")
}
tx, err := r.routerInstance.Route(
&bind.TransactOpts{
From: r.account,
Expand All @@ -80,7 +84,7 @@ func (r *processor) process(taskID common.Hash) error {
t.ProjectID,
t.ID,
r.account,
t.DeviceID,
crypto.PubkeyToAddress(*pubkey),
proof,
)
if err != nil {
Expand Down
13 changes: 7 additions & 6 deletions task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (
)

type Task struct {
ID common.Hash `json:"id"`
ProjectID *big.Int `json:"projectID"`
ProjectVersion string `json:"projectVersion,omitempty"`
DeviceID common.Address `json:"deviceID"`
Payload []byte `json:"payload"`
Signature []byte `json:"signature,omitempty"`
ID common.Hash `json:"id"`
Nonce uint64 `json:"nonce"`
ProjectID *big.Int `json:"projectID"`
ProjectVersion string `json:"projectVersion,omitempty"`
DevicePubKey []byte `json:"devicePublicKey"`
Payload []byte `json:"payload"`
Signature []byte `json:"signature,omitempty"`
}

func (t *Task) Sign(prv *ecdsa.PrivateKey) ([]byte, error) {
Expand Down
66 changes: 59 additions & 7 deletions vm/payload.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
package vm

import (
"bytes"
"crypto/sha256"
_ "embed"
"encoding/binary"
"encoding/json"
"math/big"

"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/frontend"
"github.com/consensys/gnark/std/math/uints"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"github.com/tidwall/gjson"

"github.com/iotexproject/w3bstream/project"
"github.com/iotexproject/w3bstream/service/apinode/api"
"github.com/iotexproject/w3bstream/task"
)

var (
// TODO: use real project IDs
_pebbleProjectID = big.NewInt(1234)
_geoProjectID = big.NewInt(5678)
_pebbleProjectID = big.NewInt(923)
_geoProjectID = big.NewInt(942)
)

func LoadPayload(task *task.Task, projectConfig *project.Config) ([]byte, error) {
Expand All @@ -39,11 +47,14 @@ type ProofofLivenessCircuit struct {
func (circuit *ProofofLivenessCircuit) Define(api frontend.API) error { return nil }

func encodePebblePayload(task *task.Task, projectConfig *project.Config) ([]byte, error) {
// TODO: load from arguments
payloadHash := []byte{}
timestamp := 0
sig := []byte{}
pubbytes := []byte{}
sig := task.Signature
pubbytes := task.DevicePubKey
h1, _, data, err := recover(task, projectConfig)
if err != nil {
return nil, err
}
timestamp := data[0].(uint64)
payloadHash := h1

assignment := ProofofLivenessCircuit{
PayloadHash: uints.NewU8Array(payloadHash[:]),
Expand All @@ -57,3 +68,44 @@ func encodePebblePayload(task *task.Task, projectConfig *project.Config) ([]byte
}
return witness.MarshalBinary()
}

// TODO duplicate logic
func recover(t *task.Task, cfg *project.Config) (h1, h2 common.Hash, data []any, err error) {
req := api.CreateTaskReq{
Nonce: t.Nonce,
ProjectID: t.ProjectID.String(),
ProjectVersion: t.ProjectVersion,
Payload: t.Payload,
}
reqJson, err := json.Marshal(req)
if err != nil {
return common.Hash{}, common.Hash{}, nil, errors.Wrap(err, "failed to marshal request into json format")
}

switch cfg.HashAlgorithm {
default:
h1 = sha256.Sum256(reqJson)
if len(cfg.SignedKeys) == 0 {
h2 = h1
} else {
// concatenate payload hash with signed keys from payload json
if ok := gjson.ValidBytes(req.Payload); !ok {
return common.Hash{}, common.Hash{}, nil, errors.Wrap(err, "failed to validate payload in json format")
}
buf := new(bytes.Buffer)
buf.Write(h1[:])
for _, k := range cfg.SignedKeys {
value := gjson.GetBytes(req.Payload, k.Name)
switch k.Type {
case "uint64":
data = append(data, value.Uint())
if err := binary.Write(buf, binary.LittleEndian, value.Uint()); err != nil {
return common.Hash{}, common.Hash{}, nil, errors.New("failed to convert uint64 to bytes array")
}
}
}
h2 = sha256.Sum256(buf.Bytes())
}
}
return
}
Loading