Skip to content

Commit babf112

Browse files
committed
fix(migrations): also fix balance_start
1 parent 21db50c commit babf112

File tree

1 file changed

+90
-32
lines changed

1 file changed

+90
-32
lines changed

backend/pkg/commons/db/migrations/clickhouse/20250523100157_fix_epoch_start_in_aggregation.sql

Lines changed: 90 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,113 @@ set
77
param_epoch_duration = ${EPOCH_DURATION_IN_SECONDS?missing};
88
-- +goose StatementEnd
99
-- +goose StatementBegin
10+
-- generate table to be used to updated balance_start
11+
CREATE TABLE IF NOT EXISTS _tmp_fix_epoch_balance_start
12+
(
13+
epoch Int64,
14+
t DateTime,
15+
validator_index UInt64,
16+
balance_start Int64,
17+
) ENGINE = ReplacingMergeTree()
18+
ORDER BY (validator_index, t)
19+
SETTINGS index_granularity=128, non_replicated_deduplication_window = 2048, replicated_deduplication_window = 2048;
20+
-- +goose StatementEnd
21+
-- +goose StatementBegin
22+
CREATE DICTIONARY IF NOT EXISTS _dict_fix_epoch_balance_start (validator_index Int64, t DateTime, epoch Int64, balance_start Int64)
23+
PRIMARY KEY t, validator_index SOURCE(CLICKHOUSE(TABLE _tmp_fix_epoch_balance_start DB currentDatabase()))
24+
LAYOUT(complex_key_direct());
25+
-- +goose StatementEnd
26+
-- +goose StatementBegin
27+
INSERT INTO _tmp_fix_epoch_balance_start
28+
SELECT
29+
epoch,
30+
toStartOfHour({fork_epoch_ts:DateTime}) as t,
31+
validator_index,
32+
balance_start
33+
FROM _final_validator_dashboard_data_epoch
34+
WHERE epoch = GREATEST(
35+
0,
36+
CEIL(
37+
(
38+
toStartOfHour({fork_epoch_ts:DateTime})::DateTime - {genesis_epoch_ts:DateTime}
39+
) / {epoch_duration:Int64}
40+
)
41+
)
42+
UNION ALL
43+
SELECT
44+
epoch,
45+
toStartOfDay({fork_epoch_ts:DateTime}) as t,
46+
validator_index,
47+
balance_start
48+
FROM _final_validator_dashboard_data_epoch
49+
WHERE epoch = GREATEST(
50+
0,
51+
CEIL(
52+
(
53+
toStartOfDay({fork_epoch_ts:DateTime})::DateTime - {genesis_epoch_ts:DateTime}
54+
) / {epoch_duration:Int64}
55+
)
56+
)
57+
UNION ALL
58+
SELECT
59+
epoch,
60+
toMonday({fork_epoch_ts:DateTime}) as t,
61+
validator_index,
62+
balance_start
63+
FROM _final_validator_dashboard_data_epoch
64+
WHERE epoch = GREATEST(
65+
0,
66+
CEIL(
67+
(
68+
toMonday({fork_epoch_ts:DateTime})::DateTime - {genesis_epoch_ts:DateTime}
69+
) / {epoch_duration:Int64}
70+
)
71+
)
72+
UNION ALL
73+
SELECT
74+
epoch,
75+
toStartOfMonth({fork_epoch_ts:DateTime}) as t,
76+
validator_index,
77+
balance_start
78+
FROM _final_validator_dashboard_data_epoch
79+
WHERE epoch = GREATEST(
80+
0,
81+
CEIL(
82+
(
83+
toStartOfMonth({fork_epoch_ts:DateTime})::DateTime - {genesis_epoch_ts:DateTime}
84+
) / {epoch_duration:Int64}
85+
)
86+
)
87+
-- +goose StatementEnd
88+
-- +goose StatementBegin
89+
SYSTEM SYNC REPLICA _tmp_fix_epoch_balance_start LIGHTWEIGHT;
90+
-- +goose StatementEnd
91+
-- +goose StatementBegin
1092
ALTER TABLE _final_validator_dashboard_data_hourly
1193
UPDATE
12-
epoch_start = GREATEST(
13-
0,
14-
CEIL(
15-
(
16-
toStartOfHour({fork_epoch_ts:DateTime})::DateTime - {genesis_epoch_ts:DateTime}
17-
) / {epoch_duration:Int64}
18-
)
19-
)
94+
epoch_start = dictGet(_dict_fix_epoch_balance_start, 'epoch', (t, validator_index)),
95+
balance_start = initializeAggregation('argMinState', dictGet(_dict_fix_epoch_balance_start, 'balance_start', (t, validator_index)), dictGet(_dict_fix_epoch_balance_start, 'epoch', (t, validator_index)))
2096
WHERE t = toStartOfHour({fork_epoch_ts:DateTime})
2197
-- +goose StatementEnd
2298
-- +goose StatementBegin
2399
ALTER TABLE _final_validator_dashboard_data_daily
24100
UPDATE
25-
epoch_start = GREATEST(
26-
0,
27-
CEIL(
28-
(
29-
toStartOfDay({fork_epoch_ts:DateTime})::DateTime - {genesis_epoch_ts:DateTime}
30-
) / {epoch_duration:Int64}
31-
)
32-
)
101+
epoch_start = dictGet(_dict_fix_epoch_balance_start, 'epoch', (t, validator_index)),
102+
balance_start = initializeAggregation('argMinState', dictGet(_dict_fix_epoch_balance_start, 'balance_start', (t, validator_index)), dictGet(_dict_fix_epoch_balance_start, 'epoch', (t, validator_index)))
33103
WHERE t = toStartOfDay({fork_epoch_ts:DateTime})
34104
-- +goose StatementEnd
35105
-- +goose StatementBegin
36106
ALTER TABLE _final_validator_dashboard_data_weekly
37107
UPDATE
38-
epoch_start = GREATEST(
39-
0,
40-
CEIL(
41-
(
42-
toMonday({fork_epoch_ts:DateTime})::DateTime - {genesis_epoch_ts:DateTime}
43-
) / {epoch_duration:Int64}
44-
)
45-
)
108+
epoch_start = dictGet(_dict_fix_epoch_balance_start, 'epoch', (t, validator_index)),
109+
balance_start = initializeAggregation('argMinState', dictGet(_dict_fix_epoch_balance_start, 'balance_start', (t, validator_index)), dictGet(_dict_fix_epoch_balance_start, 'epoch', (t, validator_index)))
46110
WHERE t = toMonday({fork_epoch_ts:DateTime})
47111
-- +goose StatementEnd
48112
-- +goose StatementBegin
49113
ALTER TABLE _final_validator_dashboard_data_monthly
50114
UPDATE
51-
epoch_start = GREATEST(
52-
0,
53-
CEIL(
54-
(
55-
toStartOfMonth({fork_epoch_ts:DateTime})::DateTime - {genesis_epoch_ts:DateTime}
56-
) / {epoch_duration:Int64}
57-
)
58-
)
115+
epoch_start = dictGet(_dict_fix_epoch_balance_start, 'epoch', (t, validator_index)),
116+
balance_start = initializeAggregation('argMinState', dictGet(_dict_fix_epoch_balance_start, 'balance_start', (t, validator_index)), dictGet(_dict_fix_epoch_balance_start, 'epoch', (t, validator_index)))
59117
WHERE t = toStartOfMonth({fork_epoch_ts:DateTime})
60118
-- +goose StatementEnd
61119
-- +goose ENVSUB OFF

0 commit comments

Comments
 (0)