Skip to content

Commit

Permalink
add test cases for execute timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Silentttttt committed Jul 12, 2018
1 parent df8af1e commit a8bc312
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
3 changes: 1 addition & 2 deletions core/compatibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,6 @@ var (

// SetCompatibilityOptions set compatibility height according to chain_id
func SetCompatibilityOptions(chainID uint32) {

if chainID == MainNetID {
TransferFromContractEventRecordableHeight = MainNetTransferFromContractEventRecordableHeight
AcceptFuncAvailableHeight = MainNetAcceptFuncAvailableHeight
Expand Down Expand Up @@ -319,7 +318,7 @@ func SetCompatibilityOptions(chainID uint32) {
V8JSLibVersionHeightSlice = LocalV8JSLibVersionHeightSlice
TransferFromContractFailureEventRecordableHeight = LocalTransferFromContractFailureEventRecordableHeight
NewNvmExeTimeoutConsumeGasHeight = LocalNewNvmExeTimeoutConsumeGasHeight
NvmGasLimitWithoutTimeoutAtHeight = LocalNewNvmExeTimeoutConsumeGasHeight
NvmGasLimitWithoutTimeoutAtHeight = LocalNvmGasLimitWithoutTimeoutAtHeight
}

// sort V8JSLibVersionHeightSlice in descending order by height
Expand Down
67 changes: 62 additions & 5 deletions nf/nvm/engine_v8_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,18 @@ func TestV8ResourceLimit(t *testing.T) {
})
}
}

func TestRunScriptSourceTimeout(t *testing.T) {
// core.NebCompatibility = core.NewCompatibilityLocal()
core.SetCompatibilityOptions(100)

tests := []struct {
filepath string
filepath string
height uint64
expectedErr error
}{
{"test/test_infinite_loop.js"},
{"test/test_infinite_loop.js", 1, ErrExecutionTimeout},
{"test/test_infinite_loop.js", 2, core.ErrUnexpected},
}

for _, tt := range tests {
Expand All @@ -357,13 +364,14 @@ func TestRunScriptSourceTimeout(t *testing.T) {
// assert.Nil(t, err)

contract, _ := context.CreateContractAccount([]byte("account2"), nil, nil)
ctx, err := NewContext(mockBlock(), mockTransaction(), contract, context)
ctx, err := NewContext(mockBlockForLib(tt.height), mockTransaction(), contract, context)

// direct run.
(func() {
engine := NewV8Engine(ctx)
engine.SetTimeOut(5 * 1000 * 1000)
_, err = engine.RunScriptSource(string(data), 0)
assert.Equal(t, ErrExecutionTimeout, err)
assert.Equal(t, tt.expectedErr, err)
engine.Dispose()
})()

Expand All @@ -373,14 +381,63 @@ func TestRunScriptSourceTimeout(t *testing.T) {
runnableSource := fmt.Sprintf("require(\"%s\");", moduleID)

engine := NewV8Engine(ctx)
engine.SetTimeOut(5 * 1000 * 1000)
engine.AddModule(moduleID, string(data), 0)
_, err = engine.RunScriptSource(runnableSource, 0)
assert.Equal(t, ErrExecutionTimeout, err)
assert.Equal(t, tt.expectedErr, err)
engine.Dispose()
})()
})
}
}
func TestCallTimeout(t *testing.T) {
core.SetCompatibilityOptions(100)

tests := []struct {
name string
contractPath string
sourceType string
height uint64
gasLimit uint64
expectedErr error
}{
{"case1", "./test/test_oom_4.js", "js", 1, 10000001, ErrInsufficientGas},
{"case1", "./test/test_oom_4.js", "js", 1, 50000000000, ErrExecutionTimeout},
{"case1", "./test/test_oom_4.js", "js", 2, 10000001, ErrExecutionTimeout},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := ioutil.ReadFile(tt.contractPath)
assert.Nil(t, err, "contract path read error")

mem, _ := storage.NewMemoryStorage()
context, _ := state.NewWorldState(dpos.NewDpos(), mem)
owner, err := context.GetOrCreateUserAccount([]byte("account1"))
assert.Nil(t, err)
owner.AddBalance(newUint128FromIntWrapper(10000000))
contract, _ := context.CreateContractAccount([]byte("account2"), nil, nil)

ctx, err := NewContext(mockBlockForLib(tt.height), mockTransaction(), contract, context)
engine := NewV8Engine(ctx)
engine.SetExecutionLimits(10000, 10000000)
_, err = engine.DeployAndInit(string(data), "js", "1")
engine.CollectTracingStats()
fmt.Printf("total:%v", engine.actualTotalMemorySize)
engine.Dispose()

engine = NewV8Engine(ctx)
engine.SetExecutionLimits(tt.gasLimit, 10000000)
_, err = engine.Call(string(data), "js", "loop", "")
// assert.Nil(t, err)
// assert.Equal(t, tt.initExceptErr, err.Error)

assert.Equal(t, tt.expectedErr, err)
engine.Dispose()

})
}
}

func TestDeployAndInitAndCall(t *testing.T) {
tests := []struct {
Expand Down
9 changes: 8 additions & 1 deletion nf/nvm/test/test_oom_4.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ SampleContract.prototype = {
newMem: function (mem) {
var c = new ArrayBuffer(mem);
console.log("c[1]:", c[1]);
}
},
loop: function(){
var a = 0;
while(true){
a +=1;
};
return a;
},
};

module.exports = SampleContract;

0 comments on commit a8bc312

Please sign in to comment.