How to wait programatically until block is added in the network?

+2 votes

Hi,

Currently, I am working on streams. I have used the following code to publish data in the stream using node.js. 

packet = new Buffer(data[0]).toString("hex")

mc.publish({ stream: "datastream", key: "through", data: packet }).then(x => {

sucess++;

//total_size += passedSize;

callback(null, sucess);

 

}).catch(y => {

//sucess++;

//total_size += passedSize;

callback(null, sucess);

})

Here, I want to find the latency of adding data in the block for publishing data in the stream. But I think in this code, the callback is returned when data is added as an unspent transaction. But I want to wait till it added in the block. How to do that? any help? 

here is the parameter: 

chain-protocol = multichain           
chain-description = MultiChain buchain  
root-stream-name = root               
root-stream-open = true               
chain-is-testnet = false               
target-block-time = 2                 
maximum-block-size = 1000000000         

# Global permissions

anyone-can-connect = false              
anyone-can-send = false                 
anyone-can-receive = false             
anyone-can-receive-empty = true        
anyone-can-create = false             
anyone-can-issue = false              
anyone-can-mine = false                
anyone-can-activate = false             
anyone-can-admin = false                
support-miner-precheck = true          
allow-arbitrary-outputs = false      
allow-p2sh-outputs = true              
allow-multisig-outputs = true          

setup-first-blocks = 60                 
mining-diversity = 0.3                  
admin-consensus-upgrade = 0.5          
admin-consensus-admin = 0.5             
admin-consensus-activate = 0.5         
admin-consensus-mine = 0.5              
admin-consensus-create = 0.0           
admin-consensus-issue = 0.0           

lock-admin-mine-rounds = 10           
mining-requires-peers = true            
mine-empty-rounds = 10                  
mining-turnover = 0.5                 

initial-block-reward = 0               
first-block-reward = -1                 
reward-halving-interval = 52560000      
reward-spendable-delay = 1              
minimum-per-output = 0                  
maximum-per-output = 100000000000000   
minimum-relay-fee = 0                   
native-currency-multiple = 100000000   

skip-pow-check = false                
pow-minimum-bits = 1                    
target-adjust-freq = -1                 
allow-min-difficulty-blocks = false   

only-accept-std-txs = true              
max-std-tx-size = 100000000             
max-std-op-returns-count = 1024         
max-std-op-return-size = 2097152        
max-std-op-drops-count = 5              
max-std-element-size = 32768          

Thanks in advance.

asked May 11, 2020 by aiubian

1 Answer

0 votes

You can use the blocknotify runtime parameter to trigger an external command when a new block comes in:

https://www.multichain.com/developers/runtime-parameters/

This can then query to check if the stream items were confirmed.

answered May 14, 2020 by MultiChain
Thanks for your comment. I want to check within the same code block of submitting stream data instead of triggering another script. Is there any way to do that programmatically?
The item will not be confirmed immediately, there will likely be a delay of a few seconds. But if you want you can use the script to keep checking, say once per second, until the item is confirmed, in a polling kind of pattern.
Thanks for your suggestions. I am trying to use getWalletTransaction and check confirmation > 0 property to confirm that transaction is added in the block or not. Is it the right way?
Yes, it's one way to do it. If the node is subscribed to the stream you can also use the regular stream querying commands.
Thanks for your help.
...