Skip to content

Commit

Permalink
define proofType in project file, del hardcoding projectID judge (#802)
Browse files Browse the repository at this point in the history
  • Loading branch information
huangzhiran authored Jan 7, 2025
1 parent 83c3aea commit 51630d7
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 49 deletions.
6 changes: 4 additions & 2 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ func TestE2E(t *testing.T) {
project := &project.Project{Configs: []*project.Config{{
Version: "v1",
VMTypeID: 1,
ProofType: "liveness",
SignedKeys: []project.SignedKey{{Name: "timestamp", Type: "uint64"}},
}}}

Expand Down Expand Up @@ -199,8 +200,9 @@ func TestE2E(t *testing.T) {
gnarkCodePath := "./testdata/geodnet.circuit"
gnarkMetadataPath := "./testdata/geodnet.pk"
project := &project.Project{Configs: []*project.Config{{
Version: "v1",
VMTypeID: 1,
Version: "v1",
VMTypeID: 1,
ProofType: "movement",
SignedKeys: []project.SignedKey{
{Name: "timestamp", Type: "uint64"},
{Name: "latitude", Type: "uint64"},
Expand Down
1 change: 1 addition & 0 deletions project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type SignedKey struct {
type Config struct {
Version string `json:"version"`
VMTypeID uint64 `json:"vmTypeID"`
ProofType string `json:"proofType"` // liveness, movement
SignedKeys []SignedKey `json:"signedKeys"`
SignatureAlgorithm string `json:"signatureAlgorithm"`
HashAlgorithm string `json:"hashAlgorithm"`
Expand Down
34 changes: 27 additions & 7 deletions service/apinode/aggregator/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ import (
"fmt"
"io"
"log/slog"
"math/big"
"net/http"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/iotexproject/w3bstream/project"
apidb "github.com/iotexproject/w3bstream/service/apinode/db"
"github.com/iotexproject/w3bstream/service/sequencer/api"
"github.com/pkg/errors"
)

func Run(db *apidb.DB, sequencerAddr string, interval time.Duration) {
func Run(projectManager *project.Manager, db *apidb.DB, sequencerAddr string, interval time.Duration) {
ticker := time.NewTicker(interval)
for range ticker.C {
ts, err := db.FetchAllTask()
Expand All @@ -27,22 +29,40 @@ func Run(db *apidb.DB, sequencerAddr string, interval time.Duration) {
continue
}

tasksbyProject := make(map[string][]*apidb.Task)
tasksByProject := make(map[string][]*apidb.Task)
for i := range ts {
tasksbyProject[ts[i].ProjectID] = append(tasksbyProject[ts[i].ProjectID], ts[i])
tasksByProject[ts[i].ProjectID] = append(tasksByProject[ts[i].ProjectID], ts[i])
}

if tasks, ok := tasksbyProject["3"]; ok && len(tasks) > 0 {
prevTaskID := tasks[0].TaskID
tasks[len(tasks)-1].PrevTaskID = prevTaskID
for pidStr, tasks := range tasksByProject {
pid, ok := new(big.Int).SetString(pidStr, 10)
if !ok {
slog.Error("failed to decode project id string", "project_string", pidStr)
continue
}
p, err := projectManager.Project(pid)
if err != nil {
slog.Error("failed to get project", "error", err, "project_id", pidStr)
continue
}
// TODO support project config
cfg, err := p.DefaultConfig()
if err != nil {
slog.Error("failed to get project config", "error", err, "project_id", pidStr)
continue
}
if cfg.ProofType == "movement" && len(tasks) > 0 {
prevTaskID := tasks[0].TaskID
tasks[len(tasks)-1].PrevTaskID = prevTaskID
}
}

if err := dumpTasks(db, ts); err != nil {
slog.Error("failed to dump tasks", "error", err)
continue
}

for _, tasks := range tasksbyProject {
for _, tasks := range tasksByProject {
if len(tasks) == 0 {
continue
}
Expand Down
20 changes: 0 additions & 20 deletions service/apinode/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,6 @@ func newErrResp(err error) *errResp {
return &errResp{Error: err.Error()}
}

// TODO move to project file
// mainnet 6
// testnet 923
var (
pebbleProject = project.Config{
SignedKeys: []project.SignedKey{{Name: "timestamp", Type: "uint64"}},
SignatureAlgorithm: "ecdsa",
HashAlgorithm: "sha256",
}
geoProject = project.Config{
SignedKeys: []project.SignedKey{
{Name: "timestamp", Type: "uint64"},
{Name: "latitude", Type: "uint64"},
{Name: "longitude", Type: "uint64"},
},
SignatureAlgorithm: "ecdsa",
HashAlgorithm: "sha256",
}
)

type CreateTaskReq struct {
Nonce uint64 `json:"nonce" binding:"required"`
ProjectID string `json:"projectID" binding:"required"`
Expand Down
2 changes: 1 addition & 1 deletion service/apinode/apinode.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (n *APINode) Start() error {

projectManager := project.NewManager(n.db.Project, n.db.ProjectFile, n.db.UpsertProjectFile)

go aggregator.Run(n.db, n.cfg.SequencerServiceEndpoint, time.Duration(n.cfg.TaskAggregatorIntervalSecond)*time.Second)
go aggregator.Run(projectManager, n.db, n.cfg.SequencerServiceEndpoint, time.Duration(n.cfg.TaskAggregatorIntervalSecond)*time.Second)

go func() {
if err := api.Run(n.db, projectManager, n.cfg.ServiceEndpoint, n.cfg.SequencerServiceEndpoint, n.cfg.ProverServiceEndpoint); err != nil {
Expand Down
32 changes: 14 additions & 18 deletions vm/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package vm

import (
_ "embed"
"math/big"

"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/frontend"
Expand All @@ -14,18 +13,15 @@ import (
"github.com/iotexproject/w3bstream/task"
)

var (
_pebbleProjectID = big.NewInt(923)
_geoProjectID = big.NewInt(942)
)

func LoadPayload(task *task.Task, projectConfig *project.Config) ([]byte, error) {
if l := len(projectConfig.SignedKeys); l == 1 {
return encodePebblePayload(task, projectConfig)
} else if l > 1 {
return encodeGeodnetPayload(task, projectConfig)
func loadPayload(task *task.Task, projectConfig *project.Config) ([]byte, error) {
switch projectConfig.ProofType {
case "liveness":
return encodeLivenessPayload(task, projectConfig)
case "movement":
return encodeMovementPayload(task, projectConfig)
default:
return task.Payload, nil
}
return task.Payload, nil
}

type ProofofLivenessCircuit struct {
Expand All @@ -37,7 +33,7 @@ type ProofofLivenessCircuit struct {

func (circuit *ProofofLivenessCircuit) Define(api frontend.API) error { return nil }

func encodePebblePayload(task *task.Task, projectConfig *project.Config) ([]byte, error) {
func encodeLivenessPayload(task *task.Task, projectConfig *project.Config) ([]byte, error) {
sig := task.Signature[:64]
pubbytes := task.DevicePubKey
payloadHash, _, _, data, err := api.HashTask(
Expand Down Expand Up @@ -65,7 +61,7 @@ func encodePebblePayload(task *task.Task, projectConfig *project.Config) ([]byte
return witness.MarshalBinary()
}

type ProofofMovenessCircuit struct {
type ProofofMovementCircuit struct {
LastPayloadHash []uints.U8
LastTimestamp frontend.Variable
LastLatitude frontend.Variable
Expand All @@ -83,11 +79,11 @@ type ProofofMovenessCircuit struct {
PubBytes []uints.U8 `gnark:",public"`
}

func (circuit *ProofofMovenessCircuit) Define(api frontend.API) error { return nil }
func (circuit *ProofofMovementCircuit) Define(api frontend.API) error { return nil }

func encodeGeodnetPayload(task *task.Task, projectConfig *project.Config) ([]byte, error) {
func encodeMovementPayload(task *task.Task, projectConfig *project.Config) ([]byte, error) {
if task.PrevTask == nil {
return nil, errors.New("geodnet project miss previous task")
return nil, errors.New("movement project miss previous task")
}
lastPayloadHash, _, _, lastData, err := api.HashTask(
&api.CreateTaskReq{
Expand Down Expand Up @@ -122,7 +118,7 @@ func encodeGeodnetPayload(task *task.Task, projectConfig *project.Config) ([]byt
isMove = 1
}

assignment := ProofofMovenessCircuit{
assignment := ProofofMovementCircuit{
LastPayloadHash: uints.NewU8Array(lastPayloadHash[:]),
LastTimestamp: lastTimestamp,
LastLatitude: lastLatitude,
Expand Down
2 changes: 1 addition & 1 deletion vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (r *Handler) Handle(task *task.Task, projectConfig *project.Config) ([]byte
if err != nil {
return nil, errors.Wrap(err, "failed to decode metadata")
}
taskPayload, err := LoadPayload(task, projectConfig)
taskPayload, err := loadPayload(task, projectConfig)
if err != nil {
return nil, errors.Wrap(err, "failed to load payload")
}
Expand Down

0 comments on commit 51630d7

Please sign in to comment.