Skip to content

Commit

Permalink
Add encoding flag to chain genesis command (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ceyonur authored Dec 28, 2022
1 parent 3d5bb42 commit 7e46d42
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
29 changes: 24 additions & 5 deletions cmd/chain/genesis/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
package genesis

import (
"errors"
"fmt"
"os"

"github.com/ava-labs/xsvm/genesis"
"github.com/spf13/cobra"

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

"github.com/ava-labs/xsvm/genesis"
)

var errUnknownEncoding = errors.New("unknown encoding")

func Command() *cobra.Command {
c := &cobra.Command{
Use: "genesis",
Expand All @@ -23,16 +30,28 @@ func Command() *cobra.Command {

func genesisFunc(c *cobra.Command, args []string) error {
flags := c.Flags()
g, err := ParseFlags(flags, args)
config, err := ParseFlags(flags, args)
if err != nil {
return err
}

genesisBytes, err := genesis.Codec.Marshal(genesis.Version, g)
genesisBytes, err := genesis.Codec.Marshal(genesis.Version, config.Genesis)
if err != nil {
return err
}

_, err = os.Stdout.Write(genesisBytes)
return err
switch config.Encoding {
case binaryEncoding:
_, err = os.Stdout.Write(genesisBytes)
return err
case hexEncoding:
encoded, err := formatting.Encode(formatting.Hex, genesisBytes)
if err != nil {
return err
}
_, err = fmt.Println(encoded)
return err
default:
return fmt.Errorf("%w: %q", errUnknownEncoding, config.Encoding)
}
}
41 changes: 28 additions & 13 deletions cmd/chain/genesis/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package genesis

import (
"fmt"
"math"
"time"

Expand All @@ -16,22 +17,28 @@ import (
)

const (
TimeKey = "time"
AddressKey = "address"
BalanceKey = "balance"
TimeKey = "time"
AddressKey = "address"
BalanceKey = "balance"
EncodingKey = "encoding"

binaryEncoding = "binary"
hexEncoding = "hex"
)

func AddFlags(flags *pflag.FlagSet) {
flags.Int64(TimeKey, time.Now().Unix(), "Unix timestamp to include in the genesis")
flags.String(AddressKey, genesis.EWOQKey.Address().String(), "Address to fund in the genesis")
flags.Uint64(BalanceKey, math.MaxUint64, "Amount to provide the funded address in the genesis")
flags.String(EncodingKey, hexEncoding, fmt.Sprintf("Encoding to use for the genesis. Available values: %s or %s", hexEncoding, binaryEncoding))
}

func ParseFlags(flags *pflag.FlagSet, args []string) (*xsgenesis.Genesis, error) {
if err := flags.Parse(args); err != nil {
return nil, err
}
type Config struct {
Genesis *xsgenesis.Genesis
Encoding string
}

func ParseFlags(flags *pflag.FlagSet, args []string) (*Config, error) {
if err := flags.Parse(args); err != nil {
return nil, err
}
Expand All @@ -56,13 +63,21 @@ func ParseFlags(flags *pflag.FlagSet, args []string) (*xsgenesis.Genesis, error)
return nil, err
}

return &xsgenesis.Genesis{
Timestamp: timestamp,
Allocations: []xsgenesis.Allocation{
{
Address: addr,
Balance: balance,
encoding, err := flags.GetString(EncodingKey)
if err != nil {
return nil, err
}

return &Config{
Genesis: &xsgenesis.Genesis{
Timestamp: timestamp,
Allocations: []xsgenesis.Allocation{
{
Address: addr,
Balance: balance,
},
},
},
Encoding: encoding,
}, nil
}

0 comments on commit 7e46d42

Please sign in to comment.