Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Commit

Permalink
add genesis utxos
Browse files Browse the repository at this point in the history
  • Loading branch information
alanorwick committed Mar 13, 2024
1 parent c654e99 commit ba55b34
Show file tree
Hide file tree
Showing 21 changed files with 480,626 additions and 0 deletions.
2 changes: 2 additions & 0 deletions consensus/blake3pow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,8 @@ func (blake3pow *Blake3pow) Finalize(chain consensus.ChainHeaderReader, header *
continue
}
}

core.AddGenesisUtxos(state, nodeLocation, blake3pow.logger)
}
header.SetUTXORoot(state.UTXORoot())
header.SetEVMRoot(state.IntermediateRoot(true))
Expand Down
1 change: 1 addition & 0 deletions consensus/progpow/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ func (progpow *Progpow) Finalize(chain consensus.ChainHeaderReader, header *type
continue
}
}
core.AddGenesisUtxos(state, nodeLocation, progpow.logger)
}
header.SetUTXORoot(state.UTXORoot())
header.SetEVMRoot(state.IntermediateRoot(true))
Expand Down
59 changes: 59 additions & 0 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ type GenesisAccount struct {
PrivateKey []byte `json:"secretKey,omitempty"` // for tests
}

type GenesisUTXO struct {
Denomination uint32 `json:"denomination"`
Index uint32 `json:"index"`
Hash string `json:"hash"`
}

// field type overrides for gencodec
type genesisSpecMarshaling struct {
Nonce math.HexOrDecimal64
Expand Down Expand Up @@ -467,3 +473,56 @@ func ReadGenesisAlloc(filename string, logger *log.Logger) map[string]GenesisAcc
// Use the parsed data
return data
}

func ReadGenesisQiAlloc(filename string, logger *log.Logger) map[string]GenesisUTXO {
jsonFile, err := os.Open(filename)
if err != nil {
logger.Error(err.Error())
return nil
}
defer jsonFile.Close()
// Read the file contents
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
logger.Error(err.Error())
return nil
}

// Parse the JSON data
var data map[string]GenesisUTXO
err = json.Unmarshal(byteValue, &data)
if err != nil {
logger.Error(err.Error())
return nil
}

// Use the parsed data
return data
}

// WriteGenesisUtxoSet writes the genesis utxo set to the database
func AddGenesisUtxos(state *state.StateDB, nodeLocation common.Location, logger *log.Logger) {
qiAlloc := ReadGenesisQiAlloc("genallocs/gen_alloc_qi_"+nodeLocation.Name()+".json", logger)
// logger.WithField("alloc", len(qiAlloc)).Info("Allocating genesis accounts")
for addressString, utxo := range qiAlloc {
addr := common.HexToAddress(addressString, nodeLocation)
internal, err := addr.InternalAddress()
if err != nil {
logger.Error("Provided address in genesis block is out of scope")
}

hash := common.HexToHash(utxo.Hash)

// check if utxo.Denomination is less than uint8
if utxo.Denomination > 255 {
logger.Error("Provided denomination is larger than uint8")
}

newUtxo := &types.UtxoEntry{
Address: internal.Bytes(),
Denomination: uint8(utxo.Denomination),
}

state.CreateUTXO(hash, uint16(utxo.Index), newUtxo)
}
}
Loading

0 comments on commit ba55b34

Please sign in to comment.