Skip to content

Commit

Permalink
Merge branch 'main' of github.com:AbmSourav/dataStructure into release
Browse files Browse the repository at this point in the history
  • Loading branch information
AbmSourav committed Jun 21, 2021
2 parents 78dd695 + 56692a9 commit 01e89fe
Show file tree
Hide file tree
Showing 9 changed files with 727 additions and 7 deletions.
465 changes: 465 additions & 0 deletions blockChain/blockChain.bundle.js

Large diffs are not rendered by default.

90 changes: 90 additions & 0 deletions blockChain/blockChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { BlockChainApi, BlockType, DataType } from "./helper.d.ts";
import { searchGenerator, iteratorGenerator } from "./generators.ts"
import { Block } from "./blockNode.ts";

export class BlockChain implements BlockChainApi {
#chain: Array<any>
length: number

constructor() {
this.#chain = new Array()
this.length = this.#chain.length
}

createBlock(data: DataType<any>) {
const newBlock = new Block(data, this.#chain.length + 1)

if (this.#chain.length === 0) {
this.#chain.push(newBlock)
this.length++

return true
}

if (this.#chain.length > 0) {
const prevBlock = this.latestBlock()
newBlock.prevHash = prevBlock!.hash
this.#chain.push(newBlock)
this.length++

return true
}

return false
}

search(key: string) {
if (this.#chain.length === 0) {
return false
}

const iterator = searchGenerator(key, this.#chain)
let iteratorNext = iterator.next()
while (iteratorNext.value) {
return iteratorNext.value
}

return false;
}

latestBlock() {
if (this.#chain.length <= 0) return false
return this.#chain[this.#chain.length - 1]
}

iterator() {
return iteratorGenerator(this.#chain)
}

checkValidation() {
const iterator = this.iterator()
let iteratNext = iterator.next()
let prevHash: null|string = null
let prevBlock: null|BlockType = null

while (iteratNext.value) {
const block = iteratNext.value
const regeneratedHash = block.regenerateHash()
if (block.hash !== regeneratedHash) {
console.error(`index: ${block.index}, block is corrupted`)
return false
}
if (prevBlock != null && block.prevHash !== prevHash) {
console.error(`index: ${prevBlock!.index}, block is corrupted`)
return false
}

prevHash = block.regenerateHash()
prevBlock = block
iteratNext = iterator.next()
}

console.log('The Chain is valid.')
return true
}

log() {
console.log(this.#chain)
}

}
32 changes: 32 additions & 0 deletions blockChain/blockNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { DataType } from "./helper.d.ts";
import { createHash } from "https://deno.land/std/hash/mod.ts";

function blockHash(index: number, data: any, time: Date) {
let hash = createHash("sha256")
hash = hash.update(JSON.stringify(data) + index + time)

return hash.toString();
}

export class Block {
index: number
data: DataType<any>
hash: string
prevHash: string|null
time: Date

constructor(data: DataType<any>, index: number) {
this.index = index
this.data = data
this.time = new Date()
this.hash = blockHash(index, data, this.time)
this.prevHash = null;
}

regenerateHash() {
const data: DataType<any>|any = this.data
const hash = blockHash(this.index, data, this.time)

return hash.toString();
}
}
15 changes: 15 additions & 0 deletions blockChain/generators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function *searchGenerator(key: string|number, chain: Array<any>) {
for (let block in chain) {
if (chain[block].data.key === key) {
yield chain[block]
}
}
return false
}

export function *iteratorGenerator(chain: Array<any>) {
for (let block in chain) {
yield chain[block]
}
return false
}
25 changes: 25 additions & 0 deletions blockChain/helper.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

export type DataType<T> = {
key: string
value: T
}

// singly linked list node class
export type BlockType = {
index: number
data: DataType<any>
time: Date
hash: string
prevHash: null|string
}|null

// singly linked list interface
export interface BlockChainApi {
length: number
createBlock(data: any): boolean
search(key: string): boolean|BlockType
latestBlock(): boolean|BlockType
log(): void;
iterator(): Generator
checkValidation(): boolean
}
70 changes: 70 additions & 0 deletions blockChain/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
## BlockChain Api
<p><code>sha256</code> has been used for creating Hashes.</p>

```ts
DataType<T> = {
key: string|number
value: T
}

// Time Complexity: O(1)
length: number;

// *generator function, it returens an iterator.
// loop through all blocks in the chain
iterator(): Generator

// Time Complexity: O(n)
// check for 'the blockChain is corrupted/manipulated or valid'.
checkValidation(): boolean

// Time Complexity: O(1)
createBlock(data: any): boolean

// Time Complexity: O(1)
latestBlock(): boolean|BlockType

// Time Complexity: O(n)
log(): void;

// Time Complexity: O(n)
search(key: string): boolean|BlockType

```

<br>
<br>

## Examples
```ts
import { BlockChain } from "https://deno.land/x/datastructure/mod.ts";

const blockChain = new BlockChain()

// iterator method returns a *generator function
const iterator = blockChain.iterator()
let iteratorNext = iterator.next()
// console.log(iteratorNext.next(), iteratorNext.next());
while (iteratorNext.done === false) {
console.log(iteratorNext.value);
iteratorNext = iterator.next()
}

// length or count of total blocks.
blockChain.length

// create a block in the blockChain.
blockChain.createBlock({key: 'sourav', value: {name: "Sourav"}})
blockChain.createBlock({key: 'abm', value: "AbmSourav"})
blockChain.createBlock({key: 'JS', value: ['JS', 'TS']})

// get latest created block
blockChain.latestBlock()

// console all values
blockChain.log()

// searching data in in the blocks
blockChain.search('JS')

```
21 changes: 21 additions & 0 deletions blockChain/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { BlockChain } from "./blockChain.ts";

const blockChain = new BlockChain()

blockChain.createBlock({key: 'sourav', value: "Sourav"})
blockChain.createBlock({key: 'abm', value: "AbmSourav"})
blockChain.createBlock({key: 'apple', value: "Apple Inc."})

// const it = blockChain.iterator()
// console.log(it.next(), it.next(), it.next(), it.next());

// const latestBlock = blockChain.latestBlock()
// console.log(latestBlock);

// latestBlock.data.key = '1'
// blockChain.test()
// console.log(blockChain.checkValidation());

// blockChain.log()
// console.log(blockChain.search('apple'))
console.log(blockChain.length)
13 changes: 7 additions & 6 deletions mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
* @email <[email protected]>
* @authorUrl https://abmsourav.com
* @sourceCode https://github.com/AbmSourav/dataStructure
* @version 1.0.4
* @version 1.0.5
*
* Copyright (c) 2021 Keramot UL Islam
*/

export { HashTable } from "./hashTable/hashTable.bundle.js";
export { SinglyLinkedList } from "./linkedList/singly/singlyLinkedList.bundle.js";
export { DoublyLinkedList } from "./linkedList/doubly/doublyLinkedList.bundle.js";
export { Stack } from "./stack/stack.bundle.js"
export { Queue } from "./queue/queue.bundle.js"
export { BlockChain } from "./blockChain/blockChain.bundle.js";
export { HashTable } from "./hashTable/hashTable.bundle.js";
export { SinglyLinkedList } from "./linkedList/singly/singlyLinkedList.bundle.js";
export { DoublyLinkedList } from "./linkedList/doubly/doublyLinkedList.bundle.js";
export { Stack } from "./stack/stack.bundle.js"
export { Queue } from "./queue/queue.bundle.js"
3 changes: 2 additions & 1 deletion mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
* @email <[email protected]>
* @authorUrl https://abmsourav.com
* @sourceCode https://github.com/AbmSourav/dataStructure
* @version 1.0.4
* @version 1.0.5
*
* Copyright (c) 2021 Keramot UL Islam
*/

export { BlockChain } from "./blockChain/blockChain.ts";
export { HashTable } from "./hashTable/hashTable.ts";
export { SinglyLinkedList } from "./linkedList/singly/singlyLinkedList.ts";
export { DoublyLinkedList } from "./linkedList/doubly/doublyLinkedList.ts";
Expand Down

0 comments on commit 01e89fe

Please sign in to comment.