v1.3.5-beta.0
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "warp-contracts",
|
||||
"version": "1.3.4-beta.0",
|
||||
"version": "1.3.5-beta.0",
|
||||
"description": "An implementation of the SmartWeave smart contract protocol.",
|
||||
"types": "./lib/types/index.d.ts",
|
||||
"main": "./lib/cjs/index.js",
|
||||
|
||||
@@ -2,7 +2,7 @@ import fs from 'fs';
|
||||
|
||||
import ArLocal from 'arlocal';
|
||||
import Arweave from 'arweave';
|
||||
import {JWKInterface} from 'arweave/node/lib/wallet';
|
||||
import { JWKInterface } from 'arweave/node/lib/wallet';
|
||||
import path from 'path';
|
||||
import { mineBlock } from '../_helpers';
|
||||
import { PstContract, PstState } from '../../../contract/PstContract';
|
||||
@@ -33,8 +33,8 @@ describe('Testing unsafe client in nested contracts with "skip" option', () => {
|
||||
warp = WarpFactory.forLocal(1667).use(new DeployPlugin());
|
||||
warpUnsafe = WarpFactory.forLocal(1667).use(new DeployPlugin());
|
||||
|
||||
({arweave} = warp);
|
||||
({jwk: wallet, address: walletAddress} = await warp.generateWallet());
|
||||
({ arweave } = warp);
|
||||
({ jwk: wallet, address: walletAddress } = await warp.generateWallet());
|
||||
|
||||
safeContractSrc = fs.readFileSync(path.join(__dirname, '../data/token-pst.js'), 'utf8');
|
||||
const stateFromFile: PstState = JSON.parse(fs.readFileSync(path.join(__dirname, '../data/token-pst.json'), 'utf8'));
|
||||
@@ -50,7 +50,7 @@ describe('Testing unsafe client in nested contracts with "skip" option', () => {
|
||||
}
|
||||
};
|
||||
|
||||
({contractTxId} = await warp.createContract.deploy({
|
||||
({ contractTxId } = await warp.createContract.deploy({
|
||||
wallet,
|
||||
initState: JSON.stringify(initialState),
|
||||
src: safeContractSrc
|
||||
@@ -62,14 +62,14 @@ describe('Testing unsafe client in nested contracts with "skip" option', () => {
|
||||
pst.connect(wallet);
|
||||
|
||||
unsafeContractSrc = fs.readFileSync(path.join(__dirname, '../data/token-pst-unsafe.js'), 'utf8');
|
||||
({contractTxId: foreignUnsafeContractTxId} = await warp.createContract.deploy({
|
||||
({ contractTxId: foreignUnsafeContractTxId } = await warp.createContract.deploy({
|
||||
wallet,
|
||||
initState: JSON.stringify(initialState),
|
||||
src: unsafeContractSrc
|
||||
}));
|
||||
await mineBlock(warp);
|
||||
|
||||
({contractTxId: foreignSafeContractTxId} = await warp.createContract.deploy({
|
||||
({ contractTxId: foreignSafeContractTxId } = await warp.createContract.deploy({
|
||||
wallet,
|
||||
initState: JSON.stringify(initialState),
|
||||
src: safeContractSrc
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
import fs from "fs";
|
||||
import fs from 'fs';
|
||||
|
||||
import ArLocal from "arlocal";
|
||||
import Arweave from "arweave";
|
||||
import { JWKInterface } from "arweave/node/lib/wallet";
|
||||
import path from "path";
|
||||
import { mineBlock } from "../_helpers";
|
||||
import { PstContract, PstState } from "../../../contract/PstContract";
|
||||
import { Warp } from "../../../core/Warp";
|
||||
import { DEFAULT_LEVEL_DB_LOCATION, WarpFactory } from "../../../core/WarpFactory";
|
||||
import { LoggerFactory } from "../../../logging/LoggerFactory";
|
||||
import { WriteInteractionResponse } from "../../../contract/Contract";
|
||||
import ArLocal from 'arlocal';
|
||||
import Arweave from 'arweave';
|
||||
import { JWKInterface } from 'arweave/node/lib/wallet';
|
||||
import path from 'path';
|
||||
import { mineBlock } from '../_helpers';
|
||||
import { PstContract, PstState } from '../../../contract/PstContract';
|
||||
import { Warp } from '../../../core/Warp';
|
||||
import { DEFAULT_LEVEL_DB_LOCATION, WarpFactory } from '../../../core/WarpFactory';
|
||||
import { LoggerFactory } from '../../../logging/LoggerFactory';
|
||||
import { WriteInteractionResponse } from '../../../contract/Contract';
|
||||
import { DeployPlugin } from 'warp-contracts-plugin-deploy';
|
||||
|
||||
describe('Testing the PST kv storage range access', () => {
|
||||
@@ -139,7 +139,6 @@ describe('Testing the PST kv storage range access', () => {
|
||||
expect((await pst.currentBalance('uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M')).balance).toEqual(10_000 + 655);
|
||||
});
|
||||
|
||||
|
||||
it('should properly check minted status', async () => {
|
||||
const viewResult = await pst.viewState<unknown, MintedResult>({ function: 'minted' });
|
||||
|
||||
@@ -148,15 +147,14 @@ describe('Testing the PST kv storage range access', () => {
|
||||
expect(viewResult.result.minted).toEqual(23676891);
|
||||
});
|
||||
|
||||
|
||||
it('should write check', async () => {
|
||||
await pst.writeInteraction({ function: 'writeCheck',
|
||||
await pst.writeInteraction({
|
||||
function: 'writeCheck',
|
||||
target: 'uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M',
|
||||
qty: 200_000 });
|
||||
qty: 200_000
|
||||
});
|
||||
|
||||
await pst.writeInteraction({ function: 'writeCheck',
|
||||
target: aliceWalletAddress,
|
||||
qty: 200_000 });
|
||||
await pst.writeInteraction({ function: 'writeCheck', target: aliceWalletAddress, qty: 200_000 });
|
||||
|
||||
await mineBlock(warp);
|
||||
|
||||
@@ -176,25 +174,32 @@ describe('Testing the PST kv storage range access', () => {
|
||||
|
||||
expect((await pst.currentBalance(aliceWalletAddress)).balance).toEqual(200_000);
|
||||
expect((await pst.currentBalance(walletAddress)).balance).toEqual(555669 - 655 - 200_000);
|
||||
expect((await pst.getStorageValues(['check.' + walletAddress + '.' + aliceWalletAddress]))
|
||||
.cachedValue.get('check.' + walletAddress + '.' + aliceWalletAddress)).toBeNull()
|
||||
expect(
|
||||
(await pst.getStorageValues(['check.' + walletAddress + '.' + aliceWalletAddress])).cachedValue.get(
|
||||
'check.' + walletAddress + '.' + aliceWalletAddress
|
||||
)
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it('should not be able to write check', async () => {
|
||||
const wrongCheck = await pst.writeInteraction({ function: 'writeCheck',
|
||||
const wrongCheck = await pst.writeInteraction({
|
||||
function: 'writeCheck',
|
||||
target: 'uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M',
|
||||
qty: 360_000 });
|
||||
qty: 360_000
|
||||
});
|
||||
|
||||
await mineBlock(warp);
|
||||
|
||||
expect((await pst.getStorageValues(['check.' + walletAddress + '.uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M']))
|
||||
.cachedValue.get('check.' + walletAddress + '.uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M')).toBe(200_000);
|
||||
expect(
|
||||
(
|
||||
await pst.getStorageValues(['check.' + walletAddress + '.uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M'])
|
||||
).cachedValue.get('check.' + walletAddress + '.uhE-QeYS8i4pmUtnxQyHD7dzXFNaJ9oMK-IM-QPNY6M')
|
||||
).toBe(200_000);
|
||||
expect((await pst.readState()).cachedValue.errorMessages[wrongCheck.originalTxId]).toMatch(
|
||||
'Caller balance 355014 not high enough to write check for 360000!'
|
||||
);
|
||||
expect((await pst.currentBalance(walletAddress)).balance).toEqual(555669 - 655 - 200_000);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
export interface MintedResult {
|
||||
|
||||
@@ -2,14 +2,14 @@
|
||||
import fs from 'fs';
|
||||
|
||||
import ArLocal from 'arlocal';
|
||||
import {JWKInterface} from 'arweave/node/lib/wallet';
|
||||
import { JWKInterface } from 'arweave/node/lib/wallet';
|
||||
import path from 'path';
|
||||
import {mineBlock} from '../_helpers';
|
||||
import {Contract} from '../../../contract/Contract';
|
||||
import {Warp} from '../../../core/Warp';
|
||||
import {WarpFactory} from '../../../core/WarpFactory';
|
||||
import {LoggerFactory} from '../../../logging/LoggerFactory';
|
||||
import {DeployPlugin} from 'warp-contracts-plugin-deploy';
|
||||
import { mineBlock } from '../_helpers';
|
||||
import { Contract } from '../../../contract/Contract';
|
||||
import { Warp } from '../../../core/Warp';
|
||||
import { WarpFactory } from '../../../core/WarpFactory';
|
||||
import { LoggerFactory } from '../../../logging/LoggerFactory';
|
||||
import { DeployPlugin } from 'warp-contracts-plugin-deploy';
|
||||
|
||||
interface ExampleContractState {
|
||||
counter: number;
|
||||
@@ -74,20 +74,20 @@ describe('Testing internal writes', () => {
|
||||
|
||||
async function deployContracts() {
|
||||
warp = WarpFactory.forLocal(port).use(new DeployPlugin());
|
||||
({jwk: wallet, address: walletAddress} = await warp.generateWallet());
|
||||
({ jwk: wallet, address: walletAddress } = await warp.generateWallet());
|
||||
|
||||
callingContractSrc = fs.readFileSync(path.join(__dirname, '../data/kv-storage-inner-calls.js'), 'utf8');
|
||||
callingContractInitialState = fs.readFileSync(path.join(__dirname, '../data/token-pst.json'), 'utf8');
|
||||
calleeContractSrc = fs.readFileSync(path.join(__dirname, '../data/kv-storage-inner-calls.js'), 'utf8');
|
||||
calleeInitialState = fs.readFileSync(path.join(__dirname, '../data/token-pst.json'), 'utf8');
|
||||
|
||||
({contractTxId: calleeTxId} = await warp.deploy({
|
||||
({ contractTxId: calleeTxId } = await warp.deploy({
|
||||
wallet,
|
||||
initState: calleeInitialState,
|
||||
src: calleeContractSrc
|
||||
}));
|
||||
|
||||
({contractTxId: callingTxId} = await warp.deploy({
|
||||
({ contractTxId: callingTxId } = await warp.deploy({
|
||||
wallet,
|
||||
initState: callingContractInitialState,
|
||||
src: callingContractSrc
|
||||
@@ -120,7 +120,7 @@ describe('Testing internal writes', () => {
|
||||
});
|
||||
|
||||
it('should write direct interactions', async () => {
|
||||
await calleeContract.writeInteraction({function: 'mintAdd', target: walletAddress, qty: 100});
|
||||
await calleeContract.writeInteraction({ function: 'mintAdd', target: walletAddress, qty: 100 });
|
||||
await mineBlock(warp);
|
||||
|
||||
await calleeContract.readState();
|
||||
@@ -145,7 +145,7 @@ describe('Testing internal writes', () => {
|
||||
});
|
||||
|
||||
it('should write another direct interaction', async () => {
|
||||
await calleeContract.writeInteraction({function: 'mintAdd', target: walletAddress, qty: 100});
|
||||
await calleeContract.writeInteraction({ function: 'mintAdd', target: walletAddress, qty: 100 });
|
||||
await mineBlock(warp);
|
||||
|
||||
await calleeContract.readState();
|
||||
@@ -173,7 +173,7 @@ describe('Testing internal writes', () => {
|
||||
const kvValues = await calleeContract.getStorageValues([walletAddress]);
|
||||
expect(kvValues.cachedValue.get(walletAddress)).toEqual(500);
|
||||
|
||||
await calleeContract.writeInteraction({function: 'mintAdd', target: walletAddress, qty: 100});
|
||||
await calleeContract.writeInteraction({ function: 'mintAdd', target: walletAddress, qty: 100 });
|
||||
await mineBlock(warp);
|
||||
await calleeContract.readState();
|
||||
|
||||
@@ -190,7 +190,7 @@ describe('Testing internal writes', () => {
|
||||
});
|
||||
await mineBlock(warp);
|
||||
|
||||
await calleeContract.writeInteraction({function: 'mintAdd', target: walletAddress, qty: 100});
|
||||
await calleeContract.writeInteraction({ function: 'mintAdd', target: walletAddress, qty: 100 });
|
||||
await mineBlock(warp);
|
||||
|
||||
await calleeContract.readState();
|
||||
@@ -207,7 +207,7 @@ describe('Testing internal writes', () => {
|
||||
});
|
||||
await mineBlock(warp);
|
||||
|
||||
await calleeContract.writeInteraction({function: 'mintAdd', target: walletAddress, qty: 100});
|
||||
await calleeContract.writeInteraction({ function: 'mintAdd', target: walletAddress, qty: 100 });
|
||||
await mineBlock(warp);
|
||||
|
||||
await calleeContract.readState();
|
||||
@@ -216,7 +216,7 @@ describe('Testing internal writes', () => {
|
||||
});
|
||||
|
||||
it('should write combination of direct and internal interaction - at one block', async () => {
|
||||
await calleeContract.writeInteraction({function: 'mintAdd', target: walletAddress, qty: 100});
|
||||
await calleeContract.writeInteraction({ function: 'mintAdd', target: walletAddress, qty: 100 });
|
||||
await callingContract.writeInteraction({
|
||||
function: 'innerWriteKV',
|
||||
txId: calleeTxId,
|
||||
@@ -239,7 +239,7 @@ describe('Testing internal writes', () => {
|
||||
});
|
||||
await mineBlock(warp);
|
||||
|
||||
await calleeContract.writeInteraction({function: 'mintAdd', target: walletAddress, qty: 100});
|
||||
await calleeContract.writeInteraction({ function: 'mintAdd', target: walletAddress, qty: 100 });
|
||||
await mineBlock(warp);
|
||||
|
||||
await calleeContract.readState();
|
||||
@@ -254,14 +254,13 @@ describe('Testing internal writes', () => {
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('with read state at the end', () => {
|
||||
beforeAll(async () => {
|
||||
await deployContracts();
|
||||
});
|
||||
|
||||
it('should properly write a combination of direct and internal interactions', async () => {
|
||||
await calleeContract.writeInteraction({function: 'mintAdd', target: walletAddress, qty: 100});
|
||||
await calleeContract.writeInteraction({ function: 'mintAdd', target: walletAddress, qty: 100 });
|
||||
await mineBlock(warp);
|
||||
|
||||
await callingContract.writeInteraction({
|
||||
@@ -272,7 +271,7 @@ describe('Testing internal writes', () => {
|
||||
});
|
||||
await mineBlock(warp);
|
||||
|
||||
await calleeContract.writeInteraction({function: 'mintAdd', target: walletAddress, qty: 100});
|
||||
await calleeContract.writeInteraction({ function: 'mintAdd', target: walletAddress, qty: 100 });
|
||||
await mineBlock(warp);
|
||||
|
||||
await callingContract.writeInteraction({
|
||||
@@ -289,7 +288,7 @@ describe('Testing internal writes', () => {
|
||||
});
|
||||
await mineBlock(warp);
|
||||
|
||||
await calleeContract.writeInteraction({function: 'mintAdd', target: walletAddress, qty: 100});
|
||||
await calleeContract.writeInteraction({ function: 'mintAdd', target: walletAddress, qty: 100 });
|
||||
await mineBlock(warp);
|
||||
|
||||
await callingContract.writeInteraction({
|
||||
@@ -300,7 +299,7 @@ describe('Testing internal writes', () => {
|
||||
});
|
||||
await mineBlock(warp);
|
||||
|
||||
await calleeContract.writeInteraction({function: 'mintAdd', target: walletAddress, qty: 100});
|
||||
await calleeContract.writeInteraction({ function: 'mintAdd', target: walletAddress, qty: 100 });
|
||||
await mineBlock(warp);
|
||||
|
||||
await callingContract.writeInteraction({
|
||||
@@ -311,10 +310,10 @@ describe('Testing internal writes', () => {
|
||||
});
|
||||
await mineBlock(warp);
|
||||
|
||||
await calleeContract.writeInteraction({function: 'mintAdd', target: walletAddress, qty: 100});
|
||||
await calleeContract.writeInteraction({ function: 'mintAdd', target: walletAddress, qty: 100 });
|
||||
await mineBlock(warp);
|
||||
|
||||
await calleeContract.writeInteraction({function: 'mintAdd', target: walletAddress, qty: 100});
|
||||
await calleeContract.writeInteraction({ function: 'mintAdd', target: walletAddress, qty: 100 });
|
||||
await callingContract.writeInteraction({
|
||||
function: 'innerWriteKV',
|
||||
txId: calleeTxId,
|
||||
@@ -331,7 +330,7 @@ describe('Testing internal writes', () => {
|
||||
});
|
||||
await mineBlock(warp);
|
||||
|
||||
await calleeContract.writeInteraction({function: 'mintAdd', target: walletAddress, qty: 100});
|
||||
await calleeContract.writeInteraction({ function: 'mintAdd', target: walletAddress, qty: 100 });
|
||||
await mineBlock(warp);
|
||||
|
||||
await calleeContract.readState();
|
||||
@@ -344,7 +343,5 @@ describe('Testing internal writes', () => {
|
||||
const kvValues = await calleeContract.getStorageValues([walletAddress]);
|
||||
expect(kvValues.cachedValue.get(walletAddress)).toEqual(1400);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import {DEFAULT_LEVEL_DB_LOCATION, WarpFactory} from '../../core/WarpFactory';
|
||||
import { DEFAULT_LEVEL_DB_LOCATION, WarpFactory } from '../../core/WarpFactory';
|
||||
import fs from 'fs';
|
||||
import { SmartWeaveGlobal } from '../../legacy/smartweave-global';
|
||||
import Arweave from 'arweave';
|
||||
@@ -6,7 +6,7 @@ import { DefaultEvaluationOptions } from '../../core/modules/StateEvaluator';
|
||||
import { LevelDbCache } from '../../cache/impl/LevelDbCache';
|
||||
import { GQLNodeInterface } from '../../legacy/gqlResult';
|
||||
import { CacheKey } from '../../cache/SortKeyCache';
|
||||
import {ContractInteractionState} from "../../contract/states/ContractInteractionState";
|
||||
import { ContractInteractionState } from '../../contract/states/ContractInteractionState';
|
||||
|
||||
describe('KV database', () => {
|
||||
describe('with the SmartWeave Global KV implementation', () => {
|
||||
@@ -18,7 +18,13 @@ describe('KV database', () => {
|
||||
|
||||
const interactionState = new ContractInteractionState(WarpFactory.forTestnet());
|
||||
|
||||
const sut = new SmartWeaveGlobal(arweave, { id: 'KV_TRIE_TEST_SW_GLOBAL', owner: '' }, new DefaultEvaluationOptions(), interactionState, db);
|
||||
const sut = new SmartWeaveGlobal(
|
||||
arweave,
|
||||
{ id: 'KV_TRIE_TEST_SW_GLOBAL', owner: '' },
|
||||
new DefaultEvaluationOptions(),
|
||||
interactionState,
|
||||
db
|
||||
);
|
||||
|
||||
it('should set values', async () => {
|
||||
sut._activeTx = { sortKey: '123' } as GQLNodeInterface;
|
||||
@@ -40,7 +46,7 @@ describe('KV database', () => {
|
||||
await sut.kv.put('one', '1');
|
||||
await sut.kv.put('three', 3);
|
||||
await sut.kv.commit();
|
||||
await db.close();
|
||||
await db.close();
|
||||
await interactionState.commit(sut._activeTx);
|
||||
await db.open();
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { EvaluationOptions } from '../core/modules/StateEvaluator';
|
||||
import { GQLNodeInterface, GQLTagInterface, VrfData } from './gqlResult';
|
||||
import { CacheKey, SortKeyCache } from '../cache/SortKeyCache';
|
||||
import { SortKeyCacheRangeOptions } from '../cache/SortKeyCacheRangeOptions';
|
||||
import {InteractionState} from "../contract/states/InteractionState";
|
||||
import { InteractionState } from '../contract/states/InteractionState';
|
||||
|
||||
/**
|
||||
*
|
||||
@@ -262,11 +262,12 @@ export class KV {
|
||||
private readonly _storage: SortKeyCache<any> | null,
|
||||
private readonly _interactionState: InteractionState,
|
||||
private readonly _transaction: SWTransaction,
|
||||
private readonly _contractTxId: string) {}
|
||||
private readonly _contractTxId: string
|
||||
) {}
|
||||
|
||||
async put(key: string, value: any): Promise<void> {
|
||||
this.checkStorageAvailable();
|
||||
await this._storage.put(new CacheKey(key, this._transaction.sortKey), value)
|
||||
await this._storage.put(new CacheKey(key, this._transaction.sortKey), value);
|
||||
}
|
||||
|
||||
async get(key: string): Promise<unknown | null> {
|
||||
@@ -274,7 +275,10 @@ export class KV {
|
||||
const sortKey = this._transaction.sortKey;
|
||||
|
||||
// then we're checking if the values exists in the interactionState
|
||||
const interactionStateValue = await this._interactionState.getKV(this._contractTxId, new CacheKey(key, this._transaction.sortKey));
|
||||
const interactionStateValue = await this._interactionState.getKV(
|
||||
this._contractTxId,
|
||||
new CacheKey(key, this._transaction.sortKey)
|
||||
);
|
||||
if (interactionStateValue != null) {
|
||||
return interactionStateValue;
|
||||
}
|
||||
@@ -288,7 +292,10 @@ export class KV {
|
||||
const sortKey = this._transaction.sortKey;
|
||||
|
||||
// then we're checking if the values exists in the interactionState
|
||||
const interactionStateValue = await this._interactionState.delKV(this._contractTxId, new CacheKey(key, this._transaction.sortKey));
|
||||
const interactionStateValue = await this._interactionState.delKV(
|
||||
this._contractTxId,
|
||||
new CacheKey(key, this._transaction.sortKey)
|
||||
);
|
||||
if (interactionStateValue != null) {
|
||||
return interactionStateValue;
|
||||
}
|
||||
@@ -298,27 +305,27 @@ export class KV {
|
||||
|
||||
async keys(options?: SortKeyCacheRangeOptions): Promise<string[]> {
|
||||
const sortKey = this._transaction.sortKey;
|
||||
return await this._storage.keys(sortKey, options)
|
||||
return await this._storage.keys(sortKey, options);
|
||||
}
|
||||
|
||||
async kvMap<V>(options?: SortKeyCacheRangeOptions): Promise<Map<string, V>> {
|
||||
const sortKey = this._transaction.sortKey;
|
||||
return this._storage.kvMap(sortKey, options)
|
||||
return this._storage.kvMap(sortKey, options);
|
||||
}
|
||||
|
||||
async commit(): Promise<void> {
|
||||
if (this._storage) {
|
||||
if (this._transaction.dryRun) {
|
||||
this._storage.rollback()
|
||||
this._storage.rollback();
|
||||
} else {
|
||||
this._storage.commit()
|
||||
this._storage.commit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async rollback(): Promise<void> {
|
||||
if (this._storage) {
|
||||
this._storage.rollback()
|
||||
this._storage.rollback();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user