@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "warp-contracts",
|
"name": "warp-contracts",
|
||||||
"version": "1.4.25",
|
"version": "1.5.0-rc.0",
|
||||||
"description": "An implementation of the SmartWeave smart contract protocol.",
|
"description": "An implementation of the SmartWeave smart contract protocol.",
|
||||||
"types": "./lib/types/index.d.ts",
|
"types": "./lib/types/index.d.ts",
|
||||||
"main": "./lib/cjs/index.js",
|
"main": "./lib/cjs/index.js",
|
||||||
|
|||||||
@@ -17,9 +17,11 @@ interface ExampleContractState {
|
|||||||
counter: number;
|
counter: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
const DECENTRALIZED_SEQUENCER_URLS = ['http://sequencer-0.testnet.warp.cc:1317',
|
const DECENTRALIZED_SEQUENCER_URLS = [
|
||||||
|
'http://sequencer-0.testnet.warp.cc:1317',
|
||||||
'http://sequencer-1.testnet.warp.cc:1317',
|
'http://sequencer-1.testnet.warp.cc:1317',
|
||||||
'http://sequencer-2.testnet.warp.cc:1317'];
|
'http://sequencer-2.testnet.warp.cc:1317'
|
||||||
|
];
|
||||||
const GW_URL = 'http://35.242.203.146:5666/';
|
const GW_URL = 'http://35.242.203.146:5666/';
|
||||||
|
|
||||||
describe('Testing sending of interactions to a decentralized sequencer', () => {
|
describe('Testing sending of interactions to a decentralized sequencer', () => {
|
||||||
@@ -42,10 +44,12 @@ describe('Testing sending of interactions to a decentralized sequencer', () => {
|
|||||||
mockGwServer = createServer((req, res) => {
|
mockGwServer = createServer((req, res) => {
|
||||||
if (req.url === '/gateway/sequencer/address') {
|
if (req.url === '/gateway/sequencer/address') {
|
||||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||||
res.end(JSON.stringify({
|
res.end(
|
||||||
|
JSON.stringify({
|
||||||
urls: centralizedSequencerType ? [mockGwUrl] : DECENTRALIZED_SEQUENCER_URLS,
|
urls: centralizedSequencerType ? [mockGwUrl] : DECENTRALIZED_SEQUENCER_URLS,
|
||||||
type: centralizedSequencerType ? 'centralized' : 'decentralized'
|
type: centralizedSequencerType ? 'centralized' : 'decentralized'
|
||||||
}));
|
})
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
} else if (req.url === '/gateway/v2/sequencer/register') {
|
} else if (req.url === '/gateway/v2/sequencer/register') {
|
||||||
centralizedSequencerType = false;
|
centralizedSequencerType = false;
|
||||||
@@ -68,7 +72,7 @@ describe('Testing sending of interactions to a decentralized sequencer', () => {
|
|||||||
|
|
||||||
var proxy = request(options, (gwRes) => {
|
var proxy = request(options, (gwRes) => {
|
||||||
if (gwRes.statusCode) {
|
if (gwRes.statusCode) {
|
||||||
res.writeHead(gwRes.statusCode, gwRes.headers)
|
res.writeHead(gwRes.statusCode, gwRes.headers);
|
||||||
gwRes.pipe(res, {
|
gwRes.pipe(res, {
|
||||||
end: true
|
end: true
|
||||||
});
|
});
|
||||||
@@ -79,14 +83,14 @@ describe('Testing sending of interactions to a decentralized sequencer', () => {
|
|||||||
end: true
|
end: true
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
await new Promise<void>(resolve => {
|
await new Promise<void>((resolve) => {
|
||||||
mockGwServer.listen(() => {
|
mockGwServer.listen(() => {
|
||||||
const address = mockGwServer.address() as AddressInfo
|
const address = mockGwServer.address() as AddressInfo;
|
||||||
mockGwUrl = `http://localhost:${address.port}`
|
mockGwUrl = `http://localhost:${address.port}`;
|
||||||
resolve()
|
resolve();
|
||||||
})
|
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
};
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
LoggerFactory.INST.logLevel('debug');
|
LoggerFactory.INST.logLevel('debug');
|
||||||
@@ -107,11 +111,14 @@ describe('Testing sending of interactions to a decentralized sequencer', () => {
|
|||||||
const cacheOptions = {
|
const cacheOptions = {
|
||||||
...defaultCacheOptions,
|
...defaultCacheOptions,
|
||||||
inMemory: true
|
inMemory: true
|
||||||
}
|
};
|
||||||
const gatewayOptions = { ...defaultWarpGwOptions, source: SourceType.WARP_SEQUENCER, confirmationStatus: { notCorrupted: true } }
|
const gatewayOptions = {
|
||||||
|
...defaultWarpGwOptions,
|
||||||
|
source: SourceType.WARP_SEQUENCER,
|
||||||
|
confirmationStatus: { notCorrupted: true }
|
||||||
|
};
|
||||||
|
|
||||||
warp = WarpFactory
|
warp = WarpFactory.custom(arweave, cacheOptions, 'custom')
|
||||||
.custom(arweave, cacheOptions, 'custom')
|
|
||||||
.useWarpGateway(gatewayOptions, cacheOptions)
|
.useWarpGateway(gatewayOptions, cacheOptions)
|
||||||
.build()
|
.build()
|
||||||
.useGwUrl(mockGwUrl)
|
.useGwUrl(mockGwUrl)
|
||||||
@@ -132,26 +139,25 @@ describe('Testing sending of interactions to a decentralized sequencer', () => {
|
|||||||
sequencerUrl: mockGwUrl
|
sequencerUrl: mockGwUrl
|
||||||
});
|
});
|
||||||
contract.connect(wallet);
|
contract.connect(wallet);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await arlocal.stop();
|
await arlocal.stop();
|
||||||
await new Promise(resolve => {
|
await new Promise((resolve) => {
|
||||||
mockGwServer.close(resolve)
|
mockGwServer.close(resolve);
|
||||||
})
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const getNonceFromResult = (result: WriteInteractionResponse | null): number => {
|
const getNonceFromResult = (result: WriteInteractionResponse | null): number => {
|
||||||
if (result) {
|
if (result) {
|
||||||
for (let tag of result.interactionTx.tags) {
|
for (let tag of result.interactionTx.tags) {
|
||||||
if (tag.name === WARP_TAGS.SEQUENCER_NONCE) {
|
if (tag.name === WARP_TAGS.SEQUENCER_NONCE) {
|
||||||
return Number(tag.value)
|
return Number(tag.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1
|
return -1;
|
||||||
}
|
};
|
||||||
|
|
||||||
it('should follow the redirection returned by the centralized sequencer.', async () => {
|
it('should follow the redirection returned by the centralized sequencer.', async () => {
|
||||||
confirmAnyTx = true;
|
confirmAnyTx = true;
|
||||||
@@ -162,23 +168,23 @@ describe('Testing sending of interactions to a decentralized sequencer', () => {
|
|||||||
|
|
||||||
await contract.writeInteraction({ function: 'add' });
|
await contract.writeInteraction({ function: 'add' });
|
||||||
const result = await contract.writeInteraction({ function: 'add' });
|
const result = await contract.writeInteraction({ function: 'add' });
|
||||||
expect(getNonceFromResult(result)).toEqual(1)
|
expect(getNonceFromResult(result)).toEqual(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should add new interactions waiting for confirmation from the gateway', async () => {
|
it('should add new interactions waiting for confirmation from the gateway', async () => {
|
||||||
contract.setEvaluationOptions({ waitForConfirmation: true })
|
contract.setEvaluationOptions({ waitForConfirmation: true });
|
||||||
setTimeout(() => confirmAnyTx = true, 2000);
|
setTimeout(() => (confirmAnyTx = true), 2000);
|
||||||
|
|
||||||
await contract.writeInteraction({ function: 'add' });
|
await contract.writeInteraction({ function: 'add' });
|
||||||
const result = await contract.writeInteraction({ function: 'add' });
|
const result = await contract.writeInteraction({ function: 'add' });
|
||||||
expect(getNonceFromResult(result)).toEqual(3)
|
expect(getNonceFromResult(result)).toEqual(3);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should add new interactions without waiting for confirmation from the gateway', async () => {
|
it('should add new interactions without waiting for confirmation from the gateway', async () => {
|
||||||
contract.setEvaluationOptions({ waitForConfirmation: false })
|
contract.setEvaluationOptions({ waitForConfirmation: false });
|
||||||
|
|
||||||
await contract.writeInteraction({ function: 'add' });
|
await contract.writeInteraction({ function: 'add' });
|
||||||
const result = await contract.writeInteraction({ function: 'add' });
|
const result = await contract.writeInteraction({ function: 'add' });
|
||||||
expect(getNonceFromResult(result)).toEqual(5)
|
expect(getNonceFromResult(result)).toEqual(5);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -13,33 +13,38 @@ const SEQUENCER_URL = 'http://sequencer-0.testnet.warp.cc:1317';
|
|||||||
const GW_URL = 'https://gw-testnet.warp.cc';
|
const GW_URL = 'https://gw-testnet.warp.cc';
|
||||||
|
|
||||||
describe('Testing a decentralized sequencer client', () => {
|
describe('Testing a decentralized sequencer client', () => {
|
||||||
|
|
||||||
const createClient = (): SequencerClient => {
|
const createClient = (): SequencerClient => {
|
||||||
const warpFetchWrapper = new WarpFetchWrapper(WarpFactory.forLocal())
|
const warpFetchWrapper = new WarpFetchWrapper(WarpFactory.forLocal());
|
||||||
return new DecentralizedSequencerClient(SEQUENCER_URL, GW_URL, warpFetchWrapper);
|
return new DecentralizedSequencerClient(SEQUENCER_URL, GW_URL, warpFetchWrapper);
|
||||||
}
|
};
|
||||||
|
|
||||||
const createSignature = async (): Promise<Signature> => {
|
const createSignature = async (): Promise<Signature> => {
|
||||||
const wallet = await Arweave.crypto.generateJWK();
|
const wallet = await Arweave.crypto.generateJWK();
|
||||||
const signer = new ArweaveSigner(wallet);
|
const signer = new ArweaveSigner(wallet);
|
||||||
return new Signature(WarpFactory.forLocal(), signer)
|
return new Signature(WarpFactory.forLocal(), signer);
|
||||||
}
|
};
|
||||||
|
|
||||||
const createDataItem = async (signature: Signature, nonce: number, addNonceTag = true, addContractTag = true, signDataItem = true): Promise<DataItem> => {
|
const createDataItem = async (
|
||||||
|
signature: Signature,
|
||||||
|
nonce: number,
|
||||||
|
addNonceTag = true,
|
||||||
|
addContractTag = true,
|
||||||
|
signDataItem = true
|
||||||
|
): Promise<DataItem> => {
|
||||||
const signer = signature.bundlerSigner;
|
const signer = signature.bundlerSigner;
|
||||||
const tags: Tag[] = [];
|
const tags: Tag[] = [];
|
||||||
if (addNonceTag) {
|
if (addNonceTag) {
|
||||||
tags.push(new Tag(WARP_TAGS.SEQUENCER_NONCE, String(nonce)));
|
tags.push(new Tag(WARP_TAGS.SEQUENCER_NONCE, String(nonce)));
|
||||||
}
|
}
|
||||||
if (addContractTag) {
|
if (addContractTag) {
|
||||||
tags.push(new Tag(SMART_WEAVE_TAGS.CONTRACT_TX_ID, "unit test contract"));
|
tags.push(new Tag(SMART_WEAVE_TAGS.CONTRACT_TX_ID, 'unit test contract'));
|
||||||
}
|
}
|
||||||
const dataItem = createData('some data', signer, { tags });
|
const dataItem = createData('some data', signer, { tags });
|
||||||
if (signDataItem) {
|
if (signDataItem) {
|
||||||
await dataItem.sign(signer);
|
await dataItem.sign(signer);
|
||||||
}
|
}
|
||||||
return dataItem;
|
return dataItem;
|
||||||
}
|
};
|
||||||
|
|
||||||
it('should return consecutive nonces for a given signature', async () => {
|
it('should return consecutive nonces for a given signature', async () => {
|
||||||
const client = createClient();
|
const client = createClient();
|
||||||
@@ -56,9 +61,9 @@ describe('Testing a decentralized sequencer client', () => {
|
|||||||
const signature = await createSignature();
|
const signature = await createSignature();
|
||||||
const dataItem = await createDataItem(signature, 13);
|
const dataItem = await createDataItem(signature, 13);
|
||||||
|
|
||||||
expect(client.sendDataItem(dataItem, false))
|
expect(client.sendDataItem(dataItem, false)).rejects.toThrowError(
|
||||||
.rejects
|
'account sequence mismatch, expected 0, got 13: incorrect account sequence'
|
||||||
.toThrowError('account sequence mismatch, expected 0, got 13: incorrect account sequence');
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should reject a data item without nonce', async () => {
|
it('should reject a data item without nonce', async () => {
|
||||||
@@ -66,9 +71,7 @@ describe('Testing a decentralized sequencer client', () => {
|
|||||||
const signature = await createSignature();
|
const signature = await createSignature();
|
||||||
const dataItem = await createDataItem(signature, 0, false);
|
const dataItem = await createDataItem(signature, 0, false);
|
||||||
|
|
||||||
expect(client.sendDataItem(dataItem, true))
|
expect(client.sendDataItem(dataItem, true)).rejects.toThrowError('no sequencer nonce tag');
|
||||||
.rejects
|
|
||||||
.toThrowError('no sequencer nonce tag');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should reject a data item without contract', async () => {
|
it('should reject a data item without contract', async () => {
|
||||||
@@ -76,9 +79,7 @@ describe('Testing a decentralized sequencer client', () => {
|
|||||||
const signature = await createSignature();
|
const signature = await createSignature();
|
||||||
const dataItem = await createDataItem(signature, 0, true, false);
|
const dataItem = await createDataItem(signature, 0, true, false);
|
||||||
|
|
||||||
expect(client.sendDataItem(dataItem, true))
|
expect(client.sendDataItem(dataItem, true)).rejects.toThrowError('no contract tag');
|
||||||
.rejects
|
|
||||||
.toThrowError('no contract tag');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should reject an unsigned data item', async () => {
|
it('should reject an unsigned data item', async () => {
|
||||||
@@ -86,9 +87,7 @@ describe('Testing a decentralized sequencer client', () => {
|
|||||||
const signature = await createSignature();
|
const signature = await createSignature();
|
||||||
const dataItem = await createDataItem(signature, 0, true, true, false);
|
const dataItem = await createDataItem(signature, 0, true, true, false);
|
||||||
|
|
||||||
expect(client.sendDataItem(dataItem, true))
|
expect(client.sendDataItem(dataItem, true)).rejects.toThrowError('data item verification error');
|
||||||
.rejects
|
|
||||||
.toThrowError('data item verification error');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return an unconfirmed result', async () => {
|
it('should return an unconfirmed result', async () => {
|
||||||
|
|||||||
@@ -166,9 +166,9 @@ export class SmartWeaveGlobal {
|
|||||||
throw new Error(`Integer max value must be in the range [1, ${Number.MAX_SAFE_INTEGER}]`);
|
throw new Error(`Integer max value must be in the range [1, ${Number.MAX_SAFE_INTEGER}]`);
|
||||||
}
|
}
|
||||||
const base64 = this._activeTx.random.replace(/-/g, '+').replace(/_/g, '/');
|
const base64 = this._activeTx.random.replace(/-/g, '+').replace(/_/g, '/');
|
||||||
const array = Uint8Array.from(atob(base64), c => c.charCodeAt(0));
|
const array = Uint8Array.from(atob(base64), (c) => c.charCodeAt(0));
|
||||||
const bigInt = Buffer.from(array).readBigUInt64BE();
|
const bigInt = Buffer.from(array).readBigUInt64BE();
|
||||||
const result = (bigInt % BigInt(maxValue)) + BigInt(1)
|
const result = (bigInt % BigInt(maxValue)) + BigInt(1);
|
||||||
return Number(result);
|
return Number(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user