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
2122type 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
2829type 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
3536type 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
4243type 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
8081func (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
104109func (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
143148func (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
148153func (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
170175func New (localDBDir string , prover common.Address ) (* DB , error ) {
0 commit comments