forked from multiversx/mx-chain-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
systemSCFactory_test.go
304 lines (239 loc) · 8.92 KB
/
systemSCFactory_test.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package factory
import (
"errors"
"fmt"
"testing"
arwenConfig "github.com/ElrondNetwork/arwen-wasm-vm/v1_4/config"
"github.com/ElrondNetwork/elrond-go-core/core/check"
"github.com/ElrondNetwork/elrond-go/common"
"github.com/ElrondNetwork/elrond-go/config"
"github.com/ElrondNetwork/elrond-go/testscommon/hashingMocks"
"github.com/ElrondNetwork/elrond-go/vm"
"github.com/ElrondNetwork/elrond-go/vm/mock"
"github.com/ElrondNetwork/elrond-go/vm/systemSmartContracts/defaults"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func createMockNewSystemScFactoryArgs() ArgsNewSystemSCFactory {
gasMap := arwenConfig.MakeGasMapForTests()
gasMap = defaults.FillGasMapInternal(gasMap, 1)
gasSchedule := mock.NewGasScheduleNotifierMock(gasMap)
return ArgsNewSystemSCFactory{
SystemEI: &mock.SystemEIStub{},
Economics: &mock.EconomicsHandlerStub{},
SigVerifier: &mock.MessageSignVerifierMock{},
GasSchedule: gasSchedule,
NodesConfigProvider: &mock.NodesConfigProviderStub{},
Marshalizer: &mock.MarshalizerMock{},
Hasher: &hashingMocks.HasherMock{},
SystemSCConfig: &config.SystemSmartContractsConfig{
ESDTSystemSCConfig: config.ESDTSystemSCConfig{
BaseIssuingCost: "100000000",
OwnerAddress: "aaaaaa",
},
GovernanceSystemSCConfig: config.GovernanceSystemSCConfig{
Active: config.GovernanceSystemSCConfigActive{
ProposalCost: "500",
MinQuorum: "50",
MinPassThreshold: "50",
MinVetoThreshold: "50",
},
FirstWhitelistedAddress: "3132333435363738393031323334353637383930313233343536373839303234",
},
StakingSystemSCConfig: config.StakingSystemSCConfig{
GenesisNodePrice: "1000",
UnJailValue: "10",
MinStepValue: "10",
MinStakeValue: "1",
UnBondPeriod: 1,
NumRoundsWithoutBleed: 1,
MaximumPercentageToBleed: 1,
BleedPercentagePerRound: 1,
MaxNumberOfNodesForStake: 100,
ActivateBLSPubKeyMessageVerification: false,
MinUnstakeTokensValue: "1",
},
DelegationSystemSCConfig: config.DelegationSystemSCConfig{
MinServiceFee: 0,
MaxServiceFee: 10000,
},
DelegationManagerSystemSCConfig: config.DelegationManagerSystemSCConfig{
MinCreationDeposit: "10",
MinStakeAmount: "10",
ConfigChangeAddress: "3132333435363738393031323334353637383930313233343536373839303234",
},
},
EpochNotifier: &mock.EpochNotifierStub{},
AddressPubKeyConverter: &mock.PubkeyConverterMock{},
EpochConfig: &config.EpochConfig{
EnableEpochs: config.EnableEpochs{
StakingV2EnableEpoch: 1,
StakeEnableEpoch: 0,
DelegationSmartContractEnableEpoch: 0,
DelegationManagerEnableEpoch: 0,
},
},
ShardCoordinator: &mock.ShardCoordinatorStub{},
}
}
func TestNewSystemSCFactory_NilSystemEI(t *testing.T) {
t.Parallel()
arguments := createMockNewSystemScFactoryArgs()
arguments.SystemEI = nil
scFactory, err := NewSystemSCFactory(arguments)
assert.Nil(t, scFactory)
assert.True(t, errors.Is(err, vm.ErrNilSystemEnvironmentInterface))
}
func TestNewSystemSCFactory_NilSigVerifier(t *testing.T) {
t.Parallel()
arguments := createMockNewSystemScFactoryArgs()
arguments.SigVerifier = nil
scFactory, err := NewSystemSCFactory(arguments)
assert.Nil(t, scFactory)
assert.True(t, errors.Is(err, vm.ErrNilMessageSignVerifier))
}
func TestNewSystemSCFactory_NilNodesConfigProvider(t *testing.T) {
t.Parallel()
arguments := createMockNewSystemScFactoryArgs()
arguments.NodesConfigProvider = nil
scFactory, err := NewSystemSCFactory(arguments)
assert.Nil(t, scFactory)
assert.True(t, errors.Is(err, vm.ErrNilNodesConfigProvider))
}
func TestNewSystemSCFactory_NilMarshalizer(t *testing.T) {
t.Parallel()
arguments := createMockNewSystemScFactoryArgs()
arguments.Marshalizer = nil
scFactory, err := NewSystemSCFactory(arguments)
assert.Nil(t, scFactory)
assert.True(t, errors.Is(err, vm.ErrNilMarshalizer))
}
func TestNewSystemSCFactory_NilHasher(t *testing.T) {
t.Parallel()
arguments := createMockNewSystemScFactoryArgs()
arguments.Hasher = nil
scFactory, err := NewSystemSCFactory(arguments)
assert.Nil(t, scFactory)
assert.True(t, errors.Is(err, vm.ErrNilHasher))
}
func TestNewSystemSCFactory_NilEconomicsData(t *testing.T) {
t.Parallel()
arguments := createMockNewSystemScFactoryArgs()
arguments.Economics = nil
scFactory, err := NewSystemSCFactory(arguments)
assert.Nil(t, scFactory)
assert.True(t, errors.Is(err, vm.ErrNilEconomicsData))
}
func TestNewSystemSCFactory_NilSystemScConfig(t *testing.T) {
t.Parallel()
arguments := createMockNewSystemScFactoryArgs()
arguments.SystemSCConfig = nil
scFactory, err := NewSystemSCFactory(arguments)
assert.Nil(t, scFactory)
assert.True(t, errors.Is(err, vm.ErrNilSystemSCConfig))
}
func TestNewSystemSCFactory_NilEpochNotifier(t *testing.T) {
t.Parallel()
arguments := createMockNewSystemScFactoryArgs()
arguments.EpochNotifier = nil
scFactory, err := NewSystemSCFactory(arguments)
assert.Nil(t, scFactory)
assert.True(t, errors.Is(err, vm.ErrNilEpochNotifier))
}
func TestNewSystemSCFactory_NilPubKeyConverter(t *testing.T) {
t.Parallel()
arguments := createMockNewSystemScFactoryArgs()
arguments.AddressPubKeyConverter = nil
scFactory, err := NewSystemSCFactory(arguments)
assert.Nil(t, scFactory)
assert.True(t, errors.Is(err, vm.ErrNilAddressPubKeyConverter))
}
func TestNewSystemSCFactory_NilShardCoordinator(t *testing.T) {
t.Parallel()
arguments := createMockNewSystemScFactoryArgs()
arguments.ShardCoordinator = nil
scFactory, err := NewSystemSCFactory(arguments)
assert.True(t, check.IfNil(scFactory))
assert.True(t, errors.Is(err, vm.ErrNilShardCoordinator))
}
func TestNewSystemSCFactory_Ok(t *testing.T) {
t.Parallel()
arguments := createMockNewSystemScFactoryArgs()
scFactory, err := NewSystemSCFactory(arguments)
assert.Nil(t, err)
assert.NotNil(t, scFactory)
}
func TestNewSystemSCFactory_GasScheduleChangeMissingElementsShouldNotPanic(t *testing.T) {
t.Parallel()
defer func() {
r := recover()
if r != nil {
assert.Fail(t, fmt.Sprintf("should have not panicked: %v", r))
}
}()
arguments := createMockNewSystemScFactoryArgs()
scFactory, _ := NewSystemSCFactory(arguments)
gasSchedule, err := common.LoadGasScheduleConfig("../../cmd/node/config/gasSchedules/gasScheduleV3.toml")
delete(gasSchedule["MetaChainSystemSCsCost"], "UnstakeTokens")
require.Nil(t, err)
scFactory.GasScheduleChange(gasSchedule)
assert.Equal(t, uint64(1), scFactory.gasCost.MetaChainSystemSCsCost.UnStakeTokens)
}
func TestNewSystemSCFactory_GasScheduleChangeShouldWork(t *testing.T) {
t.Parallel()
defer func() {
r := recover()
if r != nil {
assert.Fail(t, fmt.Sprintf("should have not panicked: %v", r))
}
}()
arguments := createMockNewSystemScFactoryArgs()
scFactory, _ := NewSystemSCFactory(arguments)
gasSchedule, err := common.LoadGasScheduleConfig("../../cmd/node/config/gasSchedules/gasScheduleV3.toml")
require.Nil(t, err)
scFactory.GasScheduleChange(gasSchedule)
assert.Equal(t, uint64(5000000), scFactory.gasCost.MetaChainSystemSCsCost.UnStakeTokens)
}
func TestSystemSCFactory_CreateWithBadDelegationManagerConfigChangeAddressShouldError(t *testing.T) {
t.Parallel()
arguments := createMockNewSystemScFactoryArgs()
arguments.SystemSCConfig.DelegationManagerSystemSCConfig.ConfigChangeAddress = "not a hex string"
scFactory, _ := NewSystemSCFactory(arguments)
container, err := scFactory.Create()
assert.True(t, check.IfNil(container))
assert.True(t, errors.Is(err, vm.ErrInvalidAddress))
}
func TestSystemSCFactory_CreateWithFirstWhiteListAddressShouldError(t *testing.T) {
t.Parallel()
arguments := createMockNewSystemScFactoryArgs()
arguments.SystemSCConfig.GovernanceSystemSCConfig.FirstWhitelistedAddress = "not a hex string"
scFactory, _ := NewSystemSCFactory(arguments)
container, err := scFactory.Create()
assert.True(t, check.IfNil(container))
assert.True(t, errors.Is(err, vm.ErrInvalidAddress))
}
func TestSystemSCFactory_Create(t *testing.T) {
t.Parallel()
arguments := createMockNewSystemScFactoryArgs()
scFactory, _ := NewSystemSCFactory(arguments)
container, err := scFactory.Create()
assert.Nil(t, err)
require.NotNil(t, container)
assert.Equal(t, 6, container.Len())
}
func TestSystemSCFactory_CreateForGenesis(t *testing.T) {
t.Parallel()
arguments := createMockNewSystemScFactoryArgs()
scFactory, _ := NewSystemSCFactory(arguments)
container, err := scFactory.CreateForGenesis()
assert.Nil(t, err)
assert.Equal(t, 4, container.Len())
}
func TestSystemSCFactory_IsInterfaceNil(t *testing.T) {
t.Parallel()
arguments := createMockNewSystemScFactoryArgs()
scFactory, _ := NewSystemSCFactory(arguments)
assert.False(t, scFactory.IsInterfaceNil())
scFactory = nil
assert.True(t, check.IfNil(scFactory))
}