Skip to content

Commit

Permalink
zone and shard enums
Browse files Browse the repository at this point in the history
  • Loading branch information
robertlincecum authored and rileystephens28 committed Jun 7, 2024
1 parent f1dd254 commit b82abde
Show file tree
Hide file tree
Showing 23 changed files with 722 additions and 593 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
"_dist-stats": "gzip -k9f -S '.gz' ./dist/quais.min.js && gzip -k9f -S '.gz' ./dist/quais.umd.min.js && gzip -k9f -S '.gz' ./dist/wordlists-extra.min.js && du -hs ./dist/*.gz && echo '' && du -hs ./dist/*.js",
"auto-build": "npm run build -- -w",
"lint": "eslint src/**/*.ts --fix",
"build": "tsc --project tsconfig.esm.json",
"build": "tsc --project tsconfig.esm.json && cp ./misc/basedirs/lib.esm/package.json ./lib/esm",
"build-all": "npm run build && npm run build-commonjs",
"build-clean": "npm run clean && npm run build && npm run build-all && npm run _build-dist && npm run _dist-stats",
"build-commonjs": "tsc --project tsconfig.commonjs.json",
Expand Down
131 changes: 66 additions & 65 deletions src/_tests/test-providers-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import assert from 'assert';
//import type { Provider } from "../index.js";
import { getTxType, quais } from '../index.js';
import axios from 'axios';
import { stall } from "./utils.js";
import dotenv from "dotenv";
import { stall } from './utils.js';
import dotenv from 'dotenv';
import { Shard } from '../constants/index.js';
dotenv.config();
// import {
// networkFeatureAtBlock, networkNames,
Expand All @@ -20,13 +21,12 @@ dotenv.config();

//setupProviders();


const providerC1 = new quais.JsonRpcProvider(process.env.CYPRUS1URL);
const privateKey = process.env.CYPRUS1PK;
console.log(privateKey)
console.log(privateKey);
const wallet = new quais.Wallet(process.env.CYPRUS1PK || '', providerC1);
const destinationC1 = '0x0047f9CEa7662C567188D58640ffC48901cde02a'
const destinationC2 = '0x011ae0a1Bd5B71b4F16F8FdD3AEF278C3D042449'
const destinationC1 = '0x0047f9CEa7662C567188D58640ffC48901cde02a';
const destinationC2 = '0x011ae0a1Bd5B71b4F16F8FdD3AEF278C3D042449';

