-
Notifications
You must be signed in to change notification settings - Fork 663
/
genesis.go
51 lines (43 loc) · 1.16 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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package execute
import (
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/example/xsvm/block"
"github.com/ava-labs/avalanchego/vms/example/xsvm/genesis"
"github.com/ava-labs/avalanchego/vms/example/xsvm/state"
)
func Genesis(db database.KeyValueReaderWriterDeleter, chainID ids.ID, g *genesis.Genesis) error {
isInitialized, err := state.IsInitialized(db)
if err != nil {
return err
}
if isInitialized {
return nil
}
blk, err := genesis.Block(g)
if err != nil {
return err
}
for _, allocation := range g.Allocations {
if err := state.SetBalance(db, allocation.Address, chainID, allocation.Balance); err != nil {
return err
}
}
blkID, err := blk.ID()
if err != nil {
return err
}
blkBytes, err := block.Codec.Marshal(block.Version, blk)
if err != nil {
return err
}
if err := state.AddBlock(db, blk.Height, blkID, blkBytes); err != nil {
return err
}
if err := state.SetLastAccepted(db, blkID); err != nil {
return err
}
return state.SetInitialized(db)
}