【发布时间】:2021-08-26 22:21:15
【问题描述】:
首先让我先说我是一名网络开发人员,对区块链领域非常陌生,所以如果我遗漏了一些明显的东西,我提前道歉。话虽如此,我在将广播交易方法实施到我正在从事的加密货币项目中时遇到了问题。每次我能够成功地向我的 API 发出事务请求并且我能够在主开发网络上看到正确的事务池时,但是我的对等网络在我重新启动它之前看不到任何新事务。我知道这意味着我的广播事务实现缺少一些东西,但我不确定我需要修复它。
请参考以下图片
这是我使用 PUBNUB 实现 pubsub 的代码 sn-ps
const CHANNELS = {
TEST: "TEST",
BLOCKCHAIN: "BLOCKCHAIN",
TRANSACTION: "TRANSACTION"
};
class PubSub {
constructor({blockchain, transactionPool}) {
this.blockchain = blockchain;
this.transactionPool = transactionPool;
this.pubnub = new PubNub(credentials); // defined above but omitted for obvious reasons
this.pubnub.subscribe({channels: Object.values(CHANNELS)}); // defined in channels object
this.pubnub.addListener(this.listener());
};
listener() {
return {
message: messageObject => {
const { channel, message } = messageObject;
console.log(`Message received. Channel: ${channel}. Message: ${message}`);
const parsedMessage = JSON.parse(message);
switch(channel) {
case CHANNELS.BLOCKCHAIN:
this.blockchain.replaceChain(parsedMessage);
break;
case CHANNELS.TRANSACTION:
this.transactionPool.setTransaction(parsedMessage);
break;
default:
return;
}
}
};
}
publish({channel, message}) {
this.pubnub.unsubscribe(channel, () => {
this.pubnub.publish(channel, message, () => {
this.pubnub.subscribe(channel);
});
});
}
broadcastChain() {
this.publish({
channel: CHANNELS.BLOCKCHAIN,
message: JSON.stringify(this.blockchain.chain)
})
}
broadcastTransaction(transaction) {
this.publish({
channel: CHANNELS.TRANSACTION,
message: JSON.stringify(transaction)
});
}
};
这里是调用broadcastTransaction方法的sn-ps
const pubsub = new PubSub({blockchain, transactionPool});
let transaction = transactionPool.existingTransaction({inputAddress: wallet.publicKey}); // Creates global binding
// Sends transaction to the network
app.post("/api/transact", (req, res) => {
const {amount, recipient} = req.body;
try {
if(transaction) {
transaction.update({senderWallet: wallet, recipient, amount });
} else {
transaction = wallet.createTransaction({recipient, amount});
}
} catch(error) {
return res.status(400).json({type: "error", message: error.message});
};
transactionPool.setTransaction(transaction);
pubsub.broadcastTransaction(transaction); // Calls broadcastTransaction from pubsub class (does not work for peers)
res.json({type: "success", transaction});
});
我试图尽可能具体,但如果我遗漏了什么,请告诉我。提前谢谢!
【问题讨论】:
-
如果能以文本形式从屏幕截图中获取一些数据,您能解释一下您正在观察什么,而不是在观察(但期望观察)当您说:“每次我能够成功地向我的 API 发出事务请求,并且我能够在主开发网络上看到正确的事务池,但是我的对等网络在我重新启动之前看不到任何新事务。”?
-
您还可以将
logVerbosity: true添加到您的new PubNub以显示来自 PN SDK 的详细日志。这样做并发布该文本文件。 -
制作日志有进展吗?
标签: javascript api publish-subscribe pubnub cryptocurrency