/
currency_register_process.go
175 lines (143 loc) · 4.14 KB
/
currency_register_process.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
170
171
172
173
174
175
package currency
import (
"sync"
"github.com/pkg/errors"
"github.com/spikeekips/mitum/base"
"github.com/spikeekips/mitum/base/key"
"github.com/spikeekips/mitum/base/operation"
"github.com/spikeekips/mitum/base/state"
"github.com/spikeekips/mitum/util/valuehash"
)
var currencyRegisterProcessorPool = sync.Pool{
New: func() interface{} {
return new(CurrencyRegisterProcessor)
},
}
func (CurrencyRegister) Process(
func(string) (state.State, bool, error),
func(valuehash.Hash, ...state.State) error,
) error {
// NOTE Process is nil func
return nil
}
type CurrencyRegisterProcessor struct {
CurrencyRegister
cp *CurrencyPool
pubs []key.Publickey
threshold base.Threshold
ga AmountState
de state.State
}
func NewCurrencyRegisterProcessor(cp *CurrencyPool, pubs []key.Publickey, threshold base.Threshold) GetNewProcessor {
return func(op state.Processor) (state.Processor, error) {
i, ok := op.(CurrencyRegister)
if !ok {
return nil, errors.Errorf("not CurrencyRegister, %T", op)
}
opp := currencyRegisterProcessorPool.Get().(*CurrencyRegisterProcessor)
opp.cp = cp
opp.CurrencyRegister = i
opp.pubs = pubs
opp.threshold = threshold
opp.ga = AmountState{}
opp.de = nil
return opp, nil
}
}
func (opp *CurrencyRegisterProcessor) PreProcess(
getState func(string) (state.State, bool, error),
_ func(valuehash.Hash, ...state.State) error,
) (state.Processor, error) {
if len(opp.pubs) < 1 {
return nil, operation.NewBaseReasonError("empty publickeys for operation signs")
} else if err := checkFactSignsByPubs(opp.pubs, opp.threshold, opp.Signs()); err != nil {
return nil, err
}
item := opp.Fact().(CurrencyRegisterFact).currency
if opp.cp != nil {
if opp.cp.Exists(item.Currency()) {
return nil, operation.NewBaseReasonError("currency already registered, %q", item.Currency())
}
}
if err := checkExistsState(StateKeyAccount(item.GenesisAccount()), getState); err != nil {
return nil, errors.Wrap(err, "genesis account not found")
}
if receiver := item.Policy().Feeer().Receiver(); receiver != nil {
if err := checkExistsState(StateKeyAccount(receiver), getState); err != nil {
return nil, errors.Wrap(err, "feeer receiver account not found")
}
}
switch st, found, err := getState(StateKeyCurrencyDesign(item.Currency())); {
case err != nil:
return nil, err
case found:
return nil, operation.NewBaseReasonError("currency already registered, %q", item.Currency())
default:
opp.de = st
}
switch st, found, err := getState(StateKeyBalance(item.GenesisAccount(), item.Currency())); {
case err != nil:
return nil, err
case found:
return nil, operation.NewBaseReasonError("genesis account has already the currency, %q", item.Currency())
default:
opp.ga = NewAmountState(st, item.Currency())
}
return opp, nil
}
func (opp *CurrencyRegisterProcessor) Process(
getState func(string) (state.State, bool, error),
setState func(valuehash.Hash, ...state.State) error,
) error {
fact := opp.Fact().(CurrencyRegisterFact)
sts := make([]state.State, 4)
sts[0] = opp.ga.Add(fact.currency.Big())
i, err := SetStateCurrencyDesignValue(opp.de, fact.currency)
if err != nil {
return err
}
sts[1] = i
{
l, err := createZeroAccount(fact.currency.Currency(), getState)
if err != nil {
return err
}
sts[2], sts[3] = l[0], l[1]
}
return setState(fact.Hash(), sts...)
}
func createZeroAccount(
cid CurrencyID,
getState func(string) (state.State, bool, error),
) ([]state.State, error) {
sts := make([]state.State, 2)
ac, err := ZeroAccount(cid)
if err != nil {
return nil, err
}
ast, err := notExistsState(StateKeyAccount(ac.Address()), "keys of zero account", getState)
if err != nil {
return nil, err
}
ast, err = SetStateAccountValue(ast, ac)
if err != nil {
return nil, err
}
sts[0] = ast
bst, _, err := getState(StateKeyBalance(ac.Address(), cid))
if err != nil {
return nil, err
}
amst := NewAmountState(bst, cid)
sts[1] = amst
return sts, nil
}
func (opp *CurrencyRegisterProcessor) Close() error {
opp.cp = nil
opp.pubs = nil
opp.threshold = base.Threshold{}
opp.ga = AmountState{}
opp.de = nil
currencyRegisterProcessorPool.Put(opp)
return nil
}