Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ledger/common/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ func NewAddress(addr string) (Address, error) {
return a, nil
}

// NewAddressFromBytes returns an Address based on the raw bytes provided
func NewAddressFromBytes(addrBytes []byte) (Address, error) {
var ret Address
if err := ret.populateFromBytes(addrBytes); err != nil {
return Address{}, err
}
return ret, nil
}

// NewAddressFromParts returns an Address based on the individual parts of the address that are provided
func NewAddressFromParts(
addrType uint8,
Expand Down
3 changes: 1 addition & 2 deletions ledger/common/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ func TestAddressFromBytes(t *testing.T) {
},
}
for _, testDef := range testDefs {
addr := Address{}
err := addr.populateFromBytes(
addr, err := NewAddressFromBytes(
test.DecodeHexString(testDef.addressBytesHex),
)
if err != nil {
Expand Down
34 changes: 33 additions & 1 deletion ledger/shelley/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type ShelleyGenesis struct {
MaxLovelaceSupply uint64 `json:"maxLovelaceSupply"`
ProtocolParameters ShelleyGenesisProtocolParams `json:"protocolParams"`
GenDelegs map[string]map[string]string `json:"genDelegs"`
InitialFunds map[string]any `json:"initialFunds"`
InitialFunds map[string]uint64 `json:"initialFunds"`
Staking any `json:"staking"`
}

Expand Down Expand Up @@ -98,6 +98,38 @@ func (g ShelleyGenesis) MarshalCBOR() ([]byte, error) {
return cbor.Encode(tmpData)
}

func (g *ShelleyGenesis) GenesisUtxos() ([]common.Utxo, error) {
ret := []common.Utxo{}
for address, amount := range g.InitialFunds {
addrBytes, err := hex.DecodeString(address)
if err != nil {
return nil, err
}
tmpAddr, err := common.NewAddressFromBytes(addrBytes)
if err != nil {
return nil, err
}
addrCborBytes, err := cbor.Encode(tmpAddr)
if err != nil {
return nil, err
}
ret = append(
ret,
common.Utxo{
Id: ShelleyTransactionInput{
TxId: common.Blake2b256Hash(addrCborBytes),
OutputIndex: 0,
},
Output: ShelleyTransactionOutput{
OutputAddress: tmpAddr,
OutputAmount: amount,
},
},
)
}
return ret, nil
}

type ShelleyGenesisProtocolParams struct {
cbor.StructAsArray
MinFeeA uint `json:"minFeeA"`
Expand Down
56 changes: 55 additions & 1 deletion ledger/shelley/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package shelley_test

import (
"encoding/json"
"math/big"
"reflect"
"strings"
Expand Down Expand Up @@ -176,7 +177,7 @@ var expectedGenesisObj = shelley.ShelleyGenesis{
"vrf": "6394a632af51a32768a6f12dac3485d9c0712d0b54e3f389f355385762a478f2",
},
},
InitialFunds: map[string]any{},
InitialFunds: map[string]uint64{},
}

func TestGenesisFromJson(t *testing.T) {
Expand All @@ -194,3 +195,56 @@ func TestGenesisFromJson(t *testing.T) {
)
}
}

func TestGenesisUtxos(t *testing.T) {
testHexAddr := "000045183c1dcaeb0ca5cf583a68b9e31a6301bcbde487065bd35b955a98ba9d3061e1bd15749cc857e94b30583c120e3255adb93b44681bad"
testAmount := uint64(120_000_000_000_000)
expectedTxId := "23e41590bf49ad07dd6f28db73f9f16c804b9b5791b9dd669bfc58df8a9a1129"
expectedAddr := "addr_test1qqqy2xpurh9wkr99eavr569euvdxxqduhhjgwpjm6dde2k5ch2wnqc0ph52hf8xg2l55kvzc8sfquvj44kunk3rgrwksfahlvw"
// Generate genesis config JSON
tmpGenesisData := map[string]any{
"initialFunds": map[string]uint64{
testHexAddr: testAmount,
},
}
tmpGenesisJson, err := json.Marshal(tmpGenesisData)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
// Parse genesis config JSON
tmpGenesis, err := shelley.NewShelleyGenesisFromReader(
strings.NewReader(string(tmpGenesisJson)),
)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
tmpGenesisUtxos, err := tmpGenesis.GenesisUtxos()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if len(tmpGenesisUtxos) != 1 {
t.Fatalf("did not get expected count of genesis UTxOs")
}
tmpUtxo := tmpGenesisUtxos[0]
if tmpUtxo.Id.Id().String() != expectedTxId {
t.Fatalf(
"did not get expected TxID: got %s, wanted %s",
tmpUtxo.Id.Id().String(),
expectedTxId,
)
}
if tmpUtxo.Output.Address().String() != expectedAddr {
t.Fatalf(
"did not get expected address: got %s, wanted %s",
tmpUtxo.Output.Address().String(),
expectedAddr,
)
}
if tmpUtxo.Output.Amount() != testAmount {
t.Fatalf(
"did not get expected amount: got %d, wanted %d",
tmpUtxo.Output.Amount(),
testAmount,
)
}
}
Loading