Skip to content

Commit 6ba4fc7

Browse files
committed
fix: nullable block hashes
1 parent a00a9a5 commit 6ba4fc7

File tree

8 files changed

+15
-10
lines changed

8 files changed

+15
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "krist",
3-
"version": "3.5.7",
3+
"version": "3.5.8",
44
"description": "The new Krist node written in TypeScript.",
55
"type": "module",
66
"scripts": {

src/database/schemas.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ export class Block extends Model {
9090

9191
@Attribute(DataTypes.STRING(64))
9292
@Unique
93-
@NotNull
94-
declare hash: string;
93+
declare hash: string | null;
9594
@Attribute(DataTypes.STRING(NONCE_MAX_SIZE * 2))
9695
declare nonce: string;
9796
@Attribute(DataTypes.INTEGER.UNSIGNED)

src/krist/blocks/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ import { cachedFindAndCountAll } from "../../utils/cache.js";
4646
import { getBaseBlockValue, getLegacyWork, sanitiseLimit, sanitiseOffset } from "../../utils/index.js";
4747
import { getUnpaidNameCount } from "../names/index.js";
4848

49+
export const GENESIS_HASH = "0".repeat(64);
50+
4951
export async function getBlock(id: number): Promise<Block | null> {
5052
return Block.findByPk(id);
5153
}

src/krist/blocks/submit.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { isMiningEnabled } from "../switches.js";
3636
import { createTransaction } from "../transactions/create.js";
3737
import { getWork, setWork } from "../work.js";
3838
import { blockToJson, getBlockValue, getLastBlock } from "./index.js";
39+
import { GENESIS_HASH } from "./index.js";
3940

4041
const promBlockCounter = new promClient.Counter({
4142
name: "krist_blocks_total",
@@ -60,7 +61,7 @@ export async function submitBlock(
6061
if (!lastBlock) throw new ErrorMiningDisabled();
6162

6263
// Verify the provided nonce is a solution to the current block
63-
const last = lastBlock.hash.substring(0, 12);
64+
const last = (lastBlock.hash ?? GENESIS_HASH).substring(0, 12);
6465
const work = await getWork();
6566
const hash = sha256(address, last, nonce);
6667

src/webserver/routes/blocks.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { Router } from "express";
2424
import { ctrlGetBlock, ctrlGetLastBlock } from "../../controllers/blocks.js";
2525
import {
2626
blockToJson,
27+
GENESIS_HASH,
2728
getBlock,
2829
getBlocks,
2930
getBlockValue,
@@ -90,8 +91,8 @@ export default (): Router => {
9091
}>, res, next) => {
9192
if (req.query.lastblock !== undefined) {
9293
const block = await getLastBlock();
93-
if (!block) return res.send("000000000000");
94-
return res.send(block.hash.substring(0, 12));
94+
if (!block) return res.send(GENESIS_HASH.substring(0, 12));
95+
return res.send((block.hash ?? GENESIS_HASH).substring(0, 12));
9596
}
9697

9798
if (req.query.getbaseblockvalue !== undefined) {

src/webserver/routes/submission.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
KristError
2929
} from "../../errors/index.js";
3030
import { addressToJson } from "../../krist/addresses/index.js";
31-
import { blockToJson, getLastBlock } from "../../krist/blocks/index.js";
31+
import { blockToJson, GENESIS_HASH, getLastBlock } from "../../krist/blocks/index.js";
3232
import { ReqQuery } from "../index.js";
3333

3434
export default (): Router => {
@@ -100,7 +100,7 @@ export default (): Router => {
100100
// solutions, not sure why
101101
const lastBlock = await getLastBlock();
102102
if (!lastBlock) return res.send("Mining disabled");
103-
return res.send(address + lastBlock.hash.substring(0, 12) + nonce);
103+
return res.send(address + (lastBlock.hash ?? GENESIS_HASH).substring(0, 12) + nonce);
104104
}
105105
}
106106

test/routes/lookup.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import { Name, Transaction } from "../../src/database/index.js";
2525
import { invalidateCountCache } from "../../src/utils/cache.js";
2626
import { api } from "../api.js";
2727
import { seed } from "../seed.js";
28+
import { GENESIS_HASH } from "../../src/krist/blocks/index.js";
2829

2930
describe("v2 routes: lookup api", function() {
3031
before(seed);
@@ -161,7 +162,7 @@ describe("v2 routes: lookup api", function() {
161162
expect(res.body.query).to.deep.include({ matchBlock: true, hasID: true, cleanID: 1 });
162163
expect(res.body.matches).to.deep.include({ exactAddress: false, exactName: false, exactTransaction: false });
163164
expect(res.body.matches.exactBlock).to.be.an("object");
164-
expect(res.body.matches.exactBlock).to.deep.include({ hash: "0000000000000000000000000000000000000000000000000000000000000000" });
165+
expect(res.body.matches.exactBlock).to.deep.include({ hash: GENESIS_HASH });
165166
});
166167

167168
it("should create a transaction to test", async function() {

test/seed.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import chalkT from "chalk-template";
2323
import { Address, Block, db } from "../src/database/index.js";
2424
import { redis, rKey } from "../src/database/redis.js";
2525
import { REDIS_PREFIX, TEST_DEBUG } from "../src/utils/vars.js";
26+
import { GENESIS_HASH } from "../src/krist/blocks/index.js";
2627

2728
export async function seed(): Promise<void> {
2829
const debug = TEST_DEBUG;
@@ -41,7 +42,7 @@ export async function seed(): Promise<void> {
4142
// Create the genesis block
4243
Block.create({
4344
value: 50,
44-
hash: "0000000000000000000000000000000000000000000000000000000000000000",
45+
hash: GENESIS_HASH,
4546
address: "0000000000",
4647
nonce: "0",
4748
difficulty: 4294967295,

0 commit comments

Comments
 (0)