Skip to content
This repository has been archived by the owner on Dec 16, 2021. It is now read-only.

test: Top Up identity functional test #268

Merged
merged 4 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
},
"dependencies": {
"@dashevo/dapi-grpc": "~0.13.0-dev.4",
"@dashevo/dashcore-lib": "~0.18.0",
"@dashevo/dashcore-lib": "~0.18.1",
"@dashevo/dashd-rpc": "^2.0.0",
"@dashevo/dpp": "~0.13.0-dev.4",
"@dashevo/dpp": "~0.13.0-dev.7",
"@dashevo/grpc-common": "~0.3.0",
"ajv": "^6.4.0",
"bs58": "^4.0.1",
Expand All @@ -49,7 +49,7 @@
"zeromq": "^5.2.0"
},
"devDependencies": {
"@dashevo/dapi-client": "~0.13.0-dev.2",
"@dashevo/dapi-client": "~0.13.0-dev.3",
"@dashevo/dp-services-ctl": "~0.13.0-dev.1",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
Expand Down
189 changes: 189 additions & 0 deletions test/functional/grpcServer/handlers/platform/topUpIdentity.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
const {
startDapi,
} = require('@dashevo/dp-services-ctl');

const {
PrivateKey,
PublicKey,
Transaction,
} = require('@dashevo/dashcore-lib');

const DashPlatformProtocol = require('@dashevo/dpp');

const GrpcErrorCodes = require('@dashevo/grpc-common/lib/server/error/GrpcErrorCodes');

const { convertSatoshiToCredits } = require(
'@dashevo/dpp/lib/identity/creditsConverter',
);

const wait = require('../../../../../lib/utils/wait');

describe('topUpIdentity', function main() {
this.timeout(200000);

let removeDapi;
let dapiClient;
let dpp;
let identity;
let identityCreateTransition;
let identityTopUpTransition;
let coreAPI;
let addressString;
let publicKeyHash;
let privateKey;

before(async () => {
const {
dapiCore,
dashCore,
remove,
} = await startDapi();

removeDapi = remove;
coreAPI = dashCore.getApi();
dapiClient = dapiCore.getApi();

dpp = new DashPlatformProtocol({
dataProvider: {},
});
});

beforeEach(async () => {
({ result: addressString } = await coreAPI.getNewAddress());
const { result: privateKeyString } = await coreAPI.dumpPrivKey(addressString);

privateKey = new PrivateKey(privateKeyString);
const publicKey = new PublicKey({
...privateKey.toPublicKey().toObject(),
compressed: true,
});
const pubKeyBase = publicKey.toBuffer()
.toString('base64');

// eslint-disable-next-line no-underscore-dangle
publicKeyHash = PublicKey.fromBuffer(Buffer.from(pubKeyBase, 'base64'))
._getID();

await coreAPI.generateToAddress(500, addressString);

const { result: unspent } = await coreAPI.listUnspent();
const inputs = unspent.filter(input => input.address === addressString);

const transaction = new Transaction();

transaction.from(inputs.slice(-1)[0])
.addBurnOutput(10000, publicKeyHash)
.change(addressString)
.fee(668)
.sign(privateKey);

await coreAPI.sendrawtransaction(transaction.serialize());

await coreAPI.generateToAddress(1, addressString);

await wait(2000); // wait a couple of seconds for tx to be confirmed

const outPoint = transaction.getOutPointBuffer(0);

identity = dpp.identity.create(
outPoint,
[publicKey],
);

identityCreateTransition = dpp.identity.createIdentityCreateTransition(identity);
identityCreateTransition.signByPrivateKey(privateKey);

await dapiClient.applyStateTransition(identityCreateTransition);
});

after(async () => {
await removeDapi();
});

it('should top up created identity', async () => {
shumkov marked this conversation as resolved.
Show resolved Hide resolved
const { result: unspent } = await coreAPI.listUnspent();
const inputs = unspent.filter(input => input.address === addressString);
const topUpTransaction = new Transaction();
const topUpAmount = 3000;

topUpTransaction.from(inputs.slice(-1)[0])
.addBurnOutput(topUpAmount, publicKeyHash)
.change(addressString)
.fee(668)
.sign(privateKey);

await coreAPI.sendrawtransaction(topUpTransaction.serialize());

await coreAPI.generateToAddress(1, addressString);

await wait(2000); // wait a couple of seconds for tx to be confirmed

const topUpOutPoint = topUpTransaction.getOutPointBuffer(0);

identityTopUpTransition = dpp.identity.createIdentityTopUpTransition(
identity.getId(),
topUpOutPoint,
);
identityTopUpTransition.signByPrivateKey(privateKey);

await dapiClient.applyStateTransition(identityTopUpTransition);

const serializedIdentity = await dapiClient.getIdentity(
identityCreateTransition.getIdentityId(),
);

const receivedIdentity = dpp.identity.createFromSerialized(
serializedIdentity,
{ skipValidation: true },
);

const balance = convertSatoshiToCredits(10000)
+ convertSatoshiToCredits(topUpAmount)
- identityCreateTransition.calculateFee()
- identityTopUpTransition.calculateFee();

expect(balance).to.equal(receivedIdentity.getBalance());
});

it('should fail top up created identity ', async () => {
const { result: unspent } = await coreAPI.listUnspent();
const inputs = unspent.filter(input => input.address === addressString);
const topUpTransaction = new Transaction();
const topUpAmount = 3000;

topUpTransaction.from(inputs.slice(-1)[0])
.addBurnOutput(topUpAmount, publicKeyHash)
.change(addressString)
.fee(668)
.sign(privateKey);

const topUpOutPoint = topUpTransaction.getOutPointBuffer(0);

identityTopUpTransition = dpp.identity.createIdentityTopUpTransition(
identity.getId(),
topUpOutPoint,
);
identityTopUpTransition.signByPrivateKey(privateKey);

try {
await dapiClient.applyStateTransition(identityTopUpTransition);

expect.fail('Should fail with error');
} catch (e) {
expect(e.code).to.equal(GrpcErrorCodes.INVALID_ARGUMENT);
expect(e.details).to.equal('State Transition is invalid');
}

const serializedIdentity = await dapiClient.getIdentity(
identityCreateTransition.getIdentityId(),
);

const receivedIdentity = dpp.identity.createFromSerialized(
serializedIdentity,
{ skipValidation: true },
);

expect(convertSatoshiToCredits(10000) - identityCreateTransition.calculateFee())
.to.equal(receivedIdentity.getBalance());
});
});