This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
presets.go
169 lines (147 loc) · 4.82 KB
/
presets.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package spec
import (
"sort"
"github.com/hyperledger/burrow/acm/balance"
"github.com/hyperledger/burrow/permission"
)
// Files here can be used as starting points for building various 'chain types' but are otherwise
// a fairly unprincipled collection of GenesisSpecs that we find useful in testing and development
func FullAccount(name string) GenesisSpec {
// Inheriting from the arbitrary figures used by monax tool for now
amount := uint64(99999999999999)
Power := uint64(9999999999)
return GenesisSpec{
Accounts: []TemplateAccount{{
Name: name,
Amounts: balance.New().Native(amount).Power(Power),
Permissions: []string{permission.AllString},
},
},
}
}
func RootAccount(name string) GenesisSpec {
// Inheriting from the arbitrary figures used by monax tool for now
amount := uint64(99999999999999)
return GenesisSpec{
Accounts: []TemplateAccount{{
Name: name,
Amounts: balance.New().Native(amount),
Permissions: []string{permission.AllString},
},
},
}
}
func ParticipantAccount(name string) GenesisSpec {
// Inheriting from the arbitrary figures used by monax tool for now
amount := uint64(9999999999)
return GenesisSpec{
Accounts: []TemplateAccount{{
Name: name,
Amounts: balance.New().Native(amount),
Permissions: []string{permission.SendString, permission.CallString, permission.NameString,
permission.HasRoleString, permission.ProposalString, permission.InputString},
}},
}
}
func DeveloperAccount(name string) GenesisSpec {
// Inheriting from the arbitrary figures used by monax tool for now
amount := uint64(9999999999)
return GenesisSpec{
Accounts: []TemplateAccount{{
Name: name,
Amounts: balance.New().Native(amount),
Permissions: []string{permission.SendString, permission.CallString, permission.CreateContractString,
permission.CreateAccountString, permission.NameString, permission.HasRoleString,
permission.RemoveRoleString, permission.ProposalString, permission.InputString},
}},
}
}
func ValidatorAccount(name string) GenesisSpec {
// Inheriting from the arbitrary figures used by monax tool for now
amount := uint64(9999999999)
Power := amount - 1
return GenesisSpec{
Accounts: []TemplateAccount{{
Name: name,
Amounts: balance.New().Native(amount).Power(Power),
Permissions: []string{permission.BondString},
}},
}
}
func MergeGenesisSpecs(genesisSpecs ...GenesisSpec) GenesisSpec {
mergedGenesisSpec := GenesisSpec{}
// We will deduplicate and merge global permissions flags
permSet := make(map[string]struct{})
for _, genesisSpec := range genesisSpecs {
// We'll overwrite chain name for later specs
if genesisSpec.ChainName != "" {
mergedGenesisSpec.ChainName = genesisSpec.ChainName
}
// Take the max genesis time
if mergedGenesisSpec.GenesisTime == nil ||
(genesisSpec.GenesisTime != nil && genesisSpec.GenesisTime.After(*mergedGenesisSpec.GenesisTime)) {
mergedGenesisSpec.GenesisTime = genesisSpec.GenesisTime
}
for _, permString := range genesisSpec.GlobalPermissions {
permSet[permString] = struct{}{}
}
mergedGenesisSpec.Salt = append(mergedGenesisSpec.Salt, genesisSpec.Salt...)
mergedGenesisSpec.Accounts = mergeAccounts(mergedGenesisSpec.Accounts, genesisSpec.Accounts)
}
mergedGenesisSpec.GlobalPermissions = make([]string, 0, len(permSet))
for permString := range permSet {
mergedGenesisSpec.GlobalPermissions = append(mergedGenesisSpec.GlobalPermissions, permString)
}
// Make sure merged GenesisSpec is deterministic on inputs
sort.Strings(mergedGenesisSpec.GlobalPermissions)
return mergedGenesisSpec
}
// Merge accounts by adding to base list or updating previously named account
func mergeAccounts(bases, overrides []TemplateAccount) []TemplateAccount {
indexOfBase := make(map[string]int, len(bases))
for i, ta := range bases {
if ta.Name != "" {
indexOfBase[ta.Name] = i
}
}
for _, override := range overrides {
if override.Name != "" {
if i, ok := indexOfBase[override.Name]; ok {
bases[i] = mergeAccount(bases[i], override)
continue
}
}
bases = append(bases, override)
}
return bases
}
func mergeAccount(base, override TemplateAccount) TemplateAccount {
if override.Address != nil {
base.Address = override.Address
}
if override.PublicKey != nil {
base.PublicKey = override.PublicKey
}
if override.Name != "" {
base.Name = override.Name
}
base.Amounts = base.Balances().Sum(override.Balances())
base.Permissions = mergeStrings(base.Permissions, override.Permissions)
base.Roles = mergeStrings(base.Roles, override.Roles)
return base
}
func mergeStrings(as, bs []string) []string {
var strs []string
strSet := make(map[string]struct{})
for _, a := range as {
strSet[a] = struct{}{}
}
for _, b := range bs {
strSet[b] = struct{}{}
}
for str := range strSet {
strs = append(strs, str)
}
sort.Strings(strs)
return strs
}