Synchronizing your local database with the data on the chain
Every time a dApp needs to fetch the data stored on the chain (i.e., transaction records, etc.), it can do so by querying the public Explorer API. But this would only be feasible for the dApps that have a low query rate, or execute queries at a lower frequency.
dApps with higher query requirements both in terms of number and frequency cannot work at optimum efficiency when using the Explorer interface. Therefore, to target this specific issue, Ontology introduced a new method which allows the application's local database to synchronize its data with the chain.
Synchronizing the local database with the chain is an option, and not a requirement for dApp development. Developers are advised to analyze the specific requirements for their respective applications to make this judgement.
Connecting to Ontology's Nodes
Connecting to Ontology's nodes is the first step of data synchronization process. This can be performed in two different ways-
1. Connecting to Ontology's Consensus nodes
Generally speaking, personally deploying and running a node is a very tedious task for developers. And that is why Ontology made Polaris test net and main net nodes available for developers to employ. It supports RPC, Restful, and WebSocket invocation, and use default ports.
10334 port of the first nodes of the Polaris test net net and the main net support HTTPS
2. Running your own node
Depending on the architecture of the dApp the developer may choose to personally run a synchronization node. For more details and a quick walkthrough, refer to -
Running the Synchronization Sequence
What synchronization basically does is all the data that is stored on the chain, say all the block, transaction and contract event related data, is imported to a local database that the dApp has direct access to, thereby increasing the efficiency with which the app can query it.
Ontology's Explorer is a model synchronization program. The user may selectively synchronize the data that serves their purpose. For e.g., data related to the events pertaining to a smart contract that you deployed.
1. Synchronizing with all the blocks on the chain
There are dApps that needs to synchronize the information from all the blocks on the chain, a typical case of which would be Ontology's Explorer. Developers developing dApps with needs analogous to those of Explorer may find this helpful.
On the basis of the height of the given block, the getblk_by_height interface returns the following JSON response.
Most dApps only need to synchronize the events that are generated by their own contracts. This suffices for any functionalities that dApp may be implementing, and so there is no need to fetch and store all the data stored in all the different blocks.
The code to synchronize the data will always be application specific, because it would also have to adhere to the logic of the application. But here's an example of how a contract specific synchronization method would look like:
The code for the data synchronization is application specific, because it needs to adhere to the logic of the application.
The developer defines the argument set for Notify
Notify(["param1", "param2", "param3"])
When querying a smart contract event using the block height, the reply that the getSmartCodeEvent interface sends would look like:
The composition of the ExecuteNotify data structure is
typeExecuteNotifystruct { TxHash common.Uint256//Transcation hash State byte//1 signifies that the transaction was successful, 0 signifies failure GasConsumed uint64 Notify []*NotifyEventInfo}
The composition of NotifyEventInfo data structure is
typeNotifyEventInfostruct { ContractAddress common.Address//Address of the smart contract States interface{} //The message to be notified}
The method that listens for special or specific contract events could look something like:
publicvoidrun() {try{while (true) {//Fetch the current block heightint remoteBlockHieght =getRemoteBlockHeight();logger.info("######remote blockheight:{}", remoteBlockHieght);//Find out the height of synchronized blocks in the databaseint dbBlockHeight =blkHeightMapper.selectDBHeight();logger.info("######db blockheight:{}", dbBlockHeight); dbBlockHeight = dbBlockHeight +1;//If the synchronized block height exceeds, or is equal to that of//the newest block added, wait for the next block to be created, then syncif (dbBlockHeight >= remoteBlockHieght) {//TODO }//For each block, determine all the corresponding events, an event is a JSONArray object//The data type of every element is ExecuteNotifyObject event =sdk.getConnect().getSmartCodeEvent(dbBlockHeight);if (event !=null) {for(Object obj : (JSONArray)event){//Filter successful transactionsif (obj.get("State") ==1) {for(Object notify:obj.get("Notify")) {//Filter the events that we were listening forif(notify.getString("ContractAddress") == contractAddress) {//TODO } } } } }//Update the block height in the databaseblkHeightMapper.update(dbBlockHeight); } }catch (Exception e) {logger.error("Exception occured,Synchronization thread can't work, error ...", e); }}