在网络上实施这个 Node.js 区块链
Implementing this Node.js blockhain on a network
我正在查看来自 firebase 的这个区块链。 https://github.com/fireship-io/node-blockchain/blob/main/index.ts
区块链非常简单。有许多类似的区块链实施示例,但我并没有真正看到任何在网络中实际使用的示例。我正在努力为实现这一目标奠定基础,至少要从 2 个用户开始。
目前我感到困惑的是用户之间实际共享的是什么?是链条本身吗?只是新的交易?当用户 Wallet.sendMoney(5, satoshi.publicKey)
时,它会更新本地钱包,然后呢?我猜是将交易发送给网络上的其他人,但随后区块链的每个副本 adds/verifies 独立。这似乎有问题,因为一些交易可能会丢失(互联网中断或其他),这让我想知道是否发送了整个区块链,但这似乎很笨拙。
The thing I'm confused about at the moment is what actually gets
shared between users?
您指的是 nodes
之间,而不是“用户”。所有节点都应该有相同的链和交易。所以你必须实现一个 pub-sub 系统来监听某些事件并发布交易和链。使用redis,你可以创建一个class。我在代码上解释了:
// Note that redis stores strings.
const redis = require("redis");
// create two channels to transfer data
const CHANNELS = {
BLOCKCHAIN: "BLOCKCHAIN",
TRANSACTION: "TRANSACTION",
};
// unlike socket, we do not need to know the address of otehr nodes
// ---------------------------HERE IS BROADCASTING STATION---------------------
// it writes multiple processes to communicate over channels.
class PubSub {
constructor({ blockchain, transactionPool }) {
// it is able to broadcast its chain and replacing the valid chain
this.blockchain = blockchain;
this.transactionPool = transactionPool;
this.publisher = redis.createClient();
this.subscriber = redis.createClient();
this.subscribeToChannels();
// this.subscriber.on("message", (channel, message) => {
// return this.handleMessage(channel, message);
// });
this.subscriber.on("message", (channel, message) =>
this.handleMessage(channel, message)
);
}
// we are listening all channels
subscribeToChannels() {
Object.values(CHANNELS).forEach((channel) => {
this.subscriber.subscribe(channel);
});
}
publish({ channel, message }) {
//we unsubscrive so we dont send message to ourselves
// we subscribe again to receive messages
this.subscriber.unsubscribe(channel, () => {
this.publisher.publish(channel, message, () => {
this.subscriber.subscribe(channel);
});
});
}
// ------THIS IS WHERE BROADCASTING DONE-------------
handleMessage(channel, message) {
const parsedMessage = JSON.parse(message);
switch (channel) {
case CHANNELS.BLOCKCHAIN:
this.blockchain.replaceChain(parsedMessage, true, () => {
// we need to clear local transaction pool becasue we got a new chain
this.transactionPool.clearBlockchainTransactions({
chain: parsedMessage,
});
});
break;
case CHANNELS.TRANSACTION:
console.log("this in pubsusb", this.transactionPool);
this.transactionPool.setTransaction(parsedMessage);
break;
default:
return;
}
}
broadcastChain() {
this.publish({
channel: CHANNELS.BLOCKCHAIN,
message: JSON.stringify(this.blockchain.chain),
});
}
broadcastTransaction(transaction) {
this.publish({
channel: CHANNELS.TRANSACTION,
message: JSON.stringify(transaction),
});
}
}
export default PubSub;
我正在查看来自 firebase 的这个区块链。 https://github.com/fireship-io/node-blockchain/blob/main/index.ts
区块链非常简单。有许多类似的区块链实施示例,但我并没有真正看到任何在网络中实际使用的示例。我正在努力为实现这一目标奠定基础,至少要从 2 个用户开始。
目前我感到困惑的是用户之间实际共享的是什么?是链条本身吗?只是新的交易?当用户 Wallet.sendMoney(5, satoshi.publicKey)
时,它会更新本地钱包,然后呢?我猜是将交易发送给网络上的其他人,但随后区块链的每个副本 adds/verifies 独立。这似乎有问题,因为一些交易可能会丢失(互联网中断或其他),这让我想知道是否发送了整个区块链,但这似乎很笨拙。
The thing I'm confused about at the moment is what actually gets shared between users?
您指的是 nodes
之间,而不是“用户”。所有节点都应该有相同的链和交易。所以你必须实现一个 pub-sub 系统来监听某些事件并发布交易和链。使用redis,你可以创建一个class。我在代码上解释了:
// Note that redis stores strings.
const redis = require("redis");
// create two channels to transfer data
const CHANNELS = {
BLOCKCHAIN: "BLOCKCHAIN",
TRANSACTION: "TRANSACTION",
};
// unlike socket, we do not need to know the address of otehr nodes
// ---------------------------HERE IS BROADCASTING STATION---------------------
// it writes multiple processes to communicate over channels.
class PubSub {
constructor({ blockchain, transactionPool }) {
// it is able to broadcast its chain and replacing the valid chain
this.blockchain = blockchain;
this.transactionPool = transactionPool;
this.publisher = redis.createClient();
this.subscriber = redis.createClient();
this.subscribeToChannels();
// this.subscriber.on("message", (channel, message) => {
// return this.handleMessage(channel, message);
// });
this.subscriber.on("message", (channel, message) =>
this.handleMessage(channel, message)
);
}
// we are listening all channels
subscribeToChannels() {
Object.values(CHANNELS).forEach((channel) => {
this.subscriber.subscribe(channel);
});
}
publish({ channel, message }) {
//we unsubscrive so we dont send message to ourselves
// we subscribe again to receive messages
this.subscriber.unsubscribe(channel, () => {
this.publisher.publish(channel, message, () => {
this.subscriber.subscribe(channel);
});
});
}
// ------THIS IS WHERE BROADCASTING DONE-------------
handleMessage(channel, message) {
const parsedMessage = JSON.parse(message);
switch (channel) {
case CHANNELS.BLOCKCHAIN:
this.blockchain.replaceChain(parsedMessage, true, () => {
// we need to clear local transaction pool becasue we got a new chain
this.transactionPool.clearBlockchainTransactions({
chain: parsedMessage,
});
});
break;
case CHANNELS.TRANSACTION:
console.log("this in pubsusb", this.transactionPool);
this.transactionPool.setTransaction(parsedMessage);
break;
default:
return;
}
}
broadcastChain() {
this.publish({
channel: CHANNELS.BLOCKCHAIN,
message: JSON.stringify(this.blockchain.chain),
});
}
broadcastTransaction(transaction) {
this.publish({
channel: CHANNELS.TRANSACTION,
message: JSON.stringify(transaction),
});
}
}
export default PubSub;