Skip to content

Commit

Permalink
Fix isConnected behaviour and start addressing test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
p-offtermatt committed Nov 27, 2023
1 parent 2acf0b7 commit 750ff12
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 64 deletions.
53 changes: 7 additions & 46 deletions cometmock/abci_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,18 @@ type AbciClient struct {
AutoIncludeTx bool

// A list of transactions that will be included in the next block that is created.
// When transaction FreshTxQueue[i] is included, it will be removed from the FreshTxQueue,
// and the result will be sent to ResponseChannelQueue[i].
//
FreshTxQueue []types.Tx
StaleTxQueue []types.Tx
TxQueue []types.Tx
}

func (a *AbciClient) QueueTx(tx types.Tx) {
// lock the block mutex so txs are not queued while a block is being run
blockMutex.Lock()
a.FreshTxQueue = append(a.FreshTxQueue, tx)
a.TxQueue = append(a.TxQueue, tx)
blockMutex.Unlock()
}

func (a *AbciClient) ClearTxs() {
a.FreshTxQueue = make([]types.Tx, 0)
a.StaleTxQueue = make([]types.Tx, 0)
a.TxQueue = make([]types.Tx, 0)
}

func (a *AbciClient) CauseLightClientAttack(address string, misbehaviourType string) error {
Expand Down Expand Up @@ -249,7 +244,7 @@ func NewAbciClient(
TimeHandler: timeHandler,
ErrorOnUnequalResponses: errorOnUnequalResponses,
signingStatus: signingStatus,
FreshTxQueue: make([]types.Tx, 0),
TxQueue: make([]types.Tx, 0),
}
}

