Skip to content
This repository was archived by the owner on May 19, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.swp
build/
.DS_Store
5 changes: 1 addition & 4 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

const (
// DO NOT CHANGE VALUES IN THIS FILE
VMID = "tGas3T58KzdjLHhBDMnH2TvrddhqTji5iZAMZ3RXs2NLpSnhH"
WhitelistedSubnets = "29uVeLPJB1eQJkzRemU8g8wZDw5uJRqpab5U2mX9euieVwiEbL"

VMName = "kewl vm"
Expand All @@ -18,6 +17,4 @@ const (
FilePerms = 0777
)

var (
Chains = []string{"P", "C", "X"}
)
var Chains = []string{"P", "C", "X"}
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ module github.com/ava-labs/ava-sim
go 1.16

require (
github.com/ava-labs/avalanchego v1.7.1
github.com/ava-labs/avalanchego v1.7.5
github.com/fatih/color v1.9.0
github.com/hashicorp/go-plugin v1.4.3 // indirect
github.com/spf13/viper v1.9.0 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
)
243 changes: 232 additions & 11 deletions go.sum

Large diffs are not rendered by default.

16 changes: 13 additions & 3 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,36 @@ import (

"github.com/ava-labs/ava-sim/manager"
"github.com/ava-labs/ava-sim/runner"
"github.com/ava-labs/avalanchego/ids"
"github.com/fatih/color"
"golang.org/x/sync/errgroup"
)

func main() {
var vm, vmGenesis string
var vmID ids.ID
switch len(os.Args) {
case 1: // normal network
case 3:
case 4:
vm = path.Clean(os.Args[1])
if _, err := os.Stat(vm); os.IsNotExist(err) {
panic(fmt.Sprintf("%s does not exist", vm))
}
color.Yellow("vm set to: %s", vm)

vmGenesis = path.Clean(os.Args[2])
vmIDArg := os.Args[3]
var err error
if _, err := os.Stat(vmGenesis); os.IsNotExist(err) {
panic(fmt.Sprintf("%s does not exist", vmGenesis))
}
vmID, err = ids.FromString(vmIDArg)
if err != nil {
panic(err)
}
color.Yellow("vm-genesis set to: %s", vmGenesis)
color.Yellow("VM ID set to: %s", vmID)

default:
panic("invalid arguments (expecting no arguments or [vm] [vm-genesis])")
}
Expand Down Expand Up @@ -60,7 +70,7 @@ func main() {
})

g.Go(func() error {
return manager.StartNetwork(gctx, vm, bootstrapped)
return manager.StartNetwork(gctx, vm, vmID, bootstrapped)
})

// Only setup network if a custom VM is provided and the network has finished
Expand All @@ -69,7 +79,7 @@ func main() {
case <-bootstrapped:
if len(vm) > 0 && gctx.Err() == nil {
g.Go(func() error {
return runner.SetupSubnet(gctx, vmGenesis)
return runner.SetupSubnet(gctx, vmID, vmGenesis)
})
}
case <-gctx.Done():
Expand Down
13 changes: 7 additions & 6 deletions manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/ava-labs/avalanchego/api/info"
"github.com/ava-labs/avalanchego/app/process"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/node"
"github.com/fatih/color"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -76,7 +77,7 @@ func NodeURLs() []string {
return urls
}

func StartNetwork(ctx context.Context, vmPath string, bootstrapped chan struct{}) error {
func StartNetwork(ctx context.Context, vmPath string, vmID ids.ID, bootstrapped chan struct{}) error {
dir, err := ioutil.TempDir("", "ava-sim")
if err != nil {
panic(err)
Expand All @@ -86,7 +87,7 @@ func StartNetwork(ctx context.Context, vmPath string, bootstrapped chan struct{}
color.Cyan("tmp dir located at: %s", dir)
}()

// Copy files into custom plugins
// // Copy files into custom plugins
pluginsDir := fmt.Sprintf("%s/plugins", dir)
if err := os.MkdirAll(pluginsDir, os.FileMode(constants.FilePerms)); err != nil {
panic(err)
Expand All @@ -95,7 +96,7 @@ func StartNetwork(ctx context.Context, vmPath string, bootstrapped chan struct{}
panic(err)
}
if len(vmPath) > 0 {
if err := utils.CopyFile(vmPath, fmt.Sprintf("%s/%s", pluginsDir, constants.VMID)); err != nil {
if err := utils.CopyFile(vmPath, fmt.Sprintf("%s/%s", pluginsDir, vmID.String())); err != nil {
panic(err)
}
}
Expand Down Expand Up @@ -168,15 +169,15 @@ func checkBootstrapped(ctx context.Context, bootstrapped chan struct{}) error {
)

for i, url := range nodeURLs {
client := info.NewClient(url, constants.HTTPTimeout)
client := info.NewClient(url)
for {
if ctx.Err() != nil {
color.Red("stopping bootstrapped check: %v", ctx.Err())
return ctx.Err()
}
bootstrapped := true
for _, chain := range constants.Chains {
chainBootstrapped, _ := client.IsBootstrapped(chain)
chainBootstrapped, _ := client.IsBootstrapped(ctx, chain)
if !chainBootstrapped {
color.Yellow("waiting for %s to bootstrap %s-chain", nodeIDs[i], chain)
bootstrapped = false
Expand All @@ -187,7 +188,7 @@ func checkBootstrapped(ctx context.Context, bootstrapped chan struct{}) error {
time.Sleep(waitDiff)
continue
}
if peers, _ := client.Peers(); len(peers) < constants.NumNodes-1 {
if peers, _ := client.Peers(ctx); len(peers) < constants.NumNodes-1 {
color.Yellow("waiting for %s to connect to all peers (%d/%d)", nodeIDs[i], len(peers), constants.NumNodes-1)
time.Sleep(waitDiff)
continue
Expand Down
48 changes: 25 additions & 23 deletions runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ava-labs/avalanchego/api/keystore"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/platformvm"
"github.com/ava-labs/avalanchego/vms/platformvm/status"
"github.com/fatih/color"
)

Expand All @@ -28,7 +29,7 @@ const (
validatorEndDiff = 30 * 24 * time.Hour // 30 days
)

func SetupSubnet(ctx context.Context, vmGenesis string) error {
func SetupSubnet(ctx context.Context, vmID ids.ID, vmGenesis string) error {
color.Cyan("creating subnet")
var (
nodeURLs = manager.NodeURLs()
Expand All @@ -41,28 +42,28 @@ func SetupSubnet(ctx context.Context, vmGenesis string) error {
)

// Create user
kclient := keystore.NewClient(nodeURLs[0], constants.HTTPTimeout)
ok, err := kclient.CreateUser(userPass)
kclient := keystore.NewClient(nodeURLs[0])
ok, err := kclient.CreateUser(ctx, userPass)
if !ok || err != nil {
return fmt.Errorf("could not create user: %w", err)
}

// Connect to local network
client := platformvm.NewClient(nodeURLs[0], constants.HTTPTimeout)
client := platformvm.NewClient(nodeURLs[0])

// Import genesis key
fundedAddress, err := client.ImportKey(userPass, genesisKey)
fundedAddress, err := client.ImportKey(ctx, userPass, genesisKey)
if err != nil {
return fmt.Errorf("unable to import genesis key: %w", err)
}
balance, err := client.GetBalance(fundedAddress)
balance, err := client.GetBalance(ctx, []string{fundedAddress})
if err != nil {
return fmt.Errorf("unable to get genesis key balance: %w", err)
}
color.Cyan("found %d on address %s", balance, fundedAddress)

// Create a subnet
subnetIDTx, err := client.CreateSubnet(
subnetIDTx, err := client.CreateSubnet(ctx,
userPass,
[]string{fundedAddress},
fundedAddress,
Expand All @@ -77,8 +78,8 @@ func SetupSubnet(ctx context.Context, vmGenesis string) error {
if ctx.Err() != nil {
return ctx.Err()
}
status, _ := client.GetTxStatus(subnetIDTx, true)
if status.Status == platformvm.Committed {
txStatus, _ := client.GetTxStatus(ctx, subnetIDTx, true)
if txStatus.Status == status.Committed {
break
}
color.Yellow("waiting for subnet creation tx (%s) to be accepted", subnetIDTx)
Expand All @@ -87,7 +88,7 @@ func SetupSubnet(ctx context.Context, vmGenesis string) error {
color.Cyan("subnet creation tx (%s) accepted", subnetIDTx)

// Confirm created subnet appears in subnet list
subnets, err := client.GetSubnets([]ids.ID{})
subnets, err := client.GetSubnets(ctx, []ids.ID{})
if err != nil {
return fmt.Errorf("cannot query subnets: %w", err)
}
Expand All @@ -99,7 +100,7 @@ func SetupSubnet(ctx context.Context, vmGenesis string) error {

// Add all validators to subnet with equal weight
for _, nodeID := range manager.NodeIDs() {
txID, err := client.AddSubnetValidator(
txID, err := client.AddSubnetValidator(ctx,
userPass, []string{fundedAddress}, fundedAddress,
subnetID, nodeID, validatorWeight,
uint64(time.Now().Add(validatorStartDiff).Unix()),
Expand All @@ -113,8 +114,8 @@ func SetupSubnet(ctx context.Context, vmGenesis string) error {
if ctx.Err() != nil {
return ctx.Err()
}
status, _ := client.GetTxStatus(txID, true)
if status.Status == platformvm.Committed {
txStatus, _ := client.GetTxStatus(ctx, txID, true)
if txStatus.Status == status.Committed {
break
}
color.Yellow("waiting for add subnet validator (%s) tx (%s) to be accepted", nodeID, txID)
Expand All @@ -128,9 +129,9 @@ func SetupSubnet(ctx context.Context, vmGenesis string) error {
if err != nil {
return fmt.Errorf("could not read genesis file (%s): %w", vmGenesis, err)
}
txID, err := client.CreateBlockchain(
txID, err := client.CreateBlockchain(ctx,
userPass, []string{fundedAddress}, fundedAddress, rSubnetID,
constants.VMID, []string{}, constants.VMName, genesis,
vmID.String(), []string{}, constants.VMName, genesis,
)
if err != nil {
return fmt.Errorf("could not create blockchain: %w", err)
Expand All @@ -139,8 +140,8 @@ func SetupSubnet(ctx context.Context, vmGenesis string) error {
if ctx.Err() != nil {
return ctx.Err()
}
status, _ := client.GetTxStatus(txID, true)
if status.Status == platformvm.Committed {
txStatus, _ := client.GetTxStatus(ctx, txID, true)
if txStatus.Status == status.Committed {
break
}
color.Yellow("waiting for create blockchain tx (%s) to be accepted", txID)
Expand All @@ -149,7 +150,7 @@ func SetupSubnet(ctx context.Context, vmGenesis string) error {
color.Cyan("create blockchain tx (%s) accepted", txID)

// Validate blockchain exists
blockchains, err := client.GetBlockchains()
blockchains, err := client.GetBlockchains(ctx)
if err != nil {
return fmt.Errorf("could not query blockchains: %w", err)
}
Expand All @@ -166,13 +167,13 @@ func SetupSubnet(ctx context.Context, vmGenesis string) error {

// Ensure all nodes are validating subnet
for i, url := range nodeURLs {
nClient := platformvm.NewClient(url, constants.HTTPTimeout)
nClient := platformvm.NewClient(url)
for {
if ctx.Err() != nil {
return ctx.Err()
}
status, _ := nClient.GetBlockchainStatus(blockchainID.String())
if status == platformvm.Validating {
txStatus, _ := nClient.GetBlockchainStatus(ctx, blockchainID.String())
if txStatus == status.Validating {
break
}
color.Yellow("waiting for validating status for %s", nodeIDs[i])
Expand All @@ -183,12 +184,12 @@ func SetupSubnet(ctx context.Context, vmGenesis string) error {

// Ensure network bootstrapped
for i, url := range nodeURLs {
nClient := info.NewClient(url, constants.HTTPTimeout)
nClient := info.NewClient(url)
for {
if ctx.Err() != nil {
return ctx.Err()
}
bootstrapped, _ := nClient.IsBootstrapped(blockchainID.String())
bootstrapped, _ := nClient.IsBootstrapped(ctx, blockchainID.String())
if bootstrapped {
break
}
Expand All @@ -203,5 +204,6 @@ func SetupSubnet(ctx context.Context, vmGenesis string) error {
for i, url := range nodeURLs {
color.Green("%s: %s/ext/bc/%s", nodeIDs[i], url, blockchainID.String())
}
color.Green("Custom VM ID: %s", vmID)
return nil
}
6 changes: 3 additions & 3 deletions scripts/constants.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/bin/bash
build_dir="${MAIN_PATH}/build"

subnetevm_version='v0.1.0'
subnetevm_version='v0.1.1'
subnetevm_path="${build_dir}/subnet-evm/subnet-evm"

timestampvm_version='v1.2.0'
timestampvm_version='v1.2.2'
timestampvm_path="${build_dir}/timestampvm/timestampvm"

coreth_version='v0.8.1-rc.0'
coreth_version='v0.8.5-rc.2'
evm_path="${build_dir}/system-plugins/evm"
4 changes: 2 additions & 2 deletions scripts/run.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/bash
if [ $# -eq 0 ]; then
go run main/main.go
elif [ $# -eq 2 ]; then
go run main/main.go $1 $2
elif [ $# -eq 3 ]; then
go run main/main.go $1 $2 $3
else
echo 'invalid number of arguments (expected no args or [vm-path] [vm-genesis]'
exit 1
Expand Down
6 changes: 4 additions & 2 deletions scripts/subnet-evm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ source "$MAIN_PATH"/scripts/constants.sh

# Create genesis
# 56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027 => 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
vm_id="spePNvBxaWSYL2tB5e2xMmMNBQkXMN8z2XEbz1ML2Aahatwoc"
subnetevm_genesis_path="${build_dir}/subnet-evm/genesis.txt"
cat <<EOF > $subnetevm_genesis_path
{
Expand All @@ -31,7 +32,8 @@ cat <<EOF > $subnetevm_genesis_path
"maxBlockGasCost": 1000000,
"targetBlockRate": 2,
"blockGasCostStep": 200000
}
},
"allowFeeRecipients": false
},
"alloc": {
"8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC": {
Expand All @@ -51,4 +53,4 @@ cat <<EOF > $subnetevm_genesis_path
}
EOF

source "$MAIN_PATH"/scripts/run.sh $subnetevm_path $subnetevm_genesis_path
source "$MAIN_PATH"/scripts/run.sh $subnetevm_path $subnetevm_genesis_path $vm_id
3 changes: 2 additions & 1 deletion scripts/timestampvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ source "$MAIN_PATH"/scripts/constants.sh

# Create genesis
timestamp_genesis_path="${build_dir}/timestampvm/genesis.txt"
vm_id="tGas3T58KzdjLHhBDMnH2TvrddhqTji5iZAMZ3RXs2NLpSnhH"
touch $timestamp_genesis_path
echo "fP1vxkpyLWnH9dD6BQA" > $timestamp_genesis_path

source "$MAIN_PATH"/scripts/run.sh $timestampvm_path $timestamp_genesis_path
source "$MAIN_PATH"/scripts/run.sh $timestampvm_path $timestamp_genesis_path $vm_id