The main endpoint of a Cosmos SDK application is the daemon client, otherwise known as the full-node client. The full-node runs the state-machine, starting from a genesis file. It connects to peers running the same client in order to receive and relay transactions, block proposals and signatures. The full-node is constituted of the application, defined with the Cosmos SDK, and of a consensus engine connected to the application via the ABCI.
Pre-requisite Readings
main
function
The full-node client of any Cosmos SDK application is built by running a main
function. The client is generally named by appending the -d
suffix to the application name (e.g. appd
for an application named app
), and the main
function is defined in a ./appd/cmd/main.go
file. Running this function creates an executable appd
that comes with a set of commands. For an app named app
, the main command is appd start
, which starts the full-node.
In general, developers will implement the main.go
function with the following structure:
- First, an
encodingCodec
is instantiated for the application. - Then, the
config
is retrieved and config parameters are set. This mainly involves setting the Bech32 prefixes for addresses.
- Using cobra, the root command of the full-node client is created. After that, all the custom commands of the application are added using the
AddCommand()
method ofrootCmd
. - Add default server commands to
rootCmd
using theserver.AddCommands()
method. These commands are separated from the ones added above since they are standard and defined at Cosmos SDK level. They should be shared by all Cosmos SDK-based applications. They include the most important command: thestart
command. - Prepare and execute the
executor
.
main
function from the simapp
application, the Cosmos SDK’s application for demo purposes:
simapp/simd/main.go
start
command
The start
command is defined in the /server
folder of the Cosmos SDK. It is added to the root command of the full-node client in the main
function and called by the end-user to start their node:
start
command is pretty straightforward. First, it retrieves the config
from the context
in order to open the db
(a leveldb
instance by default). This db
contains the latest known state of the application (empty if the application is started from the first time.
With the db
, the start
command creates a new instance of the application using an appCreator
function:
server/start.go
appCreator
is a function that fulfills the AppCreator
signature:
server/types/app.go
appCreator
.
simapp/simd/cmd/root.go
app
is used to instantiate a new CometBFT node:
server/start.go
app
because the latter satisfies the abci.Application
interface (given that app
extends baseapp
). As part of the node.New
method, CometBFT makes sure that the height of the application (i.e. number of blocks since genesis) is equal to the height of the CometBFT node. The difference between these two heights should always be negative or null. If it is strictly negative, node.New
will replay blocks until the height of the application reaches the height of the CometBFT node. Finally, if the height of the application is 0
, the CometBFT node will call InitChain
on the application to initialize the state from the genesis file.
Once the CometBFT node is instantiated and in sync with the application, the node can be started:
server/start.go