Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add encoding to genesis func #2

Merged
merged 4 commits into from
Dec 28, 2022
Merged
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
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
}