function equals(name: string, actual: any, expected: any): void {
if (expected && expected.eq) {
Expand Down Expand Up @@ -100,43 +100,43 @@ async function getRPCGasPrice(url: string | undefined) {
async function sendTransaction(to: string) {
let txResponse;
let typeValue;
try{
console.log("Nonce: ", await providerC1.getTransactionCount(wallet.address, 'latest'),)
do{
typeValue = getTxType(wallet.address, to);
const gas = await getRPCGasPrice(process.env.CYPRUS1URL);
let tx: {
from: string;
to: string;
value: any;
gasPrice: any;
maxFeePerGas: any;
maxPriorityFeePerGas:any;
nonce: number;
data: string;
type: number;
gasLimit: number;
chainId: number;
etxGasLimit?: any;
etxGasTip?: any;
etxGasPrice?: any;
} = {
from: wallet.address,
to,
value: quais.parseEther("0.1"), // Sending 0.1 ether
gasPrice: gas*2,
maxFeePerGas: quais.parseUnits('20', 'gwei'),
maxPriorityFeePerGas: quais.parseUnits('20', 'gwei'),
nonce: await providerC1.getTransactionCount(wallet.address, 'latest'),
data: '',
type: typeValue,
gasLimit: typeValue == 0 ? 21000 : 42000,
chainId: Number(process.env.CHAIN_ID || 1337),
};
txResponse = await wallet.sendTransaction(tx);
console.log(txResponse)
await stall(15000);
} while (txResponse.hash == null);
try {
console.log('Nonce: ', await providerC1.getTransactionCount(wallet.address, 'latest'));
do {
typeValue = getTxType(wallet.address, to);
const gas = await getRPCGasPrice(process.env.CYPRUS1URL);
const tx: {
from: string;
to: string;
value: any;
gasPrice: any;
maxFeePerGas: any;
maxPriorityFeePerGas: any;
nonce: number;
data: string;
type: number;
gasLimit: number;
chainId: number;
etxGasLimit?: any;
etxGasTip?: any;
etxGasPrice?: any;
} = {
from: wallet.address,
to,
value: quais.parseEther('0.1'), // Sending 0.1 ether
gasPrice: gas * 2,
maxFeePerGas: quais.parseUnits('20', 'gwei'),
maxPriorityFeePerGas: quais.parseUnits('20', 'gwei'),
nonce: await providerC1.getTransactionCount(wallet.address, 'latest'),
data: '',
type: typeValue,
gasLimit: typeValue == 0 ? 21000 : 42000,
chainId: Number(process.env.CHAIN_ID || 1337),
};
txResponse = await wallet.sendTransaction(tx);
console.log(txResponse);
await stall(15000);
} while (txResponse.hash == null);

console.log(`Transaction hash for type ${typeValue}: `, txResponse.hash);
return txResponse;
Expand All @@ -152,16 +152,13 @@ async function fetchRPCBlock(blockNumber: string | null) {
try {
let response;
do {
response = await axios.post(process.env.CYPRUS1URL || "http://localhost:8610", {
jsonrpc: "2.0",
method: "quai_getBlockByNumber",
params: [
blockNumber || '0xA',
false
],
id: 1
});
}while (response?.data?.result?.woHeader?.headerHash == null)
response = await axios.post(process.env.CYPRUS1URL || 'http://localhost:8610', {
jsonrpc: '2.0',
method: 'quai_getBlockByNumber',
params: [blockNumber || '0xA', false],
id: 1,
});
} while (response?.data?.result?.woHeader?.headerHash == null);
return response.data.result;
} catch (error: any) {
throw error;
Expand Down Expand Up @@ -242,7 +239,7 @@ describe('Test Provider Block operations', function () {
});

it('should fetch block by number', async function () {
const responseBlock = (await providerC1.getBlock('0,0', '0xA')) as quais.Block;
const responseBlock = (await providerC1.getBlock(Shard.Cyprus1, '0xA')) as quais.Block;
assert.ok(responseBlock != null, 'block != null');
// TODO: `provider` is not used, remove?
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand All @@ -256,7 +253,7 @@ describe('Test Provider Block operations', function () {

it('should fetch block by hash', async function () {
assert.ok(block.hash != null, 'block.hash != null');
const responseBlock = (await providerC1.getBlock('0,0', block.hash)) as quais.Block;
const responseBlock = (await providerC1.getBlock(Shard.Paxos2, block.hash)) as quais.Block;
assert.ok(responseBlock != null, 'block != null');
// TODO: `provider` is not used, remove?
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand All @@ -274,20 +271,22 @@ describe('Test Transaction operations', function () {
let internalToExternalTx: any;

it('should fetch balance after internal tx', async function () {
this.timeout(60000)
const oldBal = await fetchRPCBalance(destinationC1, process.env.CYPRUS1URL || "http://localhost:8610");
this.timeout(60000);
const oldBal = await fetchRPCBalance(destinationC1, process.env.CYPRUS1URL || 'http://localhost:8610');
internalTx = await sendTransaction(destinationC1);
await stall(30000);
const expectedBal = BigInt(internalTx.value);
const balance = await providerC1.getBalance(destinationC1);
const actualBal = Number(balance) - Number(oldBal);
const tolerance = 1e-6; // Define a small tolerance level

const withinTolerance = Math.abs((actualBal - Number(expectedBal)) * 100 / Number(expectedBal)) <= tolerance;
assert(withinTolerance, `Actual balance ${actualBal} is not within the acceptable range of expected balance ${Number(expectedBal)}`);
const withinTolerance = Math.abs(((actualBal - Number(expectedBal)) * 100) / Number(expectedBal)) <= tolerance;
assert(
withinTolerance,
`Actual balance ${actualBal} is not within the acceptable range of expected balance ${Number(expectedBal)}`,
);


const receipt = await fetchRPCTxReceipt(internalTx.hash, process.env.CYPRUS1URL || "http://localhost:8610");
const receipt = await fetchRPCTxReceipt(internalTx.hash, process.env.CYPRUS1URL || 'http://localhost:8610');
const expectedReceipt = {
blockHash: receipt.blockHash,
contractAddress: receipt.contractAddress || null,
Expand All @@ -312,16 +311,18 @@ describe('Test Transaction operations', function () {
...receiptResponse,
logs: receiptResponse?.logs,
};
console.log(receiptResult.blockHash)
equals("Internal Tx Receipt", receiptResult, expectedReceipt);

console.log(receiptResult.blockHash);
equals('Internal Tx Receipt', receiptResult, expectedReceipt);
});

it('should fetch transaction receipt for internal to external tx', async function () {
this.timeout(120000);
internalToExternalTx = await sendTransaction(destinationC2);
await stall(60000);
const receipt = await fetchRPCTxReceipt(internalToExternalTx.hash, process.env.CYPRUS1URL || "http://localhost:8610");
const receipt = await fetchRPCTxReceipt(
internalToExternalTx.hash,
process.env.CYPRUS1URL || 'http://localhost:8610',
);
await stall(30000);
const etx = receipt.etxs[0];
const expectedReceipt = {
Expand Down
19 changes: 7 additions & 12 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@
* @_section: api/constants: Constants [about-constants]
*/

export { ZeroAddress } from "./addresses.js";
export { ZeroHash } from "./hashes.js";
export {
N,
WeiPerEther,
MaxUint256,
MinInt256,
MaxInt256
} from "./numbers.js";
export { quaisymbol, MessagePrefix } from "./strings.js";
export { ShardData } from "./shards.js";
export { QI_COIN_TYPE, QUAI_COIN_TYPE } from "./coins.js";
export { ZeroAddress } from './addresses.js';
export { ZeroHash } from './hashes.js';
export { N, WeiPerEther, MaxUint256, MinInt256, MaxInt256 } from './numbers.js';
export { quaisymbol, MessagePrefix } from './strings.js';
export { toShard, Shard, ShardData } from './shards.js';
export { toZone, Zone, ZoneData } from './zones.js';
export { QI_COIN_TYPE, QUAI_COIN_TYPE } from './coins.js';
117 changes: 62 additions & 55 deletions src/constants/shards.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,72 @@
import { ZoneData } from './zones.js';

export enum Shard {
Cyprus = '0x0',
Cyprus1 = '0x00',
Cyprus2 = '0x01',
Cyprus3 = '0x02',
Paxos = '0x1',
Paxos1 = '0x10',
Paxos2 = '0x11',
Paxos3 = '0x12',
Hydra = '0x2',
Hydra1 = '0x20',
Hydra2 = '0x21',
Hydra3 = '0x22',
Prime = '0x',
}

function shardFromBytes(shard: string): Shard {
switch (shard) {
case '0x00':
return Shard.Cyprus1;
case '0x01':
return Shard.Cyprus2;
case '0x02':
return Shard.Cyprus3;
case '0x10':
return Shard.Paxos1;
case '0x11':
return Shard.Paxos2;
case '0x12':
return Shard.Paxos3;
case '0x20':
return Shard.Hydra1;
case '0x21':
return Shard.Hydra2;
case '0x22':
return Shard.Hydra3;
default:
throw new Error('Invalid shard');
}
}
/**
* Constant data that defines each shard within the network.
*
* @category Constants
*/

export const ShardData = [
...ZoneData,
{
name: 'Cyprus One',
nickname: 'cyprus1',
shard: 'zone-0-0',
context: 2,
byte: '0x00', //0000 0000 region-0 zone-0
},
{
name: 'Cyprus Two',
nickname: 'cyprus2',
shard: 'zone-0-1',
context: 2,
byte: '0x01', // 0000 0001 region-0 zone-1
},
{
name: 'Cyprus Three',
nickname: 'cyprus3',
shard: 'zone-0-2',
context: 2,
byte: '0x02', // 0000 0010 region-0 zone-2
},
{
name: 'Paxos One',
nickname: 'paxos1',
shard: 'zone-1-0',
context: 2,
byte: '0x10', // 0001 0000 region-1 zone-0
},
{
name: 'Paxos Two',
nickname: 'paxos2',
shard: 'zone-1-1',
name: 'Cyprus',
nickname: 'cyprus',
shard: 'region-0',
context: 2,
byte: '0x11', // 0001 0001 region-1 zone-1
byte: '0x0',
},
{
name: 'Paxos Three',
nickname: 'paxos3',
shard: 'zone-1-2',
name: 'Paxos',
nickname: 'paxos',
shard: 'region-1',
context: 2,
byte: '0x12', // 0001 0010 region-1 zone-2
byte: '0x1',
},
{
name: 'Hydra One',
nickname: 'hydra1',
shard: 'zone-2-0',
name: 'Hydra',
nickname: 'hydra',
shard: 'region-2',
context: 2,
byte: '0x20', // 0010 0000 region-2 zone-0
},
{
name: 'Hydra Two',
nickname: 'hydra2',
shard: 'zone-2-1',
context: 2,
byte: '0x21', // 0010 0001 region-2 zone-1
},
{
name: 'Hydra Three',
nickname: 'hydra3',
shard: 'zone-2-2',
context: 2,
byte: '0x22', // 0010 0010 region-2 zone-2
byte: '0x2',
},
{
name: 'Prime',
Expand All @@ -76,3 +76,10 @@ export const ShardData = [
byte: '0x',
},
];

export function toShard(shard: string): Shard {
return shardFromBytes(
ShardData.find((it) => it.name == shard || it.byte == shard || it.nickname == shard || it.shard == shard)
?.byte || '',
);
}
Loading

0 comments on commit b82abde

Please sign in to comment.