Skip to content

Commit

Permalink
Merge pull request #40 from informalsystems/ph/fix-blocktime
Browse files Browse the repository at this point in the history
Fix behaviour of blocktime
  • Loading branch information
p-offtermatt authored Aug 21, 2023
2 parents 7a66319 + 51b562e commit 7d2bee3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ CometMock was tested with `go version go1.20.3 darwin/arm64`.
To run CometMock, start your (cosmos-sdk) application instances with the flags ```--with-tendermint=false, --transport=grpc```.
After the applications started, start CometMock like this
```
cometmock {app_address1,app_address2,...} {genesis_file} {cometmock_listen_address} {home_folder1,home_folder2,...} {connection_mode} [--block-time=XXX]
cometmock [--block-time=XXX] {app_address1,app_address2,...} {genesis_file} {cometmock_listen_address} {home_folder1,home_folder2,...} {connection_mode}
```

where:
* The `--block-time` flag is optional and specifies the time in milliseconds between blocks. The default is 1000ms(=1s). Values <= 0 mean that automatic block production is disabled. In this case, blocks can be produced by calling the `advance_blocks` endpoint or by broadcasting transactions (each transaction will be included in a fresh block). Note that all flags have to come before positional arguments.
* The `app_addresses` are the `--address` flags of the applications. This is by default `"tcp://0.0.0.0:26658"`
* The `genesis_file` is the genesis json that is also used by apps.
* The `cometmock_listen_address` can be freely chosen and will be the address that requests that would normally go to CometBFT rpc endpoints need to be directed to.
* The `home_folders` are the home folders of the applications, in the same order as the `app_addresses`. This is required to use the private keys in the application folders to sign as appropriate validators.
* Connection mode is the protocol over which CometMock should connect to the ABCI application, either `grpc` or `socket`. See the `--transport` flag for Cosmos SDK applications. For SDK applications, just make sure `--transport` and this argument match, i.e. either both `socket` or both `grpc`.
* The `--block-time` flag is optional and specifies the time in milliseconds between blocks. The default is 1000ms(=1s). Values <= 0 mean that automatic block production is disabled. In this case, blocks can be produced by calling the `advance_blocks` endpoint or by broadcasting transactions (each transaction will be included in a fresh block).

When calling the cosmos sdk cli, use as node address the `cometmock_listen_address`,
e.g. `simd q bank total --node {cometmock_listen_address}`.
Expand Down
25 changes: 15 additions & 10 deletions cometmock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,13 @@ func GetMockPVsFromNodeHomes(nodeHomes []string) []types.PrivValidator {
func main() {
logger := cometlog.NewTMLogger(cometlog.NewSyncWriter(os.Stdout))

argumentString := "<app-addresses> <genesis-file> <cometmock-listen-address> <node-homes> <abci-connection-mode> [--block-time=value]"
argumentString := "[--block-time=value] <app-addresses> <genesis-file> <cometmock-listen-address> <node-homes> <abci-connection-mode>"

app := &cli.App{
Name: "cometmock",
HideHelpCommand: true,
Usage: "Your application description",
Flags: []cli.Flag{
&cli.IntFlag{
&cli.Int64Flag{
Name: "block-time",
Usage: `
Time between blocks in milliseconds.
Expand Down Expand Up @@ -166,15 +165,21 @@ advancing blocks or broadcasting transactions.`,

go rpc_server.StartRPCServerWithDefaultConfig(cometMockListenAddress, logger)

// produce a block every second
for {
_, _, _, _, _, err := abci_client.GlobalClient.RunBlock(nil)
if err != nil {
logger.Error(err.Error())
panic(err)
if blockTime > 0 {
// produce blocks according to blockTime
for {
_, _, _, _, _, err := abci_client.GlobalClient.RunBlock(nil)
if err != nil {
logger.Error(err.Error())
panic(err)
}
time.Sleep(time.Millisecond * time.Duration(blockTime))
}
time.Sleep(1 * time.Second)
} else {
// wait forever
time.Sleep(time.Hour * 24 * 365 * 100) // 100 years
}
return nil
},
}

Expand Down

0 comments on commit 7d2bee3

Please sign in to comment.