# VM tests

### `TestABCIService`

`TestABCIService` unit test in the `service_test.go` file. Overall, the `TestABCIService` unit test is testing several methods of the virtual machine service and verifying that they behave as expected.

Here's a breakdown of what the test is doing:

1. The `TestABCIService` function starts by calling the `mustNewKVTestVm` function to create a new instance of the virtual machine and virtual machine service, and assigns them to the `vm` and `service` variables.
2. The test then runs several subtests using the `t.Run` method, each of which tests a different method of the virtual machine service.

#### Subtests

1. `ABCIInfo`, tests the `ABCIInfo` method of the virtual machine service. It creates a new `ctypes.ResultABCIInfo` variable and passes it to the `ABCIInfo` method. It then asserts that there is no error returned by the method, and that the response contains the expected values for the app version, last block height, and last block app hash.
2. `ABCIQuery`, tests the `ABCIQuery` method of the virtual machine service. It creates a new key-value transaction using the `MakeTxKV` function, broadcasts the transaction using the `BroadcastTxSync` method of the virtual machine service, builds and accepts a new block using the virtual machine, and then queries the virtual machine service for the value of the key using the `ABCIQuery` method. If the query is successful, the subtest asserts that the value returned by the method is equal to the expected value.
3. `BroadcastTxCommit`, tests the `BroadcastTxCommit` method of the virtual machine service. It creates a new key-value transaction using the `MakeTxKV` function, broadcasts the transaction using the `BroadcastTxCommit` method of the virtual machine service, and then asserts that the `CheckTx` and `DeliverTx` fields of the response contain `true`, indicating that the transaction was successfully checked and committed to the blockchain.
4. `BroadcastTxAsync`, tests the `BroadcastTxAsync` method of the virtual machine service. It creates a new key-value transaction using the `MakeTxKV` function, broadcasts the transaction using the `BroadcastTxAsync` method of the virtual machine service, and then asserts that the `Hash` field of the response is not `nil`, indicating that the transaction was successfully added to the mempool.
5. `BroadcastTxSync`, tests the `BroadcastTxSync` method of the virtual machine service. It creates a new key-value transaction using the `MakeTxKV` function, broadcasts the transaction using the `BroadcastTxSync` method of the virtual machine service, and then asserts that the `Code` field of the response is equal to `atypes.CodeTypeOK`, indicating that the transaction was successfully checked and committed to the blockchain.

### `TestHistoryService`

The `TestHistoryService` unit test in the `service_test.go` file. Overall, the `TestHistoryService` unit test is testing several methods of the virtual machine service related to blockchain history and verifying that they behave as expected. Here's a breakdown of what the test is doing:

1. The `TestHistoryService` function starts by calling the `mustNewCounterTestVm` function to create a new instance of the virtual machine and virtual machine service, and assigns them to the `vm` and `service` variables.
2. The test then broadcasts a new transaction using the `BroadcastTxSync` method of the virtual machine service and verifies that the response code is `atypes.CodeTypeOK`.
3. The test then builds and accepts a new block using the virtual machine.
4. The test then runs two subtests using the `t.Run` method, each of which tests a different method of the virtual machine service.

#### Subtests

1. `BlockchainInfo` tests the `BlockchainInfo` method of the virtual machine service. It creates a new `ctypes.ResultBlockchainInfo` variable and passes it to the `BlockchainInfo` method with a `start` height of `1` and an `end` height of `100`. It then asserts that there is no error returned by the method and that the `LastHeight` field of the response is equal to `1`.
2. `Genesis` tests the `Genesis` method of the virtual machine service. It creates a new `ctypes.ResultGenesis` variable and passes it to the `Genesis` method. It then asserts that there is no error returned by the method and that the `Genesis` field of the response is equal to the expected value from the virtual machine's genesis configuration.

### `TestNetworkService`

The `TestNetworkService` unit test in the `service_test.go` file. Overall, the `TestNetworkService` unit test is testing several methods of the virtual machine service related to **networking** and **consensus** and verifying that they behave as expected.&#x20;

Here's a breakdown of what the test is doing:

1. The `TestNetworkService` function starts by calling the `mustNewCounterTestVm` function to create a new instance of the virtual machine and virtual machine service, and assigns them to the `vm` and `service` variables.
2. The test then runs five subtests using the `t.Run` method, each of which tests a different method of the virtual machine service related to networking and consensus.

#### Subtests

1. `NetInfo` tests the `NetInfo` method of the virtual machine service. It creates a new `ctypes.ResultNetInfo` variable and passes it to the `NetInfo` method. It then asserts that there is no error returned by the method.
2. `DumpConsensusState` tests the `DumpConsensusState` method of the virtual machine service. It creates a new `ctypes.ResultDumpConsensusState` variable and passes it to the `DumpConsensusState` method. It then asserts that there is no error returned by the method.
3. `ConsensusState` tests the `ConsensusState` method of the virtual machine service. It creates a new `ctypes.ResultConsensusState` variable and passes it to the `ConsensusState` method. It then asserts that there is no error returned by the method.
4. `ConsensusParams` tests the `ConsensusParams` method of the virtual machine service. It creates a new `ctypes.ResultConsensusParams` variable and passes it to the `ConsensusParams` method. It then asserts that there is no error returned by the method and that the `BlockHeight` field of the response is equal to `0`. It then broadcasts a new transaction using the `BroadcastTxSync` method of the virtual machine service, builds and accepts a new block using the virtual machine, and asserts that the `BlockHeight` field of the `ConsensusParams` response is equal to `1`.
5. `Health` tests the `Health` method of the virtual machine service. It creates a new `ctypes.ResultHealth` variable and passes it to the `Health` method. It then asserts that there is no error returned by the method.

