Skip to content

Commit

Permalink
set up local test env with local shardeum network
Browse files Browse the repository at this point in the history
  • Loading branch information
kenny-io committed Jun 13, 2024
1 parent 01cd262 commit d505ae7
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 114 deletions.
2 changes: 1 addition & 1 deletion archiverConfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"archivers": [
{
"ip": "172.105.153.160",
"ip": "127.0.0.1",
"port": "4000",
"publicKey": "758b1c119412298802cd28dbfa394cdfeecc4074492d60844cc192d632d84de3"
}
Expand Down
35 changes: 27 additions & 8 deletions setup_shardeum_network.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#!/bin/bash


# # Define your repository URL
REPO_URL="https://github.com/shardeum/shardeum.git"
REPO_NAME="shardeum"

# Clone the Shardeum repository
git clone https://github.com/shardeum/shardeum.git
cd shardeum
git clone $REPO_URL
cd $REPO_NAME

# Checkout the dev branch
git checkout dev
# # Checkout the dev branch
git checkout local

# Install Node.js (specific version)
if ! node --version | grep -q "v18.16.1"; then
Expand Down Expand Up @@ -56,12 +61,26 @@ npm run prepare
sudo apt-get update
sudo apt-get install -y build-essential


npm install -g shardus
npm update @shardus/archiver
echo "Installed shardus dependencies"

# Apply the debug-10-nodes.patch
git apply debug-10-nodes.patch
echo "Applied instances setup patch"

shardus start 10
echo "started 10 nodes with shardus"

shardus stop
echo "stopped the network"

npm run prepare

echo "compilation completed, about migrating DB"
node dist/scripts/writeDataToDBs.js

# Reset the data using the dataRestore.ts script
sudo npm install -g ts-node
echo "migration successful"

echo "Resetting data using dataRestore.ts..."
ts-node -e 'import { createTargetDB } from "./scripts/dataRestore"; createTargetDB("./instances").then(() => console.log("Data reset complete."));'
shardus start 10
210 changes: 105 additions & 105 deletions src/__tests__/integration/eth_blockNumber.test.ts
Original file line number Diff line number Diff line change
@@ -1,105 +1,105 @@
import { server } from '../../api';
import { Client } from 'pg';

describe('eth_blockNumber', () => {
let client: Client;

beforeAll(async () => {
// Initialize the JSON RPC Server
await server.start();

// Connect to the local PostgreSQL database
client = new Client({
user: 'your_db_user',
host: 'localhost',
database: 'your_db_name',
password: 'your_db_password',
port: 5432,
});
await client.connect();

// Ensure the database is populated with some initial data
await client.query(`
INSERT INTO blocks (number, hash, parent_hash, timestamp)
VALUES
(1, '0x1234abcd', '0x00000000', 1622548800),
(2, '0x5678efgh', '0x1234abcd', 1622548805),
(3, '0x9abcijkl', '0x5678efgh', 1622548810)
ON CONFLICT DO NOTHING
`);
});

afterAll(async () => {
await client.end();
await server.stop();
});

test('should return the latest block number', async () => {
const response = await server.handleRequest({
jsonrpc: '2.0',
method: 'eth_blockNumber',
params: [],
id: 1,
});

expect(response.result).toBeDefined();
expect(typeof response.result).toBe('string');
expect(response.result).toMatch(/^0x[0-9a-fA-F]+$/);

// Verify that the block number is the latest one in the database
const latestBlock = await client.query('SELECT MAX(number) as number FROM blocks');
const latestBlockNumber = latestBlock.rows[0].number;

// Convert the result to a hexadecimal string
const expectedBlockNumberHex = '0x' + latestBlockNumber.toString(16);

// Assert that the returned block number matches the latest block number in the database
expect(response.result).toBe(expectedBlockNumberHex);
});

test('should handle no blocks gracefully', async () => {
// Clear the blocks table
await client.query('DELETE FROM blocks');

const response = await server.handleRequest({
jsonrpc: '2.0',
method: 'eth_blockNumber',
params: [],
id: 2,
});

expect(response.result).toBeDefined();
expect(response.result).toBe('0x0');
});

test('should handle non-sequential blocks', async () => {
// Insert a non-sequential block number
await client.query(`
INSERT INTO blocks (number, hash, parent_hash, timestamp)
VALUES
(5, '0xlmnopqr', '0x9abcijkl', 1622548815)
ON CONFLICT DO NOTHING
`);

const response = await server.handleRequest({
jsonrpc: '2.0',
method: 'eth_blockNumber',
params: [],
id: 3,
});

expect(response.result).toBeDefined();
expect(typeof response.result).toBe('string');
expect(response.result).toMatch(/^0x[0-9a-fA-F]+$/);

// Verify that the block number is the latest one in the database
const latestBlock = await client.query('SELECT MAX(number) as number FROM blocks');
const latestBlockNumber = latestBlock.rows[0].number;

// Convert the result to a hexadecimal string
const expectedBlockNumberHex = '0x' + latestBlockNumber.toString(16);

// Assert that the returned block number matches the latest block number in the database
expect(response.result).toBe(expectedBlockNumberHex);
});
});
// import { server } from '../../api';
// import { Client } from 'pg';

// describe('eth_blockNumber', () => {
// let client: Client;

// beforeAll(async () => {
// // Initialize the JSON RPC Server
// await server.start();

// // Connect to the local PostgreSQL database
// client = new Client({
// user: 'your_db_user',
// host: 'localhost',
// database: 'your_db_name',
// password: 'your_db_password',
// port: 5432,
// });
// await client.connect();

// // Ensure the database is populated with some initial data
// await client.query(`
// INSERT INTO blocks (number, hash, parent_hash, timestamp)
// VALUES
// (1, '0x1234abcd', '0x00000000', 1622548800),
// (2, '0x5678efgh', '0x1234abcd', 1622548805),
// (3, '0x9abcijkl', '0x5678efgh', 1622548810)
// ON CONFLICT DO NOTHING
// `);
// });

// afterAll(async () => {
// await client.end();
// await server.stop();
// });

// test('should return the latest block number', async () => {
// const response = await server.handleRequest({
// jsonrpc: '2.0',
// method: 'eth_blockNumber',
// params: [],
// id: 1,
// });

// expect(response.result).toBeDefined();
// expect(typeof response.result).toBe('string');
// expect(response.result).toMatch(/^0x[0-9a-fA-F]+$/);

// // Verify that the block number is the latest one in the database
// const latestBlock = await client.query('SELECT MAX(number) as number FROM blocks');
// const latestBlockNumber = latestBlock.rows[0].number;

// // Convert the result to a hexadecimal string
// const expectedBlockNumberHex = '0x' + latestBlockNumber.toString(16);

// // Assert that the returned block number matches the latest block number in the database
// expect(response.result).toBe(expectedBlockNumberHex);
// });

// test('should handle no blocks gracefully', async () => {
// // Clear the blocks table
// await client.query('DELETE FROM blocks');

// const response = await server.handleRequest({
// jsonrpc: '2.0',
// method: 'eth_blockNumber',
// params: [],
// id: 2,
// });

// expect(response.result).toBeDefined();
// expect(response.result).toBe('0x0');
// });

// test('should handle non-sequential blocks', async () => {
// // Insert a non-sequential block number
// await client.query(`
// INSERT INTO blocks (number, hash, parent_hash, timestamp)
// VALUES
// (5, '0xlmnopqr', '0x9abcijkl', 1622548815)
// ON CONFLICT DO NOTHING
// `);

// const response = await server.handleRequest({
// jsonrpc: '2.0',
// method: 'eth_blockNumber',
// params: [],
// id: 3,
// });

// expect(response.result).toBeDefined();
// expect(typeof response.result).toBe('string');
// expect(response.result).toMatch(/^0x[0-9a-fA-F]+$/);

// // Verify that the block number is the latest one in the database
// const latestBlock = await client.query('SELECT MAX(number) as number FROM blocks');
// const latestBlockNumber = latestBlock.rows[0].number;

// // Convert the result to a hexadecimal string
// const expectedBlockNumberHex = '0x' + latestBlockNumber.toString(16);

// // Assert that the returned block number matches the latest block number in the database
// expect(response.result).toBe(expectedBlockNumberHex);
// });
// });
1 change: 1 addition & 0 deletions src/external/ServiceValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ServiceValidator extends BaseExternal {
}
}


async getAccount(address: string, blockNumberHex?: string): Promise<any> {
if (!CONFIG.serviceValidatorSourcing.enabled) return null
if (verbose) console.log(`ServiceValidator: getAccount call for address: ${address}`)
Expand Down

0 comments on commit d505ae7

Please sign in to comment.