【问题标题】:Blockchain testing on local host在本地主机上进行区块链测试
【发布时间】:2020-08-19 16:41:39
【问题描述】:

大家,我是一名正在尝试学习一些区块链的高中生。这是我第一次使用 StackOverflow。当我尝试使用终端和 nodemon 托管时,终端会说 “错误 [ERR_PACKAGE_PATH_NOT_EXPORTED]:包子路径 './v1' 未由 /Users/coding/Desktop/blockchain/node_modules/uuid/package.json 中的“exports”定义”,我不知道该怎么办。它说“v1”不是由出口定义的,我不明白。并且 nodemon 说 [nodemon] 应用程序崩溃了 - 在开始之前等待文件更改...... 这是整个项目的代码。

const express = require('express');
const app = express();
const bodyParser =require('body-parser');
const Blockchain = require('./blockchain');
const uuid = require('uuid/v1');

const nodeAddress = uuid().slipt('-').join(''); 

const bitcoin = new Blockchain();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false}));

app.get('/blockchain', function (req, res) {
    res.send(bitcoin);

});


app.post('/transaction', function(req, res){
    const blockIndex = bitcoin.createNewTransaction(req.body.amount, req.body.sender, req.body.recipient);
    res.json({ note: `Transaction will be added in block ${blockIndex}.`});

});


app.get('/mine', function(req, res){
    const lastBlock = bitcoin.getLastBlock();
    const previousBlockHash = lastBlock['hash'];
    const currentBlockData = {
        transaction: bitcoin.pendingTransactions,
        index: lastBlock['index'] + 1,
    };

    const nonce = bitcoin.proofOfWork(previousBlockHash, currentBlockData);
    const blockHash = bitcoin.hashBlock(previousBlockHash,currentBlockData,nonce);

    bitcoin.createNewTransaction(12.5, "00", nodeAddress );


    const newBlock = bitcoin.createNewBlock(nonce, previousBlockHash, blockHash);
    res.json({
        note: "new block mined successfully",
        block: newBlock,
    });
});


app.listen(3000, function(){
    console.log("listening on port 3000....")
})

这是创建区块链的页面

const sha256 = require('sha256');

function Blockchain() {
    this.chain = [];
    this.pendingTransactions = [];
    this.createNewBlock(100, '0', '0');
}

Blockchain.prototype.createNewBlock = function(nonce, previousblockHash, hash){
    const newBlock = {
        index: this.chain.length + 1,
        timestamp: Date.now(),
        transactions: this.pendingTransactions,
        nonce: nonce,
        hash: hash,
        previousblockHash: previousblockHash,
    };
    this.pendingTransactions = [];
    this.chain.push(newBlock);

    return newBlock;
}

Blockchain.prototype.getLastBlock = function() {
    return this.chain[this.chain.length - 1];
}

Blockchain.prototype.createNewTransaction = function( amount, sender,recipient ) {
    const newTransactions = {
        amount: amount,
        sender: sender,
        recipient: recipient,

    };
    this.pendingTransactions.push(newTransactions);

    return this.getLastBlock()['index'] + 1;

}
Blockchain.prototype.hashBlock = function( previousBlockHash, currentBlockData,nonce ){
    const dataAsString = previousBlockHash + nonce.toString() + JSON.stringify(currentBlockData);
    const hash  = sha256(dataAsString);
    return hash;

}
Blockchain.prototype.proofOfWork = function(previousBlockHash, currentBlockData){
    let nonce = 0;
    let hash = this.hashBlock(previousBlockHash,currentBlockData,nonce);
    while (hash.substring(0,4) !== '0000') {
        nonce++;
        hash = this.hashBlock(previousBlockHash,currentBlockData, nonce);

    }
    return nonce;
} 


module.exports = Blockchain;

这是 json 包

{
  "name": "blockchain",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon --watch dev -e js dev/api.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "nodemon": "^2.0.3",
    "sha256": "^0.2.0",
    "uuid": "^8.0.0"
  }
}

【问题讨论】:

  • 除 uuid 外一切正常

标签: javascript node.js express


【解决方案1】:

我今天实际上遇到了这个问题,我看到另一个问题here 有一个可能的解决方案(尝试不同版本的节点)。我认为您还想将您的 require() 更改为 uuid:
const { v1: uuidv1 } = require('uuid');
更新: 刚刚在 Node v14.1.0 上进行了测试,我没有收到错误消息。我之前在 v13.1.0 上。

【讨论】:

  • nodeAddress 放什么
猜你喜欢
  • 2015-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-14
  • 1970-01-01
  • 1970-01-01
  • 2012-03-22
相关资源
最近更新 更多