Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: tests #583

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ linters:
enable:
- gofumpt
- goimports
- errcheck
- testifylint

linters-settings:
goimports:
Expand Down
10 changes: 5 additions & 5 deletions ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestIBCHandshake(t *testing.T) {
require.NoError(t, err)
assert.NotNil(t, i.Ok)
iResponse := i.Ok
require.Equal(t, 0, len(iResponse.Messages))
require.Empty(t, iResponse.Messages)

// channel open
gasMeter2 := api.NewMockGasMeter(TESTING_GAS_LIMIT)
Expand All @@ -132,7 +132,7 @@ func TestIBCHandshake(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, conn.Ok)
connResponse := conn.Ok
require.Equal(t, 1, len(connResponse.Messages))
require.Len(t, connResponse.Messages, 1)

// check for the expected custom event
expected_events := []types.Event{{
Expand Down Expand Up @@ -200,7 +200,7 @@ func TestIBCPacketDispatch(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, conn.Ok)
connResponse := conn.Ok
require.Equal(t, 1, len(connResponse.Messages))
require.Len(t, connResponse.Messages, 1)
id := connResponse.Messages[0].ID

// mock reflect init callback (to store address)
Expand Down Expand Up @@ -237,7 +237,7 @@ func TestIBCPacketDispatch(t *testing.T) {
var accounts ListAccountsResponse
err = json.Unmarshal(qResponse, &accounts)
require.NoError(t, err)
require.Equal(t, 1, len(accounts.Accounts))
require.Len(t, accounts.Accounts, 1)
require.Equal(t, CHANNEL_ID, accounts.Accounts[0].ChannelID)
require.Equal(t, REFLECT_ADDR, accounts.Accounts[0].Account)

Expand Down Expand Up @@ -332,7 +332,7 @@ func TestIBCMsgGetChannel(t *testing.T) {
require.Equal(t, msg1.GetChannel(), msg4.GetChannel())
require.Equal(t, msg1.GetChannel(), msg5.GetChannel())
require.Equal(t, msg1.GetChannel(), msg6.GetChannel())
require.Equal(t, msg1.GetChannel().Endpoint.ChannelID, CHANNEL_ID)
require.Equal(t, CHANNEL_ID, msg1.GetChannel().Endpoint.ChannelID)
}

func TestIBCMsgGetCounterVersion(t *testing.T) {
Expand Down
18 changes: 7 additions & 11 deletions internal/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,33 @@ import (
)

func TestValidateAddressFailure(t *testing.T) {
cache, cleanup := withCache(t)
defer cleanup()
// Set up cache; no need to call t.Cleanup here because withCache does it for you
cache, _ := withCache(t)

// create contract
// Create contract
wasm, err := os.ReadFile("../../testdata/hackatom.wasm")
require.NoError(t, err)
checksum, err := StoreCode(cache, wasm, true)
require.NoError(t, err)

gasMeter := NewMockGasMeter(TESTING_GAS_LIMIT)
// instantiate it with this store
gasMeter := NewMockGasMeter(testingGasLimit)
store := NewLookup(gasMeter)
api := NewMockAPI()
querier := DefaultQuerier(MOCK_CONTRACT_ADDR, types.Array[types.Coin]{types.NewCoin(100, "ATOM")})
env := MockEnvBin(t)
info := MockInfoBin(t, "creator")

// if the human address is larger than 32 bytes, this will lead to an error in the go side
longName := "long123456789012345678901234567890long"
msg := []byte(`{"verifier": "` + longName + `", "beneficiary": "bob"}`)
msg := []byte(`{"verifier":"` + longName + `","beneficiary":"bob"}`)

// make sure the call doesn't error, but we get a JSON-encoded error result from ContractResult
igasMeter := types.GasMeter(gasMeter)
res, _, err := Instantiate(cache, checksum, env, info, msg, &igasMeter, store, api, &querier, TESTING_GAS_LIMIT, TESTING_PRINT_DEBUG)
res, _, err := Instantiate(cache, checksum, env, info, msg, &igasMeter, store, api, &querier, testingGasLimit, testingPrintDebug)
require.NoError(t, err)

var result types.ContractResult
err = json.Unmarshal(res, &result)
require.NoError(t, err)

// ensure the error message is what we expect
require.Nil(t, result.Ok)
// with this error
require.Equal(t, "Generic error: addr_validate errored: human encoding too long", result.Err)
}
84 changes: 44 additions & 40 deletions internal/api/iterator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ type queueData struct {
querier types.Querier
}

// Store returns a KVStore with a given gas meter attached.
func (q queueData) Store(meter MockGasMeter) types.KVStore {
return q.store.WithGasMeter(meter)
}

// setupQueueContractWithData instantiates the queue contract with initial data
// and optionally enqueues the provided values.
func setupQueueContractWithData(t *testing.T, cache Cache, values ...int) queueData {
checksum := createQueueContract(t, cache)

gasMeter1 := NewMockGasMeter(TESTING_GAS_LIMIT)
// instantiate it with this store
gasMeter1 := NewMockGasMeter(testingGasLimit)
store := NewLookup(gasMeter1)
api := NewMockAPI()
querier := DefaultQuerier(MOCK_CONTRACT_ADDR, types.Array[types.Coin]{types.NewCoin(100, "ATOM")})
Expand All @@ -37,15 +39,15 @@ func setupQueueContractWithData(t *testing.T, cache Cache, values ...int) queueD
msg := []byte(`{}`)

igasMeter1 := types.GasMeter(gasMeter1)
res, _, err := Instantiate(cache, checksum, env, info, msg, &igasMeter1, store, api, &querier, TESTING_GAS_LIMIT, TESTING_PRINT_DEBUG)
res, _, err := Instantiate(cache, checksum, env, info, msg, &igasMeter1, store, api, &querier, testingGasLimit, testingPrintDebug)
require.NoError(t, err)
requireOkResponse(t, res, 0)

for _, value := range values {
// push 17
var gasMeter2 types.GasMeter = NewMockGasMeter(TESTING_GAS_LIMIT)
gasMeter2 := NewMockGasMeter(testingGasLimit)
igasMeter2 := types.GasMeter(gasMeter2)
push := []byte(fmt.Sprintf(`{"enqueue":{"value":%d}}`, value))
res, _, err = Execute(cache, checksum, env, info, push, &gasMeter2, store, api, &querier, TESTING_GAS_LIMIT, TESTING_PRINT_DEBUG)
res, _, err = Execute(cache, checksum, env, info, push, &igasMeter2, store, api, &querier, testingGasLimit, testingPrintDebug)
require.NoError(t, err)
requireOkResponse(t, res, 0)
}
Expand All @@ -58,6 +60,7 @@ func setupQueueContractWithData(t *testing.T, cache Cache, values ...int) queueD
}
}

// setupQueueContract instantiates a queue contract and enqueues two values: 17 and 22.
func setupQueueContract(t *testing.T, cache Cache) queueData {
return setupQueueContractWithData(t, cache, 17, 22)
}
Expand All @@ -76,6 +79,7 @@ func TestStoreIterator(t *testing.T) {
index, err = storeIterator(callID1, iter, limit)
require.NoError(t, err)
require.Equal(t, uint64(1), index)

iter, _ = store.Iterator(nil, nil)
index, err = storeIterator(callID1, iter, limit)
require.NoError(t, err)
Expand All @@ -85,10 +89,12 @@ func TestStoreIterator(t *testing.T) {
index, err = storeIterator(callID2, iter, limit)
require.NoError(t, err)
require.Equal(t, uint64(1), index)

iter, _ = store.Iterator(nil, nil)
index, err = storeIterator(callID2, iter, limit)
require.NoError(t, err)
require.Equal(t, uint64(2), index)

iter, _ = store.Iterator(nil, nil)
index, err = storeIterator(callID2, iter, limit)
require.NoError(t, err)
Expand Down Expand Up @@ -133,15 +139,19 @@ func TestRetrieveIterator(t *testing.T) {
iter, _ = store.Iterator(nil, nil)
iteratorID11, err := storeIterator(callID1, iter, limit)
require.NoError(t, err)

iter, _ = store.Iterator(nil, nil)
_, err = storeIterator(callID1, iter, limit)
require.NoError(t, err)

iter, _ = store.Iterator(nil, nil)
_, err = storeIterator(callID2, iter, limit)
require.NoError(t, err)

iter, _ = store.Iterator(nil, nil)
iteratorID22, err := storeIterator(callID2, iter, limit)
require.NoError(t, err)

iter, err = store.Iterator(nil, nil)
require.NoError(t, err)
iteratorID23, err := storeIterator(callID2, iter, limit)
Expand All @@ -153,7 +163,7 @@ func TestRetrieveIterator(t *testing.T) {
iter = retrieveIterator(callID2, iteratorID22)
require.NotNil(t, iter)

// Retrieve with non-existent iterator ID
// Retrieve with non-existent iterator IDs
iter = retrieveIterator(callID1, iteratorID23)
require.Nil(t, iter)
iter = retrieveIterator(callID1, uint64(0))
Expand All @@ -174,19 +184,19 @@ func TestRetrieveIterator(t *testing.T) {
}

func TestQueueIteratorSimple(t *testing.T) {
cache, cleanup := withCache(t)
defer cleanup()
cache, _ := withCache(t) // No need for t.Cleanup(cleanup)

setup := setupQueueContract(t, cache)
checksum, querier, api := setup.checksum, setup.querier, setup.api

// query the sum
gasMeter := NewMockGasMeter(TESTING_GAS_LIMIT)
gasMeter := NewMockGasMeter(testingGasLimit)
igasMeter := types.GasMeter(gasMeter)
store := setup.Store(gasMeter)

// query the sum
query := []byte(`{"sum":{}}`)
env := MockEnvBin(t)
data, _, err := Query(cache, checksum, env, query, &igasMeter, store, api, &querier, TESTING_GAS_LIMIT, TESTING_PRINT_DEBUG)
data, _, err := Query(cache, checksum, env, query, &igasMeter, store, api, &querier, testingGasLimit, testingPrintDebug)
require.NoError(t, err)
var qResult types.QueryResult
err = json.Unmarshal(data, &qResult)
Expand All @@ -196,49 +206,47 @@ func TestQueueIteratorSimple(t *testing.T) {

// query reduce (multiple iterators at once)
query = []byte(`{"reducer":{}}`)
data, _, err = Query(cache, checksum, env, query, &igasMeter, store, api, &querier, TESTING_GAS_LIMIT, TESTING_PRINT_DEBUG)
data, _, err = Query(cache, checksum, env, query, &igasMeter, store, api, &querier, testingGasLimit, testingPrintDebug)
require.NoError(t, err)
var reduced types.QueryResult
err = json.Unmarshal(data, &reduced)
require.NoError(t, err)
require.Equal(t, "", reduced.Err)
require.Equal(t, `{"counters":[[17,22],[22,0]]}`, string(reduced.Ok))
require.JSONEq(t, `{"counters":[[17,22],[22,0]]}`, string(reduced.Ok))
}

func TestQueueIteratorRaces(t *testing.T) {
cache, cleanup := withCache(t)
defer cleanup()
cache, _ := withCache(t) // No need for t.Cleanup(cleanup)

assert.Equal(t, 0, len(iteratorFrames))
assert.Empty(t, iteratorFrames)

contract1 := setupQueueContractWithData(t, cache, 17, 22)
contract2 := setupQueueContractWithData(t, cache, 1, 19, 6, 35, 8)
contract3 := setupQueueContractWithData(t, cache, 11, 6, 2)
env := MockEnvBin(t)

// reduceQuery queries the "reducer" endpoint and compares the result to expected.
reduceQuery := func(t *testing.T, setup queueData, expected string) {
checksum, querier, api := setup.checksum, setup.querier, setup.api
gasMeter := NewMockGasMeter(TESTING_GAS_LIMIT)
gasMeter := NewMockGasMeter(testingGasLimit)
igasMeter := types.GasMeter(gasMeter)
store := setup.Store(gasMeter)

// query reduce (multiple iterators at once)
query := []byte(`{"reducer":{}}`)
data, _, err := Query(cache, checksum, env, query, &igasMeter, store, api, &querier, TESTING_GAS_LIMIT, TESTING_PRINT_DEBUG)
require.NoError(t, err)
data, _, err := Query(cache, checksum, env, query, &igasMeter, store, api, &querier, testingGasLimit, testingPrintDebug)
assert.NoError(t, err)
var reduced types.QueryResult
err = json.Unmarshal(data, &reduced)
require.NoError(t, err)
require.Equal(t, "", reduced.Err)
require.Equal(t, fmt.Sprintf(`{"counters":%s}`, expected), string(reduced.Ok))
assert.NoError(t, err)
assert.Equal(t, "", reduced.Err)
assert.Equal(t, fmt.Sprintf(`{"counters":%s}`, expected), string(reduced.Ok))
}

// 30 concurrent batches (in go routines) to trigger any race condition
// 30 concurrent batches to trigger race conditions if any
numBatches := 30

var wg sync.WaitGroup
// for each batch, query each of the 3 contracts - so the contract queries get mixed together
wg.Add(numBatches * 3)

for i := 0; i < numBatches; i++ {
go func() {
reduceQuery(t, contract1, "[[17,22],[22,0]]")
Expand All @@ -255,42 +263,38 @@ func TestQueueIteratorRaces(t *testing.T) {
}
wg.Wait()

// when they finish, we should have removed all frames
assert.Equal(t, 0, len(iteratorFrames))
// after all done, no frames should remain
assert.Empty(t, iteratorFrames)
}

func TestQueueIteratorLimit(t *testing.T) {
cache, cleanup := withCache(t)
defer cleanup()
cache, _ := withCache(t) // No need for t.Cleanup(cleanup)

setup := setupQueueContract(t, cache)
checksum, querier, api := setup.checksum, setup.querier, setup.api

var err error
var qResult types.QueryResult
var gasLimit uint64

// Open 5000 iterators
gasLimit = TESTING_GAS_LIMIT
gasLimit := testingGasLimit
gasMeter := NewMockGasMeter(gasLimit)
igasMeter := types.GasMeter(gasMeter)
store := setup.Store(gasMeter)
query := []byte(`{"open_iterators":{"count":5000}}`)
env := MockEnvBin(t)
data, _, err := Query(cache, checksum, env, query, &igasMeter, store, api, &querier, gasLimit, TESTING_PRINT_DEBUG)
data, _, err := Query(cache, checksum, env, query, &igasMeter, store, api, &querier, gasLimit, testingPrintDebug)
require.NoError(t, err)
var qResult types.QueryResult
err = json.Unmarshal(data, &qResult)
require.NoError(t, err)
require.Equal(t, "", qResult.Err)
require.Equal(t, `{}`, string(qResult.Ok))

// Open 35000 iterators
gasLimit = TESTING_GAS_LIMIT * 4
// Open 35000 iterators, expecting limit error
gasLimit = testingGasLimit * 4
gasMeter = NewMockGasMeter(gasLimit)
igasMeter = types.GasMeter(gasMeter)
store = setup.Store(gasMeter)
query = []byte(`{"open_iterators":{"count":35000}}`)
env = MockEnvBin(t)
_, _, err = Query(cache, checksum, env, query, &igasMeter, store, api, &querier, gasLimit, TESTING_PRINT_DEBUG)
_, _, err = Query(cache, checksum, env, query, &igasMeter, store, api, &querier, gasLimit, testingPrintDebug)
require.ErrorContains(t, err, "Reached iterator limit (32768)")
}
Loading
Loading