Skip to content

Commit 821253b

Browse files
authored
store hex format for binary data in sqlite (#771)
1 parent 149fbbd commit 821253b

File tree

4 files changed

+53
-44
lines changed

4 files changed

+53
-44
lines changed

service/apinode/api/http.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ func (s *httpServer) queryTask(c *gin.Context) {
279279
resp.States = append(resp.States, &StateLog{
280280
State: "assigned",
281281
Time: assignedTask.CreatedAt,
282-
ProverID: "did:io:" + strings.TrimPrefix(assignedTask.Prover.String(), "0x"),
282+
ProverID: "did:io:" + strings.TrimPrefix(assignedTask.Prover, "0x"),
283283
})
284284

285285
// Get settled state
@@ -300,7 +300,7 @@ func (s *httpServer) queryTask(c *gin.Context) {
300300
State: "settled",
301301
Time: settledTask.CreatedAt,
302302
Comment: "The task has been completed. Please check the generated proof in the corresponding DApp contract.",
303-
Tx: settledTask.Tx.String(),
303+
Tx: settledTask.Tx,
304304
})
305305
c.JSON(http.StatusOK, resp)
306306
return

service/apinode/db/sqlite.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,26 @@ type scannedBlockNumber struct {
1818

1919
type AssignedTask struct {
2020
gorm.Model
21-
TaskID common.Hash `gorm:"uniqueIndex:assigned_task_uniq,not null"`
22-
Prover common.Address
21+
TaskID string `gorm:"uniqueIndex:assigned_task_uniq,not null"`
22+
Prover string
2323
}
2424

2525
type SettledTask struct {
2626
gorm.Model
27-
TaskID common.Hash `gorm:"uniqueIndex:settled_task_uniq,not null"`
28-
Tx common.Hash `gorm:"not null"`
27+
TaskID string `gorm:"uniqueIndex:settled_task_uniq,not null"`
28+
Tx string `gorm:"not null"`
2929
}
3030

3131
type ProjectDevice struct {
3232
gorm.Model
33-
ProjectID string `gorm:"uniqueIndex:project_device_uniq,not null"`
34-
DeviceAddress common.Address `gorm:"uniqueIndex:project_device_uniq,not null"`
33+
ProjectID string `gorm:"uniqueIndex:project_device_uniq,not null"`
34+
DeviceAddress string `gorm:"uniqueIndex:project_device_uniq,not null"`
3535
}
3636

3737
func (p *DB) UpsertAssignedTask(taskID common.Hash, prover common.Address) error {
3838
t := AssignedTask{
39-
TaskID: taskID,
40-
Prover: prover,
39+
TaskID: taskID.Hex(),
40+
Prover: prover.Hex(),
4141
}
4242
err := p.sqlite.Clauses(clause.OnConflict{
4343
Columns: []clause.Column{{Name: "task_id"}},
@@ -48,8 +48,8 @@ func (p *DB) UpsertAssignedTask(taskID common.Hash, prover common.Address) error
4848

4949
func (p *DB) UpsertSettledTask(taskID, tx common.Hash) error {
5050
t := SettledTask{
51-
TaskID: taskID,
52-
Tx: tx,
51+
TaskID: taskID.Hex(),
52+
Tx: tx.Hex(),
5353
}
5454
err := p.sqlite.Clauses(clause.OnConflict{
5555
Columns: []clause.Column{{Name: "task_id"}},
@@ -60,7 +60,7 @@ func (p *DB) UpsertSettledTask(taskID, tx common.Hash) error {
6060

6161
func (p *DB) FetchAssignedTask(taskID common.Hash) (*AssignedTask, error) {
6262
t := AssignedTask{}
63-
if err := p.sqlite.Where("task_id = ?", taskID).First(&t).Error; err != nil {
63+
if err := p.sqlite.Where("task_id = ?", taskID.Hex()).First(&t).Error; err != nil {
6464
if err == gorm.ErrRecordNotFound {
6565
return nil, nil
6666
}
@@ -71,7 +71,7 @@ func (p *DB) FetchAssignedTask(taskID common.Hash) (*AssignedTask, error) {
7171

7272
func (p *DB) FetchSettledTask(taskID common.Hash) (*SettledTask, error) {
7373
t := SettledTask{}
74-
if err := p.sqlite.Where("task_id = ?", taskID).First(&t).Error; err != nil {
74+
if err := p.sqlite.Where("task_id = ?", taskID.Hex()).First(&t).Error; err != nil {
7575
if err == gorm.ErrRecordNotFound {
7676
return nil, nil
7777
}
@@ -83,7 +83,7 @@ func (p *DB) FetchSettledTask(taskID common.Hash) (*SettledTask, error) {
8383
func (p *DB) UpsertProjectDevice(projectID *big.Int, address common.Address) error {
8484
t := ProjectDevice{
8585
ProjectID: projectID.String(),
86-
DeviceAddress: address,
86+
DeviceAddress: address.Hex(),
8787
}
8888
err := p.sqlite.Clauses(clause.OnConflict{
8989
Columns: []clause.Column{{Name: "project_id"}, {Name: "device_address"}},
@@ -94,7 +94,7 @@ func (p *DB) UpsertProjectDevice(projectID *big.Int, address common.Address) err
9494

9595
func (p *DB) IsDeviceApproved(projectID *big.Int, address common.Address) (bool, error) {
9696
t := ProjectDevice{}
97-
if err := p.sqlite.Where("project_id = ?", projectID.String()).Where("device_address = ?", address).First(&t).Error; err != nil {
97+
if err := p.sqlite.Where("project_id = ?", projectID.String()).Where("device_address = ?", address.Hex()).First(&t).Error; err != nil {
9898
if err == gorm.ErrRecordNotFound {
9999
return false, nil
100100
}

service/prover/db/db.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
"github.com/ethereum/go-ethereum/common"
9+
"github.com/ethereum/go-ethereum/common/hexutil"
910
"github.com/pkg/errors"
1011
"gorm.io/driver/sqlite"
1112
"gorm.io/gorm"
@@ -20,23 +21,23 @@ type scannedBlockNumber struct {
2021

2122
type project struct {
2223
gorm.Model
23-
ProjectID string `gorm:"uniqueIndex:project_id_project,not null"`
24-
URI string `gorm:"not null"`
25-
Hash common.Hash `gorm:"not null"`
24+
ProjectID string `gorm:"uniqueIndex:project_id_project,not null"`
25+
URI string `gorm:"not null"`
26+
Hash string `gorm:"not null"`
2627
}
2728

2829
type projectFile struct {
2930
gorm.Model
30-
ProjectID string `gorm:"uniqueIndex:project_id_project_file,not null"`
31-
File []byte `gorm:"not null"`
32-
Hash common.Hash `gorm:"not null"`
31+
ProjectID string `gorm:"uniqueIndex:project_id_project_file,not null"`
32+
File string `gorm:"not null"`
33+
Hash string `gorm:"not null"`
3334
}
3435

3536
type task struct {
3637
gorm.Model
37-
TaskID common.Hash `gorm:"uniqueIndex:task_uniq,not null"`
38-
Processed bool `gorm:"index:unprocessed_task,not null,default:false"`
39-
Error string `gorm:"not null,default:''"`
38+
TaskID string `gorm:"uniqueIndex:task_uniq,not null"`
39+
Processed bool `gorm:"index:unprocessed_task,not null,default:false"`
40+
Error string `gorm:"not null,default:''"`
4041
}
4142

4243
type DB struct {
@@ -74,14 +75,14 @@ func (p *DB) Project(projectID *big.Int) (string, common.Hash, error) {
7475
if err := p.db.Where("project_id = ?", projectID.String()).First(&t).Error; err != nil {
7576
return "", common.Hash{}, errors.Wrap(err, "failed to query project")
7677
}
77-
return t.URI, t.Hash, nil
78+
return t.URI, common.HexToHash(t.Hash), nil
7879
}
7980

8081
func (p *DB) UpsertProject(projectID *big.Int, uri string, hash common.Hash) error {
8182
t := project{
8283
ProjectID: projectID.String(),
8384
URI: uri,
84-
Hash: hash,
85+
Hash: hash.Hex(),
8586
}
8687
err := p.db.Clauses(clause.OnConflict{
8788
Columns: []clause.Column{{Name: "project_id"}},
@@ -98,14 +99,18 @@ func (p *DB) ProjectFile(projectID *big.Int) ([]byte, common.Hash, error) {
9899
}
99100
return nil, common.Hash{}, errors.Wrap(err, "failed to query project file")
100101
}
101-
return t.File, t.Hash, nil
102+
f, err := hexutil.Decode(t.File)
103+
if err != nil {
104+
return nil, common.Hash{}, errors.Wrap(err, "failed to decode file from hex format")
105+
}
106+
return f, common.HexToHash(t.Hash), nil
102107
}
103108

104109
func (p *DB) UpsertProjectFile(projectID *big.Int, file []byte, hash common.Hash) error {
105110
t := projectFile{
106111
ProjectID: projectID.String(),
107-
File: file,
108-
Hash: hash,
112+
File: hexutil.Encode(file),
113+
Hash: hash.Hex(),
109114
}
110115
err := p.db.Clauses(clause.OnConflict{
111116
Columns: []clause.Column{{Name: "project_id"}},
@@ -119,7 +124,7 @@ func (p *DB) CreateTask(taskID common.Hash, prover common.Address) error {
119124
return nil
120125
}
121126
t := &task{
122-
TaskID: taskID,
127+
TaskID: taskID.Hex(),
123128
Processed: false,
124129
}
125130
err := p.db.Clauses(clause.OnConflict{
@@ -136,18 +141,18 @@ func (p *DB) ProcessTask(taskID common.Hash, err error) error {
136141
if err != nil {
137142
t.Error = err.Error()
138143
}
139-
err = p.db.Model(t).Where("task_id = ?", taskID).Updates(t).Error
144+
err = p.db.Model(t).Where("task_id = ?", taskID.Hex()).Updates(t).Error
140145
return errors.Wrap(err, "failed to update task")
141146
}
142147

143148
func (p *DB) DeleteTask(taskID, tx common.Hash) error {
144-
err := p.db.Where("task_id = ?", taskID).Delete(&task{}).Error
149+
err := p.db.Where("task_id = ?", taskID.Hex()).Delete(&task{}).Error
145150
return errors.Wrap(err, "failed to delete task")
146151
}
147152

148153
func (p *DB) ProcessedTask(taskID common.Hash) (bool, string, time.Time, error) {
149154
t := task{}
150-
if err := p.db.Where("task_id = ?", taskID).First(&t).Error; err != nil {
155+
if err := p.db.Where("task_id = ?", taskID.Hex()).First(&t).Error; err != nil {
151156
if err == gorm.ErrRecordNotFound {
152157
return false, "", time.Now(), nil
153158
}
@@ -164,7 +169,7 @@ func (p *DB) UnprocessedTask() (common.Hash, error) {
164169
}
165170
return common.Hash{}, errors.Wrap(err, "failed to query unprocessed task")
166171
}
167-
return t.TaskID, nil
172+
return common.HexToHash(t.TaskID), nil
168173
}
169174

170175
func New(localDBDir string, prover common.Address) (*DB, error) {

service/sequencer/db/db.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ type scannedBlockNumber struct {
1818

1919
type prover struct {
2020
gorm.Model
21-
Prover common.Address `gorm:"uniqueIndex:prover,not null"`
21+
Prover string `gorm:"uniqueIndex:prover,not null"`
2222
}
2323

2424
type task struct {
2525
gorm.Model
26-
TaskID common.Hash `gorm:"uniqueIndex:task_uniq,not null"`
27-
Assigned bool `gorm:"index:unassigned_task,not null,default:false"`
26+
TaskID string `gorm:"uniqueIndex:task_uniq,not null"`
27+
Assigned bool `gorm:"index:unassigned_task,not null,default:false"`
2828
}
2929

3030
type DB struct {
@@ -63,15 +63,15 @@ func (p *DB) Provers() ([]common.Address, error) {
6363
}
6464
res := make([]common.Address, 0, len(ts))
6565
for _, t := range ts {
66-
res = append(res, t.Prover)
66+
res = append(res, common.HexToAddress(t.Prover))
6767
}
6868
metrics.ProverMtc.Set(float64(len(ts)))
6969
return res, nil
7070
}
7171

7272
func (p *DB) UpsertProver(addr common.Address) error {
7373
t := prover{
74-
Prover: addr,
74+
Prover: addr.Hex(),
7575
}
7676
err := p.db.Clauses(clause.OnConflict{
7777
Columns: []clause.Column{{Name: "prover"}},
@@ -82,7 +82,7 @@ func (p *DB) UpsertProver(addr common.Address) error {
8282

8383
func (p *DB) CreateTask(taskID common.Hash) error {
8484
t := &task{
85-
TaskID: taskID,
85+
TaskID: taskID.Hex(),
8686
Assigned: false,
8787
}
8888
if err := p.db.Create(t).Error; err != nil {
@@ -95,15 +95,19 @@ func (p *DB) AssignTasks(ids []common.Hash) error {
9595
if len(ids) == 0 {
9696
return nil
9797
}
98+
sids := make([]string, 0, len(ids))
99+
for _, id := range ids {
100+
sids = append(sids, id.Hex())
101+
}
98102
t := &task{
99103
Assigned: true,
100104
}
101-
err := p.db.Model(t).Where("task_id IN ?", ids).Updates(t).Error
105+
err := p.db.Model(t).Where("task_id IN ?", sids).Updates(t).Error
102106
return errors.Wrap(err, "failed to assign tasks")
103107
}
104108

105109
func (p *DB) DeleteTask(taskID, tx common.Hash) error {
106-
err := p.db.Where("task_id = ?", taskID).Delete(&task{}).Error
110+
err := p.db.Where("task_id = ?", taskID.Hex()).Delete(&task{}).Error
107111
return errors.Wrap(err, "failed to delete task")
108112
}
109113

@@ -114,7 +118,7 @@ func (p *DB) UnassignedTasks(limit int) ([]common.Hash, error) {
114118
}
115119
ids := []common.Hash{}
116120
for _, t := range ts {
117-
ids = append(ids, t.TaskID)
121+
ids = append(ids, common.HexToHash(t.TaskID))
118122
}
119123
return ids, nil
120124
}

0 commit comments

Comments
 (0)