github.com/MetalBlockchain/metalgo@v1.11.9/vms/example/xsvm/genesis/genesis.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package genesis
     5  
     6  import (
     7  	"github.com/MetalBlockchain/metalgo/ids"
     8  	"github.com/MetalBlockchain/metalgo/utils/hashing"
     9  	"github.com/MetalBlockchain/metalgo/vms/example/xsvm/block"
    10  )
    11  
    12  type Genesis struct {
    13  	Timestamp   int64        `serialize:"true" json:"timestamp"`
    14  	Allocations []Allocation `serialize:"true" json:"allocations"`
    15  }
    16  
    17  type Allocation struct {
    18  	Address ids.ShortID `serialize:"true" json:"address"`
    19  	Balance uint64      `serialize:"true" json:"balance"`
    20  }
    21  
    22  func Parse(bytes []byte) (*Genesis, error) {
    23  	genesis := &Genesis{}
    24  	_, err := Codec.Unmarshal(bytes, genesis)
    25  	return genesis, err
    26  }
    27  
    28  func Block(genesis *Genesis) (*block.Stateless, error) {
    29  	bytes, err := Codec.Marshal(CodecVersion, genesis)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	return &block.Stateless{
    34  		ParentID:  hashing.ComputeHash256Array(bytes),
    35  		Timestamp: genesis.Timestamp,
    36  	}, nil
    37  }