Skip to content

Commit

Permalink
change projectID type from uint64 to *big.Int
Browse files Browse the repository at this point in the history
  • Loading branch information
huangzhiran authored Nov 26, 2024
1 parent bec717b commit 050188f
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 124 deletions.
2 changes: 1 addition & 1 deletion cmd/sign/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {
log.Fatal(errors.Wrap(err, "failed to parse private key"))
}
req := &api.CreateTaskReq{
ProjectID: 912,
ProjectID: "912",
ProjectVersion: "v1.0.0",
Payloads: []string{"{\"private_input\":\"14\", \"public_input\":\"3,34\", \"receipt_type\":\"Snark\"}"},
}
Expand Down
7 changes: 6 additions & 1 deletion datasource/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package datasource
import (
"context"
"encoding/json"
"math/big"

"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
Expand Down Expand Up @@ -36,9 +37,13 @@ func (p *Clickhouse) Retrieve(taskIDs []common.Hash) ([]*task.Task, error) {
if err := json.Unmarshal(ts[i].Payloads, &ps); err != nil {
return nil, errors.Wrapf(err, "failed to unmarshal task payloads, task_id %v", ts[i].TaskID)
}
pid := new(big.Int)
if _, ok := pid.SetString(ts[i].ProjectID, 10); !ok {
return nil, errors.New("failed to decode project id string")
}
res = append(res, &task.Task{
ID: common.BytesToHash(ts[i].TaskID),
ProjectID: ts[i].ProjectID,
ProjectID: pid,
ProjectVersion: ts[i].ProjectVersion,
Payloads: ps,
DeviceID: common.BytesToAddress(ts[i].DeviceID),
Expand Down
58 changes: 0 additions & 58 deletions datasource/postgres.go

This file was deleted.

3 changes: 2 additions & 1 deletion e2e/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log"
"math/big"
"net/http"
"strconv"
"testing"
"time"

Expand All @@ -27,7 +28,7 @@ import (
func signMesssage(data []byte, projectID uint64, key *ecdsa.PrivateKey) ([]byte, error) {
req := &api.CreateTaskReq{
Nonce: uint64(time.Now().Unix()),
ProjectID: projectID,
ProjectID: strconv.Itoa(int(projectID)),
Payloads: []string{hexutil.Encode(data)},
}

Expand Down
6 changes: 0 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ require (
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.1
gorm.io/driver/postgres v1.5.9
gorm.io/driver/sqlite v1.5.6
gorm.io/gorm v1.25.12
)
Expand Down Expand Up @@ -77,10 +76,6 @@ require (
github.com/holiman/uint256 v1.2.3 // indirect
github.com/ipfs/boxo v0.17.0 // indirect
github.com/ipfs/go-cid v0.4.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.7.1 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down Expand Up @@ -156,7 +151,6 @@ require (
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.19.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
Expand Down
10 changes: 0 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,6 @@ github.com/ipfs/go-cid v0.4.1 h1:A/T3qGvxi4kpKWWcPC/PgbvDA2bjVLO7n4UeVwnbs/s=
github.com/ipfs/go-cid v0.4.1/go.mod h1:uQHwDeX4c6CtyrFwdqyhpNcxVewur1M7l7fNU7LKwZk=
github.com/ipfs/go-ipfs-api v0.7.0 h1:CMBNCUl0b45coC+lQCXEVpMhwoqjiaCwUIrM+coYW2Q=
github.com/ipfs/go-ipfs-api v0.7.0/go.mod h1:AIxsTNB0+ZhkqIfTZpdZ0VR/cpX5zrXjATa3prSay3g=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs=
github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jackpal/go-nat-pmp v1.0.2 h1:KzKSgb7qkJvOUTqYl9/Hg/me3pWgBmERKrTGD7BdWus=
github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
Expand Down Expand Up @@ -879,8 +871,6 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/postgres v1.5.9 h1:DkegyItji119OlcaLjqN11kHoUgZ/j13E0jkJZgD6A8=
gorm.io/driver/postgres v1.5.9/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE=
gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
Expand Down
8 changes: 4 additions & 4 deletions monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ type (
UpsertScannedBlockNumber func(number uint64) error
AssignTask func(taskID common.Hash, prover common.Address) error
SettleTask func(taskID, tx common.Hash) error
UpsertProject func(projectID uint64, uri string, hash common.Hash) error
UpsertProject func(projectID *big.Int, uri string, hash common.Hash) error
UpsertProver func(addr common.Address) error
UpsertProjectDevice func(projectID uint64, address common.Address) error
UpsertProjectDevice func(projectID *big.Int, address common.Address) error
)

type Handler struct {
Expand Down Expand Up @@ -146,7 +146,7 @@ func (c *contract) processLogs(logs []types.Log) error {
if err != nil {
return errors.Wrap(err, "failed to parse project config updated event")
}
if err := c.h.UpsertProject(e.ProjectId.Uint64(), e.Uri, e.Hash); err != nil {
if err := c.h.UpsertProject(e.ProjectId, e.Uri, e.Hash); err != nil {
return err
}
case proverSetTopic:
Expand All @@ -173,7 +173,7 @@ func (c *contract) processLogs(logs []types.Log) error {
if err != nil {
return errors.Wrapf(err, "failed to query device project, device_id %s", e.Did)
}
if err := c.h.UpsertProjectDevice(pid.Uint64(), address); err != nil {
if err := c.h.UpsertProjectDevice(pid, address); err != nil {
return err
}
}
Expand Down
9 changes: 5 additions & 4 deletions project/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ package project

import (
"bytes"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
)

type ContractProject func(projectID uint64) (string, common.Hash, error)
type ProjectFile func(projectID uint64) ([]byte, common.Hash, error)
type UpsertProjectFile func(projectID uint64, file []byte, hash common.Hash) error
type ContractProject func(projectID *big.Int) (string, common.Hash, error)
type ProjectFile func(projectID *big.Int) ([]byte, common.Hash, error)
type UpsertProjectFile func(projectID *big.Int, file []byte, hash common.Hash) error

type Manager struct {
contractProject ContractProject
projectFile ProjectFile
upsertProjectFile UpsertProjectFile
}

func (m *Manager) Project(projectID uint64) (*Project, error) {
func (m *Manager) Project(projectID *big.Int) (*Project, error) {
uri, hash, err := m.contractProject(projectID)
if err != nil {
return nil, errors.Errorf("failed to get project metadata, project_id %v", projectID)
Expand Down
3 changes: 2 additions & 1 deletion project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"encoding/json"
"io"
"math/big"
"net/http"
"net/url"
"strings"
Expand All @@ -26,7 +27,7 @@ type Project struct {
}

type Meta struct {
ProjectID uint64
ProjectID *big.Int
Uri string
Hash [32]byte
}
Expand Down
17 changes: 12 additions & 5 deletions service/apinode/api/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"log/slog"
"math/big"
"net/http"
"strings"
"time"
Expand All @@ -34,7 +35,7 @@ func newErrResp(err error) *errResp {

type CreateTaskReq struct {
Nonce uint64 `json:"nonce" binding:"required"`
ProjectID uint64 `json:"projectID" binding:"required"`
ProjectID string `json:"projectID" binding:"required"`
ProjectVersion string `json:"projectVersion,omitempty"`
Payloads []string `json:"payloads" binding:"required"`
Algorithm string `json:"algorithm,omitempty"` // Refer to the constants defined in JWT (JSON Web Token) https://jwt.io/
Expand All @@ -55,7 +56,7 @@ type StateLog struct {
}

type QueryTaskResp struct {
ProjectID uint64 `json:"projectID"`
ProjectID string `json:"projectID"`
TaskID string `json:"taskID"`
States []*StateLog `json:"states"`
}
Expand All @@ -75,6 +76,12 @@ func (s *httpServer) createTask(c *gin.Context) {
return
}

pid := new(big.Int)
if _, ok := pid.SetString(req.ProjectID, 10); !ok {
slog.Error("failed to decode project id string", "project_id", req.ProjectID)
c.JSON(http.StatusBadRequest, newErrResp(errors.New("failed to decode project id string")))
return
}
sig, err := hexutil.Decode(req.Signature)
if err != nil {
slog.Error("failed to decode signature from hex format", "error", err)
Expand All @@ -89,14 +96,14 @@ func (s *httpServer) createTask(c *gin.Context) {
}
addr := crypto.PubkeyToAddress(*pubkey)

ok, err := s.db.IsDeviceApproved(req.ProjectID, addr)
ok, err := s.db.IsDeviceApproved(pid, addr)
if err != nil {
slog.Error("failed to check device permission", "error", err)
c.JSON(http.StatusInternalServerError, newErrResp(errors.Wrap(err, "failed to check device permission")))
return
}
if !ok {
slog.Error("device does not have permission", "project_id", req.ProjectID, "device_address", addr.String())
slog.Error("device does not have permission", "project_id", pid.String(), "device_address", addr.String())
c.JSON(http.StatusForbidden, newErrResp(errors.New("device does not have permission")))
return
}
Expand Down Expand Up @@ -124,7 +131,7 @@ func (s *httpServer) createTask(c *gin.Context) {
DeviceID: addr.Bytes(),
TaskID: taskID.Bytes(),
Nonce: req.Nonce,
ProjectID: req.ProjectID,
ProjectID: pid.String(),
ProjectVersion: req.ProjectVersion,
Payloads: payloadsJ,
Signature: sig,
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 @@ -15,7 +15,7 @@ type Task struct {
TaskID []byte `ch:"task_id"`
DeviceID []byte `ch:"device_id"`
Nonce uint64 `ch:"nonce"`
ProjectID uint64 `ch:"project_id"`
ProjectID string `ch:"project_id"`
ProjectVersion string `ch:"project_version"`
Payloads []byte `ch:"payloads"`
Signature []byte `ch:"signature"`
Expand Down Expand Up @@ -50,7 +50,7 @@ func migrateCH(conn driver.Conn) error {
task_id Array(UInt8) NOT NULL,
device_id Array(UInt8) NOT NULL,
nonce UInt64 NOT NULL,
project_id UInt64 NOT NULL,
project_id String NOT NULL,
project_version String NOT NULL,
payloads Array(UInt8) NOT NULL,
signature Array(UInt8) NOT NULL,
Expand Down
12 changes: 7 additions & 5 deletions service/apinode/db/sqlite.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package db

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
"gorm.io/driver/sqlite"
Expand Down Expand Up @@ -28,7 +30,7 @@ type SettledTask struct {

type ProjectDevice struct {
gorm.Model
ProjectID uint64 `gorm:"uniqueIndex:project_device_uniq,not null"`
ProjectID string `gorm:"uniqueIndex:project_device_uniq,not null"`
DeviceAddress common.Address `gorm:"uniqueIndex:project_device_uniq,not null"`
}

Expand Down Expand Up @@ -78,9 +80,9 @@ func (p *DB) FetchSettledTask(taskID common.Hash) (*SettledTask, error) {
return &t, nil
}

func (p *DB) UpsertProjectDevice(projectID uint64, address common.Address) error {
func (p *DB) UpsertProjectDevice(projectID *big.Int, address common.Address) error {
t := ProjectDevice{
ProjectID: projectID,
ProjectID: projectID.String(),
DeviceAddress: address,
}
err := p.sqlite.Clauses(clause.OnConflict{
Expand All @@ -90,9 +92,9 @@ func (p *DB) UpsertProjectDevice(projectID uint64, address common.Address) error
return errors.Wrap(err, "failed to upsert project device")
}

func (p *DB) IsDeviceApproved(projectID uint64, address common.Address) (bool, error) {
func (p *DB) IsDeviceApproved(projectID *big.Int, address common.Address) (bool, error) {
t := ProjectDevice{}
if err := p.sqlite.Where("project_id = ?", projectID).Where("device_address = ?", address).First(&t).Error; err != nil {
if err := p.sqlite.Where("project_id = ?", projectID.String()).Where("device_address = ?", address).First(&t).Error; err != nil {
if err == gorm.ErrRecordNotFound {
return false, nil
}
Expand Down
Loading

0 comments on commit 050188f

Please sign in to comment.