Skip to content

Commit

Permalink
Merge pull request #116 from aws-samples/bsc
Browse files Browse the repository at this point in the history
Bsc
  • Loading branch information
frbrkoala authored Aug 22, 2024
2 parents f92ac68 + 92468ab commit f9bb515
Show file tree
Hide file tree
Showing 11 changed files with 4,315 additions and 93 deletions.
22 changes: 3 additions & 19 deletions lib/bsc/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'dotenv/config'
import "source-map-support/register";
import * as cdk from "aws-cdk-lib";
import * as config from "./lib/config/bscConfig";
import * as configTypes from "./lib/config/bscConfig.interface";
import { BscCommonStack } from "./lib/common-stack";
import { BscSingleNodeStack } from "./lib/single-node-stack";
import { BscHANodesStack } from "./lib/ha-nodes-stack";
Expand All @@ -20,29 +19,14 @@ new BscSingleNodeStack(app, "bsc-single-node", {
stackName: `bsc-single-node-${config.baseNodeConfig.nodeConfiguration}-${config.baseNodeConfig.bscNetwork}`,

env: { account: config.baseConfig.accountId, region: config.baseConfig.region },
nodeRole: <configTypes.BscNodeRole> "single-node",
instanceType: config.baseNodeConfig.instanceType,
instanceCpuType: config.baseNodeConfig.instanceCpuType,
bscNetwork: config.baseNodeConfig.bscNetwork,
nodeConfiguration: config.baseNodeConfig.nodeConfiguration,
snapshotsUrl:config.baseNodeConfig.snapshotsUrl,
dataVolume: config.baseNodeConfig.dataVolume,
...config.baseNodeConfig
});

new BscHANodesStack(app, "bsc-ha-nodes", {
stackName: `bsc-ha-nodes-${config.baseNodeConfig.nodeConfiguration}-${config.baseNodeConfig.bscNetwork}`,
env: { account: config.baseConfig.accountId, region: config.baseConfig.region },
nodeRole: <configTypes.BscNodeRole> "rpc-node",
instanceType: config.baseNodeConfig.instanceType,
instanceCpuType: config.baseNodeConfig.instanceCpuType,
bscNetwork: config.baseNodeConfig.bscNetwork,
nodeConfiguration: config.baseNodeConfig.nodeConfiguration,
snapshotsUrl:config.baseNodeConfig.snapshotsUrl,
dataVolume: config.baseNodeConfig.dataVolume,

albHealthCheckGracePeriodMin: config.haNodeConfig.albHealthCheckGracePeriodMin,
heartBeatDelayMin: config.haNodeConfig.heartBeatDelayMin,
numberOfNodes: config.haNodeConfig.numberOfNodes
...config.baseNodeConfig,
...config.haNodeConfig
});

// Security Check
Expand Down
57 changes: 29 additions & 28 deletions lib/bsc/lib/assets/user-data/node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ echo "DATA_VOLUME_TYPE=${_DATA_VOLUME_TYPE_}" >> /etc/environment
echo "DATA_VOLUME_SIZE=${_DATA_VOLUME_SIZE_}" >> /etc/environment
echo "BSC_NODE_TYPE=${_BSC_NODE_TYPE_}" >> /etc/environment
echo "BSC_NETWORK=${_BSC_NETWORK_}" >> /etc/environment
echo "BSC_DOWNLOAD_SNAPSHOT=${_BSC_DOWNLOAD_SNAPSHOT_}" >> /etc/environment
echo "LIFECYCLE_HOOK_NAME=${_LIFECYCLE_HOOK_NAME_}" >> /etc/environment
echo "AUTOSCALING_GROUP_NAME=${_AUTOSCALING_GROUP_NAME_}" >> /etc/environment
echo "NODE_ROLE=${_NODE_ROLE_}" >> /etc/environment
source /etc/environment

