Skip to content
This repository has been archived by the owner on Sep 26, 2024. It is now read-only.

Commit

Permalink
Refactor: factor out empty hash code (#1222)
Browse files Browse the repository at this point in the history
  • Loading branch information
luixo authored Jun 2, 2023
1 parent 1aaf142 commit d679650
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
5 changes: 2 additions & 3 deletions backend/src/cron/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
import * as nearApi from "@/backend/utils/near";
import { SECOND, MINUTE } from "@/backend/utils/time";
import * as RPC from "@/common/types/rpc";
import { EMPTY_CODE_HASH } from "@/common/utils/constants";
import { wait } from "@/common/utils/promise";

export const latestBlockCheck: RegularCheckFn = {
Expand Down Expand Up @@ -580,9 +581,7 @@ const updatePoolInfoMap = async (
finality: "final",
});

// 'code_hash' === 11111111111111111111111111111111 is when the validator
// does not have a staking-pool contract on it (common on testnet)
if (account.code_hash === "11111111111111111111111111111111") {
if (account.code_hash === EMPTY_CODE_HASH) {
return {
fee: null,
delegatorsCount: null,
Expand Down
3 changes: 2 additions & 1 deletion backend/src/router/account/by-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { commonProcedure } from "@/backend/router/trpc";
import { validators } from "@/backend/router/validators";
import * as nearApi from "@/backend/utils/near";
import { ignoreIfDoesNotExist } from "@/backend/utils/near";
import { EMPTY_CODE_HASH } from "@/common/utils/constants";

const getLockupAccountId = async (
accountId: string
Expand Down Expand Up @@ -166,7 +167,7 @@ export const procedures = {
return {
id,
isContract: nearCoreAccount
? nearCoreAccount.code_hash !== "11111111111111111111111111111111"
? nearCoreAccount.code_hash !== EMPTY_CODE_HASH
: false,
created: accountInfo.created,
storageUsed: accountDetails.storageUsage,
Expand Down
3 changes: 2 additions & 1 deletion backend/src/router/contract/by-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { div } from "@/backend/database/utils";
import { commonProcedure } from "@/backend/router/trpc";
import { validators } from "@/backend/router/validators";
import * as nearApi from "@/backend/utils/near";
import { EMPTY_CODE_HASH } from "@/common/utils/constants";

const queryContractFromIndexer = async (accountId: string) => {
// find the latest update in analytics db
Expand Down Expand Up @@ -119,7 +120,7 @@ export const procedure = commonProcedure
})
.catch(nearApi.ignoreIfDoesNotExist);
// see https://github.com/near/near-explorer/pull/841#discussion_r783205960
if (!account || account.code_hash === "11111111111111111111111111111111") {
if (!account || account.code_hash === EMPTY_CODE_HASH) {
return null;
}
const [contractInfo, accessKeys] = await Promise.all([
Expand Down
2 changes: 2 additions & 0 deletions common/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Code hash an account has when it has no code deployed
export const EMPTY_CODE_HASH = "11111111111111111111111111111111";
3 changes: 2 additions & 1 deletion frontend/src/components/blocks/BlockDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useTranslation } from "next-i18next";
import { Row, Col } from "react-bootstrap";

import { Block } from "@/common/types/procedures";
import { EMPTY_CODE_HASH } from "@/common/utils/constants";
import { AccountLink } from "@/frontend/components/utils/AccountLink";
import { BlockLink } from "@/frontend/components/utils/BlockLink";
import {
Expand Down Expand Up @@ -189,7 +190,7 @@ export const BlockDetails: React.FC<Props> = React.memo(({ block }) => {
/>
}
text={
block.prevHash === "11111111111111111111111111111111" ? (
block.prevHash === EMPTY_CODE_HASH ? (
"Genesis"
) : (
<BlockLink blockHash={block.prevHash}>
Expand Down
21 changes: 11 additions & 10 deletions frontend/src/components/transactions/__tests__/common.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Receipt, TransactionOld } from "@/common/types/procedures";
import { EMPTY_CODE_HASH } from "@/common/utils/constants";

export const TRANSACTIONS: TransactionOld[] = [
// no action has deposit
Expand Down Expand Up @@ -242,11 +243,11 @@ export const TRANSACTIONS: TransactionOld[] = [
];

export const TRANSACTION_WITH_SUCCESSFUL_RECEIPT: TransactionOld = {
hash: "T1111111111111111111111111111111111111111111",
hash: `T${EMPTY_CODE_HASH}`,
signerId: "signer.test",
receiverId: "receiver.test",
status: "success",
blockHash: "B111111111111111111111111111111111111111111",
blockHash: `B${EMPTY_CODE_HASH}`,
blockTimestamp: +new Date(2019, 1, 1),
actions: [
{
Expand Down Expand Up @@ -305,7 +306,7 @@ export const TRANSACTION_WITH_SUCCESSFUL_RECEIPT: TransactionOld = {
tokensBurnt: "612157772373200000000",
},
predecessorId: "signer.test",
id: "R1111111111111111111111111111111111111111111",
id: `R${EMPTY_CODE_HASH}`,
receiverId: "receiver.test",
},
outcome: {
Expand All @@ -315,11 +316,11 @@ export const TRANSACTION_WITH_SUCCESSFUL_RECEIPT: TransactionOld = {
};

export const TRANSACTION_WITH_MANY_RECEIPTS: TransactionOld = {
hash: "T1111111111111111111111111111111111111111111",
hash: `T${EMPTY_CODE_HASH}`,
signerId: "signer.test",
receiverId: "receiver.test",
status: "success",
blockHash: "B111111111111111111111111111111111111111111",
blockHash: `B${EMPTY_CODE_HASH}`,
blockTimestamp: +new Date(2019, 1, 1),
actions: [
{
Expand Down Expand Up @@ -474,7 +475,7 @@ export const TRANSACTION_WITH_MANY_RECEIPTS: TransactionOld = {
tokensBurnt: "2087691790109200000000",
},
predecessorId: "signer.test",
id: "R1111111111111111111111111111111111111111111",
id: `R${EMPTY_CODE_HASH}`,
receiverId: "receiver.test",
},
outcome: {
Expand All @@ -484,11 +485,11 @@ export const TRANSACTION_WITH_MANY_RECEIPTS: TransactionOld = {
};

export const TRANSACTION_WITH_FAILING_RECEIPT: TransactionOld = {
hash: "T1111111111111111111111111111111111111111111",
hash: `T${EMPTY_CODE_HASH}`,
signerId: "signer.test",
receiverId: "receiver.test",
status: "failure",
blockHash: "B111111111111111111111111111111111111111111",
blockHash: `B${EMPTY_CODE_HASH}`,
blockTimestamp: +new Date(2019, 1, 1),
actions: [
{
Expand All @@ -514,7 +515,7 @@ export const TRANSACTION_WITH_FAILING_RECEIPT: TransactionOld = {
},
],
outcome: {
blockHash: "B111111111111111111111111111111111111111111",
blockHash: `B${EMPTY_CODE_HASH}`,
logs: ["000028066 listings not found"],
nestedReceipts: [
{
Expand Down Expand Up @@ -555,7 +556,7 @@ export const TRANSACTION_WITH_FAILING_RECEIPT: TransactionOld = {
tokensBurnt: "338053723011200000000",
},
predecessorId: "signer.test",
id: "R1111111111111111111111111111111111111111111",
id: `R${EMPTY_CODE_HASH}`,
receiverId: "receiver.test",
},
outcome: {
Expand Down

0 comments on commit d679650

Please sign in to comment.