Skip to content

Commit

Permalink
fix: rpc-proxy Dockerfile using dependencies from the monorepo (#1378)
Browse files Browse the repository at this point in the history
* fix: first commit

* fix: ensure we run with local dependencies

* fix: ensure we run with local dependencies

* fix: ensure we run with local dependencies

* fix: ensure we run with local dependencies

* fix: ensure we run with local dependencies

* fix: ensure we run with local dependencies

* fix: added dockerignore file

* fix: added dockerignore file

* fix: added dockerignore file

* fix: added dockerignore file

* fix: test refactored

* fix: test refactored

* fix: test refactored

* fix: using . since we are in the app folder

* fix: added console log to test

* fix: added console log to test
  • Loading branch information
freemanzMrojo authored Oct 3, 2024
1 parent 8ecb124 commit d601cdc
Show file tree
Hide file tree
Showing 29 changed files with 129 additions and 95 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-rpc-proxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
id: start-solo
run: yarn start-thor-solo

- name: Start PRC Proxy
- name: Start RPC Proxy
run: |
cd packages/rpc-proxy
yarn start &
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ This section provides guidance on conducting integration tests using a local tho
### Setting Up

The integration tests interact with a local thor-solo node.
This node uses the `thor-solo/instance-a4988aba7aea69f6-v3/main.db` data directory,
This node uses the `docker/thor/data/instance-a4988aba7aea69f6-v3/main.db` data directory,
which is pre-configured with a block history and 20 seeded accounts for testing.

### Running Tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
# ...
#
# rpc-proxy:
# build: .
# build:
# context: .
# dockerfile: docker/rpc-proxy/Dockerfile
#
# # ********* SET HERE THE REQUIRED ENVIRONMENT VARIABLES *********
# environment:
Expand Down Expand Up @@ -52,7 +54,7 @@ services:
- --persist
volumes:
- type: bind
source: ./config
source: ./docker/rpc-proxy/config
target: /node/config
deploy:
resources:
Expand All @@ -62,7 +64,9 @@ services:

# Default configuration for the RPC proxy service
rpc-proxy:
build: .
build:
context: .
dockerfile: docker/rpc-proxy/Dockerfile
container_name: rpc-proxy
depends_on:
- thor-solo
Expand All @@ -73,20 +77,24 @@ services:

# 1. Example of a custom configuration file for the RPC proxy service
rpc-proxy-custom-config-file:
build: .
build:
context: .
dockerfile: docker/rpc-proxy/Dockerfile
environment:
- CONFIGURATION_FILE=/app/custom-config.json
- CONFIGURATION_FILE=/app/packages/rpc-proxy/custom-config.json
container_name: rpc-proxy-custom-config-file
depends_on:
- thor-solo
volumes:
- "./tests/config-files-fixtures/correct-proxy-config-accounts-list-of-private-keys.json:/app/custom-config.json"
- "./packages/rpc-proxy/tests/config-files-fixtures/correct-proxy-config-accounts-list-of-private-keys.json:/app/packages/rpc-proxy/custom-config.json"
ports:
- "8545:8545"

# 2. Example of a custom parameters for the RPC proxy service
rpc-proxy-custom-parameters:
build: .
build:
context: .
dockerfile: docker/rpc-proxy/Dockerfile
environment:
- PORT=9000
- URL=https://testnet.vechain.org
Expand All @@ -99,7 +107,9 @@ services:

# 3. Example of a custom accounts for the RPC proxy service
rpc-proxy-custom-accounts:
build: .
build:
context: .
dockerfile: docker/rpc-proxy/Dockerfile
environment:
- URL=https://testnet.vechain.org
- VERBOSE=true
Expand All @@ -112,7 +122,9 @@ services:

# 3. Example of a custom mnemonic for the RPC proxy service
rpc-proxy-custom-mnemonic:
build: .
build:
context: .
dockerfile: docker/rpc-proxy/Dockerfile
environment:
- URL=https://testnet.vechain.org
- VERBOSE=true
Expand All @@ -127,7 +139,9 @@ services:

# 4. Example of a custom delegation by private key for the RPC proxy service
rpc-proxy-with-delegation-by-private-key:
build: .
build:
context: .
dockerfile: docker/rpc-proxy/Dockerfile
environment:
- URL=https://testnet.vechain.org
- VERBOSE=true
Expand All @@ -144,7 +158,9 @@ services:

# 5. Example of a custom delegation by url for the RPC proxy service
rpc-proxy-with-delegation-by-url:
build: .
build:
context: .
dockerfile: docker/rpc-proxy/Dockerfile
environment:
- URL=https://testnet.vechain.org
- VERBOSE=true
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yaml → docker-compose.thor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ services:
thor-solo:
build:
context: .
dockerfile: Dockerfile
dockerfile: docker/thor/Dockerfile
container_name: thor-solo
user: root
ports:
Expand Down
4 changes: 4 additions & 0 deletions docker/rpc-proxy/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
packages/*/node_modules
packages/*/dist
packages/*/.turbo
packages/*/coverageUnit
51 changes: 51 additions & 0 deletions docker/rpc-proxy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Stage 1: Build the app
FROM node:20.17-alpine3.20 AS builder

# Set the working directory in the builder stage
WORKDIR /app

# Copy the dependencies file to the working directory
COPY ./packages/rpc-proxy ./packages/rpc-proxy
COPY ./packages/network ./packages/network
COPY ./packages/core ./packages/core
COPY ./packages/logging ./packages/logging
COPY ./packages/errors ./packages/errors
COPY ./package.json ./package.json
COPY ./turbo.json ./turbo.json
COPY ./yarn.lock ./yarn.lock
COPY ./tsconfig.json ./tsconfig.json

# Install all the dependencies
RUN yarn install

# Build the app (assumes output is in /app/dist or similar)
RUN yarn build

# Stage 2: Serve the app using node
FROM node:20.17.0-alpine3.20 AS runner

# Copy only the built files and essential runtime files from the builder stage
## rpc-proxy
COPY --from=builder /app/packages/rpc-proxy/dist /app/packages/rpc-proxy/dist
COPY --from=builder /app/packages/rpc-proxy/package.json /app/packages/rpc-proxy/package.json
## SDK dependencies
COPY --from=builder /app/packages/network/dist /app/packages/network/dist
COPY --from=builder /app/packages/network/package.json /app/packages/network/package.json
COPY --from=builder /app/packages/core/dist /app/packages/core/dist
COPY --from=builder /app/packages/core/package.json /app/packages/core/package.json
COPY --from=builder /app/packages/logging/dist /app/packages/logging/dist
COPY --from=builder /app/packages/logging/package.json /app/packages/logging/package.json
COPY --from=builder /app/packages/errors/dist /app/packages/errors/dist
COPY --from=builder /app/packages/errors/package.json /app/packages/errors/package.json
## node_modules
COPY --from=builder /app/node_modules /app/node_modules

# Create a new user to run the app so we do not use root
RUN adduser -D rpc-proxy-user
USER rpc-proxy-user

# Tell we are running with Docker
ENV RUNNING_WITH_DOCKER=true

# Set the default command to use node to run the app
CMD ["node", "/app/packages/rpc-proxy/dist/index.js"]
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion Dockerfile → docker/thor/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ FROM vechain/thor:v2.1.0
RUN rm -rf /data/thor

# Copy the initial database directory to the container
COPY ./thor-solo /data/thor
COPY ./docker/thor/data /data/thor
2 changes: 0 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
],
"type": "module",
"scripts": {
"start-thor-solo": "echo 'Starting thor solo node ...' && docker compose up -d --wait && echo '\nThor solo node started ...'",
"stop-thor-solo": "echo 'Stopping thor solo node ...' && docker compose down && echo 'Thor solo node stopped ...'",
"build": "find . -name \"*.md\" -type f -maxdepth 1 ! -name \"README.md\" -delete && tsup ./build-scripts && node dist/builddocs.cjs",
"test:examples": "ts-node-test examples/",
"test:examples:solo": "(yarn start-thor-solo && yarn test:examples && yarn stop-thor-solo) || yarn stop-thor-solo"
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
],
"packageManager": "[email protected]",
"scripts": {
"start-thor-solo": "echo 'Starting thor solo node ...' && docker compose up -d --wait && echo '\nThor solo node started ...'",
"stop-thor-solo": "echo 'Stopping thor solo node ...' && docker compose down && echo 'Thor solo node stopped ...'",
"start-thor-solo": "echo 'Starting thor solo node ...' && docker compose -f docker-compose.thor.yml up -d --wait && echo '\nThor solo node started ...'",
"stop-thor-solo": "echo 'Stopping thor solo node ...' && docker compose -f docker-compose.thor.yml down && echo 'Thor solo node stopped ...'",
"postinstall": "husky",
"build": "turbo build",
"check:circular-dependencies": "turbo check:circular-dependencies",
Expand Down
2 changes: 0 additions & 2 deletions packages/errors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
"LICENSE"
],
"scripts": {
"start-thor-solo": "echo 'Starting thor solo node ...' && docker compose up -d --wait && echo '\nThor solo node started ...'",
"stop-thor-solo": "echo 'Stopping thor solo node ...' && docker compose down && echo 'Thor solo node stopped ...'",
"build": "rm -rf ./dist && tsup-node src/index.ts --format cjs,esm --dts",
"lint": "eslint --ext .ts src --ext .ts tests",
"format": "prettier --write src/**/*.ts tests/**/*.ts",
Expand Down
2 changes: 0 additions & 2 deletions packages/hardhat-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
"LICENSE"
],
"scripts": {
"start-thor-solo": "echo 'Starting thor solo node ...' && docker compose up -d --wait && echo '\nThor solo node started ...'",
"stop-thor-solo": "echo 'Stopping thor solo node ...' && docker compose down && echo 'Thor solo node stopped ...'",
"build": "rm -rf ./dist && tsup-node src/index.ts --format cjs,esm --dts",
"lint": "eslint --ext .ts src --ext .ts tests",
"format": "prettier --write src/**/*.ts tests/**/*.ts",
Expand Down
2 changes: 0 additions & 2 deletions packages/network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
"LICENSE"
],
"scripts": {
"start-thor-solo": "echo 'Starting thor solo node ...' && docker compose up -d --wait && echo '\nThor solo node started ...'",
"stop-thor-solo": "echo 'Stopping thor solo node ...' && docker compose down && echo 'Thor solo node stopped ...'",
"build": "rm -rf ./dist && tsup-node src/index.ts --format cjs,esm --dts",
"check:circular-dependencies": "npx madge --exclude '^(provider/providers/vechain-provider/vechain-provider.ts|thor-client/contracts/model/contract.ts|thor-client/index.ts|provider/utils/formatter/blocks/index.ts|signer/signers/types.d.ts|provider/utils/helpers/transaction/transaction-helpers.ts|thor-client/contracts/model/contract-filter.ts)$' --circular --extensions ts src",
"lint": "eslint --ext .ts src --ext .ts tests",
Expand Down
3 changes: 0 additions & 3 deletions packages/rpc-proxy/.dockerignore

This file was deleted.

35 changes: 0 additions & 35 deletions packages/rpc-proxy/Dockerfile

This file was deleted.

4 changes: 3 additions & 1 deletion packages/rpc-proxy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ Simple testnet configuration with a delegator private url:
To run the RPC proxy as a Docker container, follow these steps:

``` bash
docker build . -t vechain-rpc-proxy
cd ../..
docker build -f docker/rpc-proxy/Dockerfile . -t vechain-rpc-proxy
# We are assuming that the config.json file is placed at the same level as the project root
docker run -d -p 8545:8545 -v ./config.json:/app/config.json -t vechain-rpc-proxy
```

Expand Down
5 changes: 4 additions & 1 deletion packages/rpc-proxy/tests/rpc_proxy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ let environment: StartedDockerComposeEnvironment;
const RPC_PROXY_URL = `http://localhost:8545`;

beforeAll(async () => {
environment = await new DockerComposeEnvironment('./', 'docker-compose.yml')
environment = await new DockerComposeEnvironment(
'../../',
'docker-compose.rpc-proxy.yml'
)
.withPullPolicy(PullPolicy.alwaysPull())
.withWaitStrategy(
'thor-solo',
Expand Down
64 changes: 34 additions & 30 deletions scripts/test-rpc-proxy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import assert from 'assert';
import { fail, strictEqual } from 'assert';

const endpointsTestCases = [
{
Expand Down Expand Up @@ -31,37 +31,41 @@ const endpointsTestCases = [
}
];

async function testRPCProxy() {
const proxyUrl = 'http://localhost:8545';
const proxyUrl = 'http://localhost:8545';

try {
// Send RPC requests to test it
endpointsTestCases.forEach(async({method, params, expected})=>{
const response = await fetch(proxyUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method,
params
})
});
const data = await response.json();

assert.ok(data && data.result, 'Response does not contain result');
assert.strictEqual(data.result, expected, 'Expected a different result');
return 0;
async function testRPCProxy(): Promise<void> {
// Send RPC requests to test it
for (const { method, params, expected } of endpointsTestCases) {
const response = await fetch(proxyUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method,
params
})
});
} catch (error) {
console.error(
'Error occurred while testing RPC Proxy:',
(error as Error).message
);
return 1;

// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const data: { result?: unknown } = await response.json();

if (data.result === undefined) {
throw new Error('Response does not contain result');
}

strictEqual(data.result, expected, 'Expected a different result');

console.log(`RPC Proxy test for ${method} passed`);
}
}

testRPCProxy();
testRPCProxy().catch((error) => {
console.error(
'Error occurred while testing RPC Proxy:',
(error as Error).message
);
fail();
});

1 comment on commit d601cdc

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test Coverage

Summary

Lines Statements Branches Functions
Coverage: 97%
97.79% (4171/4265) 95.79% (1366/1426) 97.09% (870/896)
Title Tests Skipped Failures Errors Time
core 782 0 💤 0 ❌ 0 🔥 1m 52s ⏱️
network 731 0 💤 0 ❌ 0 🔥 4m 55s ⏱️
errors 42 0 💤 0 ❌ 0 🔥 18.05s ⏱️

Please sign in to comment.