Skip to content

Commit d326da0

Browse files
remagpiemajecty
authored andcommitted
Create a new CCCChange for ReportDoubleVote
1 parent 734162b commit d326da0

File tree

2 files changed

+109
-17
lines changed

2 files changed

+109
-17
lines changed

src/models/logic/cccChange.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,3 +288,24 @@ export async function getByBlockNumber(
288288
throw Exception.DBError();
289289
}
290290
}
291+
292+
export async function getNominations(
293+
address: string,
294+
blockNumber: number,
295+
transaction?: Transaction
296+
): Promise<CCCChangeInstance[]> {
297+
try {
298+
return await models.CCCChange.findAll({
299+
attributes: ["change"],
300+
where: {
301+
address,
302+
blockNumber,
303+
reason: "deposit"
304+
},
305+
transaction
306+
});
307+
} catch (err) {
308+
console.error(err);
309+
throw Exception.DBError();
310+
}
311+
}

src/worker/staticFeeDistribution.ts

Lines changed: 88 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { SignedTransaction, UnwrapCCC } from "codechain-sdk/lib/core/classes";
44
import { Custom } from "codechain-sdk/lib/core/transaction/Custom";
55
import {
66
actionFromCustom as stakeActionFromCustom,
7+
getCandidates,
78
getCCSHolders,
89
getUndelegatedCCS
910
} from "codechain-stakeholder-sdk";
@@ -179,6 +180,41 @@ export async function payFee(
179180
return Promise.all(queries);
180181
}
181182

183+
async function getDeposit(
184+
sdk: SDK,
185+
blockNumber: number,
186+
pubkey: H512,
187+
transaction: Transaction
188+
): Promise<U64> {
189+
// Get the deposit at the previous block
190+
const candidates = await getCandidates(sdk, blockNumber - 1);
191+
const target = candidates.find(c => c.pubkey.isEqualTo(pubkey));
192+
let deposit = new U64(0);
193+
if (target != null) {
194+
deposit = target.deposit;
195+
}
196+
197+
// Get the accumulated self nomination transactions
198+
const address = PlatformAddress.fromPublic(pubkey, {
199+
networkId: sdk.networkId
200+
});
201+
const nominations = await CCCChangeModel.getNominations(
202+
address.toString(),
203+
blockNumber,
204+
transaction
205+
);
206+
for (const n of nominations) {
207+
const change: string = n.get("change");
208+
if (!change.startsWith("-")) {
209+
throw new Error(
210+
'CCCChange with reason "deposit" must have negative change'
211+
);
212+
}
213+
deposit = deposit.plus(change.slice(1));
214+
}
215+
return deposit;
216+
}
217+
182218
export async function trackBalanceChangeByTx(
183219
sdk: SDK,
184220
blockNumber: number,
@@ -280,23 +316,58 @@ export async function trackBalanceChangeByTx(
280316
sdk,
281317
tx.unsigned as Custom
282318
);
283-
if (stakeAction && stakeAction.type === "selfNominate") {
284-
const signer = tx.getSignerAddress({
285-
networkId: sdk.networkId
286-
});
287-
const deposit = stakeAction.deposit;
288-
queries.push(
289-
CCCChangeModel.stakeDeposit(
290-
{
291-
address: signer.value,
292-
change: deposit,
293-
isNegative: true,
294-
blockNumber,
295-
transactionHash
296-
},
297-
{ transaction }
298-
)
299-
);
319+
if (stakeAction === null) {
320+
break;
321+
}
322+
switch (stakeAction.type) {
323+
case "selfNominate": {
324+
const signer = tx.getSignerAddress({
325+
networkId: sdk.networkId
326+
});
327+
const deposit = stakeAction.deposit;
328+
queries.push(
329+
CCCChangeModel.stakeDeposit(
330+
{
331+
address: signer.value,
332+
change: deposit,
333+
isNegative: true,
334+
blockNumber,
335+
transactionHash
336+
},
337+
{ transaction }
338+
)
339+
);
340+
break;
341+
}
342+
case "reportDoubleVote": {
343+
const signer = tx.getSignerAddress({
344+
networkId: sdk.networkId
345+
});
346+
const criminal = stakeAction.criminal();
347+
const deposit = await getDeposit(
348+
sdk,
349+
blockNumber,
350+
criminal,
351+
transaction
352+
);
353+
if (deposit.isGreaterThan(0)) {
354+
queries.push(
355+
CCCChangeModel.reportDoubleVote(
356+
{
357+
address: signer.value,
358+
change: deposit,
359+
blockNumber,
360+
transactionHash
361+
},
362+
{ transaction }
363+
)
364+
);
365+
}
366+
break;
367+
}
368+
default: {
369+
break;
370+
}
300371
}
301372
}
302373
default:

0 commit comments

Comments
 (0)