Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.
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
3 changes: 2 additions & 1 deletion chain/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func init() {
c.RegisterType(&Transaction{}),
c.RegisterType(&StatefulBlock{}),
c.RegisterType(&SpaceInfo{}),
c.RegisterType(&Allocation{}),
c.RegisterType(&CustomAllocation{}),
c.RegisterType(&Airdrop{}),
c.RegisterType(&Genesis{}),
codecManager.RegisterCodec(codecVersion, c),
)
Expand Down
44 changes: 38 additions & 6 deletions chain/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,22 @@
package chain

import (
"encoding/json"
"fmt"

"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
log "github.com/inconshreveable/log15"
)

type Allocation struct {
type Airdrop struct {
// Address strings are hex-formatted common.Address
Address common.Address `serialize:"true" json:"address"`
}

type CustomAllocation struct {
// Address strings are hex-formatted common.Address
Address common.Address `serialize:"true" json:"address"`
Balance uint64 `serialize:"true" json:"balance"`
Expand Down Expand Up @@ -55,8 +62,9 @@ type Genesis struct {
MinBlockCost uint64 `serialize:"true" json:"minBlockCost"`

// Allocations
// TODO: move to a hash and use external file to avoid 1MB limit
Allocations []*Allocation `serialize:"true" json:"allocations"`
CustomAllocation []*CustomAllocation `serialize:"true" json:"customAllocation"`
AirdropHash string `serialize:"true" json:"airdropHash"`
AirdropUnits uint64 `serialize:"true" json:"airdropUnits"`
}

func DefaultGenesis() *Genesis {
Expand Down Expand Up @@ -110,12 +118,36 @@ func (g *Genesis) Verify() error {
return nil
}

func (g *Genesis) Load(db database.KeyValueWriter) error {
for _, alloc := range g.Allocations {
func (g *Genesis) Load(db database.KeyValueWriter, airdropData []byte) error {
if len(g.AirdropHash) > 0 {
h := common.BytesToHash(crypto.Keccak256(airdropData)).Hex()
if g.AirdropHash != h {
return fmt.Errorf("expected standard allocation %s but got %s", g.AirdropHash, h)
}

standardAllocation := []*Airdrop{}
if err := json.Unmarshal(airdropData, &standardAllocation); err != nil {
return err
}

for _, alloc := range standardAllocation {
if err := SetBalance(db, alloc.Address, g.AirdropUnits); err != nil {
return fmt.Errorf("%w: addr=%s, bal=%d", err, alloc.Address, g.AirdropUnits)
}
}
log.Debug(
"applied airdrop allocation",
"hash", h, "addrs", len(standardAllocation), "balance", g.AirdropUnits,
)
}

// Do custom allocation last in case an address shows up in standard
// allocation
for _, alloc := range g.CustomAllocation {
if err := SetBalance(db, alloc.Address, alloc.Balance); err != nil {
return fmt.Errorf("%w: addr=%s, bal=%d", err, alloc.Address, alloc.Balance)
}
log.Debug("loaded genesis balance", "addr", alloc.Address, "balance", alloc.Balance)
log.Debug("applied custom allocation", "addr", alloc.Address, "balance", alloc.Balance)
}
return nil
}
4 changes: 2 additions & 2 deletions chain/move_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestMoveTx(t *testing.T) {
defer db.Close()

g := DefaultGenesis()
g.Allocations = []*Allocation{
g.CustomAllocation = []*CustomAllocation{
{
Address: sender,
Balance: 10000000,
Expand All @@ -50,7 +50,7 @@ func TestMoveTx(t *testing.T) {
},
// sender3 is not given any balance
}
if err := g.Load(db); err != nil {
if err := g.Load(db, nil); err != nil {
t.Fatal(err)
}

Expand Down
4 changes: 2 additions & 2 deletions chain/transfer_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestTransferTx(t *testing.T) {
defer db.Close()

g := DefaultGenesis()
g.Allocations = []*Allocation{
g.CustomAllocation = []*CustomAllocation{
{
Address: sender,
Balance: 10000000,
Expand All @@ -49,7 +49,7 @@ func TestTransferTx(t *testing.T) {
},
// sender3 is not given any balance
}
if err := g.Load(db); err != nil {
if err := g.Load(db, nil); err != nil {
t.Fatal(err)
}

Expand Down
4 changes: 2 additions & 2 deletions chain/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ func TestTransactionErrInvalidSignature(t *testing.T) {
}
for i, tv := range tt {
db := memdb.New()
g.Allocations = []*Allocation{
g.CustomAllocation = []*CustomAllocation{
{
Address: sender,
Balance: 10000000,
},
// sender2 is not given any balance
}
if err := g.Load(db); err != nil {
if err := g.Load(db, nil); err != nil {
t.Fatal(err)
}
tx := tv.createTx()
Expand Down
30 changes: 25 additions & 5 deletions cmd/spacescli/cmd/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import (

var (
genesisFile string
magic uint64

minPrice int64
minBlockCost int64
claimReward int64
lifelineUnitReward int64

magic uint64
airdropHash string
airdropUnits uint64
)

func init() {
Expand Down Expand Up @@ -58,10 +60,22 @@ func init() {
-1,
"seconds per unit of fee that will be rewarded in a lifeline transaction",
)
genesisCmd.PersistentFlags().StringVar(
&airdropHash,
"airdrop-hash",
"",
"hash of airdrop data",
)
genesisCmd.PersistentFlags().Uint64Var(
&airdropUnits,
"airdrop-units",
0,
"units to allocate to each airdrop address",
)
}

var genesisCmd = &cobra.Command{
Use: "genesis [magic] [allocations file] [options]",
Use: "genesis [magic] [custom allocations file] [options]",
Short: "Creates a new genesis in the default location",
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
Expand Down Expand Up @@ -97,17 +111,23 @@ func genesisFunc(cmd *cobra.Command, args []string) error {
if lifelineUnitReward >= 0 {
genesis.LifelineUnitReward = uint64(lifelineUnitReward)
}
if len(airdropHash) > 0 {
genesis.AirdropHash = airdropHash
if airdropUnits == 0 {
return errors.New("non-zero airdrop units required")
}
genesis.AirdropUnits = airdropUnits
}

a, err := os.ReadFile(args[1])
if err != nil {
return err
}
allocs := []*chain.Allocation{}
allocs := []*chain.CustomAllocation{}
if err := json.Unmarshal(a, &allocs); err != nil {
return err
}
// Store hash instead
genesis.Allocations = allocs
genesis.CustomAllocation = allocs

b, err := json.Marshal(genesis)
if err != nil {
Expand Down
Loading