Skip to content

Commit

Permalink
Execute TestExecuteStorageLoop before TestExecuteCpuLoop
Browse files Browse the repository at this point in the history
  • Loading branch information
webmaster128 committed Dec 6, 2022
1 parent 0f5be88 commit 13e21c1
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions internal/api/lib_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,68 +392,68 @@ func TestExecute(t *testing.T) {
assert.Equal(t, expectedData, result.Ok.Data)
}

func TestExecuteCpuLoop(t *testing.T) {
func TestExecuteStorageLoop(t *testing.T) {
cache, cleanup := withCache(t)
defer cleanup()
checksum := createCyberpunkContract(t, cache)
checksum := createHackatomContract(t, cache)

gasMeter1 := NewMockGasMeter(TESTING_GAS_LIMIT)
maxGas := TESTING_GAS_LIMIT
gasMeter1 := NewMockGasMeter(maxGas)
igasMeter1 := GasMeter(gasMeter1)
// instantiate it with this store
store := NewLookup(gasMeter1)
api := NewMockAPI()
querier := DefaultQuerier(MOCK_CONTRACT_ADDR, nil)
balance := types.Coins{types.NewCoin(250, "ATOM")}
querier := DefaultQuerier(MOCK_CONTRACT_ADDR, balance)
env := MockEnvBin(t)
info := MockInfoBin(t, "creator")

msg := []byte(`{}`)
msg := []byte(`{"verifier": "fred", "beneficiary": "bob"}`)

start := time.Now()
res, cost, err := Instantiate(cache, checksum, env, info, msg, &igasMeter1, store, api, &querier, TESTING_GAS_LIMIT, TESTING_PRINT_DEBUG)
diff := time.Now().Sub(start)
res, cost, err := Instantiate(cache, checksum, env, info, msg, &igasMeter1, store, api, &querier, maxGas, TESTING_PRINT_DEBUG)
require.NoError(t, err)
requireOkResponse(t, res, 0)
assert.Equal(t, uint64(0xf5c79de0), cost)
t.Logf("Time (%d gas): %s\n", cost, diff)

// execute a cpu loop
maxGas := uint64(40_000_000)
// execute a storage loop
gasMeter2 := NewMockGasMeter(maxGas)
igasMeter2 := GasMeter(gasMeter2)
store.SetGasMeter(gasMeter2)
info = MockInfoBin(t, "fred")
start = time.Now()
res, cost, err = Execute(cache, checksum, env, info, []byte(`{"cpu_loop":{}}`), &igasMeter2, store, api, &querier, maxGas, TESTING_PRINT_DEBUG)
diff = time.Now().Sub(start)
start := time.Now()
res, cost, err = Execute(cache, checksum, env, info, []byte(`{"storage_loop":{}}`), &igasMeter2, store, api, &querier, maxGas, TESTING_PRINT_DEBUG)
diff := time.Now().Sub(start)
require.Error(t, err)
assert.Equal(t, cost, maxGas)
t.Logf("CPULoop Time (%d gas): %s\n", cost, diff)
t.Logf("StorageLoop Time (%d gas): %s\n", cost, diff)
t.Logf("Gas used: %d\n", gasMeter2.GasConsumed())
t.Logf("Wasm gas: %d\n", cost)

// the "sdk gas" * GasMultiplier + the wasm cost should equal the maxGas (or be very close)
totalCost := cost + gasMeter2.GasConsumed()
require.Equal(t, int64(maxGas), int64(totalCost))
}

// Old version using hackatom. Delete me at some point.
func TestExecuteCpuLoopOld(t *testing.T) {
func TestExecuteCpuLoop(t *testing.T) {
cache, cleanup := withCache(t)
defer cleanup()
checksum := createHackatomContract(t, cache)
checksum := createCyberpunkContract(t, cache)

gasMeter1 := NewMockGasMeter(TESTING_GAS_LIMIT)
igasMeter1 := GasMeter(gasMeter1)
// instantiate it with this store
store := NewLookup(gasMeter1)
api := NewMockAPI()
balance := types.Coins{types.NewCoin(250, "ATOM")}
querier := DefaultQuerier(MOCK_CONTRACT_ADDR, balance)
querier := DefaultQuerier(MOCK_CONTRACT_ADDR, nil)
env := MockEnvBin(t)
info := MockInfoBin(t, "creator")

msg := []byte(`{"verifier": "fred", "beneficiary": "bob"}`)
msg := []byte(`{}`)

start := time.Now()
res, cost, err := Instantiate(cache, checksum, env, info, msg, &igasMeter1, store, api, &querier, TESTING_GAS_LIMIT, TESTING_PRINT_DEBUG)
diff := time.Now().Sub(start)
require.NoError(t, err)
requireOkResponse(t, res, 0)
assert.Equal(t, uint64(0x1432036ec), cost)
assert.Equal(t, uint64(0xf5c79de0), cost)
t.Logf("Time (%d gas): %s\n", cost, diff)

// execute a cpu loop
Expand All @@ -470,13 +470,13 @@ func TestExecuteCpuLoopOld(t *testing.T) {
t.Logf("CPULoop Time (%d gas): %s\n", cost, diff)
}

func TestExecuteStorageLoop(t *testing.T) {
// Old version using hackatom. Delete me at some point.
func TestExecuteCpuLoopOld(t *testing.T) {
cache, cleanup := withCache(t)
defer cleanup()
checksum := createHackatomContract(t, cache)

maxGas := TESTING_GAS_LIMIT
gasMeter1 := NewMockGasMeter(maxGas)
gasMeter1 := NewMockGasMeter(TESTING_GAS_LIMIT)
igasMeter1 := GasMeter(gasMeter1)
// instantiate it with this store
store := NewLookup(gasMeter1)
Expand All @@ -488,26 +488,26 @@ func TestExecuteStorageLoop(t *testing.T) {

msg := []byte(`{"verifier": "fred", "beneficiary": "bob"}`)

res, cost, err := Instantiate(cache, checksum, env, info, msg, &igasMeter1, store, api, &querier, maxGas, TESTING_PRINT_DEBUG)
start := time.Now()
res, cost, err := Instantiate(cache, checksum, env, info, msg, &igasMeter1, store, api, &querier, TESTING_GAS_LIMIT, TESTING_PRINT_DEBUG)
diff := time.Now().Sub(start)
require.NoError(t, err)
requireOkResponse(t, res, 0)
assert.Equal(t, uint64(0x1432036ec), cost)
t.Logf("Time (%d gas): %s\n", cost, diff)

// execute a storage loop
// execute a cpu loop
maxGas := uint64(40_000_000)
gasMeter2 := NewMockGasMeter(maxGas)
igasMeter2 := GasMeter(gasMeter2)
store.SetGasMeter(gasMeter2)
info = MockInfoBin(t, "fred")
start := time.Now()
res, cost, err = Execute(cache, checksum, env, info, []byte(`{"storage_loop":{}}`), &igasMeter2, store, api, &querier, maxGas, TESTING_PRINT_DEBUG)
diff := time.Now().Sub(start)
start = time.Now()
res, cost, err = Execute(cache, checksum, env, info, []byte(`{"cpu_loop":{}}`), &igasMeter2, store, api, &querier, maxGas, TESTING_PRINT_DEBUG)
diff = time.Now().Sub(start)
require.Error(t, err)
t.Logf("StorageLoop Time (%d gas): %s\n", cost, diff)
t.Logf("Gas used: %d\n", gasMeter2.GasConsumed())
t.Logf("Wasm gas: %d\n", cost)

// the "sdk gas" * GasMultiplier + the wasm cost should equal the maxGas (or be very close)
totalCost := cost + gasMeter2.GasConsumed()
require.Equal(t, int64(maxGas), int64(totalCost))
assert.Equal(t, cost, maxGas)
t.Logf("CPULoop Time (%d gas): %s\n", cost, diff)
}

func TestExecuteUserErrorsInApiCalls(t *testing.T) {
Expand Down

0 comments on commit 13e21c1

Please sign in to comment.