如何通过链码批量插入文档到couchdb

How to insert documents to couchdb in bulk by chaincode

如何通过链码insert/write将文件批量上传到couchdb?似乎链码垫片库(https://godoc.org/github.com/hyperledger/fabric/core/chaincode/shim)没有这样的 API.

阅读文档,好像有个叫"GetQueryResult"(https://godoc.org/github.com/hyperledger/fabric/core/chaincode/shim#ChaincodeStub.GetQueryResult)的API。在字符串参数"query"中,我们可以构造bulk get request.

但是对于 inserting/writing 文档,是否有用于链码的批量 API?提前致谢。

如果您的意思是要将多个交易分批处理,那么不建议这样做,因为如果它们正在更新相同的值,那么在查看历史记录时,只能跟踪最终的更改。所以,还是提交个人交易比较好。

当链代码执行时,每个 PutState() 都会向事务的建议写入集添加一个 key/value。您可以在链代码中多次调用 PutState(),最终 keys/values 的集合将出现在事务的建议写入集中。请注意,在链代码执行时没有任何内容写入 CouchDB。

只有当交易被提交排序时,交易才会出现在一个被所有节点处理的区块中。每个节点验证交易,然后将块中所有有效交易的写入集应用到 CouchDB 状态数据库。请注意,状态数据库提交确实使用了 CouchDB 批量更新 API (HTTP _bulk_docs),因此您会自动在 CouchDB 中获得所需的批量更新性能。

如果块中有许多 key/value 更新,Fabric 实际上会将它们分组为 1000 个批次(可使用 core.yaml maxBatchUpdateSize 属性 配置)提交到 CouchDB 以避免任何负载过大的问题。最后,为了确保所有写入对 Fabric 来说都是原子提交,Fabric 将每个块的最终保存点写入 CouchDB 状态数据库,并将 CouchDB 数据刷新到磁盘。这确保任何链代码执行都获得一致的状态数据视图,并确保 Fabric 可以从任何对等崩溃中恢复并保持完整的数据完整性。

不确定这是否回答了您的问题,但对于文档,我们假设您指的是 PDF 文件。 这有两个功能,读取一个键值对和写入一个。这里没什么特别的。

'use strict';
const { Contract } = require('fabric-contract-api');

Class SomeName extends Contract {

    async initLedger(ctx) { }

    async writeDocument(ctx, documentId, documentAsString) {
        await ctx.stub.putState(documentId, Buffer.from(documentAsString));
    };

    async getDocument(ctx, documentId) {
        const docAsBytes = await ctx.stub.getState(documentId);
        return carAsBytes.toString();
    };
}

现在是服务器端代码

const { FileSystemWallet, Gateway } = require('fabric-network');

async main() {
    const walletPath = path.join(process.cwd(), 'wallet');
    const wallet = new FileSystemWallet(walletPath); // can be wherever your path is

    const gateway = new Gateway();
    await gateway.connect(ccp, { wallet, identity: 'user1', discovery: { enabled: false }  });
    const network = await gateway.getNetwork('mychannel');
    // Get the contract from the network.
    const contract = network.getContract('SomeName');

    // Now over here lets assume you get the PDF file from a POST request using Multer
    // We get the PDF file as a buffer, then convert to a string and send it
    let pdfFile = req.file;

    await contract.submitTransaction('writeDocument', req.file.buffer.toString('utf-8);
}

如果您想检索 PDF 文件

const getDocument = async (req, res, next) => {
    const walletPath = path.join(process.cwd(), 'wallet');
    const wallet = new FileSystemWallet(walletPath); // can be wherever your path is

    const gateway = new Gateway();
    await gateway.connect(ccp, { wallet, identity: 'user1', discovery: { enabled: false }  });
    const network = await gateway.getNetwork('mychannel');
    // Get the contract from the network.
    const contract = network.getContract('SomeName');
    const pdfAsString= await contract.evaluateTransaction('getDocument', req.body.documentId);
    // Now do whatever with this
};

所以我希望这能回答您关于文件上传的问题,据说在区块链上上传文件不是一个好习惯,因为它会显着降低交易速度。 在这种情况下,您可以将文档存储在本地服务器上,例如 mongoDB,然后在区块链上引用文档的 sha256 哈希,以便在稍后阶段交叉检查文档的真实性。