Skip to content

Commit a6c4a3b

Browse files
authored
feat: Shelley genesis UTxOs (#1041)
Fixes #1040 Signed-off-by: Aurora Gaffney <[email protected]>
1 parent 96b1ab3 commit a6c4a3b

File tree

4 files changed

+98
-4
lines changed

4 files changed

+98
-4
lines changed

ledger/common/address.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,15 @@ func NewAddress(addr string) (Address, error) {
9191
return a, nil
9292
}
9393

94+
// NewAddressFromBytes returns an Address based on the raw bytes provided
95+
func NewAddressFromBytes(addrBytes []byte) (Address, error) {
96+
var ret Address
97+
if err := ret.populateFromBytes(addrBytes); err != nil {
98+
return Address{}, err
99+
}
100+
return ret, nil
101+
}
102+
94103
// NewAddressFromParts returns an Address based on the individual parts of the address that are provided
95104
func NewAddressFromParts(
96105
addrType uint8,

ledger/common/address_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ func TestAddressFromBytes(t *testing.T) {
6565
},
6666
}
6767
for _, testDef := range testDefs {
68-
addr := Address{}
69-
err := addr.populateFromBytes(
68+
addr, err := NewAddressFromBytes(
7069
test.DecodeHexString(testDef.addressBytesHex),
7170
)
7271
if err != nil {

ledger/shelley/genesis.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ type ShelleyGenesis struct {
4040
MaxLovelaceSupply uint64 `json:"maxLovelaceSupply"`
4141
ProtocolParameters ShelleyGenesisProtocolParams `json:"protocolParams"`
4242
GenDelegs map[string]map[string]string `json:"genDelegs"`
43-
InitialFunds map[string]any `json:"initialFunds"`
43+
InitialFunds map[string]uint64 `json:"initialFunds"`
4444
Staking any `json:"staking"`
4545
}
4646

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

101+
func (g *ShelleyGenesis) GenesisUtxos() ([]common.Utxo, error) {
102+
ret := []common.Utxo{}
103+
for address, amount := range g.InitialFunds {
104+
addrBytes, err := hex.DecodeString(address)
105+
if err != nil {
106+
return nil, err
107+
}
108+
tmpAddr, err := common.NewAddressFromBytes(addrBytes)
109+
if err != nil {
110+
return nil, err
111+
}
112+
addrCborBytes, err := cbor.Encode(tmpAddr)
113+
if err != nil {
114+
return nil, err
115+
}
116+
ret = append(
117+
ret,
118+
common.Utxo{
119+
Id: ShelleyTransactionInput{
120+
TxId: common.Blake2b256Hash(addrCborBytes),
121+
OutputIndex: 0,
122+
},
123+
Output: ShelleyTransactionOutput{
124+
OutputAddress: tmpAddr,
125+
OutputAmount: amount,
126+
},
127+
},
128+
)
129+
}
130+
return ret, nil
131+
}
132+
101133
type ShelleyGenesisProtocolParams struct {
102134
cbor.StructAsArray
103135
MinFeeA uint `json:"minFeeA"`

ledger/shelley/genesis_test.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package shelley_test
1616

1717
import (
18+
"encoding/json"
1819
"math/big"
1920
"reflect"
2021
"strings"
@@ -176,7 +177,7 @@ var expectedGenesisObj = shelley.ShelleyGenesis{
176177
"vrf": "6394a632af51a32768a6f12dac3485d9c0712d0b54e3f389f355385762a478f2",
177178
},
178179
},
179-
InitialFunds: map[string]any{},
180+
InitialFunds: map[string]uint64{},
180181
}
181182

182183
func TestGenesisFromJson(t *testing.T) {
@@ -194,3 +195,56 @@ func TestGenesisFromJson(t *testing.T) {
194195
)
195196
}
196197
}
198+
199+
func TestGenesisUtxos(t *testing.T) {
200+
testHexAddr := "000045183c1dcaeb0ca5cf583a68b9e31a6301bcbde487065bd35b955a98ba9d3061e1bd15749cc857e94b30583c120e3255adb93b44681bad"
201+
testAmount := uint64(120_000_000_000_000)
202+
expectedTxId := "23e41590bf49ad07dd6f28db73f9f16c804b9b5791b9dd669bfc58df8a9a1129"
203+
expectedAddr := "addr_test1qqqy2xpurh9wkr99eavr569euvdxxqduhhjgwpjm6dde2k5ch2wnqc0ph52hf8xg2l55kvzc8sfquvj44kunk3rgrwksfahlvw"
204+
// Generate genesis config JSON
205+
tmpGenesisData := map[string]any{
206+
"initialFunds": map[string]uint64{
207+
testHexAddr: testAmount,
208+
},
209+
}
210+
tmpGenesisJson, err := json.Marshal(tmpGenesisData)
211+
if err != nil {
212+
t.Fatalf("unexpected error: %s", err)
213+
}
214+
// Parse genesis config JSON
215+
tmpGenesis, err := shelley.NewShelleyGenesisFromReader(
216+
strings.NewReader(string(tmpGenesisJson)),
217+
)
218+
if err != nil {
219+
t.Fatalf("unexpected error: %s", err)
220+
}
221+
tmpGenesisUtxos, err := tmpGenesis.GenesisUtxos()
222+
if err != nil {
223+
t.Fatalf("unexpected error: %s", err)
224+
}
225+
if len(tmpGenesisUtxos) != 1 {
226+
t.Fatalf("did not get expected count of genesis UTxOs")
227+
}
228+
tmpUtxo := tmpGenesisUtxos[0]
229+
if tmpUtxo.Id.Id().String() != expectedTxId {
230+
t.Fatalf(
231+
"did not get expected TxID: got %s, wanted %s",
232+
tmpUtxo.Id.Id().String(),
233+
expectedTxId,
234+
)
235+
}
236+
if tmpUtxo.Output.Address().String() != expectedAddr {
237+
t.Fatalf(
238+
"did not get expected address: got %s, wanted %s",
239+
tmpUtxo.Output.Address().String(),
240+
expectedAddr,
241+
)
242+
}
243+
if tmpUtxo.Output.Amount() != testAmount {
244+
t.Fatalf(
245+
"did not get expected amount: got %d, wanted %d",
246+
tmpUtxo.Output.Amount(),
247+
testAmount,
248+
)
249+
}
250+
}

0 commit comments

Comments
 (0)