MCPcopy
hub / github.com/canopy-network/canopy / ValidateGenesisState

Method ValidateGenesisState

fsm/genesis.go:105–158  ·  view source on GitHub ↗

ValidateGenesisState() validates a GenesisState object

(genesis *GenesisState)

Source from the content-addressed store, hash-verified

103
104// ValidateGenesisState() validates a GenesisState object
105func (s *StateMachine) ValidateGenesisState(genesis *GenesisState) (err lib.ErrorI) {
106 // ensure the governance params from the genesis object are valid
107 if err = genesis.Params.Check(); err != nil {
108 return
109 }
110 // for each validator, apply basic validations on the required fields
111 for _, val := range genesis.Validators {
112 // ensure the validator address is the proper length
113 if len(val.Address) != crypto.AddressSize {
114 return ErrAddressSize()
115 }
116 // ensure the validator public key is the proper length
117 if !val.Delegate && len(val.PublicKey) != crypto.BLS12381PubKeySize {
118 return ErrPublicKeySize()
119 }
120 // ensure the validator output address is the proper length
121 if len(val.Output) != crypto.AddressSize {
122 return ErrAddressSize()
123 }
124 }
125 // for each account, apply basic validations on the required fields
126 for _, account := range genesis.Accounts {
127 // ensure the account address has the proper size
128 if len(account.Address) != crypto.AddressSize {
129 return ErrAddressSize()
130 }
131 }
132 // if the order books aren't nil
133 if genesis.OrderBooks != nil {
134 // de-duplicate the committee order books
135 deDuplicateCommittees := lib.NewDeDuplicator[uint64]()
136 // for each order book in the list
137 for _, orderBook := range genesis.OrderBooks.OrderBooks {
138 // if already found
139 if found := deDuplicateCommittees.Found(orderBook.ChainId); found {
140 return InvalidSellOrder()
141 }
142 // if the book exists but there's no sell orders
143 if len(orderBook.Orders) == 0 {
144 return InvalidSellOrder()
145 }
146 // ensure there's no duplicate order-ids within the book
147 deDuplicateIds := lib.NewDeDuplicator[string]()
148 // for each order in the book
149 for _, order := range orderBook.Orders {
150 // check if order already found
151 if found := deDuplicateIds.Found(lib.BytesToString(order.Id)); found {
152 return InvalidSellOrder()
153 }
154 }
155 }
156 }
157 return
158}
159
160// ExportState() creates a GenesisState object from the current state
161func (s *StateMachine) ExportState() (genesis *GenesisState, err lib.ErrorI) {

Callers 2

ReadGenesisFromFileMethod · 0.95
TestValidateGenesisStateFunction · 0.80

Calls 6

BytesToStringFunction · 0.92
ErrAddressSizeFunction · 0.85
ErrPublicKeySizeFunction · 0.85
InvalidSellOrderFunction · 0.85
FoundMethod · 0.80
CheckMethod · 0.65

Tested by 1

TestValidateGenesisStateFunction · 0.64