From 51630d72e3062222c081d98e9bb56540c3d27d4e Mon Sep 17 00:00:00 2001 From: huangzhiran <30522704+huangzhiran@users.noreply.github.com> Date: Tue, 7 Jan 2025 17:09:17 +0800 Subject: [PATCH] define proofType in project file, del hardcoding projectID judge (#802) --- e2e/e2e_test.go | 6 +++-- project/project.go | 1 + service/apinode/aggregator/aggregator.go | 34 +++++++++++++++++++----- service/apinode/api/http.go | 20 -------------- service/apinode/apinode.go | 2 +- vm/payload.go | 32 ++++++++++------------ vm/vm.go | 2 +- 7 files changed, 48 insertions(+), 49 deletions(-) diff --git a/e2e/e2e_test.go b/e2e/e2e_test.go index cfba7c07..1d5f1ac4 100644 --- a/e2e/e2e_test.go +++ b/e2e/e2e_test.go @@ -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"}}, }}} @@ -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"}, diff --git a/project/project.go b/project/project.go index 01961ee3..e353820a 100644 --- a/project/project.go +++ b/project/project.go @@ -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"` diff --git a/service/apinode/aggregator/aggregator.go b/service/apinode/aggregator/aggregator.go index b4000570..0c03e0c4 100644 --- a/service/apinode/aggregator/aggregator.go +++ b/service/apinode/aggregator/aggregator.go @@ -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() @@ -27,14 +29,32 @@ 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 { @@ -42,7 +62,7 @@ func Run(db *apidb.DB, sequencerAddr string, interval time.Duration) { continue } - for _, tasks := range tasksbyProject { + for _, tasks := range tasksByProject { if len(tasks) == 0 { continue } diff --git a/service/apinode/api/http.go b/service/apinode/api/http.go index 0ae2951f..464a7446 100644 --- a/service/apinode/api/http.go +++ b/service/apinode/api/http.go @@ -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"` diff --git a/service/apinode/apinode.go b/service/apinode/apinode.go index 4ce91f23..4398d6d9 100644 --- a/service/apinode/apinode.go +++ b/service/apinode/apinode.go @@ -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 { diff --git a/vm/payload.go b/vm/payload.go index 6d22b1ee..1d332883 100644 --- a/vm/payload.go +++ b/vm/payload.go @@ -2,7 +2,6 @@ package vm import ( _ "embed" - "math/big" "github.com/consensys/gnark-crypto/ecc" "github.com/consensys/gnark/frontend" @@ -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 { @@ -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( @@ -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 @@ -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{ @@ -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, diff --git a/vm/vm.go b/vm/vm.go index 006bbb28..86f97b26 100644 --- a/vm/vm.go +++ b/vm/vm.go @@ -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") }