forked from ava-labs/hypersdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
genesis.go
164 lines (139 loc) · 5.17 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// 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"
smath "github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/x/merkledb"
"github.com/ava-labs/hypersdk/chain"
hconsts "github.com/ava-labs/hypersdk/consts"
"github.com/ava-labs/hypersdk/crypto/ed25519"
"github.com/ava-labs/hypersdk/examples/tokenvm/consts"
"github.com/ava-labs/hypersdk/examples/tokenvm/storage"
"github.com/ava-labs/hypersdk/examples/tokenvm/utils"
"github.com/ava-labs/hypersdk/state"
"github.com/ava-labs/hypersdk/vm"
)
var _ vm.Genesis = (*Genesis)(nil)
type CustomAllocation struct {
Address string `json:"address"` // bech32 address
Balance uint64 `json:"balance"`
}
type Genesis struct {
HRP string `json:"hrp"`
// State Parameters
StateBranchFactor merkledb.BranchFactor `json:"stateBranchFactor"`
// Chain Parameters
MinBlockGap int64 `json:"minBlockGap"` // ms
MinEmptyBlockGap int64 `json:"minEmptyBlockGap"` // ms
// Chain Fee Parameters
MinUnitPrice chain.Dimensions `json:"minUnitPrice"`
UnitPriceChangeDenominator chain.Dimensions `json:"unitPriceChangeDenominator"`
WindowTargetUnits chain.Dimensions `json:"windowTargetUnits"` // 10s
MaxBlockUnits chain.Dimensions `json:"maxBlockUnits"` // must be possible to reach before block too large
// Tx Parameters
ValidityWindow int64 `json:"validityWindow"` // ms
// Tx Fee Parameters
BaseComputeUnits uint64 `json:"baseUnits"`
BaseWarpComputeUnits uint64 `json:"baseWarpUnits"`
WarpComputeUnitsPerSigner uint64 `json:"warpUnitsPerSigner"`
OutgoingWarpComputeUnits uint64 `json:"outgoingWarpComputeUnits"`
ColdStorageKeyReadUnits uint64 `json:"coldStorageKeyReadUnits"`
ColdStorageValueReadUnits uint64 `json:"coldStorageValueReadUnits"` // per chunk
WarmStorageKeyReadUnits uint64 `json:"warmStorageKeyReadUnits"`
WarmStorageValueReadUnits uint64 `json:"warmStorageValueReadUnits"` // per chunk
StorageKeyCreateUnits uint64 `json:"storageKeyCreateUnits"`
StorageValueCreateUnits uint64 `json:"storageKeyValueUnits"` // per chunk
ColdStorageKeyModificationUnits uint64 `json:"coldStorageKeyModificationUnits"`
ColdStorageValueModificationUnits uint64 `json:"coldStorageValueModificationUnits"` // per chunk
WarmStorageKeyModificationUnits uint64 `json:"warmStorageKeyModificationUnits"`
WarmStorageValueModificationUnits uint64 `json:"warmStorageValueModificationUnits"` // per chunk
// Allocations
CustomAllocation []*CustomAllocation `json:"customAllocation"`
}
func Default() *Genesis {
return &Genesis{
HRP: consts.HRP,
// State Parameters
StateBranchFactor: merkledb.BranchFactor16,
// Chain Parameters
MinBlockGap: 100,
MinEmptyBlockGap: 2_500,
// Chain Fee Parameters
MinUnitPrice: chain.Dimensions{100, 100, 100, 100, 100},
UnitPriceChangeDenominator: chain.Dimensions{48, 48, 48, 48, 48},
WindowTargetUnits: chain.Dimensions{20_000_000, 1_000, 1_000, 1_000, 1_000},
MaxBlockUnits: chain.Dimensions{1_800_000, 2_000, 2_000, 2_000, 2_000},
// Tx Parameters
ValidityWindow: 60 * hconsts.MillisecondsPerSecond, // ms
// Tx Fee Compute Parameters
BaseComputeUnits: 1,
BaseWarpComputeUnits: 1_024,
WarpComputeUnitsPerSigner: 128,
OutgoingWarpComputeUnits: 1_024,
// Tx Fee Storage Parameters
//
// TODO: tune this
ColdStorageKeyReadUnits: 5,
ColdStorageValueReadUnits: 2,
WarmStorageKeyReadUnits: 1,
WarmStorageValueReadUnits: 1,
StorageKeyCreateUnits: 20,
StorageValueCreateUnits: 5,
ColdStorageKeyModificationUnits: 10,
ColdStorageValueModificationUnits: 3,
WarmStorageKeyModificationUnits: 5,
WarmStorageValueModificationUnits: 3,
}
}
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)
}
}
return g, nil
}
func (g *Genesis) Load(ctx context.Context, tracer trace.Tracer, mu state.Mutable) error {
ctx, span := tracer.Start(ctx, "Genesis.Load")
defer span.End()
if consts.HRP != g.HRP {
return ErrInvalidHRP
}
if err := g.StateBranchFactor.Valid(); err != nil {
return err
}
supply := uint64(0)
for _, alloc := range g.CustomAllocation {
pk, err := utils.ParseAddress(alloc.Address)
if err != nil {
return err
}
supply, err = smath.Add64(supply, alloc.Balance)
if err != nil {
return err
}
if err := storage.SetBalance(ctx, mu, pk, ids.Empty, alloc.Balance); err != nil {
return fmt.Errorf("%w: addr=%s, bal=%d", err, alloc.Address, alloc.Balance)
}
}
return storage.SetAsset(
ctx,
mu,
ids.Empty,
[]byte(consts.Symbol),
consts.Decimals,
[]byte(consts.Name),
supply,
ed25519.EmptyPublicKey,
false,
)
}
func (g *Genesis) GetStateBranchFactor() merkledb.BranchFactor {
return g.StateBranchFactor
}