Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ide 655 compaction and unused #29

Merged
merged 11 commits into from
Nov 16, 2021
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@waves/ride-js",
"version": "2.1.2",
"version": "2.1.4",
"description": "Js compiler for Ride - Waves smart contract language.",
"main": "src/index.js",
"typings": "src/index.d.ts",
"scripts": {
"build": "./node_modules/.bin/webpack --config webpack.config.js",
"test": "echo \"Error: no test specified\" && exit 1",
"test": "mocha",
"prepublishOnly": "npm run build",
"getNewCompiler": "sh scripts/getNewCompiler.sh"
},
Expand Down
2 changes: 1 addition & 1 deletion scripts/getNewCompiler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ cd ../Waves
sbt langJS/fullOptJS
sbt replJS/fullOptJS
mv lang/js/target/lang-opt.js ../ride-js/src
mv repl/js/target/repl-opt.js ../ride-js/src
mv repl/js/target/repl-opt.js ../ride-js/src
17 changes: 14 additions & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,23 @@ export interface IFlattenedCompilationResult {
error?: string
}

export function compile(code: string, estimatorVersion?: number): ICompilationResult | ICompilationError;
//todo разобраться в акой версии компилятора импорту рыботают
export function compile(
code: string,
estimatorVersion?: number,
needCompaction?: boolean,
removeUnusedCode?: boolean,
): ICompilationResult | ICompilationError;

export function parseAndCompile(
code: string,
estimatorVersion?: number,
needCompaction?: boolean,
removeUnusedCode?: boolean,
): IParseAndCompileResult | ICompilationError;

export function flattenCompilationResult(compiled: ICompilationResult | ICompilationError): IFlattenedCompilationResult

export function parseAndCompile(code: string, estimatorVersion?: number): IParseAndCompileResult | ICompilationError;

export function scriptInfo(code: string): IScriptInfo | ICompilationError;

export function getTypes(stdlibVersion?: number, isTokenContext?: boolean): TStructField[];
Expand Down
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ const crypto = require('@waves/ts-lib-crypto');
const scalaJsCompiler = require('./lang-opt.js');
const replJs = require('./repl-opt.js');

function wrappedCompile(code, estimatorVersion = 2) {
function wrappedCompile(code, estimatorVersion = 2, needCompaction, removeUnusedCode) {
if (typeof code !== 'string') {
return {
error: 'Type error: contract should be string'
}
}
try {
const result = scalaJsCompiler.compile(code, estimatorVersion);
const result = scalaJsCompiler.compile(code, estimatorVersion, needCompaction, removeUnusedCode);
if (result.error) {
try {
result.size = new Uint8Array(result.result).length;
Expand Down
101 changes: 89 additions & 12 deletions test/compiler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,18 +315,18 @@ func asd() = {

it('complexity by funcs', () => {
const contract = `
{-# STDLIB_VERSION 3 #-}
{-# CONTENT_TYPE DAPP #-}
{-# SCRIPT_TYPE ACCOUNT #-}

@Callable(i)
func foo() = {
WriteSet([])
}

@Verifier(tx)
func standardVerifier() = sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey)
`
{-# STDLIB_VERSION 3 #-}
{-# CONTENT_TYPE DAPP #-}
{-# SCRIPT_TYPE ACCOUNT #-}
@Callable(i)
func foo() = {
WriteSet([])
}
@Verifier(tx)
func standardVerifier() = sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey)
`

const flattenResult = compiler.flattenCompilationResult(compiler.compile(contract))
expect(typeof flattenResult.verifierComplexity).to.eq('number')
Expand All @@ -337,6 +337,83 @@ func standardVerifier() = sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicK

});

it('presence of errors ride v5', () => {
const contract = `
{-# STDLIB_VERSION 5 #-}
{-# CONTENT_TYPE DAPP #-}
{-#SCRIPT_TYPE ACCOUNT#-}
@Verifier(tx)
func verify() = sigVerify(tx.bodyBytes, tx.proofs[0], tx.senderPublicKey)

@Callable(i)
func foo() = {
let nextDAppAddr = Address(base58'')

strict invResult = Invoke(nextDAppAddr, "bar", [], [])

if invResult == 42
then
([], unit)
else
throw("Internal invoke state update error")
}

@Callable(i)
func foo1() = {
let nextDAppAddr = Address(base58'')

strict invResult = Invoke(nextDAppAddr, "bar", [], [])

if invResult == 42
then
([], unit)
else
throw("Internal invoke state update error")
}
`

const result = compiler.compile(contract)
console.log(result)
expect(typeof result.error).to.eq('undefined')
});

it('library', () => {
const liba = `
{-# STDLIB_VERSION 4 #-}
{-# SCRIPT_TYPE ACCOUNT #-}
{-# CONTENT_TYPE LIBRARY #-}

let a = 1
let b = 2

func inc(a: Int) = a + 1
`

const result = compiler.scriptInfo(liba)
console.log(result)
expect(typeof result.error).to.eq('undefined')
});

it('imports', () => {
const scriptec = `
{-# STDLIB_VERSION 4 #-}
{-# SCRIPT_TYPE ACCOUNT #-}
{-# IMPORT lib1,lib2 #-}

let a = 5

multiply(inc(a), dec(a)) == (5 + 1) * (5 - 1)
`

const result = compiler.scriptInfo(scriptec)
console.log(result)
expect(result.imports.toString()).to.eq('lib1,lib2')
});

it('compiler version', () => {
console.log(compiler.version)
})

it('compiler version', () => {
console.log(compiler.version)
})
Expand Down
15 changes: 15 additions & 0 deletions test/ride/compileCompaction.ride
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{-# STDLIB_VERSION 4 #-}
{-# CONTENT_TYPE DAPP #-}

let unusedVar = 10
let fooVar = 42
func barFunc(barFuncArg1: Int) = 100500 + barFuncArg1

@Callable(invocation)
func bazCallableFunc(bazCallableFuncArg1: Int, bazCallableFuncArg2: String) = {
let result = barFunc(fooVar) + bazCallableFuncArg1
[
IntegerEntry("integerEntryKey", result),
StringEntry("stringEntryKey", bazCallableFuncArg2)
]
}
24 changes: 24 additions & 0 deletions test/sb.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const compiler = require('../src');
const {expect} = require('chai');
const getRide = require('./utils').getRide;

describe('Sandbox Test', function () {
this.timeout(50000);

it('Remove unused code and Compaction mode', () => {
// source
const source = getRide('./ride/compileCompaction.ride');

// compile
const baseRes = compiler.compile(source, 3, false, false);
const compactionRes = compiler.compile(source, 3, true, false);
const unusedRes = compiler.compile(source, 3, false, true);

console.log(`\tBase: ${baseRes.result.size}. | Compaction: ${compactionRes.result.size} | Unused: ${unusedRes.result.size}`);
expect(baseRes.result.size).to.not.equal(compactionRes.result.size);
expect(baseRes.result.size).to.not.equal(unusedRes.result.size);

// console.log(compiler.decompile(baseRes.result.base64));
// console.log(compiler.decompile(unusedRes.result.base64));
});
});
9 changes: 9 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

const fs = require('fs');
const path = require('path');

const getRideSrc = function(filename) {
return fs.readFileSync(path.resolve(__dirname, filename), 'utf8');
}

module.exports.getRide = getRideSrc;