### `TestSignService`

Sure, I can help you document the `TestSignService` unit test in the `service_test.go` file. Here's a breakdown of what the test is doing:

1. The `TestSignService` function starts by creating a new key-value transaction (`tx`) and creating a new instance of the virtual machine and virtual machine service, which it assigns to the `vm` and `service` variables.
2. The test then performs several subtests using the `t.Run` method, each of which tests a different method of the virtual machine service related to signing and verification of transactions.The `TestSignService` has the following subtests:

#### Subtests

* `Block`which tests the `Block` method of the virtual machine service with a valid block height. It creates two new `ctypes.ResultBlock` variables and passes them to the `Block` method with a `Height` parameter of the block height. It then asserts that both methods return a `nil` error and that the `Height` field of the response is equal to the expected block height.
* `BlockByHash` which tests the `BlockByHash` method of the virtual machine service with a valid block hash. It creates two new `ctypes.ResultBlock` variables and passes them to the `BlockByHash` method with a `Hash` parameter of the block hash. It then asserts that both methods return a `nil` error and that the `Hash` field of the response is equal to the expected block hash.
* `BlockResults` which tests the `BlockResults` method of the virtual machine service with a valid block height. It creates two new `ctypes.ResultBlockResults` variables and passes them to the `BlockResults` method with a `Height` parameter of the block height. It then asserts that both methods return a `nil` error and that the `Height` field of the response is equal to the expected block height.
* `Tx` which tests the `Tx` method of the virtual machine service with a valid transaction hash. It creates a new `ctypes.ResultTx` variable and passes it to the `Tx` method with a `Hash` parameter of the transaction hash. It then asserts that the method returns a `nil` error and that the `Hash` and `Tx` fields of the response are equal to the expected transaction hash and transaction, respectively.

Overall, the `TestSignService` unit test is testing several methods of the virtual machine service related to signing and verification of transactions and verifying that they behave as expected.

### `TestStatusService`

The `TestStatusService` unit test in the `service_test.go` file. Here's a breakdown of what the test is doing:

1. The `TestStatusService` function starts by creating a new instance of the virtual machine and virtual machine service, which it assigns to the `vm` and `service` variables.
2. The test then attempts to build a new block from the virtual machine with no pending transactions. It expects this operation to fail with an `errNoPendingTxs` error and a `nil` block.
3. Next, the test creates a new key-value transaction (`tx`) and uses the `BroadcastTxSync` method of the virtual machine service to broadcast the transaction. It asserts that the method returns no error and that the `Code` field of the response is equal to `atypes.CodeTypeOK`.
4. The test then creates a new subtest using the `t.Run` method, which tests the `Status` method of the virtual machine service with two different blocks. It creates two new `ctypes.ResultStatus` variables and passes them to the `Status` method. The first call is made with no arguments and expects the response to contain a `LatestBlockHeight` field with a value of `0`. The second call is made after successfully building and accepting a new block and expects the response to contain a `LatestBlockHeight` field with a value of `1`.

Overall, the `TestStatusService` unit test is testing the `Status` method of the virtual machine service with two different blocks and verifying that the expected block height is returned in the response.

### &#x20;`TestMempoolService`

This unit test suite tests the functionalities of the `MempoolService` struct.

**Sub-tests:**

1. `UnconfirmedTxs`
   * Description: Tests the functionality of retrieving unconfirmed transactions in the mempool.
   * Steps:
     1. Build a transaction and broadcast it to the mempool.
     2. Call the `UnconfirmedTxs` function of the `MempoolService`.
   * Expected Results: The function should return the single transaction that was broadcasted to the mempool.
2. `NumUnconfirmedTxs`
   * Description: Tests the functionality of retrieving the number of unconfirmed transactions in the mempool.
   * Steps:
     1. Build a transaction and broadcast it to the mempool.
     2. Call the `NumUnconfirmedTxs` function of the `MempoolService`.
   * Expected Results: The function should return a count of 1, indicating the single transaction that was broadcasted to the mempool.
3. `CheckTx`
   * Description: Tests the functionality of checking if a transaction is valid and can be accepted into the mempool.
   * Steps:
     1. Build a transaction and broadcast it to the mempool.
     2. Call the `CheckTx` function of the `MempoolService` with the same transaction.
     3. Build a block and accept it.
     4. Call the `CheckTx` function of the `MempoolService` with the same transaction.
   * Expected Results:
     * The first `CheckTx` function should return no errors, indicating the transaction is valid and can be accepted into the mempool.
     * The second `CheckTx` function should return an error, indicating the transaction has already been accepted into the blockchain and cannot be accepted into the mempool again.
