@dashevo/wasm-dpp 0.24.0-dev.23 → 0.24.0-dev.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/wasm/wasm_dpp.d.ts +753 -731
- package/dist/wasm/wasm_dpp.js +60 -52
- package/dist/wasm/wasm_dpp_bg.js +1 -1
- package/lib/test/fixtures/getIdentityTopUpTransitionFixture.js +26 -1
- package/lib/test/fixtures/getIdentityTopUpTransitionFixtureJs.js +1 -0
- package/lib/wasm/wasm_dpp.d.ts +753 -731
- package/package.json +3 -3
- package/src/errors/consensus/fee/balance_is_not_enough_error.rs +4 -4
- package/src/identity/credits_converter.rs +7 -0
- package/src/identity/mod.rs +1 -0
- package/src/state_transition/fee/mod.rs +2 -0
- package/src/state_transition/validation/mod.rs +1 -0
- package/src/state_transition/validation/state_transition_fee_validator.rs +39 -0
- package/test/integration/identity/stateTransition/IdentityTopUpTransition/validation/basic/validateIdentityTopUpTransitionBasicFactory.spec.js +1 -1
- package/test/integration/stateTransition/StateTransitionFacade.spec.js +1 -0
- package/test/unit/stateTransition/validation/validateStateTransitionFeeFactory.spec.js +124 -199
- package/test/unit/stateTransition/validation/validateStateTransitionKeySignatureFactory.spec.js +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dashevo/wasm-dpp",
|
|
3
|
-
"version": "0.24.0-dev.
|
|
3
|
+
"version": "0.24.0-dev.25",
|
|
4
4
|
"description": "The JavaScript implementation of the Dash Platform Protocol",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -44,8 +44,8 @@
|
|
|
44
44
|
"@babel/core": "^7.15.5",
|
|
45
45
|
"@babel/preset-env": "^7.15.4",
|
|
46
46
|
"@dashevo/dashcore-lib": "~0.20.3",
|
|
47
|
-
"@dashevo/dpns-contract": "0.24.0-dev.
|
|
48
|
-
"@dashevo/dpp": "0.24.0-dev.
|
|
47
|
+
"@dashevo/dpns-contract": "0.24.0-dev.25",
|
|
48
|
+
"@dashevo/dpp": "0.24.0-dev.25",
|
|
49
49
|
"@dashevo/wasm-re2": "~1.0.2",
|
|
50
50
|
"@types/bs58": "^4.0.1",
|
|
51
51
|
"@types/node": "^14.6.0",
|
|
@@ -26,13 +26,13 @@ impl BalanceIsNotEnoughErrorWasm {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
#[wasm_bindgen(js_name=getBalance)]
|
|
29
|
-
pub fn get_balance(&self) ->
|
|
30
|
-
self.inner.balance()
|
|
29
|
+
pub fn get_balance(&self) -> f64 {
|
|
30
|
+
self.inner.balance() as f64
|
|
31
31
|
}
|
|
32
32
|
|
|
33
33
|
#[wasm_bindgen(js_name=getFee)]
|
|
34
|
-
pub fn get_fee(&self) ->
|
|
35
|
-
self.inner.fee()
|
|
34
|
+
pub fn get_fee(&self) -> f64 {
|
|
35
|
+
self.inner.fee() as f64
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
#[wasm_bindgen(js_name=getCode)]
|
package/src/identity/mod.rs
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
use crate::state_repository::{ExternalStateRepositoryLike, ExternalStateRepositoryLikeWrapper};
|
|
2
|
+
use dpp::state_transition::validation::validate_state_transition_fee::StateTransitionFeeValidator;
|
|
3
|
+
use std::sync::Arc;
|
|
4
|
+
use wasm_bindgen::prelude::wasm_bindgen;
|
|
5
|
+
use wasm_bindgen::JsValue;
|
|
6
|
+
|
|
7
|
+
use crate::state_transition::conversion::create_state_transition_from_wasm_instance;
|
|
8
|
+
use crate::utils::WithJsError;
|
|
9
|
+
use crate::validation::ValidationResultWasm;
|
|
10
|
+
|
|
11
|
+
#[wasm_bindgen(js_name=StateTransitionFeeValidator)]
|
|
12
|
+
pub struct StateTransitionFeeValidatorWasm(
|
|
13
|
+
StateTransitionFeeValidator<ExternalStateRepositoryLikeWrapper>,
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
#[wasm_bindgen(js_class=StateTransitionFeeValidator)]
|
|
17
|
+
impl StateTransitionFeeValidatorWasm {
|
|
18
|
+
#[wasm_bindgen(constructor)]
|
|
19
|
+
pub fn new(state_repository: ExternalStateRepositoryLike) -> Self {
|
|
20
|
+
Self(StateTransitionFeeValidator::new(Arc::new(
|
|
21
|
+
ExternalStateRepositoryLikeWrapper::new(state_repository),
|
|
22
|
+
)))
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
#[wasm_bindgen]
|
|
26
|
+
pub async fn validate(
|
|
27
|
+
&self,
|
|
28
|
+
state_transition: &JsValue,
|
|
29
|
+
) -> Result<ValidationResultWasm, JsValue> {
|
|
30
|
+
let st = create_state_transition_from_wasm_instance(state_transition)?;
|
|
31
|
+
Ok(self
|
|
32
|
+
.0
|
|
33
|
+
.validate(&st)
|
|
34
|
+
.await
|
|
35
|
+
.map(|v| v.map(|_| JsValue::undefined()))
|
|
36
|
+
.with_js_error()?
|
|
37
|
+
.into())
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const createStateRepositoryMock = require('@dashevo/dpp/lib/test/mocks/createStateRepositoryMock');
|
|
2
|
-
const getIdentityTopUpTransitionFixture = require('../../../../../../../lib/test/fixtures/
|
|
2
|
+
const getIdentityTopUpTransitionFixture = require('../../../../../../../lib/test/fixtures/getIdentityTopUpTransitionFixtureJs');
|
|
3
3
|
|
|
4
4
|
const {
|
|
5
5
|
expectJsonSchemaError,
|
|
@@ -171,6 +171,7 @@ describe('StateTransitionFacade', () => {
|
|
|
171
171
|
});
|
|
172
172
|
|
|
173
173
|
it('should return invalid result if not enough balance to pay fee for State Transition', async () => {
|
|
174
|
+
identity.setBalance(0);
|
|
174
175
|
stateRepositoryMock.fetchIdentityBalance.resolves(0);
|
|
175
176
|
const result = await dpp.stateTransition.validate(dataContractCreateTransition);
|
|
176
177
|
|
|
@@ -1,167 +1,157 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
const createStateRepositoryMock = require('
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const getDataContractFixture = require('
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const getIdentityTopUpTransitionFixture = require('
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const {
|
|
20
|
-
const StateTransitionExecutionContext = require('@dashevo/dpp/lib/stateTransition/StateTransitionExecutionContext');
|
|
1
|
+
const { expectValidationError } = require('../../../../lib/test/expect/expectError');
|
|
2
|
+
|
|
3
|
+
const createStateRepositoryMock = require('../../../../lib/test/mocks/createStateRepositoryMock');
|
|
4
|
+
const getBlsMock = require('../../../../lib/test/mocks/getBlsAdapterMock');
|
|
5
|
+
|
|
6
|
+
const getDataContractFixture = require('../../../../lib/test/fixtures/getDataContractFixture');
|
|
7
|
+
const getDocumentsFixture = require('../../../../lib/test/fixtures/getDocumentsFixture');
|
|
8
|
+
const getIdentityCreateTransitionFixture = require('../../../../lib/test/fixtures/getIdentityCreateTransitionFixture');
|
|
9
|
+
const getIdentityTopUpTransitionFixture = require('../../../../lib/test/fixtures/getIdentityTopUpTransitionFixture');
|
|
10
|
+
|
|
11
|
+
let {
|
|
12
|
+
StateTransitionFeeValidator,
|
|
13
|
+
DashPlatformProtocol,
|
|
14
|
+
IdentityBalanceIsNotEnoughError,
|
|
15
|
+
StateTransitionExecutionContext,
|
|
16
|
+
PreCalculatedOperation,
|
|
17
|
+
getCreditsConversionRatio,
|
|
18
|
+
} = require('../../../..');
|
|
19
|
+
const { default: loadWasmDpp } = require('../../../..');
|
|
21
20
|
|
|
22
21
|
describe('validateStateTransitionFeeFactory', () => {
|
|
23
22
|
let stateRepositoryMock;
|
|
24
23
|
let validateStateTransitionFee;
|
|
25
24
|
let dataContract;
|
|
26
|
-
let
|
|
27
|
-
let
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
let dpp;
|
|
26
|
+
let dataContractOwnerId;
|
|
27
|
+
let executionContext;
|
|
28
|
+
|
|
29
|
+
beforeEach(async function beforeEach() {
|
|
30
|
+
({
|
|
31
|
+
StateTransitionFeeValidator,
|
|
32
|
+
IdentityBalanceIsNotEnoughError,
|
|
33
|
+
DashPlatformProtocol,
|
|
34
|
+
StateTransitionExecutionContext,
|
|
35
|
+
PreCalculatedOperation,
|
|
36
|
+
getCreditsConversionRatio,
|
|
37
|
+
} = await loadWasmDpp());
|
|
30
38
|
stateRepositoryMock = createStateRepositoryMock(this.sinonSandbox);
|
|
39
|
+
stateRepositoryMock.fetchDataContract.resolves(undefined);
|
|
31
40
|
|
|
32
|
-
|
|
41
|
+
dpp = new DashPlatformProtocol(getBlsMock(), stateRepositoryMock);
|
|
33
42
|
|
|
34
|
-
|
|
35
|
-
desiredAmount: 42,
|
|
36
|
-
});
|
|
43
|
+
const validator = new StateTransitionFeeValidator(stateRepositoryMock);
|
|
37
44
|
|
|
38
|
-
|
|
45
|
+
validateStateTransitionFee = (st) => validator.validate(st);
|
|
39
46
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
calculateStateTransitionFeeMock,
|
|
43
|
-
fetchAssetLockTransactionOutputMock,
|
|
44
|
-
);
|
|
47
|
+
executionContext = new StateTransitionExecutionContext();
|
|
48
|
+
executionContext.disableDryRun();
|
|
45
49
|
|
|
46
|
-
dataContract = getDataContractFixture();
|
|
50
|
+
dataContract = await getDataContractFixture();
|
|
47
51
|
});
|
|
48
52
|
|
|
49
53
|
describe('DataContractCreateTransition', () => {
|
|
50
54
|
let dataContractCreateTransition;
|
|
51
55
|
|
|
52
56
|
beforeEach(() => {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
});
|
|
57
|
+
dataContractOwnerId = dataContract.getOwnerId().toBuffer();
|
|
58
|
+
dataContractCreateTransition = dpp.dataContract
|
|
59
|
+
.createDataContractCreateTransition(dataContract);
|
|
57
60
|
});
|
|
58
61
|
|
|
59
62
|
it('should return invalid result if balance is not enough', async () => {
|
|
63
|
+
executionContext.addOperation(new PreCalculatedOperation(0, 42, []));
|
|
64
|
+
dataContractCreateTransition.setExecutionContext(executionContext);
|
|
60
65
|
stateRepositoryMock.fetchIdentityBalance.resolves(1);
|
|
61
66
|
|
|
62
67
|
const result = await validateStateTransitionFee(dataContractCreateTransition);
|
|
63
68
|
|
|
64
|
-
expectValidationError(result, IdentityBalanceIsNotEnoughError);
|
|
69
|
+
await expectValidationError(result, IdentityBalanceIsNotEnoughError);
|
|
65
70
|
|
|
66
71
|
const [error] = result.getErrors();
|
|
67
72
|
|
|
68
73
|
expect(error.getCode()).to.equal(3000);
|
|
69
74
|
expect(error.getBalance()).to.equal(1);
|
|
70
75
|
|
|
71
|
-
expect(stateRepositoryMock.fetchIdentityBalance).to.be.
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
);
|
|
75
|
-
|
|
76
|
-
expect(calculateStateTransitionFeeMock).to.be.calledOnceWithExactly(
|
|
77
|
-
dataContractCreateTransition,
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
expect(fetchAssetLockTransactionOutputMock).to.not.be.called();
|
|
76
|
+
expect(stateRepositoryMock.fetchIdentityBalance).to.be.calledOnce();
|
|
77
|
+
expect(
|
|
78
|
+
stateRepositoryMock.fetchIdentityBalance.getCall(0).args[0].toBuffer(),
|
|
79
|
+
).to.be.deep.equal(dataContractOwnerId);
|
|
81
80
|
});
|
|
82
81
|
|
|
83
82
|
it('should return valid result', async () => {
|
|
84
83
|
stateRepositoryMock.fetchIdentityBalance.resolves(42);
|
|
85
84
|
|
|
85
|
+
executionContext.addOperation(new PreCalculatedOperation(0, 42, []));
|
|
86
|
+
dataContractCreateTransition.setExecutionContext(executionContext);
|
|
87
|
+
|
|
86
88
|
const result = await validateStateTransitionFee(dataContractCreateTransition);
|
|
87
89
|
|
|
88
90
|
expect(result.isValid()).to.be.true();
|
|
89
91
|
|
|
90
|
-
expect(stateRepositoryMock.fetchIdentityBalance).to.be.
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
);
|
|
94
|
-
|
|
95
|
-
expect(calculateStateTransitionFeeMock).to.be.calledOnceWithExactly(
|
|
96
|
-
dataContractCreateTransition,
|
|
97
|
-
);
|
|
98
|
-
|
|
99
|
-
expect(fetchAssetLockTransactionOutputMock).to.not.be.called();
|
|
92
|
+
expect(stateRepositoryMock.fetchIdentityBalance).to.be.calledOnce();
|
|
93
|
+
expect(
|
|
94
|
+
stateRepositoryMock.fetchIdentityBalance.getCall(0).args[0].toBuffer(),
|
|
95
|
+
).to.be.deep.equal(dataContractOwnerId);
|
|
100
96
|
});
|
|
101
97
|
});
|
|
102
98
|
|
|
103
99
|
describe('DocumentsBatchTransition', () => {
|
|
104
100
|
let documentsBatchTransition;
|
|
101
|
+
let ownerId;
|
|
105
102
|
|
|
106
|
-
beforeEach(() => {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const documentTransitions = getDocumentTransitionsFixture({
|
|
110
|
-
create: documents,
|
|
103
|
+
beforeEach(async () => {
|
|
104
|
+
documentsBatchTransition = await dpp.document.createStateTransition({
|
|
105
|
+
create: await getDocumentsFixture(),
|
|
111
106
|
});
|
|
112
107
|
|
|
113
|
-
|
|
114
|
-
ownerId: getDocumentsFixture.ownerId,
|
|
115
|
-
contractId: dataContract.getId(),
|
|
116
|
-
transitions: documentTransitions.map((t) => t.toObject()),
|
|
117
|
-
}, [dataContract]);
|
|
108
|
+
ownerId = documentsBatchTransition.getOwnerId().toBuffer();
|
|
118
109
|
});
|
|
119
110
|
|
|
120
111
|
it('should return invalid result if balance is not enough', async () => {
|
|
121
112
|
stateRepositoryMock.fetchIdentityBalance.resolves(1);
|
|
122
113
|
|
|
114
|
+
executionContext.addOperation(new PreCalculatedOperation(0, 42, []));
|
|
115
|
+
documentsBatchTransition.setExecutionContext(executionContext);
|
|
116
|
+
|
|
123
117
|
const result = await validateStateTransitionFee(documentsBatchTransition);
|
|
124
118
|
|
|
125
|
-
expectValidationError(result, IdentityBalanceIsNotEnoughError);
|
|
119
|
+
await expectValidationError(result, IdentityBalanceIsNotEnoughError);
|
|
126
120
|
|
|
127
121
|
const [error] = result.getErrors();
|
|
128
122
|
|
|
129
123
|
expect(error.getCode()).to.equal(3000);
|
|
130
124
|
expect(error.getBalance()).to.equal(1);
|
|
131
125
|
|
|
132
|
-
expect(stateRepositoryMock.fetchIdentityBalance).to.be.
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
);
|
|
136
|
-
|
|
137
|
-
expect(calculateStateTransitionFeeMock).to.be.calledOnceWithExactly(
|
|
138
|
-
documentsBatchTransition,
|
|
139
|
-
);
|
|
140
|
-
|
|
141
|
-
expect(fetchAssetLockTransactionOutputMock).to.not.be.called();
|
|
126
|
+
expect(stateRepositoryMock.fetchIdentityBalance).to.be.calledOnce();
|
|
127
|
+
expect(
|
|
128
|
+
stateRepositoryMock.fetchIdentityBalance.getCall(0).args[0].toBuffer(),
|
|
129
|
+
).to.be.deep.equal(ownerId);
|
|
142
130
|
});
|
|
143
131
|
|
|
144
132
|
it('should return valid result', async () => {
|
|
145
133
|
stateRepositoryMock.fetchIdentityBalance.resolves(42);
|
|
146
134
|
|
|
135
|
+
executionContext.addOperation(new PreCalculatedOperation(0, 42, []));
|
|
136
|
+
documentsBatchTransition.setExecutionContext(executionContext);
|
|
137
|
+
|
|
147
138
|
const result = await validateStateTransitionFee(documentsBatchTransition);
|
|
148
139
|
|
|
149
140
|
expect(result.isValid()).to.be.true();
|
|
150
141
|
|
|
151
|
-
expect(stateRepositoryMock.fetchIdentityBalance).to.be.
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
);
|
|
155
|
-
|
|
156
|
-
expect(calculateStateTransitionFeeMock).to.be.calledOnceWithExactly(
|
|
157
|
-
documentsBatchTransition,
|
|
158
|
-
);
|
|
159
|
-
|
|
160
|
-
expect(fetchAssetLockTransactionOutputMock).to.not.be.called();
|
|
142
|
+
expect(stateRepositoryMock.fetchIdentityBalance).to.be.calledOnce();
|
|
143
|
+
expect(
|
|
144
|
+
stateRepositoryMock.fetchIdentityBalance.getCall(0).args[0].toBuffer(),
|
|
145
|
+
).to.be.deep.equal(ownerId);
|
|
161
146
|
});
|
|
162
147
|
|
|
163
148
|
it('should not increase balance on dry run', async () => {
|
|
164
|
-
|
|
149
|
+
stateRepositoryMock.fetchIdentityBalance.resolves(1);
|
|
150
|
+
|
|
151
|
+
executionContext.enableDryRun();
|
|
152
|
+
|
|
153
|
+
executionContext.addOperation(new PreCalculatedOperation(0, 42, []));
|
|
154
|
+
documentsBatchTransition.setExecutionContext(executionContext);
|
|
165
155
|
|
|
166
156
|
const result = await validateStateTransitionFee(documentsBatchTransition);
|
|
167
157
|
|
|
@@ -169,12 +159,10 @@ describe('validateStateTransitionFeeFactory', () => {
|
|
|
169
159
|
|
|
170
160
|
expect(result.isValid()).to.be.true();
|
|
171
161
|
|
|
172
|
-
expect(
|
|
173
|
-
expect(
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
);
|
|
177
|
-
expect(fetchAssetLockTransactionOutputMock).to.not.be.called();
|
|
162
|
+
expect(stateRepositoryMock.fetchIdentityBalance).to.be.calledOnce();
|
|
163
|
+
expect(
|
|
164
|
+
stateRepositoryMock.fetchIdentityBalance.getCall(0).args[0].toBuffer(),
|
|
165
|
+
).to.be.deep.equal(ownerId);
|
|
178
166
|
});
|
|
179
167
|
});
|
|
180
168
|
|
|
@@ -182,22 +170,23 @@ describe('validateStateTransitionFeeFactory', () => {
|
|
|
182
170
|
let identityCreateTransition;
|
|
183
171
|
let outputAmount;
|
|
184
172
|
|
|
185
|
-
beforeEach(() => {
|
|
186
|
-
identityCreateTransition = getIdentityCreateTransitionFixture();
|
|
173
|
+
beforeEach(async () => {
|
|
174
|
+
identityCreateTransition = await getIdentityCreateTransitionFixture();
|
|
187
175
|
|
|
188
176
|
const { satoshis } = identityCreateTransition
|
|
189
177
|
.getAssetLockProof()
|
|
190
178
|
.getOutput();
|
|
191
179
|
|
|
192
|
-
outputAmount = satoshis *
|
|
180
|
+
outputAmount = satoshis * getCreditsConversionRatio();
|
|
193
181
|
});
|
|
194
182
|
|
|
195
183
|
it('should return invalid result if asset lock output amount is not enough', async () => {
|
|
196
|
-
|
|
184
|
+
executionContext.addOperation(new PreCalculatedOperation(0, outputAmount + 1, []));
|
|
185
|
+
identityCreateTransition.setExecutionContext(executionContext);
|
|
197
186
|
|
|
198
187
|
const result = await validateStateTransitionFee(identityCreateTransition);
|
|
199
188
|
|
|
200
|
-
expectValidationError(result, IdentityBalanceIsNotEnoughError);
|
|
189
|
+
await expectValidationError(result, IdentityBalanceIsNotEnoughError);
|
|
201
190
|
|
|
202
191
|
const [error] = result.getErrors();
|
|
203
192
|
|
|
@@ -205,122 +194,91 @@ describe('validateStateTransitionFeeFactory', () => {
|
|
|
205
194
|
expect(error.getBalance()).to.equal(outputAmount);
|
|
206
195
|
|
|
207
196
|
expect(stateRepositoryMock.fetchIdentity).to.be.not.called();
|
|
208
|
-
|
|
209
|
-
expect(calculateStateTransitionFeeMock).to.be.calledOnceWithExactly(
|
|
210
|
-
identityCreateTransition,
|
|
211
|
-
);
|
|
212
|
-
|
|
213
|
-
expect(fetchAssetLockTransactionOutputMock).to.be.calledOnceWithExactly(
|
|
214
|
-
identityCreateTransition.getAssetLockProof(),
|
|
215
|
-
identityCreateTransition.getExecutionContext(),
|
|
216
|
-
);
|
|
217
197
|
});
|
|
218
198
|
|
|
219
199
|
it('should return valid result', async () => {
|
|
220
|
-
|
|
200
|
+
executionContext.addOperation(new PreCalculatedOperation(0, outputAmount, []));
|
|
201
|
+
identityCreateTransition.setExecutionContext(executionContext);
|
|
221
202
|
|
|
222
203
|
const result = await validateStateTransitionFee(identityCreateTransition);
|
|
223
204
|
|
|
224
205
|
expect(result.isValid()).to.be.true();
|
|
225
206
|
|
|
226
207
|
expect(stateRepositoryMock.fetchIdentity).to.be.not.called();
|
|
227
|
-
|
|
228
|
-
expect(calculateStateTransitionFeeMock).to.be.calledOnceWithExactly(
|
|
229
|
-
identityCreateTransition,
|
|
230
|
-
);
|
|
231
|
-
|
|
232
|
-
expect(fetchAssetLockTransactionOutputMock).to.be.calledOnceWithExactly(
|
|
233
|
-
identityCreateTransition.getAssetLockProof(),
|
|
234
|
-
identityCreateTransition.getExecutionContext(),
|
|
235
|
-
);
|
|
236
208
|
});
|
|
237
209
|
|
|
238
210
|
it('should not increase balance on dry run', async () => {
|
|
239
|
-
|
|
211
|
+
executionContext.enableDryRun();
|
|
212
|
+
executionContext.addOperation(new PreCalculatedOperation(0, outputAmount + 1, []));
|
|
213
|
+
identityCreateTransition.setExecutionContext(executionContext);
|
|
240
214
|
|
|
241
215
|
const result = await validateStateTransitionFee(identityCreateTransition);
|
|
242
216
|
|
|
243
217
|
identityCreateTransition.getExecutionContext().disableDryRun();
|
|
244
218
|
|
|
245
219
|
expect(result.isValid()).to.be.true();
|
|
246
|
-
|
|
247
|
-
expect(calculateStateTransitionFeeMock).to.be.not.called();
|
|
248
|
-
expect(fetchAssetLockTransactionOutputMock).to.be.calledOnceWithExactly(
|
|
249
|
-
identityCreateTransition.getAssetLockProof(),
|
|
250
|
-
identityCreateTransition.getExecutionContext(),
|
|
251
|
-
);
|
|
252
220
|
});
|
|
253
221
|
});
|
|
254
222
|
|
|
255
223
|
describe('IdentityTopUpTransition', () => {
|
|
256
224
|
let identityTopUpTransition;
|
|
257
225
|
let outputAmount;
|
|
226
|
+
let identityId;
|
|
258
227
|
|
|
259
|
-
beforeEach(() => {
|
|
260
|
-
identityTopUpTransition = getIdentityTopUpTransitionFixture();
|
|
228
|
+
beforeEach(async () => {
|
|
229
|
+
identityTopUpTransition = await getIdentityTopUpTransitionFixture();
|
|
230
|
+
identityId = identityTopUpTransition.getIdentityId().toBuffer();
|
|
261
231
|
|
|
262
232
|
const { satoshis } = identityTopUpTransition
|
|
263
233
|
.getAssetLockProof()
|
|
264
234
|
.getOutput();
|
|
265
235
|
|
|
266
|
-
outputAmount = satoshis *
|
|
236
|
+
outputAmount = satoshis * getCreditsConversionRatio();
|
|
267
237
|
});
|
|
268
238
|
|
|
269
239
|
it('should return invalid result if sum of balance and asset lock output amount is not enough', async () => {
|
|
270
240
|
stateRepositoryMock.fetchIdentityBalanceWithDebt.resolves(1);
|
|
271
241
|
|
|
272
|
-
|
|
242
|
+
executionContext.addOperation(new PreCalculatedOperation(0, outputAmount + 2, []));
|
|
243
|
+
identityTopUpTransition.setExecutionContext(executionContext);
|
|
273
244
|
|
|
274
245
|
const result = await validateStateTransitionFee(identityTopUpTransition);
|
|
275
246
|
|
|
276
|
-
expectValidationError(result, IdentityBalanceIsNotEnoughError);
|
|
247
|
+
await expectValidationError(result, IdentityBalanceIsNotEnoughError);
|
|
277
248
|
|
|
278
249
|
const [error] = result.getErrors();
|
|
279
250
|
|
|
280
251
|
expect(error.getCode()).to.equal(3000);
|
|
281
252
|
expect(error.getBalance()).to.equal(outputAmount + 1);
|
|
282
253
|
|
|
283
|
-
expect(stateRepositoryMock.fetchIdentityBalanceWithDebt).to.be.
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
);
|
|
287
|
-
|
|
288
|
-
expect(calculateStateTransitionFeeMock).to.be.calledOnceWithExactly(
|
|
289
|
-
identityTopUpTransition,
|
|
290
|
-
);
|
|
291
|
-
|
|
292
|
-
expect(fetchAssetLockTransactionOutputMock).to.be.calledOnceWithExactly(
|
|
293
|
-
identityTopUpTransition.getAssetLockProof(),
|
|
294
|
-
identityTopUpTransition.getExecutionContext(),
|
|
295
|
-
);
|
|
254
|
+
expect(stateRepositoryMock.fetchIdentityBalanceWithDebt).to.be.calledOnce();
|
|
255
|
+
expect(
|
|
256
|
+
stateRepositoryMock.fetchIdentityBalanceWithDebt.getCall(0).args[0].toBuffer(),
|
|
257
|
+
).to.be.deep.equal(identityId);
|
|
296
258
|
});
|
|
297
259
|
|
|
298
260
|
it('should return valid result', async () => {
|
|
299
261
|
stateRepositoryMock.fetchIdentityBalanceWithDebt.resolves(41);
|
|
300
262
|
|
|
301
|
-
|
|
263
|
+
executionContext.addOperation(new PreCalculatedOperation(0, outputAmount - 1, []));
|
|
264
|
+
identityTopUpTransition.setExecutionContext(executionContext);
|
|
302
265
|
|
|
303
266
|
const result = await validateStateTransitionFee(identityTopUpTransition);
|
|
304
267
|
|
|
305
268
|
expect(result.isValid()).to.be.true();
|
|
306
269
|
|
|
307
|
-
expect(stateRepositoryMock.fetchIdentityBalanceWithDebt).to.be.
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
);
|
|
311
|
-
|
|
312
|
-
expect(calculateStateTransitionFeeMock).to.be.calledOnceWithExactly(
|
|
313
|
-
identityTopUpTransition,
|
|
314
|
-
);
|
|
315
|
-
|
|
316
|
-
expect(fetchAssetLockTransactionOutputMock).to.be.calledOnceWithExactly(
|
|
317
|
-
identityTopUpTransition.getAssetLockProof(),
|
|
318
|
-
identityTopUpTransition.getExecutionContext(),
|
|
319
|
-
);
|
|
270
|
+
expect(stateRepositoryMock.fetchIdentityBalanceWithDebt).to.be.calledOnce();
|
|
271
|
+
expect(
|
|
272
|
+
stateRepositoryMock.fetchIdentityBalanceWithDebt.getCall(0).args[0].toBuffer(),
|
|
273
|
+
).to.be.deep.equal(identityId);
|
|
320
274
|
});
|
|
321
275
|
|
|
322
276
|
it('should not increase balance on dry run', async () => {
|
|
323
|
-
|
|
277
|
+
stateRepositoryMock.fetchIdentityBalanceWithDebt.resolves(1);
|
|
278
|
+
|
|
279
|
+
executionContext.enableDryRun();
|
|
280
|
+
executionContext.addOperation(new PreCalculatedOperation(0, outputAmount + 42, []));
|
|
281
|
+
identityTopUpTransition.setExecutionContext(executionContext);
|
|
324
282
|
|
|
325
283
|
const result = await validateStateTransitionFee(identityTopUpTransition);
|
|
326
284
|
|
|
@@ -328,43 +286,10 @@ describe('validateStateTransitionFeeFactory', () => {
|
|
|
328
286
|
|
|
329
287
|
expect(result.isValid()).to.be.true();
|
|
330
288
|
|
|
331
|
-
expect(
|
|
332
|
-
expect(
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
);
|
|
336
|
-
expect(stateRepositoryMock.fetchIdentityBalanceWithDebt).to.be.calledOnceWithExactly(
|
|
337
|
-
identityTopUpTransition.getIdentityId(),
|
|
338
|
-
identityTopUpTransition.getExecutionContext(),
|
|
339
|
-
);
|
|
289
|
+
expect(stateRepositoryMock.fetchIdentityBalanceWithDebt).to.be.calledOnce();
|
|
290
|
+
expect(
|
|
291
|
+
stateRepositoryMock.fetchIdentityBalanceWithDebt.getCall(0).args[0].toBuffer(),
|
|
292
|
+
).to.be.deep.equal(identityId);
|
|
340
293
|
});
|
|
341
294
|
});
|
|
342
|
-
|
|
343
|
-
it('should throw InvalidStateTransitionTypeError on invalid State Transition', async function it() {
|
|
344
|
-
const rawStateTransitionMock = {
|
|
345
|
-
data: 'sample data',
|
|
346
|
-
type: -1,
|
|
347
|
-
};
|
|
348
|
-
|
|
349
|
-
const stateTransitionMock = {
|
|
350
|
-
getType: this.sinonSandbox.stub().returns(rawStateTransitionMock.type),
|
|
351
|
-
toBuffer: this.sinonSandbox.stub().returns(Buffer.alloc(0)),
|
|
352
|
-
toObject: this.sinonSandbox.stub().returns(rawStateTransitionMock),
|
|
353
|
-
getExecutionContext: this.sinonSandbox.stub().returns(new StateTransitionExecutionContext()),
|
|
354
|
-
};
|
|
355
|
-
|
|
356
|
-
try {
|
|
357
|
-
await validateStateTransitionFee(stateTransitionMock);
|
|
358
|
-
|
|
359
|
-
expect.fail('should throw InvalidStateTransitionTypeError');
|
|
360
|
-
} catch (error) {
|
|
361
|
-
expect(error).to.be.an.instanceOf(InvalidStateTransitionTypeError);
|
|
362
|
-
expect(error.getType()).to.equal(rawStateTransitionMock.type);
|
|
363
|
-
|
|
364
|
-
expect(calculateStateTransitionFeeMock).to.not.be.called();
|
|
365
|
-
expect(stateRepositoryMock.fetchIdentity).to.not.be.called();
|
|
366
|
-
|
|
367
|
-
expect(fetchAssetLockTransactionOutputMock).to.not.be.called();
|
|
368
|
-
}
|
|
369
|
-
});
|
|
370
295
|
});
|
package/test/unit/stateTransition/validation/validateStateTransitionKeySignatureFactory.spec.js
CHANGED
|
@@ -2,7 +2,7 @@ const { Transaction, PrivateKey, Script } = require('@dashevo/dashcore-lib');
|
|
|
2
2
|
|
|
3
3
|
const createStateRepositoryMock = require('@dashevo/dpp/lib/test/mocks/createStateRepositoryMock');
|
|
4
4
|
const getIdentityCreateTransitionFixture = require('@dashevo/dpp/lib/test/fixtures/getIdentityCreateTransitionFixture');
|
|
5
|
-
const getIdentityTopUpTransitionFixture = require('../../../../lib/test/fixtures/
|
|
5
|
+
const getIdentityTopUpTransitionFixture = require('../../../../lib/test/fixtures/getIdentityTopUpTransitionFixtureJs');
|
|
6
6
|
const { expectValidationError } = require('../../../../lib/test/expect/expectError');
|
|
7
7
|
|
|
8
8
|
const { default: loadWasmDpp } = require('../../../../dist');
|