/
shared_params.go
90 lines (75 loc) · 2.43 KB
/
shared_params.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package sqlite
import (
"context"
"github.com/filecoin-project/go-state-types/big"
"gorm.io/gorm"
types "github.com/filecoin-project/venus/venus-shared/types/messager"
"github.com/ipfs-force-community/sophon-messager/models/mtypes"
"github.com/ipfs-force-community/sophon-messager/models/repo"
)
type sqliteSharedParams struct {
ID uint `gorm:"primary_key;column:id;type:INT unsigned AUTO_INCREMENT;NOT NULL" json:"id"`
SelMsgNum uint64 `gorm:"column:sel_msg_num;type:unsigned bigint;NOT NULL"`
FeeSpec
}
func fromSharedParams(sp types.SharedSpec) *sqliteSharedParams {
return &sqliteSharedParams{
ID: sp.ID,
SelMsgNum: sp.SelMsgNum,
FeeSpec: FeeSpec{
BaseFee: mtypes.SafeFromGo(sp.BaseFee.Int),
GasOverEstimation: sp.GasOverEstimation,
MaxFee: mtypes.SafeFromGo(sp.MaxFee.Int),
GasFeeCap: mtypes.SafeFromGo(sp.GasFeeCap.Int),
GasOverPremium: sp.GasOverPremium,
},
}
}
func (ssp sqliteSharedParams) SharedParams() *types.SharedSpec {
return &types.SharedSpec{
ID: ssp.ID,
SelMsgNum: ssp.SelMsgNum,
FeeSpec: types.FeeSpec{
GasOverEstimation: ssp.GasOverEstimation,
MaxFee: big.Int(mtypes.SafeFromGo(ssp.MaxFee.Int)),
GasFeeCap: big.Int(mtypes.SafeFromGo(ssp.GasFeeCap.Int)),
BaseFee: big.Int(mtypes.SafeFromGo(ssp.BaseFee.Int)),
GasOverPremium: ssp.GasOverPremium,
},
}
}
func (ssp sqliteSharedParams) TableName() string {
return "shared_params"
}
var _ repo.SharedParamsRepo = (*sqliteSharedParamsRepo)(nil)
type sqliteSharedParamsRepo struct {
*gorm.DB
}
func newSqliteSharedParamsRepo(db *gorm.DB) sqliteSharedParamsRepo {
return sqliteSharedParamsRepo{DB: db}
}
func (s sqliteSharedParamsRepo) GetSharedParams(ctx context.Context) (*types.SharedSpec, error) {
var ssp sqliteSharedParams
if err := s.DB.Take(&ssp).Error; err != nil {
return nil, err
}
return ssp.SharedParams(), nil
}
func (s sqliteSharedParamsRepo) SetSharedParams(ctx context.Context, params *types.SharedSpec) (uint, error) {
var ssp sqliteSharedParams
// make sure ID is 1
params.ID = 1
if err := s.DB.Where("id = ?", 1).Take(&ssp).Error; err != nil {
if err == gorm.ErrRecordNotFound {
if err := s.DB.Save(fromSharedParams(*params)).Error; err != nil {
return 0, err
}
return params.ID, nil
}
return 0, err
}
if err := s.DB.Save(fromSharedParams(*params)).Error; err != nil {
return 0, err
}
return params.ID, nil
}