-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathinstanceSmartContractCreatorTest.go
More file actions
133 lines (113 loc) · 5.2 KB
/
instanceSmartContractCreatorTest.go
File metadata and controls
133 lines (113 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Package testcommon contains utility definitions used in unit and integration tests
package testcommon
import (
"testing"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
"github.com/multiversx/mx-chain-vm-go/executor"
executorwrapper "github.com/multiversx/mx-chain-vm-go/executor/wrapper"
contextmock "github.com/multiversx/mx-chain-vm-go/mock/context"
"github.com/multiversx/mx-chain-vm-go/vmhost"
"github.com/stretchr/testify/require"
)
// InstanceCreatorTestTemplate holds the data to build a contract creation test
type InstanceCreatorTestTemplate struct {
tb testing.TB
address []byte
input *vmcommon.ContractCreateInput
setup func(vmhost.VMHost, *contextmock.BlockchainHookStub)
assertResults func(*contextmock.BlockchainHookStub, *VMOutputVerifier)
host vmhost.VMHost
hostBuilder *TestHostBuilder
stubAccountInitialNonce uint64
}
// BuildInstanceCreatorTest starts the building process for a contract creation test
func BuildInstanceCreatorTest(tb testing.TB) *InstanceCreatorTestTemplate {
return &InstanceCreatorTestTemplate{
tb: tb,
setup: func(vmhost.VMHost, *contextmock.BlockchainHookStub) {},
hostBuilder: NewTestHostBuilder(tb),
stubAccountInitialNonce: 24,
}
}
// WithExecutorFactory allows caller to choose the Executor type.
func (template *InstanceCreatorTestTemplate) WithExecutorFactory(factory executor.ExecutorAbstractFactory) *InstanceCreatorTestTemplate {
template.hostBuilder.WithExecutorFactory(factory)
return template
}
// WithExecutorLogs sets an ExecutorLogger
func (template *InstanceCreatorTestTemplate) WithExecutorLogs(executorLogger executorwrapper.ExecutorLogger) *InstanceCreatorTestTemplate {
template.hostBuilder.WithExecutorLogs(executorLogger)
return template
}
// WithInput provides the ContractCreateInput for a TestCreateTemplateConfig
func (template *InstanceCreatorTestTemplate) WithInput(input *vmcommon.ContractCreateInput) *InstanceCreatorTestTemplate {
template.input = input
return template
}
// WithWasmerSIGSEGVPassthrough sets the wasmerSIGSEGVPassthrough flag
func (template *InstanceCreatorTestTemplate) WithWasmerSIGSEGVPassthrough(passthrough bool) *InstanceCreatorTestTemplate {
template.hostBuilder.WithWasmerSIGSEGVPassthrough(passthrough)
return template
}
// WithEnableEpochsHandler sets the enableEpochsHandler
func (template *InstanceCreatorTestTemplate) WithEnableEpochsHandler(enableEpochsHandler vmcommon.EnableEpochsHandler) *InstanceCreatorTestTemplate {
template.hostBuilder.WithEnableEpochsHandler(enableEpochsHandler)
return template
}
// WithAddress provides the address for a TestCreateTemplateConfig
func (template *InstanceCreatorTestTemplate) WithAddress(address []byte) *InstanceCreatorTestTemplate {
template.address = address
return template
}
// WithSetup provides the setup function for a TestCreateTemplateConfig
func (template *InstanceCreatorTestTemplate) WithSetup(setup func(vmhost.VMHost, *contextmock.BlockchainHookStub)) *InstanceCreatorTestTemplate {
template.setup = setup
return template
}
// AndAssertResults provides the function that will aserts the results
func (template *InstanceCreatorTestTemplate) AndAssertResults(assertResults func(*contextmock.BlockchainHookStub, *VMOutputVerifier)) {
template.assertResults = assertResults
template.runTestWithVerification(true)
}
// AndAssertResultsWithoutReset provides the function that will aserts the results
func (template *InstanceCreatorTestTemplate) AndAssertResultsWithoutReset(assertResults func(*contextmock.BlockchainHookStub, *VMOutputVerifier)) {
template.assertResults = assertResults
template.runTestWithVerification(false)
}
func (template *InstanceCreatorTestTemplate) runTestWithVerification(reset bool) {
blhookStub, vmOutput, err := template.RunTest(reset)
verify := NewVMOutputVerifier(template.tb, vmOutput, err)
template.assertResults(blhookStub, verify)
}
// RunTest executes the built test directly, without any assertions.
func (template *InstanceCreatorTestTemplate) RunTest(reset bool) (*contextmock.BlockchainHookStub, *vmcommon.VMOutput, error) {
var blhookStub *contextmock.BlockchainHookStub
if template.host == nil {
blhookStub = template.createBlockchainStub()
template.hostBuilder.WithBlockchainHook(blhookStub)
template.host = template.hostBuilder.Build()
template.setup(template.host, blhookStub)
}
defer func() {
if reset {
template.host.Reset()
}
// Extra verification for instance leaks
err := template.host.Runtime().ValidateInstances()
require.Nil(template.tb, err)
}()
vmOutput, err := template.host.RunSmartContractCreate(template.input)
return blhookStub, vmOutput, err
}
func (template *InstanceCreatorTestTemplate) createBlockchainStub() *contextmock.BlockchainHookStub {
stubBlockchainHook := &contextmock.BlockchainHookStub{}
stubBlockchainHook.GetUserAccountCalled = func(address []byte) (vmcommon.UserAccountHandler, error) {
return &contextmock.StubAccount{
Nonce: 24,
}, nil
}
stubBlockchainHook.NewAddressCalled = func(creatorAddress []byte, nonce uint64, vmType []byte) ([]byte, error) {
return template.address, nil
}
return stubBlockchainHook
}