Skip to content

Commit 0a85358

Browse files
authored
Feature/enhancements and fixes (#58)
- Removed the `Relay` entity and its related references across claim processing functions for simplicity and optimized structure. - Stopped saving some unnecessary fields (like proof, root_hash, etc) to save processing time. - Added `numRelays`, `numClaimedComputedUnits`, `numEstimatedComputedUnits` and `claimedAmount` to `MsgCreateClaim` - Improved proof validation status handling with fallback for better event processing - Replaced generation of reports using `fetchPaginatedRecords` with processing manually with functions that generate the reports directly in the db - Updated db functions of delegator rewards - Improved update of balances to do it in bulk of 10 instead of one by one - Added the followings db functions: - get_rewards_by_date db function - get_rewards_by_addresses_and_time_group_by_service - get_rewards_by_addresses_and_time_group_by_address_and_date
1 parent df4af11 commit 0a85358

28 files changed

+1382
-896
lines changed

proto/pocket/proof/event.proto

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ message EventClaimUpdated {
2727

2828
message EventProofSubmitted {
2929
pocket.proof.Claim claim = 1 [(gogoproto.jsontag) = "claim"];
30-
pocket.proof.Proof proof = 2 [(gogoproto.jsontag) = "proof"];
3130
uint64 num_relays = 3 [(gogoproto.jsontag) = "num_relays"];
3231
uint64 num_claimed_compute_units = 4 [(gogoproto.jsontag) = "num_claimed_compute_units"];
3332
uint64 num_estimated_compute_units = 5 [(gogoproto.jsontag) = "num_estimated_compute_units"];
@@ -37,7 +36,6 @@ message EventProofSubmitted {
3736
// TODO_TEST: Add coverage for proof updates.
3837
message EventProofUpdated {
3938
pocket.proof.Claim claim = 1 [(gogoproto.jsontag) = "claim"];
40-
pocket.proof.Proof proof = 2 [(gogoproto.jsontag) = "proof"];
4139
uint64 num_relays = 3 [(gogoproto.jsontag) = "num_relays"];
4240
uint64 num_claimed_compute_units = 4 [(gogoproto.jsontag) = "num_claimed_compute_units"];
4341
uint64 num_estimated_compute_units = 5 [(gogoproto.jsontag) = "num_estimated_compute_units"];
@@ -47,9 +45,15 @@ message EventProofUpdated {
4745
// Event emitted after a proof has been checked for validity in the proof module's
4846
// EndBlocker.
4947
message EventProofValidityChecked {
50-
pocket.proof.Proof proof = 1 [(gogoproto.jsontag) = "proof"];
48+
// Next index: 6
49+
50+
// The following fields were removed to minimize block size.
51+
// See https://github.com/pokt-network/poktroll/issues/1497.
52+
reserved 1; // pocket.proof.Proof proof = 1 [(gogoproto.jsontag) = "proof"];
53+
reserved 3; // pocket.proof.ClaimProofStatus proof_status = 3 [(gogoproto.jsontag) = "proof_status"];
54+
55+
pocket.proof.Claim claim = 5 [(gogoproto.jsontag) = "claim"];
5156
uint64 block_height = 2 [(gogoproto.jsontag) = "block_height"];
52-
pocket.proof.ClaimProofStatus proof_status = 3 [(gogoproto.jsontag) = "proof_status"];
5357
// reason is the string representation of the error that led to the proof being
5458
// marked as invalid (e.g. "invalid closest merkle proof", "invalid relay request signature")
5559
string failure_reason = 4 [(gogoproto.jsontag) = "failure_reason"];

proto/pocket/proof/types.proto

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ message Claim {
3131
pocket.session.SessionHeader session_header = 2;
3232

3333
// Root hash from smt.SMST#Root().
34+
// TODO_UP_NEXT(@bryanchriswhite, #1497): Dehydrate the claim's root hash from onchain events.
3435
bytes root_hash = 3;
3536

3637
// Important: This field MUST only be set by proofKeeper#EnsureValidProofSignaturesAndClosestPath
@@ -71,4 +72,4 @@ message SessionSMT {
7172
pocket.session.SessionHeader session_header = 1;
7273
string supplier_operator_address = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"];
7374
bytes smt_root = 3;
74-
}
75+
}

proto/pocket/session/types.proto

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ message SessionHeader {
2323
string application_address = 1 [(cosmos_proto.scalar) = "cosmos.AddressString"]; // The Bech32 address of the application.
2424
string service_id = 2; // The service id this session is for
2525
// NOTE: session_id can be derived from the above values using onchain but is included in the header for convenience
26-
string session_id = 3; // A unique pseudoranom ID for this session
26+
string session_id = 3; // A unique pseudorandom ID for this session
2727
int64 session_start_block_height = 4; // The height at which this session started
2828
// Note that`session_end_block_height` is a derivative of (`start` + `num_blocks_per_session`)
29-
// as goverened by onchain params at the time of the session start.
30-
// It is stored as an additional field to simplofy business logic in case
29+
// as governed by onchain params at the time of the session start.
30+
// It is stored as an additional field to simplify business logic in case
3131
// the number of blocks_per_session changes during the session.
3232
int64 session_end_block_height = 5; // The height at which this session ended, this is the last block of the session
3333
}
3434

3535
// Session is a fully hydrated session object that contains all the information for the Session
36-
// and its parcipants.
36+
// and its participants.
3737
message Session {
3838
SessionHeader header = 1; // The header of the session containing lightweight data
39-
string session_id = 2; // A unique pseudoranom ID for this session
39+
string session_id = 2; // A unique pseudorandom ID for this session
4040
int64 session_number = 3; // The session number since genesis
4141
int64 num_blocks_per_session = 4; // The number of blocks per session when this session started
4242
pocket.application.Application application = 5; // A fully hydrated application object this session is for

schema.graphql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,10 +348,17 @@ type Transaction @entity {
348348
isMultisig: Boolean!
349349
# could be null is isMulti = false
350350
multisig: Multisig
351+
amountOfMessages: [AmountOfMessages]
352+
amountSentByDenom: [Coin]
351353
messages: [Message] @derivedFrom(field: "transaction")
352354
events: [Event]@derivedFrom(field: "transaction")
353355
}
354356

357+
type AmountOfMessages @jsonField {
358+
type: String!
359+
amount: Int!
360+
}
361+
355362
type Message @entity {
356363
id: ID!
357364
idx: Int!
@@ -926,6 +933,11 @@ type MsgCreateClaim @entity {
926933
sessionEndHeight: BigInt!
927934
rootHash: String
928935
transaction: Transaction!
936+
numRelays: BigInt
937+
numClaimedComputedUnits: BigInt
938+
numEstimatedComputedUnits: BigInt
939+
claimedAmount: BigInt
940+
claimedDenom: String
929941
block: Block!
930942
}
931943

src/mappings/authz/exec.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@ import { AuthzMsgExecProps } from "../../types/models/AuthzMsgExec";
66
import { MessageProps } from "../../types/models/Message";
77
import { MorseClaimableAccountProps } from "../../types/models/MorseClaimableAccount";
88
import { MsgImportMorseClaimableAccountsProps } from "../../types/models/MsgImportMorseClaimableAccounts";
9+
import { MsgRecoverMorseAccountProps } from "../../types/models/MsgRecoverMorseAccount";
910
import { ParamProps } from "../../types/models/Param";
10-
import { handleMsgImportMorseClaimableAccounts } from "../pocket/migration";
11+
import {
12+
handleMsgImportMorseClaimableAccounts,
13+
handleMsgRecoverMorseAccount,
14+
updateMorseClaimableAccounts,
15+
} from "../pocket/migration";
1116
import { _handleUpdateParam } from "../pocket/params";
1217
import { AuthzExecMsg } from "../types";
1318
import {
@@ -27,6 +32,7 @@ type HandleAuthzExecResult = {
2732
params: Array<ParamProps>;
2833
msgImportMorseClaimableAccounts: Array<MsgImportMorseClaimableAccountsProps>;
2934
morseClaimableAccount: Array<MorseClaimableAccountProps>;
35+
msgRecoverMorseAccount: Array<MsgRecoverMorseAccountProps>;
3036
}
3137

3238
function _handleAuthzExec(msg: CosmosMessage<AuthzExecMsg>): HandleAuthzExecResult {
@@ -39,6 +45,7 @@ function _handleAuthzExec(msg: CosmosMessage<AuthzExecMsg>): HandleAuthzExecResu
3945
params: [],
4046
msgImportMorseClaimableAccounts: [],
4147
morseClaimableAccount: [],
48+
msgRecoverMorseAccount: [],
4249
};
4350

4451
const authzExecId = messageId(msg);
@@ -79,6 +86,16 @@ function _handleAuthzExec(msg: CosmosMessage<AuthzExecMsg>): HandleAuthzExecResu
7986
decodedMsg = response.decodedMsg;
8087
result.morseClaimableAccount.push(...response.morseClaimableAccounts);
8188
result.msgImportMorseClaimableAccounts.push(response.msgImportMorseClaimableAccounts);
89+
} else if (encodedMsg.typeUrl === "/pocket.migration.MsgRecoverMorseAccount") {
90+
const response = handleMsgRecoverMorseAccount({
91+
encodedMsg,
92+
blockId,
93+
tx: msg.tx,
94+
messageId: authzExecId,
95+
})
96+
97+
decodedMsg = response.decodedMsg;
98+
result.msgRecoverMorseAccount.push(response.msgRecoverMorseAccount);
8299
} else {
83100
// _handleUpdateParam will return the decoded message if it is a param update,
84101
// otherwise it will return undefined.
@@ -135,6 +152,7 @@ export async function handleAuthzExec(messages: CosmosMessage<AuthzExecMsg>[]):
135152
params: [],
136153
msgImportMorseClaimableAccounts: [],
137154
morseClaimableAccount: [],
155+
msgRecoverMorseAccount: [],
138156
};
139157

140158
// merge all the documents to write
@@ -158,14 +176,33 @@ export async function handleAuthzExec(messages: CosmosMessage<AuthzExecMsg>[]):
158176
if (r.morseClaimableAccount.length > 0) {
159177
allResults.morseClaimableAccount.push(...r.morseClaimableAccount);
160178
}
179+
if (r.msgRecoverMorseAccount.length > 0) {
180+
allResults.msgRecoverMorseAccount.push(...r.msgRecoverMorseAccount);
181+
}
161182
}
162183

163-
await Promise.all([
184+
const promises = [
164185
store.bulkCreate("Message", allResults.messages),
165186
store.bulkCreate("AuthzExec", allResults.authzExec),
166187
store.bulkCreate("AuthzMsgExec", allResults.authzExecMsgs),
167188
store.bulkCreate("Param", allResults.params),
168189
store.bulkCreate("MsgImportMorseClaimableAccounts", allResults.msgImportMorseClaimableAccounts),
169190
store.bulkCreate("MorseClaimableAccount", allResults.morseClaimableAccount),
170-
]);
191+
store.bulkCreate("MsgRecoverMorseAccount", allResults.msgRecoverMorseAccount),
192+
];
193+
194+
if (allResults.msgRecoverMorseAccount.length) {
195+
promises.push(
196+
updateMorseClaimableAccounts(
197+
allResults.msgRecoverMorseAccount.map(msg => ({
198+
morseAddress: msg.morseSrcAddress,
199+
destinationAddress: msg.shannonDestAddressId,
200+
claimedMsgId: msg.id,
201+
transactionHash: msg.transactionId,
202+
}))
203+
)
204+
)
205+
}
206+
207+
await Promise.all(promises);
171208
}

src/mappings/dbFunctions/dataByDelegatorAddressesAndBlocks.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ LANGUAGE sql
1010
STABLE
1111
AS $$
1212
WITH matched_suppliers AS (
13-
SELECT DISTINCT ON (elem->>'address')
14-
ssc.supplier_id,
13+
SELECT
14+
distinct ssc.supplier_id,
1515
elem->>'address' AS address
1616
FROM ${dbSchema}.supplier_service_configs ssc
1717
INNER JOIN ${dbSchema}.suppliers s ON s.id = ssc.supplier_id
@@ -26,11 +26,11 @@ AS $$
2626
SELECT
2727
mcc.supplier_id,
2828
COUNT(DISTINCT mcc.id) AS claim_count,
29-
SUM(r.num_relays) AS claim_relays,
30-
SUM(r.num_claimed_computed_units) AS claim_computed_units,
31-
SUM(r.claimed_amount) AS claim_upokt
29+
SUM(mcc.num_relays) AS claim_relays,
30+
SUM(mcc.num_claimed_computed_units) AS claim_computed_units,
31+
SUM(mcc.claimed_amount) AS claim_upokt
3232
FROM ${dbSchema}.msg_create_claims mcc
33-
INNER JOIN ${dbSchema}.relays r ON r.msg_create_claim_id = mcc.id
33+
INNER JOIN matched_suppliers ms ON ms.supplier_id = mcc.supplier_id
3434
WHERE mcc.block_id BETWEEN start_height AND end_height
3535
GROUP BY mcc.supplier_id
3636
),
@@ -43,6 +43,7 @@ AS $$
4343
SUM(ecs.num_claimed_computed_units) AS proof_computed_units,
4444
SUM(ecs.claimed_amount) AS proof_upokt
4545
FROM ${dbSchema}.event_claim_settleds ecs
46+
INNER JOIN matched_suppliers ms ON ms.supplier_id = ecs.supplier_id
4647
WHERE ecs.block_id BETWEEN start_height AND end_height
4748
GROUP BY ecs.supplier_id
4849
),
@@ -52,28 +53,41 @@ AS $$
5253
ess.supplier_id,
5354
SUM(ess.previous_stake_amount - ess.after_stake_amount) AS slashed
5455
FROM ${dbSchema}.event_supplier_slasheds ess
56+
INNER JOIN matched_suppliers ms ON ms.supplier_id = ess.supplier_id
5557
WHERE ess.block_id BETWEEN start_height AND end_height
5658
GROUP BY ess.supplier_id
5759
)
5860
5961
SELECT jsonb_agg(
6062
jsonb_build_object(
61-
'address', ms.address,
62-
'proof_relays', COALESCE(p.proof_relays, 0),
63-
'proof_computed_units', COALESCE(p.proof_computed_units, 0),
64-
'proof_upokt', COALESCE(p.proof_upokt, 0),
65-
'proof_amount', COALESCE(p.proof_count, 0),
66-
'claim_relays', COALESCE(c.claim_relays, 0),
67-
'claim_computed_units', COALESCE(c.claim_computed_units, 0),
68-
'claim_upokt', COALESCE(c.claim_upokt, 0),
69-
'claim_amount', COALESCE(c.claim_count, 0),
70-
'slashed', COALESCE(s.slashed, 0)
63+
'address', address,
64+
'proof_relays', COALESCE(proof_relays, 0),
65+
'proof_computed_units', COALESCE(proof_computed_units, 0),
66+
'proof_upokt', COALESCE(proof_upokt, 0),
67+
'proof_amount', COALESCE(proof_count, 0),
68+
'claim_relays', COALESCE(claim_relays, 0),
69+
'claim_computed_units', COALESCE(claim_computed_units, 0),
70+
'claim_upokt', COALESCE(claim_upokt, 0),
71+
'claim_amount', COALESCE(claim_count, 0),
72+
'slashed', COALESCE(slashed, 0)
7173
)
72-
)
74+
) FROM (
75+
SELECT
76+
ms.address,
77+
SUM(p.proof_relays) proof_relays,
78+
SUM(p.proof_computed_units) proof_computed_units,
79+
SUM(p.proof_upokt) proof_upokt,
80+
SUM(p.proof_count) proof_count,
81+
SUM(c.claim_relays) claim_relays,
82+
SUM(c.claim_computed_units) claim_computed_units,
83+
SUM(c.claim_upokt) claim_upokt,
84+
SUM(c.claim_count) claim_count,
85+
SUM(s.slashed) slashed
7386
FROM matched_suppliers ms
7487
LEFT JOIN claim_agg c ON c.supplier_id = ms.supplier_id
7588
LEFT JOIN proof_agg p ON p.supplier_id = ms.supplier_id
76-
LEFT JOIN slashed_agg s ON s.supplier_id = ms.supplier_id;
89+
LEFT JOIN slashed_agg s ON s.supplier_id = ms.supplier_id
90+
GROUP BY ms.address)
7791
$$;
7892
`
7993
}

src/mappings/dbFunctions/dataByDelegatorAddressesAndTimes.ts

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ LANGUAGE sql
1010
STABLE
1111
AS $$
1212
WITH matched_suppliers AS (
13-
SELECT DISTINCT ON (elem->>'address')
14-
ssc.supplier_id,
13+
SELECT
14+
distinct ssc.supplier_id,
1515
elem->>'address' AS address
1616
FROM ${dbSchema}.supplier_service_configs ssc
1717
INNER JOIN ${dbSchema}.suppliers s ON s.id = ssc.supplier_id
@@ -26,12 +26,12 @@ AS $$
2626
SELECT
2727
mcc.supplier_id,
2828
COUNT(DISTINCT mcc.id) AS claim_count,
29-
SUM(r.num_relays) AS claim_relays,
30-
SUM(r.num_claimed_computed_units) AS claim_computed_units,
31-
SUM(r.claimed_amount) AS claim_upokt
29+
SUM(mcc.num_relays) AS claim_relays,
30+
SUM(mcc.num_claimed_computed_units) AS claim_computed_units,
31+
SUM(mcc.claimed_amount) AS claim_upokt
3232
FROM ${dbSchema}.msg_create_claims mcc
3333
INNER JOIN ${dbSchema}.blocks b ON b.id = mcc.block_id
34-
INNER JOIN ${dbSchema}.relays r ON r.msg_create_claim_id = mcc.id
34+
INNER JOIN matched_suppliers ms ON ms.supplier_id = mcc.supplier_id
3535
WHERE b.timestamp BETWEEN start_ts AND end_ts
3636
GROUP BY mcc.supplier_id
3737
),
@@ -45,6 +45,7 @@ AS $$
4545
SUM(ecs.claimed_amount) AS proof_upokt
4646
FROM ${dbSchema}.event_claim_settleds ecs
4747
INNER JOIN ${dbSchema}.blocks b ON b.id = ecs.block_id
48+
INNER JOIN matched_suppliers ms ON ms.supplier_id = ecs.supplier_id
4849
WHERE b.timestamp BETWEEN start_ts AND end_ts
4950
GROUP BY ecs.supplier_id
5051
),
@@ -55,28 +56,41 @@ AS $$
5556
SUM(ess.previous_stake_amount - ess.after_stake_amount) AS slashed
5657
FROM ${dbSchema}.event_supplier_slasheds ess
5758
INNER JOIN ${dbSchema}.blocks b ON b.id = ess.block_id
59+
INNER JOIN matched_suppliers ms ON ms.supplier_id = ess.supplier_id
5860
WHERE b.timestamp BETWEEN start_ts AND end_ts
5961
GROUP BY ess.supplier_id
6062
)
6163
6264
SELECT jsonb_agg(
6365
jsonb_build_object(
64-
'address', ms.address,
65-
'proof_relays', COALESCE(p.proof_relays, 0),
66-
'proof_computed_units', COALESCE(p.proof_computed_units, 0),
67-
'proof_upokt', COALESCE(p.proof_upokt, 0),
68-
'proof_amount', COALESCE(p.proof_count, 0),
69-
'claim_relays', COALESCE(c.claim_relays, 0),
70-
'claim_computed_units', COALESCE(c.claim_computed_units, 0),
71-
'claim_upokt', COALESCE(c.claim_upokt, 0),
72-
'claim_amount', COALESCE(c.claim_count, 0),
73-
'slashed', COALESCE(s.slashed, 0)
66+
'address', address,
67+
'proof_relays', COALESCE(proof_relays, 0),
68+
'proof_computed_units', COALESCE(proof_computed_units, 0),
69+
'proof_upokt', COALESCE(proof_upokt, 0),
70+
'proof_amount', COALESCE(proof_count, 0),
71+
'claim_relays', COALESCE(claim_relays, 0),
72+
'claim_computed_units', COALESCE(claim_computed_units, 0),
73+
'claim_upokt', COALESCE(claim_upokt, 0),
74+
'claim_amount', COALESCE(claim_count, 0),
75+
'slashed', COALESCE(slashed, 0)
7476
)
75-
)
77+
) FROM (
78+
SELECT
79+
ms.address,
80+
SUM(p.proof_relays) proof_relays,
81+
SUM(p.proof_computed_units) proof_computed_units,
82+
SUM(p.proof_upokt) proof_upokt,
83+
SUM(p.proof_count) proof_count,
84+
SUM(c.claim_relays) claim_relays,
85+
SUM(c.claim_computed_units) claim_computed_units,
86+
SUM(c.claim_upokt) claim_upokt,
87+
SUM(c.claim_count) claim_count,
88+
SUM(s.slashed) slashed
7689
FROM matched_suppliers ms
7790
LEFT JOIN claim_agg c ON c.supplier_id = ms.supplier_id
7891
LEFT JOIN proof_agg p ON p.supplier_id = ms.supplier_id
79-
LEFT JOIN slashed_agg s ON s.supplier_id = ms.supplier_id;
92+
LEFT JOIN slashed_agg s ON s.supplier_id = ms.supplier_id
93+
GROUP BY ms.address);
8094
$$;
8195
`
8296
}

0 commit comments

Comments
 (0)