1515package cardano
1616
1717import (
18+ "embed"
1819 "io"
1920 "os"
2021 "path"
@@ -27,19 +28,21 @@ import (
2728 "gopkg.in/yaml.v3"
2829)
2930
30- // CardanoNodeConfig represents the config.json/yaml file used by cardano-node
31+ // CardanoNodeConfig represents the config.json/yaml file used by cardano-node.
3132type CardanoNodeConfig struct {
32- path string
33+ // Embedded filesystem for loading genesis files
34+ embedFS embed.FS
3335 alonzoGenesis * alonzo.AlonzoGenesis
36+ byronGenesis * byron.ByronGenesis
37+ conwayGenesis * conway.ConwayGenesis
38+ shelleyGenesis * shelley.ShelleyGenesis
39+ path string
3440 AlonzoGenesisFile string `yaml:"AlonzoGenesisFile"`
3541 AlonzoGenesisHash string `yaml:"AlonzoGenesisHash"`
36- byronGenesis * byron.ByronGenesis
3742 ByronGenesisFile string `yaml:"ByronGenesisFile"`
3843 ByronGenesisHash string `yaml:"ByronGenesisHash"`
39- conwayGenesis * conway.ConwayGenesis
4044 ConwayGenesisFile string `yaml:"ConwayGenesisFile"`
4145 ConwayGenesisHash string `yaml:"ConwayGenesisHash"`
42- shelleyGenesis * shelley.ShelleyGenesis
4346 ShelleyGenesisFile string `yaml:"ShelleyGenesisFile"`
4447 ShelleyGenesisHash string `yaml:"ShelleyGenesisHash"`
4548}
@@ -69,6 +72,31 @@ func NewCardanoNodeConfigFromFile(file string) (*CardanoNodeConfig, error) {
6972 return c , nil
7073}
7174
75+ // NewCardanoNodeConfigFromEmbedFS creates a CardanoNodeConfig from an embedded filesystem.
76+ // It loads the main config file and all referenced genesis files from the embedded FS.
77+ // The file parameter should be a path relative to the root of the embedded filesystem.
78+ func NewCardanoNodeConfigFromEmbedFS (
79+ fs embed.FS ,
80+ file string ,
81+ ) (* CardanoNodeConfig , error ) {
82+ f , err := fs .Open (file )
83+ if err != nil {
84+ return nil , err
85+ }
86+ defer f .Close ()
87+
88+ c , err := NewCardanoNodeConfigFromReader (f )
89+ if err != nil {
90+ return nil , err
91+ }
92+ c .path = path .Dir (file )
93+ c .embedFS = fs // Store reference to embedded FS
94+ if err := c .loadGenesisConfigsFromEmbed (); err != nil {
95+ return nil , err
96+ }
97+ return c , nil
98+ }
99+
72100func (c * CardanoNodeConfig ) loadGenesisConfigs () error {
73101 // Load Byron genesis
74102 if c .ByronGenesisFile != "" {
@@ -127,6 +155,79 @@ func (c *CardanoNodeConfig) loadGenesisConfigs() error {
127155 return nil
128156}
129157
158+ // loadGenesisFromEmbedFS loads a single genesis file from the embedded filesystem.
159+ // It handles path resolution and file opening/closing automatically.
160+ func (c * CardanoNodeConfig ) loadGenesisFromEmbedFS (
161+ filename string ,
162+ ) (io.ReadCloser , error ) {
163+ if filename == "" {
164+ return nil , nil
165+ }
166+
167+ genesisPath := filename
168+ genesisPath = path .Join (c .path , genesisPath )
169+
170+ f , err := c .embedFS .Open (genesisPath )
171+ if err != nil {
172+ return nil , err
173+ }
174+ return f , nil
175+ }
176+
177+ // loadGenesisConfigsFromEmbed loads all genesis configuration files from the embedded filesystem.
178+ // This method mirrors loadGenesisConfigs but reads from embed.FS instead of the regular filesystem.
179+ func (c * CardanoNodeConfig ) loadGenesisConfigsFromEmbed () error {
180+ // Load Byron genesis
181+ if f , err := c .loadGenesisFromEmbedFS (c .ByronGenesisFile ); err != nil {
182+ return err
183+ } else if f != nil {
184+ defer f .Close ()
185+ byronGenesis , err := byron .NewByronGenesisFromReader (f )
186+ if err != nil {
187+ return err
188+ }
189+ c .byronGenesis = & byronGenesis
190+ }
191+
192+ // Load Shelley genesis
193+ if f , err := c .loadGenesisFromEmbedFS (c .ShelleyGenesisFile ); err != nil {
194+ return err
195+ } else if f != nil {
196+ defer f .Close ()
197+ shelleyGenesis , err := shelley .NewShelleyGenesisFromReader (f )
198+ if err != nil {
199+ return err
200+ }
201+ c .shelleyGenesis = & shelleyGenesis
202+ }
203+
204+ // Load Alonzo genesis
205+ if f , err := c .loadGenesisFromEmbedFS (c .AlonzoGenesisFile ); err != nil {
206+ return err
207+ } else if f != nil {
208+ defer f .Close ()
209+ alonzoGenesis , err := alonzo .NewAlonzoGenesisFromReader (f )
210+ if err != nil {
211+ return err
212+ }
213+ c .alonzoGenesis = & alonzoGenesis
214+ }
215+
216+ // Load Conway genesis
217+ if f , err := c .loadGenesisFromEmbedFS (c .ConwayGenesisFile ); err != nil {
218+ return err
219+ } else if f != nil {
220+ defer f .Close ()
221+ conwayGenesis , err := conway .NewConwayGenesisFromReader (f )
222+ if err != nil {
223+ return err
224+ }
225+ c .conwayGenesis = & conwayGenesis
226+ }
227+
228+ return nil
229+ }
230+
130231// ByronGenesis returns the Byron genesis config specified in the cardano-node config
131232func (c * CardanoNodeConfig ) ByronGenesis () * byron.ByronGenesis {
132233 return c .byronGenesis
0 commit comments