|
1 | 1 | import { H160, H256, U64 } from "codechain-sdk/lib/core/classes"; |
| 2 | +import * as _ from "lodash"; |
2 | 3 | import * as Sequelize from "sequelize"; |
| 4 | +import sequelize = require("sequelize"); |
3 | 5 | import models from ".."; |
4 | 6 | import * as Exception from "../../exception"; |
5 | 7 | import { utxoPagination } from "../../routers/pagination"; |
@@ -447,48 +449,125 @@ export async function getByTxHashIndex( |
447 | 449 | } |
448 | 450 | } |
449 | 451 |
|
450 | | -export async function getSnapshot(assetType: H256, blockNumber: number) { |
| 452 | +export async function getSnapshot(params: { |
| 453 | + assetType: H256; |
| 454 | + blockNumber: number; |
| 455 | + lastEvaluatedKey?: number[] | null; |
| 456 | +}) { |
| 457 | + const { assetType, blockNumber, lastEvaluatedKey } = params; |
| 458 | + const transaction = await models.sequelize.transaction({ |
| 459 | + isolationLevel: |
| 460 | + models.Sequelize.Transaction.ISOLATION_LEVELS.REPEATABLE_READ, |
| 461 | + deferrable: models.Sequelize.Deferrable.SET_DEFERRED |
| 462 | + }); |
451 | 463 | try { |
452 | | - return models.UTXO.findAll({ |
453 | | - where: { |
454 | | - assetType: strip0xPrefix(assetType.value), |
455 | | - usedBlockNumber: { |
456 | | - [Sequelize.Op.or]: [ |
457 | | - { [Sequelize.Op.gt]: blockNumber }, |
458 | | - { [Sequelize.Op.eq]: null } |
459 | | - ] |
| 464 | + const [ |
| 465 | + fromBlockNumber, |
| 466 | + fromTransactionIndex, |
| 467 | + fromTransactionOutputIndex |
| 468 | + ] = lastEvaluatedKey || [Number.MAX_SAFE_INTEGER, 0, 0]; |
| 469 | + const itemsPerPage = 100; |
| 470 | + |
| 471 | + const rows = await models.sequelize.query( |
| 472 | + `SELECT SUM("UTXO"."quantity") AS "totalAssetQuantity", "UTXO"."address", "UTXO"."assetType", |
| 473 | + COUNT("UTXO"."assetType") AS "utxoQuantity" |
| 474 | + FROM (SELECT * FROM "UTXOs" WHERE ("blockNumber", "transactionIndex", "transactionOutputIndex")<(:fromBlockNumber, :fromTransactionIndex, :fromTransactionOutputIndex) |
| 475 | + AND "assetType"=:assetType |
| 476 | + ORDER BY "blockNumber" DESC, "transactionIndex" DESC, "transactionOutputIndex" DESC |
| 477 | + LIMIT :itemsPerPage) "UTXO" |
| 478 | + WHERE ("UTXO"."usedBlockNumber" > :blockNumber OR "UTXO"."usedBlockNumber" IS NULL) |
| 479 | + AND "UTXO"."blockNumber" <= :blockNumber |
| 480 | + GROUP BY "UTXO"."address", "UTXO"."assetType" |
| 481 | + `, |
| 482 | + { |
| 483 | + replacements: { |
| 484 | + fromBlockNumber, |
| 485 | + fromTransactionIndex, |
| 486 | + fromTransactionOutputIndex, |
| 487 | + blockNumber, |
| 488 | + assetType: strip0xPrefix(assetType.toString()), |
| 489 | + itemsPerPage |
460 | 490 | }, |
461 | | - blockNumber: { |
462 | | - [Sequelize.Op.lte]: blockNumber |
463 | | - } |
464 | | - }, |
465 | | - attributes: [ |
466 | | - [ |
467 | | - Sequelize.fn("SUM", Sequelize.col("quantity")), |
468 | | - "totalAssetQuantity" |
469 | | - ], |
470 | | - "address", |
471 | | - "assetType", |
472 | | - [ |
473 | | - Sequelize.fn("COUNT", Sequelize.col("UTXO.assetType")), |
474 | | - "utxoQuantity" |
475 | | - ] |
476 | | - ], |
477 | | - order: Sequelize.literal( |
478 | | - `"totalAssetQuantity" DESC, "assetType" DESC` |
479 | | - ), |
480 | | - include: [ |
| 491 | + raw: true, |
| 492 | + transaction, |
| 493 | + type: sequelize.QueryTypes.SELECT |
| 494 | + } |
| 495 | + ); |
| 496 | + |
| 497 | + // The SQL query is copied from the query above. |
| 498 | + const hasNextPage = |
| 499 | + (await models.sequelize.query( |
| 500 | + `SELECT COUNT(*) as count |
| 501 | + FROM (SELECT id FROM "UTXOs" |
| 502 | + WHERE ("blockNumber", "transactionIndex", "transactionOutputIndex")<(:fromBlockNumber, :fromTransactionIndex, :fromTransactionOutputIndex) |
| 503 | + AND "assetType"=:assetType |
| 504 | + ORDER BY "blockNumber" DESC, "transactionIndex" DESC, "transactionOutputIndex" DESC |
| 505 | + LIMIT :itemsPerPage) "UTXO"`, |
481 | 506 | { |
482 | | - as: "assetScheme", |
483 | | - model: models.AssetScheme |
| 507 | + replacements: { |
| 508 | + fromBlockNumber, |
| 509 | + fromTransactionIndex, |
| 510 | + fromTransactionOutputIndex, |
| 511 | + assetType: strip0xPrefix(assetType.toString()), |
| 512 | + itemsPerPage: itemsPerPage + 1 |
| 513 | + }, |
| 514 | + plain: true, |
| 515 | + raw: true, |
| 516 | + transaction, |
| 517 | + type: sequelize.QueryTypes.SELECT |
484 | 518 | } |
485 | | - ], |
486 | | - group: ["UTXO.address", "UTXO.assetType", "assetScheme.assetType"] |
487 | | - }).then(instances => |
488 | | - instances.map(instance => instance.get({ plain: true })) |
| 519 | + )).count === |
| 520 | + itemsPerPage + 1; |
| 521 | + |
| 522 | + // The SQL query is copied from the query above. |
| 523 | + const lastRow = await models.sequelize.query( |
| 524 | + `SELECT * |
| 525 | + FROM (SELECT * FROM "UTXOs" WHERE ("blockNumber", "transactionIndex", "transactionOutputIndex")<(:fromBlockNumber, :fromTransactionIndex, :fromTransactionOutputIndex) |
| 526 | + AND "assetType"=:assetType |
| 527 | + ORDER BY "blockNumber" DESC, "transactionIndex" DESC, "transactionOutputIndex" DESC |
| 528 | + LIMIT :itemsPerPage) "UTXO" |
| 529 | + ORDER BY "blockNumber" ASC, "transactionIndex" ASC, "transactionOutputIndex" ASC |
| 530 | + LIMIT 1 |
| 531 | + `, |
| 532 | + { |
| 533 | + replacements: { |
| 534 | + fromBlockNumber, |
| 535 | + fromTransactionIndex, |
| 536 | + fromTransactionOutputIndex, |
| 537 | + assetType: strip0xPrefix(assetType.toString()), |
| 538 | + itemsPerPage |
| 539 | + }, |
| 540 | + plain: true, |
| 541 | + raw: true, |
| 542 | + transaction, |
| 543 | + type: sequelize.QueryTypes.SELECT |
| 544 | + } |
489 | 545 | ); |
| 546 | + |
| 547 | + const assetScheme = await AssetSchemeModel.getByAssetType(assetType, { |
| 548 | + transaction |
| 549 | + }); |
| 550 | + transaction.commit(); |
| 551 | + return { |
| 552 | + data: _.map(rows, (row: any) => { |
| 553 | + row.assetScheme = |
| 554 | + assetScheme && assetScheme.get({ plain: true }); |
| 555 | + return row; |
| 556 | + }), |
| 557 | + hasNextPage, |
| 558 | + hasPreviousPage: null, |
| 559 | + firstEvaluatedKey: null, |
| 560 | + lastEvaluatedKey: lastRow |
| 561 | + ? JSON.stringify([ |
| 562 | + lastRow.blockNumber, |
| 563 | + lastRow.transactionIndex, |
| 564 | + lastRow.transactionOutputIndex |
| 565 | + ]) |
| 566 | + : null |
| 567 | + }; |
490 | 568 | } catch (err) { |
491 | 569 | console.error(err); |
| 570 | + transaction.rollback(); |
492 | 571 | throw Exception.DBError(); |
493 | 572 | } |
494 | 573 | } |
|
0 commit comments