如何通过链码将文档批量插入/写入沙发床?似乎chaincode填充程序库(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”中,我们可以构造批量获取请求。
但是对于插入/写入文档,链码是否有大量API?预先感谢。
答案 0 :(得分:4)
当执行链码时,每个PutState()都会向事务的建议写集添加一个键/值。您可以在链码中多次调用PutState(),最后一组键/值将出现在事务的建议写入集中。请注意,链代码执行时不会将任何内容写入CouchDB。
仅当提交交易以进行订购时,交易才会出现在所有同行都处理过的区块中。每个对等方都验证事务,然后将块中所有有效事务的写集应用于CouchDB状态数据库。请注意,状态数据库提交确实使用CouchDB批量更新API(HTTP _bulk_docs),因此您可以在CouchDB中自动获得所需的批量更新性能。
如果该块中有许多键/值更新,则在提交给CouchDB时,Fabric实际上会将它们分为1000个批次(可使用core.yaml maxBatchUpdateSize属性配置),以避免有效载荷过大的任何问题。最后,为确保所有写入均以原子提交的形式出现在Fabric中,Fabric将每个块的最终保存点写入CouchDB状态数据库,并将CouchDB数据刷新到磁盘。这样可以确保任何链码执行都能获得状态数据的一致视图,并确保Fabric可以从任何对等崩溃中恢复并具有完整的数据完整性。
答案 1 :(得分:0)
如果您要批量处理多个事务,则不建议这样做,因为如果它们正在更新相同的值,那么在查看历史记录时,只能跟踪最终的更改。因此,最好提交单个交易。
答案 2 :(得分:0)
不确定是不是回答了您的问题,而是根据文档,假设您的意思是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
哈希以在以后阶段交叉检查文档的真实性。 / p>