Skip to content

Commit

Permalink
Update to avalanchego v1.11.x
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJohnnyGault committed Mar 18, 2024
1 parent 6f5d834 commit b1a54ce
Show file tree
Hide file tree
Showing 18 changed files with 795 additions and 216 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ If you have problems with the `ggt node run` command (it's currently under heavy
# Mac
mkdir MySubnetProject
cd MySubnetProject
ggt utils init v1.9.7 v0.4.8 # Downloads binaries from GitHub
ggt node prepare NodeV1 --ava-bin=avalanchego-v1.9.7 --vm-name=subnetevm --vm-bin=subnet-evm-v0.4.8
ggt utils init v1.10.19 v0.5.11 # Downloads binaries from GitHub
ggt node prepare NodeV1 --ava-bin=avalanchego-v1.10.19 --vm-name=subnetevm --vm-bin=subnet-evm-v0.5.11
```

If you then `prepared` another node NodeV2 with some different binary versions, you might have a directory structure that looks like this:
Expand Down
3 changes: 2 additions & 1 deletion cmd/nodecmd/log_level.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func setLoggerLevel(level string, chainName string) error {

if chainName == "X" || chainName == "P" {
c := admin.NewClient(uri)
return c.SetLoggerLevel(ctx, chainName, level, level)
_, err := c.SetLoggerLevel(ctx, chainName, level, level)
return err
}

lvl, err := ethlog.LvlFromString(level)
Expand Down
1 change: 1 addition & 0 deletions cmd/nodecmd/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ func NewCmd(injectedApp *application.GoGoTools) *cobra.Command {
cmd.AddCommand(newPrepareCmd())
cmd.AddCommand(newRunCmd())
cmd.AddCommand(newResetCmd())
cmd.AddCommand(newValidatorsCmd())
return cmd
}
37 changes: 37 additions & 0 deletions cmd/nodecmd/validators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package nodecmd

import (
"fmt"

"github.com/multisig-labs/gogotools/pkg/utils"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tidwall/gjson"
)

func newValidatorsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "validators subnetID",
Short: "Get current validators for a subnet (leave empty for Primary Network)",
Long: ``,
Args: cobra.MaximumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
subnetID := "11111111111111111111111111111111LpoYY"
if len(args) > 0 {
subnetID = args[0]
}
result, err := getValidators(subnetID)
cobra.CheckErr(err)
fmt.Println(result.String())
},
}
return cmd
}

func getValidators(subnetID string) (*gjson.Result, error) {
uri := viper.GetString("node-url")
urlP := fmt.Sprintf("%s/ext/bc/P", uri)

currValdrs, err := utils.FetchRPCGJSON(urlP, "platform.getCurrentValidators", fmt.Sprintf(`{"subnetID":"%s"}`, subnetID))
return currValdrs, err
}
119 changes: 119 additions & 0 deletions cmd/utilscmd/_inspect_block.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package utilscmd

import (
"encoding/base64"
"encoding/json"
"fmt"
"strings"
"sync"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/formatting"
"github.com/ava-labs/avalanchego/vms/platformvm/block"

"github.com/spf13/cobra"
)

// TODO Need to fix this to work with avalanchego 1.11.x

func newInspectBlockCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "inspectblk [block]",
Short: "Decode a hex encoded block",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := genMainnetCtx()
var blkBytes []byte
var err error
if strings.HasPrefix(args[0], "0x") {
blkBytes, err = formatting.Decode(formatting.Hex, args[0])
} else {
blkBytes, err = base64.StdEncoding.DecodeString(args[0])
}
cobra.CheckErr(err)

blk, js, err := decodeBlock(ctx, blkBytes)
cobra.CheckErr(err)
fmt.Printf("%s\n", js)
fmt.Printf("%v\n", blk)
fmt.Printf("%x\n", blk.Txs()[0].Unsigned.Bytes())
// _, _, b2, err := address.Parse("P-avax1gfpj30csekhwmf4mqkncelus5zl2ztqzvv7aww")
// cobra.CheckErr(err)
// fmt.Printf("%x\n", b2)

// factory := secp256k1.Factory{}
// pk, err := factory.ToPrivateKey(b)
// cobra.CheckErr(err)
// fmt.Printf("PrivKey: %x\n", pk)

// a := pk.PublicKey().Bytes()
// fmt.Printf("Serialized compressed pub key bytes: %x\n", a)

// addr, err := address.FormatBech32("P-avax1", id.Bytes())
// cobra.CheckErr(err)
// fmt.Printf("Addr: %s\n", addr)

// fmt.Printf("%x\n", b)
return nil
},
}
return cmd
}

func decodeBlock(ctx *snow.Context, b []byte) (block.Block, string, error) {
decoded := decodeProposerBlock(b)

blk, js, err := decodeInnerBlock(ctx, decoded)
if err != nil {
return blk, "", err
}
return blk, string(js), nil
}

// Tries to decode as proposal block (post-Banff) if it fails just return the original bytes
func decodeProposerBlock(b []byte) []byte {
innerBlk, err := block.Parse(b)
if err != nil {
return b
}
return innerBlk.Block()
}

func decodeInnerBlock(ctx *snow.Context, b []byte) (block.Block, string, error) {
res, err := block.Parse(block.GenesisCodec, b)
if err != nil {
return res, "", fmt.Errorf("blocks.Parse error: %w", err)
}

res.InitCtx(ctx)
j, err := json.Marshal(res)
if err != nil {
return res, "", fmt.Errorf("json.Marshal error: %w", err)
}
return res, string(j), nil
}

// Simple context so that Marshal works
func genMainnetCtx() *snow.Context {
pChainID, _ := ids.FromString("11111111111111111111111111111111LpoYY")
xChainID, _ := ids.FromString("2oYMBNV4eNHyqk2fjjV5nVQLDbtmNJzq5s3qs3Lo6ftnC6FByM")
cChainID, _ := ids.FromString("2q9e4r6Mu3U68nU1fYjgbR6JvwrRx36CohpAX5UQxse55x1Q5")
avaxAssetID, _ := ids.FromString("FvwEAhmxKfeiG8SnEvq42hc6whRyY3EFYAvebMqDNDGCgxN5Z")
lookup := ids.NewAliaser()
lookup.Alias(xChainID, "X")
lookup.Alias(cChainID, "C")
lookup.Alias(pChainID, "P")
c := &snow.Context{
NetworkID: 1,
SubnetID: [32]byte{},
ChainID: [32]byte{},
NodeID: [20]byte{},
XChainID: xChainID,
CChainID: cChainID,
AVAXAssetID: avaxAssetID,
Lock: sync.RWMutex{},
BCLookup: lookup,
}
return c
}
4 changes: 2 additions & 2 deletions cmd/utilscmd/addr_variants.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ func addrVariants(addr string) ([]string, error) {
hrps := []string{"avax", "fuji", "local", "custom"}
chains := []string{"X", "P"}

out := []string{}

id, err := address.ParseToID(addr)
if err != nil {
return nil, err
}

out := []string{fmt.Sprintf("Raw Bytes: %s", id.Hex())}

for _, hrp := range hrps {
for _, chain := range chains {
a, err := address.Format(chain, hrp, id.Bytes())
Expand Down
66 changes: 66 additions & 0 deletions cmd/utilscmd/cb58.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package utilscmd

import (
"encoding/json"
"fmt"

"github.com/ava-labs/avalanchego/utils/cb58"

"github.com/spf13/cobra"
)

func newCB58DecodeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "cb58decode [value]",
Short: "Decode a CB58 encoded string",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
b, err := cb58.Decode(args[0])
cobra.CheckErr(err)
fmt.Printf("%x\n", b)
// _, _, b2, err := address.Parse("P-avax1gfpj30csekhwmf4mqkncelus5zl2ztqzvv7aww")
// cobra.CheckErr(err)
// fmt.Printf("%x\n", b2)

// factory := secp256k1.Factory{}
// pk, err := factory.ToPrivateKey(b)
// cobra.CheckErr(err)
// fmt.Printf("PrivKey: %x\n", pk)

// a := pk.PublicKey().Bytes()
// fmt.Printf("Serialized compressed pub key bytes: %x\n", a)

// addr, err := address.FormatBech32("P-avax1", id.Bytes())
// cobra.CheckErr(err)
// fmt.Printf("Addr: %s\n", addr)

// fmt.Printf("%x\n", b)
return nil
},
}
return cmd
}

func newCB58DecodeSigCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "cb58decodesig [value]",
Short: "Decode a CB58 encoded signature into r,s,v",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
b, err := cb58.Decode(args[0])
cobra.CheckErr(err)
sig := struct {
R string `json:"r"`
S string `json:"s"`
V string `json:"v"`
}{}
sig.R = fmt.Sprintf("0x%x", b[0:32])
sig.S = fmt.Sprintf("0x%x", b[32:64])
sig.V = fmt.Sprintf("0x%x", b[64:])
j, _ := json.Marshal(sig)
fmt.Printf("%s\n", j)
return nil
},
}
return cmd
}
44 changes: 44 additions & 0 deletions cmd/utilscmd/random.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package utilscmd

import (
"crypto/rand"
"fmt"

"github.com/ava-labs/avalanchego/ids"
"github.com/spf13/cobra"
)

func newRandomCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "random",
Short: "Generate random ids of various types",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
err := cmd.Help()
if err != nil {
fmt.Println(err)
}
},
}

cmd.AddCommand(newRandomNodeIDCmd())

return cmd
}

func newRandomNodeIDCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "nodeid",
Short: "Create random NodeID",
Long: ``,
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, args []string) {
r := make([]byte, 20)
_, err := rand.Read(r)
cobra.CheckErr(err)
nodeid := ids.NodeID(r)
fmt.Println(nodeid)
},
}
return cmd
}
4 changes: 4 additions & 0 deletions cmd/utilscmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func NewCmd(injectedApp *application.GoGoTools) *cobra.Command {
cmd.AddCommand(newMnemonicKeysCmd())
cmd.AddCommand(newMnemonicAddrsCmd())
cmd.AddCommand(newPortFwdCmd())
cmd.AddCommand(newCB58DecodeCmd())
cmd.AddCommand(newCB58DecodeSigCmd())
// cmd.AddCommand(newInspectBlockCmd())
cmd.AddCommand(newRandomCmd())

return cmd
}
Loading

0 comments on commit b1a54ce

Please sign in to comment.