Skip to content

Commit

Permalink
update(第四次)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForMikasaEver committed Jun 2, 2023
1 parent e6bbc3b commit 5e9c74a
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 28 additions & 14 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 80 additions & 0 deletions src/cryptoCurrency/MerkleTree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { createHash } from 'crypto';
class MerkleTree {
constructor(data) {
this.leaves = data.map(item => this.hash(item));
this.tree = this.buildTree(this.leaves.slice());
this.root = this.tree[0];
}

hash(data) {
return createHash('sha256').update(data).digest('hex');
}

buildTree(leaves) {
let tree = leaves;
while (tree.length > 1) {
let parentLevel = [];
for (let i = 0; i < tree.length; i += 2) {
let left = tree[i];
let right = tree[i + 1] || left;
parentLevel.push(this.hash(left + right));
}
tree = parentLevel;
}
return tree;
}

// 添加节点
addNode(data) {
this.leaves.push(this.hash(data));
this.tree = this.buildTree(this.leaves.slice());
this.root = this.tree[0];
}

// 删除节点
removeNode(data) {
const hashedData = this.hash(data);
const index = this.leaves.findIndex(leaf => leaf === hashedData);
if (index !== -1) {
this.leaves.splice(index, 1);
this.tree = this.buildTree(this.leaves.slice());
this.root = this.tree[0];
}
}

// 验证函数
verify(data) {
const hashedData = this.hash(data);
let tree = this.leaves.slice();
while (tree.length > 1) {
let parentLevel = [];
for (let i = 0; i < tree.length; i += 2) {
let left = tree[i];
let right = tree[i + 1] || left;
if (hashedData === left || hashedData === right) {
return true;
}
parentLevel.push(this.hash(left + right));
}
tree = parentLevel;
}
return false;
}
}

const data = ['A', 'B', 'C', 'D'];
const merkleTree = new MerkleTree(data);
console.log('Merkle Root:', merkleTree.root);

// 验证数据块 'A' 是否在 Merkle Tree 中
console.log('Verify A:', merkleTree.verify('A')); // 应该输出 true

// 添加一个新的数据块 'E'
merkleTree.addNode('E');
console.log('Merkle Root after adding E:', merkleTree.root); // Merkle Root 应该发生变化

// 删除数据块 'E'
merkleTree.removeNode('E');
console.log('Merkle Root after removing E:', merkleTree.root); // Merkle Root 应该恢复到最初的值

export default MerkleTree
24 changes: 24 additions & 0 deletions src/models/Transaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sha256 from 'crypto-js/sha256.js'
import t from "ramda/src/T.js";


class Transaction {
constructor(miner, receiverPubKey, value, hash) {
this.miner = miner
this.receiverPubKey = receiverPubKey
this.value = value
this.hash = hash || this._calculateHash()
}

// 更新交易 hash
_setHash() {
this.hash = this._calculateHash()
}

// 计算交易 hash 的摘要函数
_calculateHash() {
return sha256(this.miner + this.receiverPubKey + this.value).toString()
}
}

export default Transaction
28 changes: 28 additions & 0 deletions src/models/UTXOPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,34 @@ class UTXOPool {
clone() {
return new UTXOPool({...this.utxos})
}

// 处理交易函数
handleTransaction(transaction) {
if (!this.isValidTransaction(transaction.miner, transaction.value)) {
return
}

// miner地址的amount减去交易的value
this.utxos[transaction.miner].amount -= transaction.value

// !!!注意!!!
// 这里把公钥的地址加入到utxos里面,就不用在addUTXO函数里面添加判断这个地址到底是miner还是receiverPubKey了
// 因为addUTXO里面也判断不了,没有传transaction判断个屁
this.utxos[transaction.receiverPubKey] = new UTXO(transaction.receiverPubKey, 0)

// 把receiverPubKey加入到UTXOPool里面
this.addUTXO(transaction.receiverPubKey, transaction.value);
}

/**
* 验证交易合法性
* 验证余额
* 返回 bool
*/
isValidTransaction(address, amount) {
return this.utxos[address].amount && this.utxos[address].amount >= amount
}

}

export default UTXOPool
2 changes: 1 addition & 1 deletion src/tests/lesson1.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const main = () => {
// console.log(blockchain)

// 区块检查
console.assert(longestChain.length === 3, 'Block height should be 2')
console.assert(longestChain.length === 3, 'Block height should be 3')
console.assert(
longestChain[2].hash === thirdBlock.hash,
`Height block hash should be ${thirdBlock.hash}`,
Expand Down
2 changes: 1 addition & 1 deletion src/tests/lesson2.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const main = () => {
longestChain = blockchain.longestChain()

// 区块检查
console.assert(longestChain.length === 3, 'Block height should be 2')
console.assert(longestChain.length === 3, 'Block height should be 3')
console.assert(
longestChain[2].hash === thirdBlock.hash,
`Height block hash should be ${thirdBlock.hash}`,
Expand Down
2 changes: 1 addition & 1 deletion src/tests/lesson3.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const main = () => {
// console.log(blockchain)

// 区块检查
console.assert(longestChain.length === 3, 'Block height should be 2')
console.assert(longestChain.length === 3, 'Block height should be 3')
console.assert(
longestChain[2].hash === thirdBlock.hash,
`Height block hash should be ${thirdBlock.hash}`,
Expand Down
Loading

0 comments on commit 5e9c74a

Please sign in to comment.