Skip to content

Commit

Permalink
Add 'TestZeroComplexitySanityCheckInComplexityCalculator'.
Browse files Browse the repository at this point in the history
  • Loading branch information
nickeskov committed Jul 27, 2024
1 parent b360d19 commit e6fd7b8
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions pkg/ride/tree_evaluation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6090,3 +6090,56 @@ func TestNewRideV8Functions(t *testing.T) {
assert.Equal(t, expectedComplexity, res.Complexity())
})
}

func TestZeroComplexitySanityCheckInComplexityCalculator(t *testing.T) {
dApp1 := newTestAccount(t, "DAPP1") // 3MzDtgL5yw73C2xVLnLJCrT5gCL4357a4sz
sender := newTestAccount(t, "SENDER") // 3N8CkZAyS4XcDoJTJoKNuNk2xmNKmQj7myW

const src1 = `
{-# STDLIB_VERSION 5 #-}
{-# CONTENT_TYPE DAPP #-}
{-# SCRIPT_TYPE ACCOUNT #-}

@Callable(i)
func call() = {
let dApp = Address(base58'%[1]s')
if dApp != this then
throw("dApp != this")
else
let caller = Address(base58'%[2]s')
if caller != i.caller then
throw("caller != i.caller")
else
[]
}
`
tree1, errs := ridec.CompileToTree(fmt.Sprintf(src1, dApp1.address().String(), sender.address().String()))
require.Empty(t, errs)

createEnv := func(t *testing.T, cc complexityCalculator) environment {
env := newTestEnv(t).withLibVersion(ast.LibV5).
withBlockV5Activated().withProtobufTx().
withDataEntriesSizeV2().withMessageLengthV3().withValidateInternalPayments().
withThis(dApp1).withDApp(dApp1).withSender(sender).
withInvocation("call", withTransactionID(crypto.Digest{})).withTree(dApp1, tree1).
withWrappedState().toEnv()
env.complexityCalculatorFunc = func() complexityCalculator { return cc }
return env
}
t.Run("complexity_calculator_v1", func(t *testing.T) {
const expectedSpentComplexity = 18
env := createEnv(t, &complexityCalculatorV1{l: expectedSpentComplexity})
res, err := CallFunction(env, tree1, proto.NewFunctionCall("call", proto.Arguments{}))
assert.NoError(t, err)
assert.NotNil(t, res)
assert.Equal(t, expectedSpentComplexity, env.complexityCalculator().complexity())
})
t.Run("complexity_calculator_v2", func(t *testing.T) {
const expectedSpentComplexity = 0
env := createEnv(t, &complexityCalculatorV2{l: expectedSpentComplexity})
res, err := CallFunction(env, tree1, proto.NewFunctionCall("call", proto.Arguments{}))
assert.Nil(t, res)
assert.EqualError(t, err, "failed to test complexity of system function: node 'Address' has zero complexity")
assert.Equal(t, expectedSpentComplexity, env.complexityCalculator().complexity())
})
}

0 comments on commit e6fd7b8

Please sign in to comment.