Skip to content

Commit aed4ea9

Browse files
committed
BSC unit tests done
1 parent bc88f19 commit aed4ea9

File tree

6 files changed

+212
-49
lines changed

6 files changed

+212
-49
lines changed

lib/bsc/README.md

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Sample AWS Blockchain Node Runner app for BNB Smart Chain(BSC) Nodes
22

3-
BNB Smart Chain, BSC for shot, is a blockchain that supports EVM-compatible smart contracts and protocols. It utilizes the [Proof of Stake Authority(PoSA)](https://docs.bnbchain.org/docs/learn/intro#proof-of-staked-authority) consensus mechanism, which is a hybrid consensus mechanism based on a combination of [Proof of Authority (PoA)](https://en.wikipedia.org/wiki/Proof_of_authority) and [Delegated Proof of Stake (DPoS)](https://bitcoinwiki.org/wiki/DPoS).
3+
BNB Smart Chain (BSC), is a blockchain that supports EVM-compatible smart contracts and protocols. It utilizes the [Proof of Stake Authority(PoSA)](https://docs.bnbchain.org/docs/learn/intro#proof-of-staked-authority) consensus mechanism, which is a hybrid consensus mechanism based on a combination of [Proof of Authority (PoA)](https://en.wikipedia.org/wiki/Proof_of_authority) and [Delegated Proof of Stake (DPoS)](https://bitcoinwiki.org/wiki/DPoS).
44

5-
This blueprint is designed to assist in deploying a High Availability (HA) BNB Smart Chain (BSC) Fullnode on AWS. It is intended for use in development, testing, or Proof of Concept purposes. Learn more information about configuring the [Run A Fullnode on BNB Smart Chain](https://docs.bnbchain.org/docs/validator/fullnode), the following provides detailed information on High Availability deployment settings.
5+
This blueprint is designed to assist in deploying a single node or a Highly Available (HA) [BNB Smart Chain (BSC) Fullnode](https://docs.bnbchain.org/docs/validator/fullnode/) on AWS. It is intended for use in development, testing, or Proof of Concept purposes.
66

77
## Overview of Deployment Architectures for HA setups
88

@@ -116,27 +116,27 @@ npm install
116116
117117
### Option 1: Single RPC Node
118118
119-
1. Deploying a BSC Fullnode node typically takes about 2-3 hours. The Fullnode uses snapshots data, and downloading and decompressing the data can take a considerable amount of time. You can grab a cup of coffee☕️ and patiently wait during this process. After deployment, you'll need to wait for the node to synchronize with the BSC Blockchain Network.
119+
1. The inital deployment a BSC full node and dwonloading its snapshot typically takes about 2-3 hours. The Full node uses snapshots data, and downloading and decompressing the data takes time. You can grab a cup of coffee☕️ and patiently wait during this process. After deployment, you'll need to wait for the node to synchronize with the BSC Blockchain Network (next step).
120120
121121
```bash
122-
pwd
123-
# Make sure you are in aws-blockchain-node-runners/lib/bsc
124-
npx cdk deploy bsc-single-node --json --outputs-file single-node.json
122+
pwd
123+
# Make sure you are in aws-blockchain-node-runners/lib/bsc
124+
npx cdk deploy bsc-single-node --json --outputs-file single-node.json
125125
```
126126
127-
2. Give the new RPC nodes about few hours to initialize and then run the following query against the load balancer behind the RPC node created
127+
2. After the node is initialised from the snapshot you need to wait from another half a day to a day for the inital syncronization process to finish. The time depends on how fresh the snapshot was. You can use Amazon CloudWatch to track the progress. There is a script that publishes CloudWatch metrics every 5 minutes, where you can watch `sync distance` for consensus client and `blocks behind` for execution client. When the node is fully synced those two metrics shold show 0. To see them:
128128
129-
```bash
130-
INSTANCE_ID=$(cat single-node-deploy.json | jq -r '..|.node-instance-id? | select(. != null)')
131-
NODE_INTERNAL_IP=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query 'Reservations[*].Instances[*].PrivateIpAddress' --output text)
129+
- Navigate to [CloudWatch service](https://console.aws.amazon.com/cloudwatch/) (make sure you are in the region you have specified for `AWS_REGION`)
130+
- Open `Dashboards` and select `bsc-single-node-<node_configuration>-<your_bsc_network>-<ec2_instance_id>` from the list of dashboards.
132131
133-
```
134-
135-
Check [Geth Syncing](https://geth.ethereum.org/docs/fundamentals/logs#syncing) Status:
132+
Alternatively, you can manually check [Geth Syncing Status](https://geth.ethereum.org/docs/fundamentals/logs#syncing). Run the following query from within the same VPC and against the private IP of the single RPC node you deployed:
136133
137134
```bash
138-
curl http://$NODE_INTERNAL_IP:8545 -X POST -H "Content-Type: application/json" \
139-
--data '{ "jsonrpc": "2.0", "id": 1, "method": "eth_syncing", "params": []}'
135+
INSTANCE_ID=$(cat single-node-deploy.json | jq -r '..|.node-instance-id? | select(. != null)')
136+
NODE_INTERNAL_IP=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query 'Reservations[*].Instances[*].PrivateIpAddress' --output text)
137+
138+
curl http://$NODE_INTERNAL_IP:8545 -X POST -H "Content-Type: application/json" \
139+
--data '{ "jsonrpc": "2.0", "id": 1, "method": "eth_syncing", "params": []}'
140140
```
141141
142142
It will return `false` if the node is in sync. If `eth_syncing` returns anything other than false it has not finished syncing. Generally, if syncing is still ongoing, `eth_syncing` will return block info that looks as follows:
@@ -165,24 +165,40 @@ npm install
165165
}
166166
```
167167
168+
3. Once the initial synchronization is done, you should be able to access the RPC API of that node from within the same VPC. The RPC port is not exposed to the Internet. Run the following query against the private IP of the single RPC node you deployed:
169+
170+
```bash
171+
INSTANCE_ID=$(cat single-node-deploy.json | jq -r '..|.node-instance-id? | select(. != null)')
172+
NODE_INTERNAL_IP=$(aws ec2 describe-instances --instance-ids $INSTANCE_ID --query 'Reservations[*].Instances[*].PrivateIpAddress' --output text)
173+
174+
# We query token balance of one of the system contracts: https://bscscan.com/address/0x0000000000000000000000000000000000001006
175+
curl http://$NODE_INTERNAL_IP:8545 -X POST -H "Content-Type: application/json" \
176+
--data '{"method":"eth_getBalance","params":["0x0000000000000000000000000000000000001006", "latest"],"id":1,"jsonrpc":"2.0"}'
177+
```
178+
You will get a response similar to this:
179+
180+
```json
181+
{"jsonrpc":"2.0","id":1,"result":"0x3635c9adc5dea00000"}
182+
```
183+
168184
### Option 2: Highly Available RPC Nodes
169185
170-
1. Deploy a BSC Fullnode node typically takes about 2-3 hours. The Fullnode uses snapshots data, and downloading and decompressing the data can take a considerable amount of time. You can grab a cup of coffee☕️ and patiently wait during this process. After deployment, you'll need to wait for the node to synchronize with the BSC Blockchain Network.
186+
1. The inital deployment a BSC full node and dwonloading its snapshot typically takes about 2-3 hours. The Full node uses snapshots data, and downloading and decompressing the data takes time. You can grab a cup of coffee☕️ and patiently wait during this process. After deployment, you'll need to wait for your another half a day to a day for your nodes to synchronize with the BSC Blockchain Network, depending on how fresh the snapshot was.
171187
172188
```bash
173-
pwd
174-
# Make sure you are in aws-blockchain-node-runners/lib/bsc
175-
npx cdk deploy bsc-ha-nodes --json --outputs-file ha-nodes-deploy.json
189+
pwd
190+
# Make sure you are in aws-blockchain-node-runners/lib/bsc
191+
npx cdk deploy bsc-ha-nodes --json --outputs-file ha-nodes-deploy.json
176192
```
177193
178194
2. Give the new RPC nodes about few hours to initialize and then run the following query against the load balancer behind the RPC node created
179195
180196
```bash
181-
export RPC_ALB_URL=$(cat ha-nodes-deploy.json | jq -r '.["bsc-ha-nodes-full"].alburl? | select(. != null)')
182-
echo $RPC_ALB_URL
197+
export RPC_ALB_URL=$(cat ha-nodes-deploy.json | jq -r '.["bsc-ha-nodes-full"].alburl? | select(. != null)')
198+
echo $RPC_ALB_URL
183199
```
184200
185-
Check [Geth Syncing](https://geth.ethereum.org/docs/fundamentals/logs#syncing) Status:
201+
Periodically check [Geth Syncing Status](https://geth.ethereum.org/docs/fundamentals/logs#syncing). Run the following query from within the same VPC and against the private IP of the load balancer fronting your nodes:
186202
187203
```bash
188204
curl http://$RPC_ALB_URL:8545 -X POST -H "Content-Type: application/json" \
@@ -217,6 +233,22 @@ npm install
217233
218234
**NOTE:** By default and for security reasons the load balancer is available only from within the default VPC in the region where it is deployed. It is not available from the Internet and is not open for external connections. Before opening it up please make sure you protect your RPC APIs.
219235
236+
3. Once the initial synchronization is done, you should be able to access the RPC API of that node from within the same VPC. The RPC port is not exposed to the Internet. Run the following query against the private IP of the single RPC node you deployed:
237+
238+
```bash
239+
export RPC_ALB_URL=$(cat ha-nodes-deploy.json | jq -r '.["bsc-ha-nodes-full"].alburl? | select(. != null)')
240+
echo $RPC_ALB_URL
241+
242+
# We query token balance of one of the system contracts: https://bscscan.com/address/0x0000000000000000000000000000000000001006
243+
curl http://$RPC_ALB_URL:8545 -X POST -H "Content-Type: application/json" \
244+
--data '{"method":"eth_getBalance","params":["0x0000000000000000000000000000000000001006", "latest"],"id":1,"jsonrpc":"2.0"}'
245+
```
246+
You will get a response similar to this:
247+
248+
```json
249+
{"jsonrpc":"2.0","id":1,"result":"0x3635c9adc5dea00000"}
250+
```
251+
220252
### Clearing up and undeploy everything
221253
222254
1. Undeploy HA Nodes, Single Nodes and Common stacks

lib/bsc/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ new BscCommonStack(app, "bsc-common", {
1717
});
1818

1919
new BscSingleNodeStack(app, "bsc-single-node", {
20-
stackName: `bsc-single-node`,
20+
stackName: `bsc-single-node-${config.baseNodeConfig.nodeConfiguration}-${config.baseNodeConfig.bscNetwork}`,
2121

2222
env: { account: config.baseConfig.accountId, region: config.baseConfig.region },
2323
nodeRole: <configTypes.BscNodeRole> "single-node",
@@ -29,7 +29,7 @@ new BscSingleNodeStack(app, "bsc-single-node", {
2929
});
3030

3131
new BscHANodesStack(app, "bsc-ha-nodes", {
32-
stackName: `bsc-ha-nodes-${config.baseNodeConfig.nodeConfiguration}`,
32+
stackName: `bsc-ha-nodes-${config.baseNodeConfig.nodeConfiguration}-${config.baseNodeConfig.bscNetwork}`,
3333
env: { account: config.baseConfig.accountId, region: config.baseConfig.region },
3434
nodeRole: <configTypes.BscNodeRole> "rpc-node",
3535
instanceType: config.baseNodeConfig.instanceType,

lib/bsc/lib/assets/user-data/node.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ else
9090
chmod -v u+x geth
9191
fi
9292

93-
# download mainnet configuration
94-
wget $(curl -s https://api.github.com/repos/bnb-chain/bsc/releases/latest |grep browser_ |grep mainnet |cut -d\" -f4)
95-
unzip mainnet.zip
93+
# download network configuration
94+
wget $(curl -s https://api.github.com/repos/bnb-chain/bsc/releases/latest |grep browser_ |grep $BSC_NETWORK |cut -d\" -f4)
95+
unzip $BSC_NETWORK.zip
9696

9797
# install aria2 a p2p downloader
9898

@@ -181,7 +181,7 @@ fi
181181

182182
lsblk -d
183183

184-
echo "Downloading BSC snapshots from 46Club."
184+
echo "Downloading BSC snapshot from 46Club."
185185

186186
cd /data
187187

@@ -214,20 +214,20 @@ do
214214
;;
215215
esac
216216
done
217-
echo "Downloading BSC snapshots from 46Club succeed"
217+
echo "Downloading BSC snapshot from 46Club succeed"
218218

219219
sleep 60
220220
# take about 2 hours to decompression the bsc snapshot
221-
echo "Decompression BSC snapshots start ..."
221+
echo "Decompression BSC snapshot start ..."
222222

223223
zstd -cd geth.tar.zst | pv | tar xvf - 2>&1 | tee unzip.log && echo "decompression success..." || echo "decompression failed..." >> bsc-snapshots-decompression.log
224-
echo "Decompression BSC snapshots success ..."
224+
echo "Decompression BSC snapshot success ..."
225225

226226
mv /data/geth.full/geth /data/
227227
sudo rm -rf /data/geth.full
228228
sudo rm -rf /data/geth.tar.zst
229229

230-
echo "BSC snapshots is ready !!!"
230+
echo "BSC snapshot is ready !!!"
231231

232232
chown bcuser:bcuser -R /data
233233

lib/bsc/lib/single-node-stack.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export class BscSingleNodeStack extends cdk.Stack {
115115
})
116116

117117
new cw.CfnDashboard(this, 'single-cw-dashboard', {
118-
dashboardName: STACK_NAME,
118+
dashboardName: `${STACK_NAME}-${node.instanceId}`,
119119
dashboardBody: dashboardString,
120120
});
121121

lib/bsc/test/ha-nodes-stack.test.ts

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as cdk from "aws-cdk-lib";
33
import * as dotenv from 'dotenv';
44
dotenv.config({ path: './test/.env-test' });
55
import * as config from "../lib/config/bscConfig";
6+
import * as configTypes from "../lib/config/bscConfig.interface";
67
import { BscHANodesStack } from "../lib/ha-nodes-stack";
78

89
describe("BscHANodesStack", () => {
@@ -13,6 +14,7 @@ describe("BscHANodesStack", () => {
1314
const bscHANodesStack = new BscHANodesStack(app, "bsc-sync-node", {
1415
stackName: `bsc-ha-nodes-${config.baseNodeConfig.nodeConfiguration}`,
1516
env: { account: config.baseConfig.accountId, region: config.baseConfig.region },
17+
nodeRole: <configTypes.BscNodeRole> "rpc-node",
1618

1719
instanceType: config.baseNodeConfig.instanceType,
1820
instanceCpuType: config.baseNodeConfig.instanceCpuType,
@@ -40,30 +42,30 @@ describe("BscHANodesStack", () => {
4042
}
4143
],
4244
SecurityGroupIngress: [
43-
{
45+
{
4446
"CidrIp": "0.0.0.0/0",
45-
"Description": "P2P protocols (gossip, turbine, repair, etc)",
47+
"Description": "P2P",
4648
"FromPort": 30303,
4749
"IpProtocol": "tcp",
4850
"ToPort": 30303
4951
},
5052
{
5153
"CidrIp": "0.0.0.0/0",
52-
"Description": "P2P protocols (gossip, turbine, repair, etc)",
54+
"Description": "P2P",
5355
"FromPort": 30303,
5456
"IpProtocol": "udp",
5557
"ToPort": 30303
5658
},
5759
{
5860
"CidrIp": "1.2.3.4/5",
59-
"Description": "RPC port HTTP (user access needs to be restricted. Allowed access only from internal IPs)",
61+
"Description": "BSC RPC Port",
6062
"FromPort": 8545,
6163
"IpProtocol": "tcp",
6264
"ToPort": 8545
6365
},
6466
{
6567
"CidrIp": "1.2.3.4/5",
66-
"Description": "RPC port WebSocket (user access needs to be restricted. Allowed access only from internal IPs)",
68+
"Description": "BSC WebSocket Port",
6769
"FromPort": 8546,
6870
"IpProtocol": "tcp",
6971
"ToPort": 8546
@@ -72,20 +74,25 @@ describe("BscHANodesStack", () => {
7274
"Description": "Allow access from ALB to Blockchain Node",
7375
"FromPort": 0,
7476
"IpProtocol": "tcp",
75-
"SourceSecurityGroupId": Match.anyValue(),
77+
"SourceSecurityGroupId": {
78+
"Fn::GetAtt": [
79+
Match.anyValue(),
80+
"GroupId"
81+
]
82+
},
7683
"ToPort": 65535
7784
},
7885
]
7986
})
8087

8188
// Has security group from ALB to EC2.
8289
template.hasResourceProperties("AWS::EC2::SecurityGroupIngress", {
83-
Description: Match.anyValue(),
84-
FromPort: 8845,
90+
Description: "Load balancer to target",
91+
FromPort: 8545,
8592
GroupId: Match.anyValue(),
8693
IpProtocol: "tcp",
8794
SourceSecurityGroupId: Match.anyValue(),
88-
ToPort: 8845,
95+
ToPort: 8545,
8996
})
9097

9198
// Has launch template profile for EC2 instances.
@@ -123,7 +130,7 @@ describe("BscHANodesStack", () => {
123130
EbsOptimized: true,
124131
IamInstanceProfile: Match.anyValue(),
125132
ImageId: Match.anyValue(),
126-
InstanceType:"m7i.4xlarge",
133+
InstanceType:"m7g.4xlarge",
127134
SecurityGroupIds: [Match.anyValue()],
128135
UserData: Match.anyValue(),
129136
TagSpecifications: Match.anyValue(),
@@ -165,9 +172,9 @@ describe("BscHANodesStack", () => {
165172
{
166173
"CidrIp": "1.2.3.4/5",
167174
"Description": "Blockchain Node RPC",
168-
"FromPort": 8845,
175+
"FromPort": 8545,
169176
"IpProtocol": "tcp",
170-
"ToPort": 8845
177+
"ToPort": 8545
171178
}
172179
],
173180
VpcId: Match.anyValue(),
@@ -213,21 +220,21 @@ describe("BscHANodesStack", () => {
213220
}
214221
],
215222
LoadBalancerArn: Match.anyValue(),
216-
Port: 8899,
223+
Port: 8545,
217224
Protocol: "HTTP"
218225
})
219226

220227
// Has ALB target group.
221228
template.hasResourceProperties("AWS::ElasticLoadBalancingV2::TargetGroup", {
222229
HealthCheckEnabled: true,
223230
HealthCheckIntervalSeconds: 30,
224-
HealthCheckPath: "/health",
225-
HealthCheckPort: "8845",
231+
HealthCheckPath: "/",
232+
HealthCheckPort: "8545",
226233
HealthyThresholdCount: 3,
227234
Matcher: {
228235
HttpCode: "200-299"
229236
},
230-
Port: 8845,
237+
Port: 8545,
231238
Protocol: "HTTP",
232239
TargetGroupAttributes: [
233240
{

0 commit comments

Comments
 (0)