arch=$(uname -m)
Expand Down Expand Up @@ -94,26 +94,6 @@ fi
wget $(curl -s https://api.github.com/repos/bnb-chain/bsc/releases/latest |grep browser_ |grep $BSC_NETWORK |cut -d\" -f4)
unzip $BSC_NETWORK.zip

# install aria2 a p2p downloader

if [ "$arch" == "x86_64" ]; then
wget https://github.com/q3aql/aria2-static-builds/releases/download/v1.36.0/aria2-1.36.0-linux-gnu-64bit-build1.tar.bz2
tar jxvf aria2-1.36.0-linux-gnu-64bit-build1.tar.bz2
cd aria2-1.36.0-linux-gnu-64bit-build1/
make install
else
wget https://github.com/q3aql/aria2-static-builds/releases/download/v1.36.0/aria2-1.36.0-linux-gnu-arm-rbpi-build1.tar.bz2
tar jxvf aria2-1.36.0-linux-gnu-arm-rbpi-build1.tar.bz2
cd aria2-1.36.0-linux-gnu-arm-rbpi-build1/
make install
fi

# finish download and archive
sudo yum install zstd -y
sudo yum install pv -y
zstd --version
pv --version

echo 'Configuring BSC Node service as a system service'
# Copy startup script to correct location
if [[ "$BSC_NODE_TYPE" == "full" ]]; then
Expand Down Expand Up @@ -149,12 +129,12 @@ chmod +x /opt/syncchecker.sh
(crontab -l; echo "*/1 * * * * /opt/syncchecker.sh >/tmp/syncchecker.log 2>&1") | crontab -
crontab -l

if [ "$NODE_ROLE" == "single-node" ]; then
if [[ "$LIFECYCLE_HOOK_NAME" == "none" ]]; then
echo "Single node. Signaling completion to CloudFormation"
/opt/aws/bin/cfn-signal --stack $STACK_NAME --resource $RESOURCE_ID --region $AWS_REGION
fi

if [ "$NODE_ROLE" == "single-node" ]; then
if [[ "$LIFECYCLE_HOOK_NAME" == "none" ]]; then
echo "Single node. Wait for one minute for the volume to be available"
sleep 60
fi
Expand Down Expand Up @@ -189,11 +169,32 @@ fi

lsblk -d

# download snapshot if network is mainnet
if [ "$BSC_NETWORK" == "mainnet" ]; then
echo "Downloading BSC snapshot"
chmod +x /opt/download-snapshot.sh
/opt/download-snapshot.sh
if [[ "$BSC_DOWNLOAD_SNAPSHOT" == "true" ]]; then
# install aria2 a p2p downloader

if [ "$arch" == "x86_64" ]; then
wget https://github.com/q3aql/aria2-static-builds/releases/download/v1.36.0/aria2-1.36.0-linux-gnu-64bit-build1.tar.bz2
tar jxvf aria2-1.36.0-linux-gnu-64bit-build1.tar.bz2
cd aria2-1.36.0-linux-gnu-64bit-build1/
make install
else
wget https://github.com/q3aql/aria2-static-builds/releases/download/v1.36.0/aria2-1.36.0-linux-gnu-arm-rbpi-build1.tar.bz2
tar jxvf aria2-1.36.0-linux-gnu-arm-rbpi-build1.tar.bz2
cd aria2-1.36.0-linux-gnu-arm-rbpi-build1/
make install
fi

# finish download and archive
sudo yum install zstd -y
sudo yum install pv -y
zstd --version
pv --version
# download snapshot if network is mainnet
if [[ "$BSC_NETWORK" == "mainnet" ]]; then
echo "Downloading BSC snapshot"
chmod +x /opt/download-snapshot.sh
/opt/download-snapshot.sh
fi
fi

chown bcuser:bcuser -R /data
Expand Down
1 change: 0 additions & 1 deletion lib/bsc/lib/common-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import * as iam from "aws-cdk-lib/aws-iam";
import * as nag from "cdk-nag";

export interface BscCommonStackProps extends cdk.StackProps {

}

export class BscCommonStack extends cdk.Stack {
Expand Down
1 change: 1 addition & 0 deletions lib/bsc/lib/config/bscConfig.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface BscBaseNodeConfig extends configTypes.BaseNodeConfig {
nodeConfiguration: BscNodeConfiguration;
snapshotsUrl: string;
dataVolume: BscDataVolumeConfig;
downloadSnapshot: boolean;
}

export interface BscHAConfig {
Expand Down
3 changes: 2 additions & 1 deletion lib/bsc/lib/config/bscConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ export const baseConfig: configTypes.BscBaseConfig = {
export const baseNodeConfig: configTypes.BscBaseNodeConfig = {
instanceType: new ec2.InstanceType(process.env.BSC_INSTANCE_TYPE ? process.env.BSC_INSTANCE_TYPE : "m7g.4xlarge"),
instanceCpuType: process.env.BSC_CPU_TYPE?.toLowerCase() == "x86_64" ? ec2.AmazonLinuxCpuType.X86_64 : ec2.AmazonLinuxCpuType.ARM_64 ,
bscNetwork: <configTypes.BscNetwork>process.env.BSC_CLUSTER || "mainnet",
bscNetwork: <configTypes.BscNetwork>process.env.BSC_NETWORK || "mainnet",
nodeConfiguration: <configTypes.BscNodeConfiguration>process.env.BSC_NODE_CONFIGURATION || "full",
snapshotsUrl: process.env.BSC_SNAPSHOTS_URL || constants.NoneValue,
downloadSnapshot: process.env.BSC_DOWNLOAD_SNAPSHOT?.toLowerCase() === "true" ? true : false,
dataVolume: {
sizeGiB: process.env.BSC_DATA_VOL_SIZE ? parseInt(process.env.BSC_DATA_VOL_SIZE) : 4000,
type: parseDataVolumeType(process.env.BSC_DATA_VOL_TYPE?.toLowerCase() ? process.env.BSC_DATA_VOL_TYPE?.toLowerCase() : "gp3"),
Expand Down
16 changes: 3 additions & 13 deletions lib/bsc/lib/ha-nodes-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,7 @@ import * as constants from "../../constructs/constants";
import { HANodesConstruct } from "../../constructs/ha-rpc-nodes-with-alb";
import * as nag from "cdk-nag";

export interface BscHANodesStackProps extends cdk.StackProps {
nodeRole: configTypes.BscNodeRole;
instanceType: ec2.InstanceType;
instanceCpuType: ec2.AmazonLinuxCpuType;
bscNetwork: configTypes.BscNetwork;
nodeConfiguration: configTypes.BscNodeConfiguration;
snapshotsUrl: string;
dataVolume: configTypes.BscDataVolumeConfig;
albHealthCheckGracePeriodMin: number;
heartBeatDelayMin: number;
numberOfNodes: number;
export interface BscHANodesStackProps extends cdk.StackProps, configTypes.BscBaseNodeConfig, configTypes.BscHAConfig {
}

export class BscHANodesStack extends cdk.Stack {
Expand All @@ -35,13 +25,13 @@ export class BscHANodesStack extends cdk.Stack {
const autoScalingGroupName = STACK_NAME;

const {
nodeRole,
instanceType,
instanceCpuType,
bscNetwork,
nodeConfiguration,
snapshotsUrl,
dataVolume,
downloadSnapshot,
albHealthCheckGracePeriodMin,
heartBeatDelayMin,
numberOfNodes
Expand Down Expand Up @@ -79,7 +69,7 @@ export class BscHANodesStack extends cdk.Stack {
_BSC_NODE_TYPE_: nodeConfiguration,
_DATA_VOLUME_TYPE_: dataVolume.type,
_DATA_VOLUME_SIZE_: dataVolumeSizeBytes.toString(),
_NODE_ROLE_: nodeRole,
_BSC_DOWNLOAD_SNAPSHOT_: downloadSnapshot.toString(),

_BSC_NETWORK_: bscNetwork,
_LIFECYCLE_HOOK_NAME_: lifecycleHookName,
Expand Down
13 changes: 3 additions & 10 deletions lib/bsc/lib/single-node-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ import * as configTypes from "./config/bscConfig.interface";
import { BscNodeSecurityGroupConstructs } from "./constructs/bsc-node-security-group"
import * as nag from "cdk-nag";

export interface BscSingleNodeStackProps extends cdk.StackProps {
nodeRole: configTypes.BscNodeRole;
instanceType: ec2.InstanceType;
instanceCpuType: ec2.AmazonLinuxCpuType;
bscNetwork: configTypes.BscNetwork;
nodeConfiguration: configTypes.BscNodeConfiguration;
snapshotsUrl: string;
dataVolume: configTypes.BscDataVolumeConfig;
export interface BscSingleNodeStackProps extends cdk.StackProps, configTypes.BscBaseNodeConfig {
}

export class BscSingleNodeStack extends cdk.Stack {
Expand All @@ -37,12 +30,12 @@ export class BscSingleNodeStack extends cdk.Stack {
// Getting our config from initialization properties
const {
instanceType,
nodeRole,
instanceCpuType,
bscNetwork,
nodeConfiguration,
snapshotsUrl,
dataVolume,
downloadSnapshot,
} = props;

// Using default VPC
Expand Down Expand Up @@ -100,7 +93,7 @@ export class BscSingleNodeStack extends cdk.Stack {
_BSC_NODE_TYPE_: nodeConfiguration,
_DATA_VOLUME_TYPE_: dataVolume.type,
_DATA_VOLUME_SIZE_: dataVolumeSizeBytes.toString(),
_NODE_ROLE_: nodeRole,
_BSC_DOWNLOAD_SNAPSHOT_: downloadSnapshot.toString(),

_BSC_NETWORK_: bscNetwork,
_LIFECYCLE_HOOK_NAME_: constants.NoneValue,
Expand Down
1 change: 1 addition & 0 deletions lib/bsc/sample-configs/.env-sample-full
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ AWS_REGION="us-east-2"

## Common configuration parameters ##
BSC_NETWORK="mainnet" # All options: "mainnet", "testnet"
BSC_DOWNLOAD_SNAPSHOT="true" # All options: "true" | "false"
BSC_NODE_CONFIGURATION="full" # All options: "full"

## Instance Nodes
Expand Down
15 changes: 2 additions & 13 deletions lib/bsc/test/ha-nodes-stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as cdk from "aws-cdk-lib";
import * as dotenv from 'dotenv';
dotenv.config({ path: './test/.env-test' });
import * as config from "../lib/config/bscConfig";
import * as configTypes from "../lib/config/bscConfig.interface";
import { BscHANodesStack } from "../lib/ha-nodes-stack";

describe("BscHANodesStack", () => {
Expand All @@ -14,18 +13,8 @@ describe("BscHANodesStack", () => {
const bscHANodesStack = new BscHANodesStack(app, "bsc-sync-node", {
stackName: `bsc-ha-nodes-${config.baseNodeConfig.nodeConfiguration}`,
env: { account: config.baseConfig.accountId, region: config.baseConfig.region },
nodeRole: <configTypes.BscNodeRole> "rpc-node",

instanceType: config.baseNodeConfig.instanceType,
instanceCpuType: config.baseNodeConfig.instanceCpuType,
bscNetwork: config.baseNodeConfig.bscNetwork,
nodeConfiguration: config.baseNodeConfig.nodeConfiguration,
snapshotsUrl:config.baseNodeConfig.snapshotsUrl,
dataVolume: config.baseNodeConfig.dataVolume,

albHealthCheckGracePeriodMin: config.haNodeConfig.albHealthCheckGracePeriodMin,
heartBeatDelayMin: config.haNodeConfig.heartBeatDelayMin,
numberOfNodes: config.haNodeConfig.numberOfNodes,
...config.baseNodeConfig,
...config.haNodeConfig
});

// Prepare the stack for assertions.
Expand Down
9 changes: 1 addition & 8 deletions lib/bsc/test/single-node-stack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import * as cdk from "aws-cdk-lib";
import * as dotenv from 'dotenv';
dotenv.config({ path: './test/.env-test' });
import * as config from "../lib/config/bscConfig";
import * as configTypes from "../lib/config/bscConfig.interface";
import { BscSingleNodeStack } from "../lib/single-node-stack";

describe("BSCSingleNodeStack", () => {
Expand All @@ -15,13 +14,7 @@ describe("BSCSingleNodeStack", () => {
stackName: `bsc-single-node`,

env: { account: config.baseConfig.accountId, region: config.baseConfig.region },
nodeRole: <configTypes.BscNodeRole> "single-node",
instanceType: config.baseNodeConfig.instanceType,
instanceCpuType: config.baseNodeConfig.instanceCpuType,
bscNetwork: config.baseNodeConfig.bscNetwork,
nodeConfiguration: config.baseNodeConfig.nodeConfiguration,
snapshotsUrl:config.baseNodeConfig.snapshotsUrl,
dataVolume: config.baseNodeConfig.dataVolume,
...config.baseNodeConfig
});

// Prepare the stack for assertions.
Expand Down
Loading

0 comments on commit f9bb515

Please sign in to comment.