-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathshared_sql.go
More file actions
219 lines (191 loc) · 7.78 KB
/
Copy pathshared_sql.go
File metadata and controls
219 lines (191 loc) · 7.78 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package schema
import (
"bytes"
"fmt"
)
const (
// We use `-1` with semantic variable names to indicate non-existence or non-validity
// in various contexts to avoid the usage of nullability in columns and for performance
// optimization purposes.
DefaultBigInt = -1
// Common SQL selectors
AllColsSelector = "*"
AnyValueSelector = "1"
// Common column names
AddressCol = "address"
BalanceCol = "balance"
PublicKeyCol = "public_key"
NameCol = "name"
StakedTokensCol = "staked_tokens"
ServiceURLCol = "service_url"
OutputAddressCol = "output_address"
UnstakingHeightCol = "unstaking_height"
PausedHeightCol = "paused_height"
ChainIDCol = "chain_id"
MaxRelaysCol = "max_relays"
HeightCol = "height"
)
func ProtocolActorTableSchema(actorSpecificColName, constraintName string) string {
return fmt.Sprintf(`(
%s TEXT NOT NULL,
%s TEXT NOT NULL,
%s TEXT NOT NULL,
%s TEXT NOT NULL,
%s TEXT NOT NULL,
%s BIGINT NOT NULL default %d,
%s BIGINT NOT NULL default %d,
%s BIGINT NOT NULL default %d,
CONSTRAINT %s UNIQUE (%s, %s)
)`,
AddressCol,
PublicKeyCol,
StakedTokensCol,
actorSpecificColName,
OutputAddressCol,
PausedHeightCol,
DefaultBigInt,
UnstakingHeightCol,
DefaultBigInt,
HeightCol,
DefaultBigInt,
constraintName,
AddressCol,
HeightCol)
}
func ProtocolActorChainsTableSchema(constraintName string) string {
return fmt.Sprintf(`(
%s TEXT NOT NULL,
%s CHAR(4) NOT NULL,
%s BIGINT NOT NULL default %d,
CONSTRAINT %s UNIQUE (%s, %s, %s)
)`, AddressCol, ChainIDCol, HeightCol, DefaultBigInt, constraintName, AddressCol, ChainIDCol, HeightCol)
}
func SelectAtHeight(selector string, height int64, tableName string) string {
return fmt.Sprintf(`SELECT %s FROM %s WHERE height=%d`,
selector, tableName, height)
}
func Select(selector, address string, height int64, tableName string) string {
return fmt.Sprintf(`SELECT %s FROM %s WHERE address='%s' AND height<=%d ORDER BY height DESC LIMIT 1`,
selector, tableName, address, height)
}
func SelectChains(selector, address string, height int64, actorTableName, chainsTableName string) string {
return fmt.Sprintf(`SELECT %s FROM %s WHERE address='%s' AND height=(%s);`,
selector, chainsTableName, address, Select(HeightCol, address, height, actorTableName))
}
func Exists(address string, height int64, tableName string) string {
return fmt.Sprintf(`SELECT EXISTS(%s)`, Select(AnyValueSelector, address, height, tableName))
}
// Explainer:
// (SELECT MAX(height), address FROM %s GROUP BY address) ->
// returns latest/max height for each address
// (height, address) IN (SELECT MAX(height), address FROM %s GROUP BY address) ->
// ensures the query is acting on max height for the addresses
func ReadyToUnstake(unstakingHeight int64, tableName string) string {
return fmt.Sprintf(`
SELECT address, staked_tokens, output_address
FROM %s WHERE unstaking_height=%d
AND (height, address) IN (SELECT MAX(height), address FROM %s GROUP BY address)`,
tableName, unstakingHeight, tableName)
}
func Insert(
actor BaseActor,
actorSpecificParam, actorSpecificParamValue,
constraintName, chainsConstraintName,
tableName, chainsTableName string,
height int64) string {
insertStatement := fmt.Sprintf(
`INSERT INTO %s (address, public_key, staked_tokens, %s, output_address, paused_height, unstaking_height, height)
VALUES('%s', '%s', '%s', '%s', '%s', %d, %d, %d)
ON CONFLICT ON CONSTRAINT %s
DO UPDATE SET staked_tokens=EXCLUDED.staked_tokens, %s=EXCLUDED.%s,
paused_height=EXCLUDED.paused_height, unstaking_height=EXCLUDED.unstaking_height,
height=EXCLUDED.height`,
tableName, actorSpecificParam,
actor.Address, actor.PublicKey, actor.StakedTokens, actorSpecificParamValue,
actor.OutputAddress, actor.PausedHeight, actor.UnstakingHeight, height,
constraintName,
actorSpecificParam, actorSpecificParam)
if actor.Chains == nil {
return insertStatement
}
return fmt.Sprintf("WITH baseTableInsert AS (%s)\n%s",
insertStatement, InsertChains(actor.Address, actor.Chains, height, chainsTableName, chainsConstraintName))
}
func InsertChains(address string, chains []string, height int64, tableName, constraintName string) string {
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("INSERT INTO %s (address, chain_id, height) VALUES", tableName))
maxIndex := len(chains) - 1
for i, chain := range chains {
buffer.WriteString(fmt.Sprintf("\n('%s', '%s', %d)", address, chain, height))
if i < maxIndex {
buffer.WriteString(",")
}
}
buffer.WriteString(fmt.Sprintf("\nON CONFLICT ON CONSTRAINT %s DO NOTHING", constraintName))
return buffer.String()
}
func Update(address, stakedTokens, actorSpecificParam, actorSpecificParamValue string, height int64, tableName, constraintName string) string {
return fmt.Sprintf(
`INSERT INTO %s(address, public_key, staked_tokens, %s, output_address, paused_height, unstaking_height, height)
(
SELECT address, public_key, '%s', '%s', output_address, paused_height, unstaking_height, %d
FROM %s WHERE address='%s' AND height<=%d ORDER BY height DESC LIMIT 1
)
ON CONFLICT ON CONSTRAINT %s
DO UPDATE SET staked_tokens=EXCLUDED.staked_tokens, %s=EXCLUDED.%s, height=EXCLUDED.height`,
tableName, actorSpecificParam,
stakedTokens, actorSpecificParamValue, height,
tableName, address, height,
constraintName,
actorSpecificParam, actorSpecificParam)
}
func UpdateUnstakingHeight(address, actorSpecificParam string, unstakingHeight, height int64, tableName, constraintName string) string {
return fmt.Sprintf(`
INSERT INTO %s(address, public_key, staked_tokens, %s, output_address, paused_height, unstaking_height, height)
(
SELECT address, public_key, staked_tokens, %s, output_address, paused_height, %d, %d
FROM %s WHERE address='%s' AND height<=%d ORDER BY height DESC LIMIT 1
)
ON CONFLICT ON CONSTRAINT %s
DO UPDATE SET unstaking_height=EXCLUDED.unstaking_height, height=EXCLUDED.height`,
tableName, actorSpecificParam,
actorSpecificParam, unstakingHeight, height,
tableName, address, height,
constraintName)
}
func UpdatePausedHeight(address, actorSpecificParam string, pausedHeight, height int64, tableName, constraintName string) string {
return fmt.Sprintf(`
INSERT INTO %s(address, public_key, staked_tokens, %s, output_address, paused_height, unstaking_height, height)
(
SELECT address, public_key, staked_tokens, %s, output_address, %d, unstaking_height, %d
FROM %s WHERE address='%s' AND height<=%d ORDER BY height DESC LIMIT 1
)
ON CONFLICT ON CONSTRAINT %s
DO UPDATE SET paused_height=EXCLUDED.paused_height, height=EXCLUDED.height`,
tableName, actorSpecificParam, actorSpecificParam,
pausedHeight, height,
tableName, address, height, constraintName)
}
func UpdateUnstakedHeightIfPausedBefore(actorSpecificParam string, unstakingHeight, pausedBeforeHeight, height int64, tableName, constraintName string) string {
return fmt.Sprintf(`
INSERT INTO %s (address, public_key, staked_tokens, %s, output_address, paused_height, unstaking_height, height)
(
SELECT address, public_key, staked_tokens, %s, output_address, paused_height, %d, %d
FROM %s WHERE paused_height<%d
AND (height,address) IN (SELECT MAX(height),address from %s GROUP BY address)
)
ON CONFLICT ON CONSTRAINT %s
DO UPDATE SET unstaking_height=EXCLUDED.unstaking_height`,
tableName, actorSpecificParam,
actorSpecificParam, unstakingHeight, height,
tableName, pausedBeforeHeight,
tableName,
constraintName)
}
func NullifyChains(address string, height int64, tableName string) string {
return fmt.Sprintf("DELETE FROM %s WHERE address='%s' AND height=%d", tableName, address, height)
}
// Exposed for debugging purposes only
func ClearAll(tableName string) string {
return fmt.Sprintf(`DELETE FROM %s`, tableName)
}