forked from ava-labs/hypersdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenesis.go
112 lines (91 loc) · 2.89 KB
/
genesis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package genesis
import (
"context"
"encoding/json"
"fmt"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/trace"
"github.com/ava-labs/hypersdk/chain"
"github.com/ava-labs/hypersdk/vm"
"github.com/ava-labs/hypersdk/examples/tokenvm/consts"
"github.com/ava-labs/hypersdk/examples/tokenvm/storage"
"github.com/ava-labs/hypersdk/examples/tokenvm/utils"
)
var _ vm.Genesis = (*Genesis)(nil)
type CustomAllocation struct {
Address string `json:"address"` // bech32 address
Balance uint64 `json:"balance"`
}
type Genesis struct {
// Address prefix
HRP string `json:"hrp"`
// Block params
MaxBlockTxs int `json:"maxBlockTxs"`
MaxBlockUnits uint64 `json:"maxBlockUnits"` // must be possible to reach before block too large
// Tx params
BaseUnits uint64 `json:"baseUnits"`
ValidityWindow int64 `json:"validityWindow"` // seconds
// Unit pricing
MinUnitPrice uint64 `json:"minUnitPrice"`
UnitPriceChangeDenominator uint64 `json:"unitPriceChangeDenominator"`
WindowTargetUnits uint64 `json:"windowTargetUnits"` // 10s
// Block pricing
MinBlockCost uint64 `json:"minBlockCost"`
BlockCostChangeDenominator uint64 `json:"blockCostChangeDenominator"`
WindowTargetBlocks uint64 `json:"windowTargetBlocks"` // 10s
// Allocations
CustomAllocation []*CustomAllocation `json:"customAllocation"`
}
func Default() *Genesis {
return &Genesis{
HRP: consts.HRP,
// Block params
MaxBlockTxs: 20_000, // rely on max block units
MaxBlockUnits: 1_800_000, // 1.8 MiB
// Tx params
BaseUnits: 48, // timestamp(8) + chainID(32) + unitPrice(8)
ValidityWindow: 60,
// Unit Pricing
MinUnitPrice: 1,
UnitPriceChangeDenominator: 48,
WindowTargetUnits: 9_000_000, // 9 MiB
// Block pricing
MinBlockCost: 0,
BlockCostChangeDenominator: 48,
WindowTargetBlocks: 20, // 10s
}
}
func New(b []byte, _ []byte /* upgradeBytes */) (*Genesis, error) {
g := Default()
if len(b) > 0 {
if err := json.Unmarshal(b, g); err != nil {
return nil, fmt.Errorf("failed to unmarshal config %s: %w", string(b), err)
}
}
if g.WindowTargetUnits == 0 {
return nil, ErrInvalidTarget
}
if g.WindowTargetBlocks == 0 {
return nil, ErrInvalidTarget
}
return g, nil
}
func (g *Genesis) GetHRP() string {
return g.HRP
}
func (g *Genesis) Load(ctx context.Context, tracer trace.Tracer, db chain.Database) error {
ctx, span := tracer.Start(ctx, "Genesis.Load")
defer span.End()
for _, alloc := range g.CustomAllocation {
pk, err := utils.ParseAddress(alloc.Address)
if err != nil {
return err
}
if err := storage.SetBalance(ctx, db, pk, ids.Empty, alloc.Balance); err != nil {
return fmt.Errorf("%w: addr=%s, bal=%d", err, alloc.Address, alloc.Balance)
}
}
return nil
}