-
Notifications
You must be signed in to change notification settings - Fork 669
/
genesis.go
72 lines (63 loc) · 1.76 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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package genesis
import (
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
)
// UTXO adds messages to UTXOs
type UTXO struct {
avax.UTXO `serialize:"true"`
Message []byte `serialize:"true" json:"message"`
}
// Genesis represents a genesis state of the platform chain
type Genesis struct {
UTXOs []*UTXO `serialize:"true"`
Validators []*txs.Tx `serialize:"true"`
Chains []*txs.Tx `serialize:"true"`
Timestamp uint64 `serialize:"true"`
InitialSupply uint64 `serialize:"true"`
Message string `serialize:"true"`
}
func Parse(genesisBytes []byte) (*Genesis, error) {
gen := &Genesis{}
if _, err := Codec.Unmarshal(genesisBytes, gen); err != nil {
return nil, err
}
for _, tx := range gen.Validators {
if err := tx.Initialize(txs.GenesisCodec); err != nil {
return nil, err
}
}
for _, tx := range gen.Chains {
if err := tx.Initialize(txs.GenesisCodec); err != nil {
return nil, err
}
}
return gen, nil
}
// State represents the genesis state of the platform chain
type State struct {
UTXOs []*avax.UTXO
Validators []*txs.Tx
Chains []*txs.Tx
Timestamp uint64
InitialSupply uint64
}
func ParseState(genesisBytes []byte) (*State, error) {
genesis, err := Parse(genesisBytes)
if err != nil {
return nil, err
}
utxos := make([]*avax.UTXO, 0, len(genesis.UTXOs))
for _, utxo := range genesis.UTXOs {
utxos = append(utxos, &utxo.UTXO)
}
return &State{
UTXOs: utxos,
Validators: genesis.Validators,
Chains: genesis.Chains,
Timestamp: genesis.Timestamp,
InitialSupply: genesis.InitialSupply,
}, nil
}