-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
184 lines (167 loc) · 5.9 KB
/
model.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"database/sql/driver"
"encoding/json"
"fmt"
"time"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/exitcode"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/ipfs/go-cid"
"gorm.io/gorm"
)
type RequestStatus string
const (
RequestStatusCreated RequestStatus = "created"
RequestStatusPending RequestStatus = "pending"
RequestStatusFailed RequestStatus = "failed"
RequestStatusPartfailed RequestStatus = "partfailed"
RequestStatusSuccess RequestStatus = "success"
)
// Request represents a request in the system.
type Request struct {
ID uint `json:"id" gorm:"primarykey"`
Miner Address `gorm:"index" json:"miner"`
From time.Time `json:"from"`
To time.Time `json:"to"`
Extension *abi.ChainEpoch `json:"extension"`
NewExpiration *abi.ChainEpoch `json:"new_expiration"`
Tolerance abi.ChainEpoch `json:"tolerance"`
MaxSectors int `json:"max_sectors"`
MaxInitialPledges float64 `json:"max_initial_pledges"`
Status RequestStatus `gorm:"index" json:"status"`
Messages []*Message `json:"messages"`
TotalSectors int `json:"total_sectors"`
PublishedSectors int `json:"published_sectors"`
SucceededSectors int `json:"succeeded_sectors"`
Error string `json:"error"`
Took float64 `json:"took"` // Time in seconds
ConfirmedAt *time.Time `json:"confirmed_at"`
DryRun bool `json:"dry_run"`
DryRunResult string `json:"dry_run_result"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
func (r *Request) MessageCids() []string {
var cids []string
for _, msg := range r.Messages {
cids = append(cids, msg.Cid.String())
}
return cids
}
func (r *Request) AfterFind(_ *gorm.DB) (err error) {
if r.DryRun {
r.SucceededSectors = r.PublishedSectors
} else {
var success int
for _, msg := range r.Messages {
if msg.OnChain && msg.ExitCode.IsSuccess() {
success += msg.Sectors
}
}
r.SucceededSectors = success
}
return nil
}
// Message represents a message in the system.
// It contains a unique identifier (MsgCid) and a list of Extensions.
type Message struct {
ID uint `gorm:"primarykey"`
Cid CID `gorm:"index"` // The unique identifier of the message
Extensions []Extension2 // The list of extensions associated with the message
RequestID uint
OnChain bool
ExitCode exitcode.ExitCode
Return []byte
GasUsed int64
Sectors int
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
}
func (m *Message) MarshalJSON() ([]byte, error) {
// We only need to marshal the CID as a string
return json.Marshal(m.Cid.String())
}
// Extension2 represents an extension in the system.
// It contains information about the deadline, partition, sectors, sectors with claims, new expiration and message ID.
type Extension2 struct {
ID uint `gorm:"primarykey" json:"id"`
Deadline uint64 `json:"deadline"` // The deadline for the extension
Partition uint64 `json:"partition"` // The partition number
Sectors string `json:"sectors"` // The sectors associated with the extension
SectorsWithClaims []byte `json:"sectors_with_claims"` // The sectors with claims
NewExpiration abi.ChainEpoch `json:"new_expiration"` // The new expiration date for the extension
MessageID uint `json:"message_id"` // The ID of the message associated with the extension
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
}
// NewExtension2FromParams creates a list of Extension2 from the provided parameters.
// It returns an error if there is an issue with counting the sectors or marshalling the sectors with claims.
func NewExtension2FromParams(params miner.ExtendSectorExpiration2Params) (extensions []Extension2, err error) {
for _, ext := range params.Extensions {
scount, err := ext.Sectors.Count()
if err != nil {
return nil, err
}
sectors, err := ext.Sectors.All(scount)
if err != nil {
return nil, err
}
sc, err := json.Marshal(ext.SectorsWithClaims)
if err != nil {
return nil, err
}
extensions = append(extensions, Extension2{
Deadline: ext.Deadline,
Partition: ext.Partition,
Sectors: ArrayToString(sectors),
SectorsWithClaims: sc,
NewExpiration: ext.NewExpiration,
})
}
return
}
type CID struct {
cid.Cid
}
// Scan value into string, implements sql.Scanner interface
func (t *CID) Scan(value interface{}) error {
v, ok := value.(string)
if !ok {
return fmt.Errorf("failed to scan value into CID")
}
result, err := cid.Decode(v)
if err != nil {
return fmt.Errorf("failed to decode CID: %w", err)
}
t.Cid = result
return nil
}
// Value return json value, implement driver.Valuer interface
func (t CID) Value() (driver.Value, error) {
return t.String(), nil
}
type Address struct {
address.Address
}
// Scan value into string, implements sql.Scanner interface
func (t *Address) Scan(value interface{}) error {
v, ok := value.(string)
if !ok {
return fmt.Errorf("failed to scan value into Address")
}
result, err := address.NewFromString(v)
if err != nil {
return fmt.Errorf("failed to decode Address: %w", err)
}
t.Address = result
return nil
}
// Value return json value, implement driver.Valuer interface
func (t Address) Value() (driver.Value, error) {
return t.String(), nil
}