估值102亿融资5.45亿的Alchemy 项目,大学每周任务逐步开始。持有大学生早期卡的伙伴们可以开始大学生活了:
官方大佬的 lens ,可以关注下:
了解更多,请关注作者:https://twitter.com/bitc2024
部分章节是看文章和视频的,自行查看哈,这里从实际操作开始。
1: Transaction Output
查看要求:
输入:
class TXO {
constructor(owner, amount) {
this.owner = owner;
this.amount = amount;
this.spent = false;
}
spend() {
this.spent = true;
}
}
module.exports = TXO;
点击运行。
2: Spent TXOs\ 查看要求:
输入:
class Transaction {
constructor(inputUTXOs, outputUTXOs) {
this.inputUTXOs = inputUTXOs;
this.outputUTXOs = outputUTXOs;
}
execute() {
for (let i = 0; i < this.inputUTXOs.length; i++) {
if (this.inputUTXOs[i].spent) {
throw new Error("already spent");
}
}
}
}
module.exports = Transaction;
点击运行。
3: Sufficient Amount
查看要求:
class Transaction {
constructor(inputUTXOs, outputUTXOs) {
this.inputUTXOs = inputUTXOs;
this.outputUTXOs = outputUTXOs;
}
execute() {
for (let i = 0; i < this.inputUTXOs.length; i++) {
if (this.inputUTXOs[i].spent) {
throw new Error("already spent");
}
}
let inn = 0;
for (let i = 0; i < this.inputUTXOs.length; i++) {
inn = inn + this.inputUTXOs[i].amount;
}
let out = 0;
for (let i = 0; i < this.outputUTXOs.length; i++) {
out = out + this.outputUTXOs[i].amount;
}
if (inn < out) {
throw new Error("less than");
}
}
}
module.exports = Transaction;
点击运行。
4: Successful Execute
查看要求:
输入:
class Transaction {
constructor(inputUTXOs, outputUTXOs) {
this.inputUTXOs = inputUTXOs;
this.outputUTXOs = outputUTXOs;
}
execute() {
for (let i = 0; i < this.inputUTXOs.length; i++) {
if (this.inputUTXOs[i].spent) {
throw new Error("already spent");
}
}
let inn = 0;
for (let i = 0; i < this.inputUTXOs.length; i++) {
inn = inn + this.inputUTXOs[i].amount;
}
let out = 0;
for (let i = 0; i < this.outputUTXOs.length; i++) {
out = out + this.outputUTXOs[i].amount;
}
if (inn < out) {
throw new Error("less than");
}
for (let i = 0; i < this.inputUTXOs.length; i++) {
this.inputUTXOs[i].spend();
}
}
}
module.exports = Transaction;
点击运行。
5: Miner's Fee
查看要求:
输入:
class Transaction {
constructor(inputUTXOs, outputUTXOs) {
this.inputUTXOs = inputUTXOs;
this.outputUTXOs = outputUTXOs;
}
execute() {
for (let i = 0; i < this.inputUTXOs.length; i++) {
if (this.inputUTXOs[i].spent) {
throw new Error("already spent");
}
}
let inn = 0;
for (let i = 0; i < this.inputUTXOs.length; i++) {
inn = inn + this.inputUTXOs[i].amount;
}
let out = 0;
for (let i = 0; i < this.outputUTXOs.length; i++) {
out = out + this.outputUTXOs[i].amount;
}
if (inn < out) {
throw new Error("less than");
}
for (let i = 0; i < this.inputUTXOs.length; i++) {
this.inputUTXOs[i].spend();
}
this.fee = inn - out;
}
}
module.exports = Transaction;
点击运行。
1: Node
查看要求:
输入:
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
module.exports = Node;
点击运行。
2: Tree
查看要求:
输入:
class Tree {
constructor() {
this.root = null;
}
}
module.exports = Tree;
点击运行。
3: Add Root
查看要求:
输入:
class Tree {
constructor() {
this.root = null;
}
addNode(node) {
this.root = node;
}
}
module.exports = Tree;
点击运行。
4: First Layer
查看要求:
输入:
class Tree {
constructor() {
this.root = null;
}
addNode(node) {
if (this.root === null) {
this.root = node;
} else if (this.root.data > node.data) {
this.root.left = node;
} else {
this.root.right = node;
}
}
}
module.exports = Tree;
点击运行。
5: Many Layers
查看要求:
输入:
class Tree {
constructor() {
this.root = null;
}
addNode(node) {
if (this.root === null) {
this.root = node;
} else {
let p = this.root;
while (true) {
if (p.data > node.data) {
if (p.left) {
p = p.left;
} else {
p.left = node;
break;
}
} else {
if (p.right) {
p = p.right;
} else {
p.right = node;
break;
}
}
}
}
}
}
module.exports = Tree;
点击运行。
6: Search
查看要求:
输入:
class Tree {
constructor() {
this.root = null;
}
addNode(node) {
if (this.root === null) {
this.root = node;
} else {
let p = this.root;
while (true) {
if (p.data > node.data) {
if (p.left) {
p = p.left;
} else {
p.left = node;
break;
}
} else {
if (p.right) {
p = p.right;
} else {
p.right = node;
break;
}
}
}
}
}
hasNode(number) {
let n = this.root;
while(n) {
if (n.data === number) {
return true;
} else {
if (n.data > number) {
n = n.left;
} else {
n = n.right;
}
}
}
return false;
}
}
module.exports = Tree;
点击运行。
1: Combine Two Leaves
查看要求:
输入:
class MerkleTree {
constructor(leaves, concat) {
this.leaves = leaves;
this.concat = concat;
}
getRoot() {
return this.concat(this.leaves[0], this.leaves[1]);
}
}
module.exports = MerkleTree;
点击运行。
2: Multiple Layers
查看要求:
输入:
class MerkleTree {
constructor(leaves, concat) {
this.leaves = leaves;
this.concat = concat;
}
getRoot(next = this.leaves) {
while(true) {
if (next.length <= 1) {
return next;
}
let level = [];
for (let i = 0; i < next.length; i+=2) {
level.push(this.concat(next[i], next[i+1]));
}
next = level;
}
}
}
module.exports = MerkleTree;
点击运行。
3: Odd Leaves
查看要求:
输入:
class MerkleTree {
constructor(leaves, concat) {
this.leaves = leaves;
this.concat = concat;
}
getRoot(next = this.leaves) {
while (true) {
if (next.length <= 1) {
return next;
}
let level = [];
for (let i = 0; i < next.length; i += 2) {
if ((i + 1) >= next.length) {
level.push(next[i]);
} else {
level.push(this.concat(next[i], next[i + 1]));
}
}
next = level;
}
}
}
module.exports = MerkleTree;
点击运行。
4: Build the Proof
查看要求:
输入:
class MerkleTree {
constructor(leaves, concat) {
this.leaves = leaves;
this.concat = concat;
}
getRoot(next = this.leaves) {
while (true) {
if (next.length <= 1) {
return next;
}
let level = [];
for (let i = 0; i < next.length; i += 2) {
if ((i + 1) >= next.length) {
level.push(next[i]);
} else {
level.push(this.concat(next[i], next[i + 1]));
}
}
next = level;
}
}
getProof(index, next = this.leaves){
let proof = [];
while (true) {
if (next.length <= 1) {
return proof;
}
let level = [];
for (let i = 0; i < next.length; i += 2) {
if ((i + 1) >= next.length) {
level.push(next[i]);
} else {
level.push(this.concat(next[i], next[i + 1]));
if (index === i || i === (index - 1)) {
if (index === i) {
proof.push({data:next[i + 1], left:false});
} else {
proof.push({data:next[i], left:true});
}
}
}
}
index = Math.floor(index/2);
next = level;
}
}
}
module.exports = MerkleTree;
点击运行。
5: Verify your Proof
查看要求:
输入:
function verifyProof(proof, node, root, concat) {
let n = node;
proof.forEach((e,_) => {
if (e.left) {
n = concat(e.data, n);
} else {
n = concat(n, e.data);
}
});
if (n === root) {
return true;
} else {
return false;
}
}
module.exports = verifyProof;
点击运行。
1: Constructors
查看要求:
这里注意要改两个文件:
class TrieNode {
constructor(key) {
this.key = key;
this.children = {};
this.isWord = false;
}
}
module.exports = TrieNode;
const TrieNode = require('./TrieNode');
class Trie {
constructor() {
this.root = new TrieNode(null);
}
}
module.exports = Trie;
点击运行。
2: Insert
查看要求:
输入:
这里注意改第二个文件:
const TrieNode = require('./TrieNode');
class Trie {
constructor() {
this.root = new TrieNode(null);
}
insert(data) {
let n = this.root;
for (let i = 0; i < data.length; i++) {
let node = new TrieNode(data[i]);
n.children[data[i]] = node;
if ((i + 1) === data.length) {
node.isWord = true;
}
n = node;
}
}
}
module.exports = Trie;
点击运行。
3: Insert Branching
查看要求:
这里还是改这个文件:
输入:
const TrieNode = require('./TrieNode');
class Trie {
constructor() {
this.root = new TrieNode(null);
}
insert(data) {
let n = this.root;
for (let i = 0; i < data.length; i++) {
let node = n.children[data[i]];
if (! node) {
node = new TrieNode(data[i]);
}
n.children[data[i]] = node;
if ((i + 1) === data.length) {
node.isWord = true;
}
n = node;
}
}
}
module.exports = Trie;
点击运行。
4: Contains
查看要求:
还是改这个文件,输入:
const TrieNode = require('./TrieNode');
class Trie {
constructor() {
this.root = new TrieNode(null);
}
insert(data) {
let n = this.root;
for (let i = 0; i < data.length; i++) {
let node = n.children[data[i]];
if (! node) {
node = new TrieNode(data[i]);
}
n.children[data[i]] = node;
if ((i + 1) === data.length) {
node.isWord = true;
}
n = node;
}
}
contains(word) {
let n = this.root.children[word[0]];
if (n === null) {
return false;
}
let i = 1;
while(n) {
if (i >= word.length) {
if (n.isWord) {
return true;
} else {
return false;
}
}
n = n.children[word[i]];
if (n === null) {
return false;
}
i = i + 1;
}
}
}
module.exports = Trie;
点击运行。
以上,Blockchain Storage,课程学习完成。
未完待续……
了解更多,请关注作者:https://twitter.com/bitc2024