Fix off_chain_data to put data to off-chain database

The off_chain_data sample fails to put data to the off-chain
database. The application does not read fetched blocks because
it uses the old interface of `addBlockListener()` to handle
block events.

This PR fixes the application to use the latest block listener
and build the off-chain database.

Signed-off-by: Yuki Kondo <yuki.kondo.ob@hitachi.com>
This commit is contained in:
Yuki Kondo 2021-07-02 04:57:24 +00:00
parent 5e933c10cb
commit 98c5b3858d

View file

@ -116,20 +116,13 @@ async function main() {
// Get the network (channel) our contract is deployed to.
const network = await gateway.getNetwork('mychannel');
const listener = await network.addBlockListener(
async (err, blockNum, block) => {
if (err) {
console.error(err);
return;
}
const listener = async (event) => {
// Add the block to the processing map by block number
await ProcessingMap.set(block.header.number, block);
console.log(`Added block ${blockNum} to ProcessingMap`)
},
// set the starting block for the listener
{ filtered: false, startBlock: parseInt(nextBlock, 10) }
);
await ProcessingMap.set(event.blockNumber, event.blockData);
console.log(`Added block ${event.blockNumber} to ProcessingMap`);
};
const options = { filtered: false, startBlock: parseInt(nextBlock, 10) };
await network.addBlockListener(listener, options);
console.log(`Listening for block events, nextblock: ${nextBlock}`);