Expand Down Expand Up @@ -823,43 +818,8 @@ func (a *AbciClient) runBlock_helper(

var err error

for index, tx := range a.FreshTxQueue {
txBytes := []byte(tx)
resCheckTx, err := a.SendCheckTx(abcitypes.CheckTxType_New, &txBytes)
if err != nil {
return fmt.Errorf("error from CheckTx: %v", err)
}
// if the CheckTx code is != 0
if resCheckTx.Code != abcitypes.CodeTypeOK {
// drop the tx by setting the index to empty
a.FreshTxQueue[index] = cmttypes.Tx{}
}
}

// recheck txs from the stale queue
for index, tx := range a.StaleTxQueue {
txBytes := []byte(tx)
resCheckTx, err := a.SendCheckTx(abcitypes.CheckTxType_Recheck, &txBytes)
if err != nil {
return fmt.Errorf("error from CheckTx: %v", err)
}
// if the CheckTx code is != 0
if resCheckTx.Code != abcitypes.CodeTypeOK {
// drop the tx by setting the index to empty
a.StaleTxQueue[index] = cmttypes.Tx{}
}
}

// filter all empty txs from the queues
newTxQueue := make([]cmttypes.Tx, 0)
for _, tx := range append(a.FreshTxQueue, a.StaleTxQueue...) {
txBytes := []byte(tx)
if len(txBytes) > 0 {
newTxQueue = append(newTxQueue, tx)
}
}

txs := cmttypes.Txs(newTxQueue)
txs := cmttypes.Txs(a.TxQueue)

// TODO: handle special case where proposer is nil
var proposerAddress types.Address
Expand Down Expand Up @@ -973,8 +933,9 @@ func (a *AbciClient) runBlock_helper(
}

var deliverTxResponses []*abcitypes.ResponseDeliverTx
for _, tx := range newTxQueue {
for _, tx := range txs {
txBytes := []byte(tx)
a.Logger.Info("Sending DeliverTx", "tx", tx)
resDeliverTx, err := a.SendDeliverTx(&txBytes)
if err != nil {
return err
Expand Down
3 changes: 3 additions & 0 deletions cometmock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ or the system time between creating the genesis request and producing the first
abci_client.GlobalClient.AutoIncludeTx = c.Bool("auto-tx")
fmt.Printf("Auto include tx: %t\n", abci_client.GlobalClient.AutoIncludeTx)

// connect to clients
abci_client.GlobalClient.RetryDisconnectedClients()

// initialize chain
err = abci_client.GlobalClient.SendInitChain(curState, genesisDoc)
if err != nil {
Expand Down
28 changes: 19 additions & 9 deletions e2e-tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package main
import (
"encoding/json"
"fmt"
"math/big"
"os/exec"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -34,9 +35,18 @@ func StartChain(
// wait until we are producing blocks
for {
// --type height 0 gets the latest height
out, err := exec.Command("bash", "-c", "simd q block --node tcp://127.0.0.1:22331 | jq -r '.header.height'").Output()
out, err := exec.Command("bash", "-c", "simd q block --node tcp://127.0.0.1:22331 | jq -r '.block.header.height'").Output()
if err != nil {
t.Log("Error running query command: ", err)
continue
}

height, err := strconv.Atoi(strings.TrimSpace(string(out)))
if err != nil {
t.Log("Could not parse height: ", string(out))
}

if err == nil {
if err == nil && height > 0 {
t.Log("We are producing blocks: ", string(out))
break
}
Expand Down Expand Up @@ -115,10 +125,10 @@ func TestAbciQuery(t *testing.T) {
t.Fatalf("Failed to unmarshal JSON %s \n error was %v", string(out), err)
}

// check that the output contains the expected params field. its contents are not important
_, ok := data["params"]
// check that the output contains the expected unbonding_time field. its contents are not important
_, ok := data["unbonding_time"]
if !ok {
t.Fatalf("Expected output to contain params field, but it did not. Output was %s", string(out))
t.Fatalf("Expected output to contain unbonding_time field, but it did not. Output was %s", string(out))
}
}

Expand All @@ -133,15 +143,15 @@ func TestTx(t *testing.T) {
require.NoError(t, err)

// send some tokens to the community pool
err = sendToCommunityPool(50000000000, "coordinator")
err = sendToCommunityPool(500000, "coordinator")
require.NoError(t, err)

// check that the amount in the community pool has increased
communityPoolSize2, err := getCommunityPoolSize()
require.NoError(t, err)

// cannot check for equality because the community pool gets dust over time
require.True(t, communityPoolSize2.Cmp(communityPoolSize.Add(communityPoolSize, big.NewInt(50000000000))) == +1)
require.True(t, communityPoolSize2 > communityPoolSize+500000)
}

// TestBlockTime checks that the basic behaviour with a specified block-time is as expected,
Expand Down Expand Up @@ -283,7 +293,7 @@ func TestNoAutoTx(t *testing.T) {
require.NoError(t, err)

// cannot check for equality because the community pool gets dust over time
require.True(t, communityPoolAfter.Cmp(communityPoolBefore.Add(communityPoolBefore, big.NewInt(100000000000))) == +1)
require.True(t, communityPoolAfter > communityPoolBefore+500000000)
}

func TestStartingTimestamp(t *testing.T) {
Expand Down
16 changes: 7 additions & 9 deletions e2e-tests/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"math/big"
"os/exec"
"strconv"
"strings"
Expand Down Expand Up @@ -111,21 +110,20 @@ func GetHeightAndTime() (int, time.Time, error) {

// Queries the size of the community pool.
// For this, it will just check the number of tokens of the first denom in the community pool.
func getCommunityPoolSize() (*big.Int, error) {
func getCommunityPoolSize() (int64, error) {
// execute the query command
cmd := exec.Command("bash", "-c", "simd q distribution community-pool --output json --node tcp://127.0.0.1:22331 | jq -r '.pool[0].amount'")
out, err := runCommandWithOutput(cmd)
if err != nil {
return big.NewInt(-1), fmt.Errorf("error running query command: %v", err)
return -1, fmt.Errorf("error running query command: %v", err)
}

res := new(big.Int)

res, ok := res.SetString(strings.TrimSpace(out), 10)
if !ok {
return big.NewInt(-1), fmt.Errorf("error parsing community pool size: %v", err)
res, err := strconv.ParseFloat(strings.TrimSpace(out), 64)
if err != nil {
return -1, fmt.Errorf("error parsing community pool size: %v, input was %v", err, strings.TrimSpace(out))
}
return res, err

return int64(res), nil
}

func sendToCommunityPool(amount int, sender string) error {
Expand Down

0 comments on commit 750ff12

Please sign in to comment.