-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
orm.go
196 lines (171 loc) · 6.77 KB
/
orm.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
185
186
187
188
189
190
191
192
193
194
195
196
package directrequestocr
import (
"fmt"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/smartcontractkit/sqlx"
"github.com/smartcontractkit/chainlink/core/logger"
"github.com/smartcontractkit/chainlink/core/services/pg"
)
//go:generate mockery --quiet --name ORM --output ./mocks/ --case=underscore
type ORM interface {
CreateRequest(requestID RequestID, receivedAt time.Time, requestTxHash *common.Hash, qopts ...pg.QOpt) error
SetResult(requestID RequestID, runID int64, computationResult []byte, readyAt time.Time, qopts ...pg.QOpt) error
SetError(requestID RequestID, runID int64, errorType ErrType, computationError []byte, readyAt time.Time, readyForProcessing bool, qopts ...pg.QOpt) error
SetFinalized(requestID RequestID, reportedResult []byte, reportedError []byte, qopts ...pg.QOpt) error
SetConfirmed(requestID RequestID, qopts ...pg.QOpt) error
TimeoutExpiredResults(cutoff time.Time, limit uint32, qopts ...pg.QOpt) ([]RequestID, error)
FindOldestEntriesByState(state RequestState, limit uint32, qopts ...pg.QOpt) ([]Request, error)
FindById(requestID RequestID, qopts ...pg.QOpt) (*Request, error)
}
type orm struct {
q pg.Q
contractAddress common.Address
}
var _ ORM = (*orm)(nil)
const requestFields = "request_id, run_id, received_at, request_tx_hash, " +
"state, result_ready_at, result, error_type, error, " +
"transmitted_result, transmitted_error"
func NewORM(db *sqlx.DB, lggr logger.Logger, cfg pg.QConfig, contractAddress common.Address) ORM {
return &orm{
q: pg.NewQ(db, lggr, cfg),
contractAddress: contractAddress,
}
}
func (o orm) CreateRequest(requestID RequestID, receivedAt time.Time, requestTxHash *common.Hash, qopts ...pg.QOpt) error {
stmt := `
INSERT INTO ocr2dr_requests (request_id, contract_address, received_at, request_tx_hash, state)
VALUES ($1,$2,$3,$4,$5);
`
return o.q.WithOpts(qopts...).ExecQ(stmt, requestID, o.contractAddress, receivedAt, requestTxHash, IN_PROGRESS)
}
func (o orm) setWithStateTransitionCheck(requestID RequestID, newState RequestState, setter func(pg.Queryer) error, qopts ...pg.QOpt) error {
err := o.q.WithOpts(qopts...).Transaction(func(tx pg.Queryer) error {
prevState := IN_PROGRESS // default initial state
stmt := `SELECT state FROM ocr2dr_requests WHERE request_id=$1 AND contract_address=$2;`
if err2 := tx.Get(&prevState, stmt, requestID, o.contractAddress); err2 != nil {
return err2
}
if err2 := CheckStateTransition(prevState, newState); err2 != nil {
return err2
}
return setter(tx)
})
return err
}
func (o orm) SetResult(requestID RequestID, runID int64, computationResult []byte, readyAt time.Time, qopts ...pg.QOpt) error {
newState := RESULT_READY
err := o.setWithStateTransitionCheck(requestID, newState, func(tx pg.Queryer) error {
stmt := `
UPDATE ocr2dr_requests
SET run_id=$3, result=$4, result_ready_at=$5, state=$6
WHERE request_id=$1 AND contract_address=$2;
`
_, err2 := tx.Exec(stmt, requestID, o.contractAddress, runID, computationResult, readyAt, newState)
return err2
}, qopts...)
return err
}
func (o orm) SetError(requestID RequestID, runID int64, errorType ErrType, computationError []byte, readyAt time.Time, readyForProcessing bool, qopts ...pg.QOpt) error {
var newState RequestState
if readyForProcessing {
newState = RESULT_READY
} else {
newState = IN_PROGRESS
}
err := o.setWithStateTransitionCheck(requestID, newState, func(tx pg.Queryer) error {
stmt := `
UPDATE ocr2dr_requests
SET run_id=$3, error=$4, error_type=$5, result_ready_at=$6, state=$7
WHERE request_id=$1 AND contract_address=$2;
`
_, err2 := tx.Exec(stmt, requestID, o.contractAddress, runID, computationError, errorType, readyAt, newState)
return err2
}, qopts...)
return err
}
func (o orm) SetFinalized(requestID RequestID, reportedResult []byte, reportedError []byte, qopts ...pg.QOpt) error {
newState := FINALIZED
err := o.setWithStateTransitionCheck(requestID, newState, func(tx pg.Queryer) error {
stmt := `
UPDATE ocr2dr_requests
SET transmitted_result=$3, transmitted_error=$4, state=$5
WHERE request_id=$1 AND contract_address=$2;
`
_, err2 := tx.Exec(stmt, requestID, o.contractAddress, reportedResult, reportedError, newState)
return err2
}, qopts...)
return err
}
func (o orm) SetConfirmed(requestID RequestID, qopts ...pg.QOpt) error {
newState := CONFIRMED
err := o.setWithStateTransitionCheck(requestID, newState, func(tx pg.Queryer) error {
stmt := `UPDATE ocr2dr_requests SET state=$3 WHERE request_id=$1 AND contract_address=$2;`
_, err2 := tx.Exec(stmt, requestID, o.contractAddress, newState)
return err2
}, qopts...)
return err
}
func (o orm) TimeoutExpiredResults(cutoff time.Time, limit uint32, qopts ...pg.QOpt) ([]RequestID, error) {
var ids []RequestID
allowedPrevStates := []RequestState{IN_PROGRESS, RESULT_READY, FINALIZED}
nextState := TIMED_OUT
for _, state := range allowedPrevStates {
// sanity checks
if err := CheckStateTransition(state, nextState); err != nil {
return ids, err
}
}
err := o.q.WithOpts(qopts...).Transaction(func(tx pg.Queryer) error {
selectStmt := `
SELECT request_id
FROM ocr2dr_requests
WHERE (state=$1 OR state=$2 OR state=$3) AND contract_address=$4 AND received_at < ($5)
ORDER BY received_at
LIMIT $6;`
if err2 := tx.Select(&ids, selectStmt, allowedPrevStates[0], allowedPrevStates[1], allowedPrevStates[2], o.contractAddress, cutoff, limit); err2 != nil {
return err2
}
if len(ids) == 0 {
return nil
}
a := map[string]any{
"nextState": nextState,
"contractAddr": o.contractAddress,
"ids": ids,
}
updateStmt, args, err2 := sqlx.Named(`
UPDATE ocr2dr_requests
SET state = :nextState
WHERE contract_address = :contractAddr AND request_id IN (:ids);`, a)
if err2 != nil {
return err2
}
updateStmt, args, err2 = sqlx.In(updateStmt, args...)
if err2 != nil {
return err2
}
updateStmt = tx.Rebind(updateStmt)
if _, err2 := tx.Exec(updateStmt, args...); err2 != nil {
return err2
}
return nil
})
return ids, err
}
func (o orm) FindOldestEntriesByState(state RequestState, limit uint32, qopts ...pg.QOpt) ([]Request, error) {
var requests []Request
stmt := fmt.Sprintf(`SELECT %s FROM ocr2dr_requests WHERE state=$1 AND contract_address=$2 ORDER BY received_at LIMIT $3;`, requestFields)
if err := o.q.WithOpts(qopts...).Select(&requests, stmt, state, o.contractAddress, limit); err != nil {
return nil, err
}
return requests, nil
}
func (o orm) FindById(requestID RequestID, qopts ...pg.QOpt) (*Request, error) {
var request Request
stmt := fmt.Sprintf(`SELECT %s FROM ocr2dr_requests WHERE request_id=$1 AND contract_address=$2;`, requestFields)
if err := o.q.WithOpts(qopts...).Get(&request, stmt, requestID, o.contractAddress); err != nil {
return nil, err
}
return &request, nil
}