-
Notifications
You must be signed in to change notification settings - Fork 6
/
genesis.go
57 lines (47 loc) · 1.38 KB
/
genesis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package nameservice
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
)
type GenesisState struct {
WhoisRecords []Whois `json:"whois_records"`
}
func NewGenesisState(whoIsRecords []Whois) GenesisState {
return GenesisState{WhoisRecords: nil}
}
func ValidateGenesis(data GenesisState) error {
for _, record := range data.WhoisRecords {
if record.Owner == nil {
return fmt.Errorf("invalid WhoisRecord: Value: %s. Error: Missing Owner", record.Value)
}
if record.Value == "" {
return fmt.Errorf("invalid WhoisRecord: Owner: %s. Error: Missing Value", record.Owner)
}
if record.Price == nil {
return fmt.Errorf("invalid WhoisRecord: Value: %s. Error: Missing Price", record.Value)
}
}
return nil
}
func DefaultGenesisState() GenesisState {
return GenesisState{
WhoisRecords: []Whois{},
}
}
func InitGenesis(ctx sdk.Context, keeper Keeper, data GenesisState) []abci.ValidatorUpdate {
for _, record := range data.WhoisRecords {
keeper.SetWhois(ctx, record.Value, record)
}
return []abci.ValidatorUpdate{}
}
func ExportGenesis(ctx sdk.Context, k Keeper) GenesisState {
var records []Whois
iterator := k.GetNamesIterator(ctx)
for ; iterator.Valid(); iterator.Next() {
name := string(iterator.Key())
whois := k.GetWhois(ctx, name)
records = append(records, whois)
}
return GenesisState{WhoisRecords: records}
}