/
instanceSmartContractCallerTest.go
116 lines (96 loc) · 4.1 KB
/
instanceSmartContractCallerTest.go
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
package testcommon
import (
"testing"
"github.com/ElrondNetwork/wasm-vm/arwen"
"github.com/ElrondNetwork/wasm-vm/config"
contextmock "github.com/ElrondNetwork/wasm-vm/mock/context"
vmcommon "github.com/ElrondNetwork/elrond-vm-common"
)
// InstanceTestSmartContract represents the config data for the smart contract instance to be tested
type InstanceTestSmartContract struct {
testSmartContract
code []byte
}
// CreateInstanceContract build a contract to be used in a test creted with BuildInstanceCallTest
func CreateInstanceContract(address []byte) *InstanceTestSmartContract {
return &InstanceTestSmartContract{
testSmartContract: testSmartContract{
address: address,
},
}
}
// WithBalance provides the balance for the InstanceTestSmartContract
func (mockSC *InstanceTestSmartContract) WithBalance(balance int64) *InstanceTestSmartContract {
mockSC.balance = balance
return mockSC
}
// WithConfig provides the config object for the InstanceTestSmartContract
func (mockSC *InstanceTestSmartContract) WithConfig(config interface{}) *InstanceTestSmartContract {
mockSC.config = config
return mockSC
}
// WithCode provides the code for the InstanceTestSmartContract
func (mockSC *InstanceTestSmartContract) WithCode(code []byte) *InstanceTestSmartContract {
mockSC.code = code
return mockSC
}
// InstancesTestTemplate holds the data to build a contract call test
type InstancesTestTemplate struct {
testTemplateConfig
contracts []*InstanceTestSmartContract
gasSchedule config.GasScheduleMap
setup func(arwen.VMHost, *contextmock.BlockchainHookStub)
assertResults func(arwen.VMHost, *contextmock.BlockchainHookStub, *VMOutputVerifier)
}
// BuildInstanceCallTest starts the building process for a contract call test
func BuildInstanceCallTest(tb testing.TB) *InstancesTestTemplate {
return &InstancesTestTemplate{
testTemplateConfig: testTemplateConfig{
tb: tb,
useMocks: false,
wasmerSIGSEGVPassthrough: false,
},
setup: func(arwen.VMHost, *contextmock.BlockchainHookStub) {},
}
}
// WithContracts provides the contracts to be used by the contract call test
func (callerTest *InstancesTestTemplate) WithContracts(usedContracts ...*InstanceTestSmartContract) *InstancesTestTemplate {
callerTest.contracts = usedContracts
return callerTest
}
// WithInput provides the ContractCallInput to be used by the contract call test
func (callerTest *InstancesTestTemplate) WithInput(input *vmcommon.ContractCallInput) *InstancesTestTemplate {
callerTest.input = input
return callerTest
}
// WithSetup provides the setup function to be used by the contract call test
func (callerTest *InstancesTestTemplate) WithSetup(setup func(arwen.VMHost, *contextmock.BlockchainHookStub)) *InstancesTestTemplate {
callerTest.setup = setup
return callerTest
}
// WithGasSchedule provides gas schedule for the test
func (callerTest *InstancesTestTemplate) WithGasSchedule(gasSchedule config.GasScheduleMap) *InstancesTestTemplate {
callerTest.gasSchedule = gasSchedule
return callerTest
}
// WithWasmerSIGSEGVPassthrough sets the wasmerSIGSEGVPassthrough flag
func (callerTest *InstancesTestTemplate) WithWasmerSIGSEGVPassthrough(wasmerSIGSEGVPassthrough bool) *InstancesTestTemplate {
callerTest.wasmerSIGSEGVPassthrough = wasmerSIGSEGVPassthrough
return callerTest
}
// AndAssertResults starts the test and asserts the results
func (callerTest *InstancesTestTemplate) AndAssertResults(assertResults func(arwen.VMHost, *contextmock.BlockchainHookStub, *VMOutputVerifier)) {
callerTest.assertResults = assertResults
runTestWithInstances(callerTest)
}
func runTestWithInstances(callerTest *InstancesTestTemplate) {
host, blockchainHookStub := defaultTestArwenForContracts(callerTest.tb, callerTest.contracts, callerTest.gasSchedule, callerTest.wasmerSIGSEGVPassthrough)
defer func() {
host.Reset()
}()
callerTest.setup(host, blockchainHookStub)
vmOutput, err := host.RunSmartContractCall(callerTest.input)
allErrors := host.Runtime().GetAllErrors()
verify := NewVMOutputVerifierWithAllErrors(callerTest.tb, vmOutput, err, allErrors)
callerTest.assertResults(host, blockchainHookStub, verify)
}