0

I use smart contracts in Ethereum to store some string data, how to synchronize these stored string data to other nodes. As far as I know, these string data are stored in the node's leveldb database, then how do other nodes synchronize this leveldb database. For example, how to synchronize the string data stored in node a to node b

0

The blockchain is formed by the nodes - without the nodes there is no blockchain. So whatever data is in the blockchain has to exist in the nodes. Furthermore, all (full) nodes have the same data as long as they're fully synchronized with the blockchain.

So if you send a transaction to a contract and the transaction gets mined succesfully by some node the data is broadcast to the entire network and eventually all nodes will receive it and store it locally. So you don't need to do anything specific to synchronize data - synchronizing the blockchain actually means the same as synchronizing the data.

You should not consider a node as "running a smart contract" as all (full) nodes have the same contracts and execute all transactions. So individual nodes are rather meaningless.

Based on your example in comments: if node A broadcasts a transaction which creates a new smart contract (and the transaction gets mined) all nodes import the same transaction and therefore have the same contract stored locally. Storing data in the contract is also done in a transaction and all nodes get this data. Therefore node B can simply read its local state to see the contract's data. If the contract has a constant function for reading the data it gets even easier; you can use web3 for it: https://web3js.readthedocs.io/en/v1.2.7/web3-eth-contract.html#methods-mymethod-call

P.S. If you still have problems feel free to contact me via Telegram, can be found through my profile

| improve this answer | |
  • For example, in the code of a smart contract deployed on a node, I use a mapping to store some data. Can another node synchronize to this mapping, or another node can access this mapping on its own local node.I don't know how to access this mapping on another node now. Thank you so much ! – wei wang Jun 2 at 12:52
  • Yes, all the data is available locally for reading (read-only local operations are called "calls"). For modifying the data you have to send a real transaction to the blockchain so all nodes get the new data. – Lauri Peltonen Jun 2 at 12:57
  • Can you be more specific, for example, node A runs a smart contract and stores string data. After all the nodes are synchronized, now how does node B extract this string data from its own node. But I searched the Internet for a long time, and I didn't find a solution. I hope you can give a specific method or what tool to use. Thank you very much – wei wang Jun 3 at 2:07
  • Edited my answer, added more details – Lauri Peltonen Jun 3 at 5:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.