Skip to content

Commit

Permalink
block: payload types
Browse files Browse the repository at this point in the history
  • Loading branch information
tammo committed Oct 5, 2020
1 parent e34fc6c commit 9057fec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
7 changes: 5 additions & 2 deletions src/Block.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { StorageId } from './Storage';
import { AssetSymbol } from './Blockchain';

export type EncryptedNumber = string;
export type EncryptedString = string;
Expand All @@ -14,6 +13,7 @@ export type SignatureData = {
refBlock: Signature | undefined;
refAsset: string | undefined;
claimSignature: string | undefined;
payload: string | undefined;
};

export enum BlockType {
Expand All @@ -35,14 +35,15 @@ export class Block {
refBlock: Signature | undefined;
refAsset: string | undefined;
claimSignature: string | undefined;
payload: string | undefined;
createdAt: number;
senderBalance: EncryptedNumber;
senderAmount: EncryptedNumber;

state?: string;

getDataForSignature(): SignatureData {
const { type, prev, senderBalance, senderAmount, refBlock, refAsset, claimSignature } = this;
const { type, prev, senderBalance, senderAmount, refBlock, refAsset, claimSignature, payload } = this;

return {
type,
Expand All @@ -52,6 +53,7 @@ export class Block {
refBlock,
refAsset,
claimSignature,
payload,
};
}

Expand Down Expand Up @@ -98,6 +100,7 @@ export function fromBlockObject(obj: any) {
block.senderBalance = obj.senderBalance;
block.senderAmount = obj.senderAmount;
block.state = obj.state;
block.payload = obj.payload;

return block;
}
Expand Down
10 changes: 5 additions & 5 deletions src/Blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ export class Blockchain {

// index points to next blocks
// you can look up which blocks points to a signature
this.blocks.forEach(block => {
this.blocks.forEach((block) => {
// only interested in asset blocks
if (block.type !== BlockType.ASSET) return;

prevIndex[block.prev as string] = block;
});

const anyAssetBlock = this.blocks.find(block => block.type === BlockType.ASSET);
const anyAssetBlock = this.blocks.find((block) => block.type === BlockType.ASSET);

if (!anyAssetBlock) {
// chains without assets block may have just one or zero blocks
Expand All @@ -48,14 +48,14 @@ export class Blockchain {

// index points to next blocks
// you can look up which blocks points to a signature
this.blocks.forEach(block => {
this.blocks.forEach((block) => {
// not interested in asset blocks
if (block.type === BlockType.ASSET) return;

prevIndex[block.prev as string] = block;
});

const assetBlock = this.blocks.find(block => block.type === BlockType.ASSET && block.refAsset === asset);
const assetBlock = this.blocks.find((block) => block.type === BlockType.ASSET && block.refAsset === asset);

// chain has no asset created yet
if (!assetBlock) return;
Expand All @@ -68,7 +68,7 @@ export class Blockchain {
return;
}

return this.blocks.filter(block => block.type === BlockType.OPENING && !block.prev).pop();
return this.blocks.filter((block) => block.type === BlockType.OPENING && !block.prev).pop();
}
}

Expand Down

0 comments on commit 9057fec

Please sign in to comment.