Skip to content

Commit

Permalink
bash scripts to build landslideVM and install avalanchego (#10)
Browse files Browse the repository at this point in the history
* bash scripts to build landslideVM and install avalanchego from github release

* * use avalanchego v1.11.2
* refactor VM Shutdown method to prevent nil pointer exception
* fix grpc connection (allow insecure connection)

* database: don't treat no data as an error "not found" for rpc database (same behaviour as cometbft database)

* fix vm error during initialization using landslide-runner

* define GOPATH for build script
  • Loading branch information
ramilexe authored Apr 16, 2024
1 parent d776f05 commit 174287f
Show file tree
Hide file tree
Showing 9 changed files with 274 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: go mod verify

- name: Build
run: go build -v ./...
run: ./scripts/build.sh

- name: Run go vet
run: go vet ./...
Expand Down
2 changes: 1 addition & 1 deletion database/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ var (
ErrNotFound = errors.New("not found")
ErrEnumToError = map[rpcdb.Error]error{
rpcdb.Error_ERROR_CLOSED: ErrClosed,
rpcdb.Error_ERROR_NOT_FOUND: ErrNotFound,
rpcdb.Error_ERROR_NOT_FOUND: nil,
}
)
12 changes: 7 additions & 5 deletions landslidevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import (
"syscall"
"time"

vmpb "github.com/consideritdone/landslidevm/proto/vm"
runtimepb "github.com/consideritdone/landslidevm/proto/vm/runtime"
"github.com/consideritdone/landslidevm/vm"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/keepalive"

vmpb "github.com/consideritdone/landslidevm/proto/vm"
runtimepb "github.com/consideritdone/landslidevm/proto/vm/runtime"
"github.com/consideritdone/landslidevm/vm"
)

const (
Expand Down Expand Up @@ -83,7 +85,7 @@ func Serve[T interface {
lvm = v
}

if len(options) > 0 {
if len(options) == 0 {
options = DefaultServerOptions
}
server := grpc.NewServer(options...)
Expand Down Expand Up @@ -129,7 +131,7 @@ func Serve[T interface {
return fmt.Errorf("required env var missing: %q", EngineAddressKey)
}

clientConn, err := grpc.Dial("passthrough:///" + runtimeAddr)
clientConn, err := grpc.Dial("passthrough:///"+runtimeAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return fmt.Errorf("failed to create client conn: %w", err)
}
Expand Down
53 changes: 53 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

go_version_minimum="1.20.12"

go_version() {
go version | sed -nE -e 's/[^0-9.]+([0-9.]+).+/\1/p'
}

version_lt() {
# Return true if $1 is a lower version than than $2,
local ver1=$1
local ver2=$2
# Reverse sort the versions, if the 1st item != ver1 then ver1 < ver2
if [[ $(echo -e -n "$ver1\n$ver2\n" | sort -rV | head -n1) != "$ver1" ]]; then
return 0
else
return 1
fi
}

if version_lt "$(go_version)" "$go_version_minimum"; then
echo "LandslideVM requires Go >= $go_version_minimum, Go $(go_version) found." >&2
exit 1
fi

# Root directory
SUBNET_EVM_PATH=$(
cd "$(dirname "${BASH_SOURCE[0]}")"
cd .. && pwd
)

# Load the versions
source "$SUBNET_EVM_PATH"/scripts/versions.sh

# Load the constants
source "$SUBNET_EVM_PATH"/scripts/constants.sh

if [[ $# -eq 1 ]]; then
BINARY_PATH=$1
elif [[ $# -eq 0 ]]; then
BINARY_PATH="$GOPATH/src/github.com/ava-labs/avalanchego/build/plugins/pjSL9ksard4YE96omaiTkGL5H6XX2W5VEo3ZgWC9S2P6gzs9A"
else
echo "Invalid arguments to build subnet-evm. Requires zero (default location) or one argument to specify binary location."
exit 1
fi

# Build Subnet EVM, which is run as a subprocess
echo "Building Landslide VM at $BINARY_PATH"
go build -o "$BINARY_PATH" "example/countervm/"*.go
4 changes: 4 additions & 0 deletions scripts/constants.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

# Set the PATHS
GOPATH="$(go env GOPATH)"
128 changes: 128 additions & 0 deletions scripts/install_avalanchego_release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/usr/bin/env bash
set -e

# Load the versions
SUBNET_EVM_PATH=$(
cd "$(dirname "${BASH_SOURCE[0]}")"
cd .. && pwd
)
source "$SUBNET_EVM_PATH"/scripts/versions.sh

## Load the constants
#source "$SUBNET_EVM_PATH"/scripts/constants.sh

############################
# download avalanchego
# https://github.com/ava-labs/avalanchego/releases
GOARCH=$(go env GOARCH)
GOOS=$(go env GOOS)
BASEDIR=${BASEDIR:-"/tmp/avalanchego-release"}
AVALANCHEGO_BUILD_PATH=${AVALANCHEGO_BUILD_PATH:-${BASEDIR}/avalanchego}

mkdir -p ${BASEDIR}

AVAGO_DOWNLOAD_URL=https://github.com/ava-labs/avalanchego/releases/download/${AVALANCHE_VERSION}/avalanchego-linux-${GOARCH}-${AVALANCHE_VERSION}.tar.gz
AVAGO_DOWNLOAD_PATH=${BASEDIR}/avalanchego-linux-${GOARCH}-${AVALANCHE_VERSION}.tar.gz

if [[ ${GOOS} == "darwin" ]]; then
AVAGO_DOWNLOAD_URL=https://github.com/ava-labs/avalanchego/releases/download/${AVALANCHE_VERSION}/avalanchego-macos-${AVALANCHE_VERSION}.zip
AVAGO_DOWNLOAD_PATH=${BASEDIR}/avalanchego-macos-${AVALANCHE_VERSION}.zip
fi

BUILD_DIR=${AVALANCHEGO_BUILD_PATH}-${AVALANCHE_VERSION}

extract_archive() {
mkdir -p ${BUILD_DIR}

if [[ ${AVAGO_DOWNLOAD_PATH} == *.tar.gz ]]; then
tar xzvf ${AVAGO_DOWNLOAD_PATH} --directory ${BUILD_DIR} --strip-components 1
elif [[ ${AVAGO_DOWNLOAD_PATH} == *.zip ]]; then
unzip ${AVAGO_DOWNLOAD_PATH} -d ${BUILD_DIR}
mv ${BUILD_DIR}/build/* ${BUILD_DIR}
rm -rf ${BUILD_DIR}/build/
fi
}

# first check if we already have the archive
if [[ -f ${AVAGO_DOWNLOAD_PATH} ]]; then
# if the download path already exists, extract and exit
echo "found avalanchego ${AVALANCHE_VERSION} at ${AVAGO_DOWNLOAD_PATH}"

extract_archive
else
# try to download the archive if it exists
if curl -s --head --request GET ${AVAGO_DOWNLOAD_URL} | grep "302" > /dev/null; then
echo "${AVAGO_DOWNLOAD_URL} found"
echo "downloading to ${AVAGO_DOWNLOAD_PATH}"
curl -L ${AVAGO_DOWNLOAD_URL} -o ${AVAGO_DOWNLOAD_PATH}

extract_archive
else
# else the version is a git commitish (or it's invalid)
GIT_CLONE_URL=https://github.com/ava-labs/avalanchego.git
GIT_CLONE_PATH=${BASEDIR}/avalanchego-repo/

# check to see if the repo already exists, if not clone it
if [[ ! -d ${GIT_CLONE_PATH} ]]; then
echo "cloning ${GIT_CLONE_URL} to ${GIT_CLONE_PATH}"
git clone --no-checkout ${GIT_CLONE_URL} ${GIT_CLONE_PATH}
fi

# check to see if the commitish exists in the repo
WORKDIR=$(pwd)

cd ${GIT_CLONE_PATH}

git fetch

echo "checking out ${AVALANCHE_VERSION}"

set +e
# try to checkout the branch
git checkout origin/${AVALANCHE_VERSION} > /dev/null 2>&1
CHECKOUT_STATUS=$?
set -e

# if it's not a branch, try to checkout the commit
if [[ $CHECKOUT_STATUS -ne 0 ]]; then
set +e
git checkout ${AVALANCHE_VERSION} > /dev/null 2>&1
CHECKOUT_STATUS=$?
set -e

if [[ $CHECKOUT_STATUS -ne 0 ]]; then
echo
echo "'${VERSION}' is not a valid release tag, commit hash, or branch name"
exit 1
fi
fi

COMMIT=$(git rev-parse HEAD)

# use the commit hash instead of the branch name or tag
BUILD_DIR=${AVALANCHEGO_BUILD_PATH}-${COMMIT}

# if the build-directory doesn't exist, build avalanchego
if [[ ! -d ${BUILD_DIR} ]]; then
echo "building avalanchego ${COMMIT} to ${BUILD_DIR}"
./scripts/build.sh
mkdir -p ${BUILD_DIR}

mv ${GIT_CLONE_PATH}/build/* ${BUILD_DIR}/
fi

cd $WORKDIR
fi
fi

AVALANCHEGO_PATH=${AVALANCHEGO_BUILD_PATH}/avalanchego
AVALANCHEGO_PLUGIN_DIR=${AVALANCHEGO_BUILD_PATH}/plugins

mkdir -p ${AVALANCHEGO_BUILD_PATH}

cp ${BUILD_DIR}/avalanchego ${AVALANCHEGO_PATH}


echo "Installed AvalancheGo release ${AVALANCHE_VERSION}"
echo "AvalancheGo Path: ${AVALANCHEGO_PATH}"
echo "Plugin Dir: ${AVALANCHEGO_PLUGIN_DIR}"
4 changes: 4 additions & 0 deletions scripts/versions.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env bash

# Don't export them as they're used in the context of other calls
AVALANCHE_VERSION=${AVALANCHE_VERSION:-'v1.11.2'}
14 changes: 14 additions & 0 deletions vm/types/block/block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package block

import (
"github.com/cometbft/cometbft/types"
)

func BlockParentHash(block *types.Block) [32]byte {
var parentHash [32]byte
if block.LastBlockID.Hash != nil {
parentHash = [32]byte(block.LastBlockID.Hash)
}

return parentHash
}
Loading

0 comments on commit 174287f

Please sign in to comment.