Simple question: is there a way to query geth to know if it was launched with --testnet flag or not?
2 Answers
Update Dec 9 2016
Testnet was reset to the Ropsten network - source.
Use the following command to get the hash of the genesis block that will uniquely identify the two separate networks:
Mainnet
> eth.getBlock(0).hash
"0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"
Testnet
> eth.getBlock(0).hash
"0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d"
My New Answer
You can get the genesis block hash without using the admin.nodeInfo call that requires enabling the admin API via RPC, as shown in my old answer.
Use the following command to get the hash of the genesis block that will uniquely identify the two separate networks:
Mainnet
> eth.getBlock(0).hash
"0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"
Testnet
> eth.getBlock(0).hash
"0x0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303"
My Old Answer Below
You can use the admin.nodeInfo information to determine which network you are connected to.
Mainnet
> admin.nodeInfo.protocols.eth.genesis
"0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"
Testnet
> admin.nodeInfo.protocols.eth.genesis
"0x0cd786a2425d16f152c658316c423e6ce1181e15c3295826d7c9904cba9ce303"
While the admin API is available be default over the IPC protocol, you will have to specifically enable the admin API over the RPC protocol if you want to use JSON-RPC to access this information.
Just be careful of the implications of enabling the admin API over the RPC protocol.
To enable the admin API over the RPC protocol:
geth --rpc --rpcaddr localhost --rpcapi "eth,net,web3,admin" console
To test your RPC connection's access to the admin API:
geth attach rpc:http://localhost:8545
> admin.nodeInfo.protocols.eth.genesis
"0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"
- 40,274
- 14
- 123
- 193
-
Does ETH and ETC have the same genesis block? If so, is there a way to distinguish between each chain? – Pabi Nov 11 '16 at 21:30
-
1ETH and ETC have the same data up until block 1,919,999. ETH has a "dao-hard-fork" marker in block 1,920,000 that ETC does not have. See http://ethereum.stackexchange.com/questions/7832/give-a-summary-of-the-fork-state-changes-in-block-1920000/7840#7840 – BokkyPooBah Nov 11 '16 at 23:15
-
1This should probably be updated to note that on the new testnet (Ropsten), the genesis block hash is '0x41941023680923e0fe4d74a34bdac8141f2540e3ae90623718e47d66d1ca4a2d' – Thomas Clowes Dec 08 '16 at 15:19
-
Can you expand just a little bit on this: "Just be careful of the implications of enabling the admin API over the RPC protocol." Or perhaps point to further information? Is this a problem if one leaves the port at its default? – Thomas Jay Rush Dec 08 '16 at 18:50
There's no easy way to do this with the JSON RPC APIs, unfortunately. One somewhat hacky option, however, is to call eth_getTransactionCount on an existing account; this will return a value <2^20 on mainnet, and >2^20 on testnet.
- 8,144
- 1
- 28
- 35