-
Notifications
You must be signed in to change notification settings - Fork 37
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
Add workflow for integration tests, fix encoding / decoding of managed decimals #479
Merged
danielailie
merged 17 commits into
main
from
TOOL-248-add-workflow-to-run-integration-tests-with-localnet
Sep 16, 2024
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
cd58c8d
Add workflow
danielailie 865ef1e
Fix mxpy install
danielailie 04a1876
Add pipx instalation step
danielailie ba0a321
Fix pipx instalation
danielailie 6f61e60
Remove verify step
danielailie 3cb06da
Add nohup
danielailie 473e284
Fix start localnet
danielailie 8ee4554
Add managed decimal signed codec
danielailie 7fd503d
Add one more test
danielailie f235986
Update workflow
danielailie ca53fb3
Fix test
danielailie d6eb8b2
remove only
danielailie 4f6a84c
Update signed codec
danielailie f15a44b
Update workflow to stop when finish
danielailie c113ff5
Fix variable name
danielailie 490dbec
Extract variable
danielailie 6d972bc
bump version
danielailie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
name: MultiversX Integration Tests | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
|
||
jobs: | ||
integration_tests: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
# Step 1: Checkout the repository | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
# Step 2: Set up Python environment | ||
- name: Set up Python 3.x | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: '3.x' | ||
|
||
# Step 3: Install pipx (to manage Python tools) | ||
- name: Install pipx | ||
run: | | ||
python3 -m pip install --user pipx | ||
python3 -m pipx ensurepath | ||
# Add the pipx binary location to PATH | ||
echo "$HOME/.local/bin" >> $GITHUB_PATH | ||
shell: bash | ||
|
||
# Step 4: Install mxpy (MultiversX Python SDK) | ||
- name: Install mxpy (MultiversX SDK) | ||
run: | | ||
pipx install multiversx-sdk-cli --force | ||
|
||
# Step 5: Set up MultiversX localnet using mxpy | ||
- name: Set up MultiversX localnet | ||
run: | | ||
# Start the local testnet with mxpy | ||
mkdir -p ~/localnet && cd ~/localnet | ||
mxpy localnet setup | ||
nohup mxpy localnet start > localnet.log 2>&1 & echo $! > localnet.pid | ||
sleep 60 # Allow time for the testnet to fully start | ||
|
||
# Step 6: Install Node.js and dependencies | ||
- name: Set up Node.js environment | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: '16.x' | ||
|
||
- name: Install Node.js dependencies | ||
run: npm install | ||
|
||
# Step 7: Run integration tests | ||
- name: Run integration tests | ||
run: | | ||
npm run tests-localnet | ||
|
||
# Step 8: Stop the testnet using the stored PID | ||
- name: Stop MultiversX local testnet | ||
if: success() || failure() | ||
run: | | ||
kill $(cat localnet.pid) || echo "Testnet already stopped" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import BigNumber from "bignumber.js"; | ||
import { BigUIntValue, ManagedDecimalType, ManagedDecimalValue, U32Value } from "../typesystem"; | ||
import { BigUIntType, BigUIntValue, ManagedDecimalType, ManagedDecimalValue, U32Value } from "../typesystem"; | ||
import { BinaryCodec } from "./binary"; | ||
import { bufferToBigInt } from "./utils"; | ||
import { SizeOfU32 } from "./constants"; | ||
|
@@ -27,25 +27,30 @@ export class ManagedDecimalCodec { | |
if (type.isVariable()) { | ||
const bigUintSize = buffer.length - SizeOfU32; | ||
|
||
const value = new BigNumber(buffer.slice(0, bigUintSize).toString("hex"), 16); | ||
const [value] = this.binaryCodec.decodeNested(buffer.slice(0, bigUintSize), new BigUIntType()); | ||
const scale = buffer.readUInt32BE(bigUintSize); | ||
|
||
return new ManagedDecimalValue(value, scale); | ||
return new ManagedDecimalValue(value.valueOf().shiftedBy(-scale), scale); | ||
} | ||
|
||
const value = bufferToBigInt(buffer); | ||
const metadata = type.getMetadata(); | ||
const scale = typeof metadata === "number" ? metadata : 0; | ||
return new ManagedDecimalValue(value, scale); | ||
const scale = metadata !== "usize" ? parseInt(metadata.toString()) : 0; | ||
return new ManagedDecimalValue(value.shiftedBy(-scale), scale); | ||
} | ||
|
||
encodeNested(value: ManagedDecimalValue): Buffer { | ||
let buffers: Buffer[] = []; | ||
if (value.isVariable()) { | ||
buffers.push(Buffer.from(this.binaryCodec.encodeNested(new BigUIntValue(value.valueOf())))); | ||
buffers.push( | ||
Buffer.from( | ||
this.binaryCodec.encodeNested(new BigUIntValue(value.valueOf().shiftedBy(value.getScale()))), | ||
), | ||
); | ||
buffers.push(Buffer.from(this.binaryCodec.encodeNested(new U32Value(value.getScale())))); | ||
} else { | ||
buffers.push(Buffer.from(this.binaryCodec.encodeTopLevel(new BigUIntValue(value.valueOf())))); | ||
buffers.push( | ||
this.binaryCodec.encodeTopLevel(new BigUIntValue(value.valueOf().shiftedBy(value.getScale()))), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, above, and in
|
||
); | ||
} | ||
return Buffer.concat(buffers); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import BigNumber from "bignumber.js"; | ||
import { BigIntType, BigIntValue, ManagedDecimalSignedType, ManagedDecimalSignedValue, U32Value } from "../typesystem"; | ||
import { BinaryCodec } from "./binary"; | ||
import { bufferToBigInt } from "./utils"; | ||
import { SizeOfU32 } from "./constants"; | ||
|
||
export class ManagedDecimalSignedCodec { | ||
private readonly binaryCodec: BinaryCodec; | ||
|
||
constructor(binaryCodec: BinaryCodec) { | ||
this.binaryCodec = binaryCodec; | ||
} | ||
|
||
decodeNested(buffer: Buffer, type: ManagedDecimalSignedType): [ManagedDecimalSignedValue, number] { | ||
const length = buffer.readUInt32BE(0); | ||
const payload = buffer.slice(0, length); | ||
|
||
const result = this.decodeTopLevel(payload, type); | ||
return [result, length]; | ||
} | ||
|
||
decodeTopLevel(buffer: Buffer, type: ManagedDecimalSignedType): ManagedDecimalSignedValue { | ||
if (buffer.length === 0) { | ||
return new ManagedDecimalSignedValue(new BigNumber(0), 0); | ||
} | ||
|
||
if (type.isVariable()) { | ||
const bigintSize = buffer.length - SizeOfU32; | ||
|
||
const [value] = this.binaryCodec.decodeNested(buffer.slice(0, bigintSize), new BigIntType()); | ||
const scale = buffer.readUInt32BE(bigintSize); | ||
|
||
return new ManagedDecimalSignedValue(value.valueOf().shiftedBy(-scale), scale); | ||
} | ||
|
||
const value = bufferToBigInt(buffer); | ||
const metadata = type.getMetadata(); | ||
const scale = metadata !== "usize" ? parseInt(metadata.toString()) : 0; | ||
return new ManagedDecimalSignedValue(value.shiftedBy(-scale), scale); | ||
} | ||
|
||
encodeNested(value: ManagedDecimalSignedValue): Buffer { | ||
let buffers: Buffer[] = []; | ||
if (value.isVariable()) { | ||
buffers.push( | ||
Buffer.from( | ||
this.binaryCodec.encodeNested( | ||
new BigIntValue(new BigNumber(value.valueOf().shiftedBy(value.getScale()))), | ||
), | ||
), | ||
); | ||
buffers.push(Buffer.from(this.binaryCodec.encodeNested(new U32Value(value.getScale())))); | ||
} else { | ||
buffers.push( | ||
Buffer.from( | ||
this.binaryCodec.encodeTopLevel(new BigIntValue(value.valueOf().shiftedBy(value.getScale()))), | ||
), | ||
); | ||
} | ||
return Buffer.concat(buffers); | ||
} | ||
|
||
encodeTopLevel(value: ManagedDecimalSignedValue): Buffer { | ||
return this.encodeNested(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,7 +185,7 @@ describe("test smart contract interactor", function () { | |
assert.isTrue(typedBundle.returnCode.equals(ReturnCode.Ok)); | ||
}); | ||
|
||
it("should interact with 'basic-features' (local testnet) using the SmartContractTransactionsFactory", async function () { | ||
it("should interact with 'basic-features' (local testnet)", async function () { | ||
this.timeout(140000); | ||
|
||
let abiRegistry = await loadAbiRegistry("src/testdata/basic-features.abi.json"); | ||
|
@@ -226,21 +226,21 @@ describe("test smart contract interactor", function () { | |
.buildTransaction(); | ||
|
||
let additionInteraction = <Interaction>contract.methods | ||
.managed_decimal_addition([new ManagedDecimalValue(2, 2), new ManagedDecimalValue(3, 2)]) | ||
.managed_decimal_addition([new ManagedDecimalValue("2.5", 2), new ManagedDecimalValue("2.7", 2)]) | ||
.withGasLimit(10000000) | ||
.withChainID(network.ChainID) | ||
.withSender(alice.address) | ||
.withValue(0); | ||
|
||
// addition() | ||
// addition(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Formatting artifact. |
||
let additionTransaction = additionInteraction | ||
.withSender(alice.address) | ||
.useThenIncrementNonceOf(alice.account) | ||
.buildTransaction(); | ||
|
||
// log | ||
let mdLnInteraction = <Interaction>contract.methods | ||
.managed_decimal_ln([new ManagedDecimalValue(23000000000, 9)]) | ||
.managed_decimal_ln([new ManagedDecimalValue("23", 9)]) | ||
.withGasLimit(10000000) | ||
.withChainID(network.ChainID) | ||
.withSender(alice.address) | ||
|
@@ -254,8 +254,8 @@ describe("test smart contract interactor", function () { | |
|
||
let additionVarInteraction = <Interaction>contract.methods | ||
.managed_decimal_addition_var([ | ||
new ManagedDecimalValue(378298000000, 9, true), | ||
new ManagedDecimalValue(378298000000, 9, true), | ||
new ManagedDecimalValue("4", 2, true), | ||
new ManagedDecimalValue("5", 2, true), | ||
]) | ||
.withGasLimit(50000000) | ||
.withChainID(network.ChainID) | ||
|
@@ -268,33 +268,53 @@ describe("test smart contract interactor", function () { | |
.useThenIncrementNonceOf(alice.account) | ||
.buildTransaction(); | ||
|
||
let lnVarInteraction = <Interaction>contract.methods | ||
.managed_decimal_ln_var([new ManagedDecimalValue("23", 9, true)]) | ||
.withGasLimit(50000000) | ||
.withChainID(network.ChainID) | ||
.withSender(alice.address) | ||
.withValue(0); | ||
|
||
// managed_decimal_ln_var() | ||
let lnVarTransaction = lnVarInteraction | ||
.withSender(alice.address) | ||
.useThenIncrementNonceOf(alice.account) | ||
.buildTransaction(); | ||
|
||
// returnEgld() | ||
await signTransaction({ transaction: returnEgldTransaction, wallet: alice }); | ||
let { bundle: bundleEgld } = await controller.execute(returnEgldInteraction, returnEgldTransaction); | ||
assert.isTrue(bundleEgld.returnCode.equals(ReturnCode.Ok)); | ||
assert.lengthOf(bundleEgld.values, 1); | ||
assert.deepEqual(bundleEgld.values[0], new ManagedDecimalValue(1, 18)); | ||
assert.deepEqual(bundleEgld.values[0], new ManagedDecimalValue("0.000000000000000001", 18)); | ||
|
||
// addition with const decimals() | ||
await signTransaction({ transaction: additionTransaction, wallet: alice }); | ||
let { bundle: bundleAdditionConst } = await controller.execute(additionInteraction, additionTransaction); | ||
assert.isTrue(bundleAdditionConst.returnCode.equals(ReturnCode.Ok)); | ||
assert.lengthOf(bundleAdditionConst.values, 1); | ||
assert.deepEqual(bundleAdditionConst.values[0], new ManagedDecimalValue(5, 2)); | ||
assert.deepEqual(bundleAdditionConst.values[0], new ManagedDecimalValue("5.2", 2)); | ||
|
||
// log | ||
await signTransaction({ transaction: mdLnTransaction, wallet: alice }); | ||
let { bundle: bundleMDLn } = await controller.execute(mdLnInteraction, mdLnTransaction); | ||
assert.isTrue(bundleMDLn.returnCode.equals(ReturnCode.Ok)); | ||
assert.lengthOf(bundleMDLn.values, 1); | ||
assert.deepEqual(bundleMDLn.values[0], new ManagedDecimalSignedValue(3135553845, 9)); | ||
assert.deepEqual(bundleMDLn.values[0], new ManagedDecimalSignedValue("3.135553845", 9)); | ||
|
||
// addition with var decimals | ||
await signTransaction({ transaction: additionVarTransaction, wallet: alice }); | ||
let { bundle: bundleAddition } = await controller.execute(additionVarInteraction, additionVarTransaction); | ||
assert.isTrue(bundleAddition.returnCode.equals(ReturnCode.Ok)); | ||
assert.lengthOf(bundleAddition.values, 1); | ||
assert.deepEqual(bundleAddition.values[0], new ManagedDecimalValue(new BigNumber(6254154138880), 9)); | ||
assert.deepEqual(bundleAddition.values[0], new ManagedDecimalValue("9", 2)); | ||
|
||
// log | ||
await signTransaction({ transaction: lnVarTransaction, wallet: alice }); | ||
let { bundle: bundleLnVar } = await controller.execute(lnVarInteraction, lnVarTransaction); | ||
assert.isTrue(bundleLnVar.returnCode.equals(ReturnCode.Ok)); | ||
assert.lengthOf(bundleLnVar.values, 1); | ||
assert.deepEqual(bundleLnVar.values[0], new ManagedDecimalSignedValue("3.135553845", 9)); | ||
}); | ||
|
||
it("should interact with 'counter' (local testnet)", async function () { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