-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into documentation-improvement-suggestions
- Loading branch information
Showing
111 changed files
with
1,219 additions
and
145 deletions.
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
151 changes: 151 additions & 0 deletions
151
benchmark/__tests__/test-collections-performance.ava.js
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,151 @@ | ||
import { Worker } from "near-workspaces"; | ||
import test from "ava"; | ||
import { logTotalGas, randomInt } from "./util.js"; | ||
|
||
const COLLECTION_SIZE = 20; | ||
|
||
test.before(async (t) => { | ||
// Init the worker and start a Sandbox server | ||
const worker = await Worker.init(); | ||
|
||
// Prepare sandbox for tests, create accounts, deploy contracts, etx. | ||
const root = worker.rootAccount; | ||
|
||
// Deploy the test contracts. | ||
const lookupMapContract = await root.devDeploy("build/lookup-map.wasm"); | ||
const lookupSetContract = await root.devDeploy("build/lookup-set.wasm"); | ||
const unorderedMapContract = await root.devDeploy("build/unordered-map.wasm"); | ||
const unorderedSetContract = await root.devDeploy("build/unordered-set.wasm"); | ||
const vectorContract = await root.devDeploy("build/vector.wasm"); | ||
|
||
// Test users | ||
const ali = await root.createSubAccount("ali"); | ||
|
||
// Save state for test runs | ||
t.context.worker = worker; | ||
t.context.accounts = { | ||
root, | ||
lookupMapContract, | ||
lookupSetContract, | ||
unorderedMapContract, | ||
unorderedSetContract, | ||
vectorContract, | ||
ali, | ||
}; | ||
}); | ||
|
||
test.after.always(async (t) => { | ||
await t.context.worker.tearDown().catch((error) => { | ||
console.log("Failed to tear down the worker:", error); | ||
}); | ||
}); | ||
|
||
test("JS lookup map contract operations", async (t) => { | ||
const { ali, lookupMapContract } = t.context.accounts; | ||
|
||
let rAdd; | ||
for (let i = 0; i < COLLECTION_SIZE; i++) { | ||
rAdd = await ali.callRaw(lookupMapContract, "addElement", { key: i, value: i }); | ||
} | ||
t.is(rAdd.result.status.SuccessValue, ""); | ||
logTotalGas("Add element", rAdd, t); | ||
|
||
const val = randomInt(COLLECTION_SIZE); | ||
const rGet = await ali.callRaw(lookupMapContract, "getElement", { key: val }); | ||
t.is(JSON.parse(Buffer.from(rGet.result.status.SuccessValue, "base64")), val); | ||
logTotalGas("Get element", rGet, t); | ||
|
||
const rRem = await ali.callRaw(lookupMapContract, "removeElement", { key: randomInt(COLLECTION_SIZE) }); | ||
t.is(rRem.result.status.SuccessValue, ""); | ||
logTotalGas("Remove element", rRem, t); | ||
}); | ||
|
||
test("JS lookup set contract operations", async (t) => { | ||
const { ali, lookupSetContract } = t.context.accounts; | ||
|
||
let rAdd; | ||
for (let i = 0; i < COLLECTION_SIZE; i++) { | ||
rAdd = await ali.callRaw(lookupSetContract, "addElement", { value: i }); | ||
} | ||
t.is(rAdd.result.status.SuccessValue, ""); | ||
logTotalGas("Add element", rAdd, t); | ||
|
||
const rGet = await ali.callRaw(lookupSetContract, "containsElement", { value: randomInt(COLLECTION_SIZE) }); | ||
t.is(JSON.parse(Buffer.from(rGet.result.status.SuccessValue, "base64")), true); | ||
logTotalGas("Get element", rGet, t); | ||
|
||
const rRem = await ali.callRaw(lookupSetContract, "removeElement", { value: randomInt(COLLECTION_SIZE) }); | ||
t.is(rRem.result.status.SuccessValue, ""); | ||
logTotalGas("Remove element", rRem, t); | ||
}); | ||
|
||
test("JS unordered map contract operations", async (t) => { | ||
const { ali, unorderedMapContract } = t.context.accounts; | ||
|
||
let rAdd; | ||
for (let i = 0; i < COLLECTION_SIZE; i++) { | ||
rAdd = await ali.callRaw(unorderedMapContract, "addElement", { key: i, value: i }); | ||
} | ||
t.is(rAdd.result.status.SuccessValue, ""); | ||
logTotalGas("Add element", rAdd, t); | ||
|
||
const val = randomInt(COLLECTION_SIZE); | ||
const rGet = await ali.callRaw(unorderedMapContract, "getElement", { key: val }); | ||
t.is(JSON.parse(Buffer.from(rGet.result.status.SuccessValue, "base64")), val); | ||
logTotalGas("Get element", rGet, t); | ||
|
||
const rIt = await ali.callRaw(unorderedMapContract, "iterate", {}); | ||
t.is(rIt.result.status.SuccessValue, ""); | ||
logTotalGas("Iterate collection", rIt, t); | ||
|
||
const rRem = await ali.callRaw(unorderedMapContract, "removeElement", { key: randomInt(COLLECTION_SIZE) }); | ||
t.is(rRem.result.status.SuccessValue, ""); | ||
logTotalGas("Remove element", rRem, t); | ||
}); | ||
|
||
test("JS unordered set contract operations", async (t) => { | ||
const { ali, unorderedSetContract } = t.context.accounts; | ||
|
||
let rAdd; | ||
for (let i = 0; i < COLLECTION_SIZE; i++) { | ||
rAdd = await ali.callRaw(unorderedSetContract, "addElement", { value: i }); | ||
} | ||
t.is(rAdd.result.status.SuccessValue, ""); | ||
logTotalGas("Add element", rAdd, t); | ||
|
||
const rGet = await ali.callRaw(unorderedSetContract, "containsElement", { value: randomInt(COLLECTION_SIZE) }); | ||
t.is(JSON.parse(Buffer.from(rGet.result.status.SuccessValue, "base64")), true); | ||
logTotalGas ("Get element", rGet, t); | ||
|
||
const rIt = await ali.callRaw(unorderedSetContract, "iterate", {}); | ||
t.is(rIt.result.status.SuccessValue, ""); | ||
logTotalGas("Iterate collection", rIt, t); | ||
|
||
const rRem = await ali.callRaw(unorderedSetContract, "removeElement", { value: randomInt(COLLECTION_SIZE) }); | ||
t.is(rRem.result.status.SuccessValue, ""); | ||
logTotalGas("Remove element", rRem, t); | ||
}); | ||
|
||
test("JS vector contract operations", async (t) => { | ||
const { ali, vectorContract } = t.context.accounts; | ||
|
||
let rAdd; | ||
for (let i = 0; i < COLLECTION_SIZE; i++) { | ||
rAdd = await ali.callRaw(vectorContract, "addElement", { value: i }); | ||
} | ||
t.is(rAdd.result.status.SuccessValue, ""); | ||
logTotalGas("Add element", rAdd, t); | ||
|
||
const val = randomInt(COLLECTION_SIZE); | ||
const rGet = await ali.callRaw(vectorContract, "getElement", { index: val }); | ||
t.is(JSON.parse(Buffer.from(rGet.result.status.SuccessValue, "base64")), val); | ||
logTotalGas("Get element", rGet, t); | ||
|
||
const rIt = await ali.callRaw(vectorContract, "iterate", {}); | ||
t.is(rIt.result.status.SuccessValue, ""); | ||
logTotalGas("Iterate collection", rIt, t); | ||
|
||
const rRem = await ali.callRaw(vectorContract, "removeElement", { index: randomInt(COLLECTION_SIZE) }); | ||
t.is(rRem.result.status.SuccessValue, ""); | ||
logTotalGas("Remove element", rRem, t); | ||
}); |
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
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
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
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,23 @@ | ||
import { NearBindgen, call, LookupMap, view } from "near-sdk-js"; | ||
|
||
@NearBindgen({}) | ||
export class LookupMapContract { | ||
constructor() { | ||
this.lookupMap = new LookupMap("LM"); | ||
} | ||
|
||
@call({}) | ||
addElement({ key, value }) { | ||
this.lookupMap.set(key, value); | ||
} | ||
|
||
@call({}) | ||
removeElement({ key }) { | ||
this.lookupMap.remove(key); | ||
} | ||
|
||
@view({}) | ||
getElement({ key }) { | ||
return this.lookupMap.get(key); | ||
} | ||
} |
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,23 @@ | ||
import { NearBindgen, call, LookupSet, view } from "near-sdk-js"; | ||
|
||
@NearBindgen({}) | ||
export class LookupSetContract { | ||
constructor() { | ||
this.lookupSet = new LookupSet("LS"); | ||
} | ||
|
||
@call({}) | ||
addElement({ value }) { | ||
this.lookupSet.set(value); | ||
} | ||
|
||
@call({}) | ||
removeElement({ value }) { | ||
this.lookupSet.remove(value); | ||
} | ||
|
||
@view({}) | ||
containsElement({ value }) { | ||
return this.lookupSet.contains(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
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,3 +1,7 @@ | ||
import { near } from "near-sdk-js"; | ||
|
||
/** | ||
* More information for that can be found in the README.md | ||
* - A minimal contract | ||
*/ | ||
export function empty() {} |
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,31 @@ | ||
import { NearBindgen, call, UnorderedMap, view } from "near-sdk-js"; | ||
|
||
@NearBindgen({}) | ||
export class UnorderedMapContract { | ||
constructor() { | ||
this.unorderedMap = new UnorderedMap("UM"); | ||
} | ||
|
||
@call({}) | ||
addElement({ key, value }) { | ||
this.unorderedMap.set(key, value); | ||
} | ||
|
||
@call({}) | ||
removeElement({ key }) { | ||
this.unorderedMap.remove(key); | ||
} | ||
|
||
@view({}) | ||
getElement({ key }) { | ||
return this.unorderedMap.get(key); | ||
} | ||
|
||
@view({}) | ||
iterate() { | ||
const size = this.unorderedMap.length; | ||
for (let i = 0; i < size; i++) { | ||
this.unorderedMap.get(i); | ||
} | ||
} | ||
} |
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,31 @@ | ||
import { NearBindgen, call, UnorderedSet, view } from "near-sdk-js"; | ||
|
||
@NearBindgen({}) | ||
export class UnorderedSetContract { | ||
constructor() { | ||
this.unorderedSet = new UnorderedSet("US"); | ||
} | ||
|
||
@call({}) | ||
addElement({ value }) { | ||
this.unorderedSet.set(value); | ||
} | ||
|
||
@call({}) | ||
removeElement({ value }) { | ||
this.unorderedSet.remove(value); | ||
} | ||
|
||
@view({}) | ||
containsElement({ value }) { | ||
return this.unorderedSet.contains(value); | ||
} | ||
|
||
@view({}) | ||
iterate() { | ||
const size = this.unorderedSet.length; | ||
for (let i = 0; i < size; i++) { | ||
this.unorderedSet.contains(i); | ||
} | ||
} | ||
} |
Oops, something went wrong.