/
consortium.go
303 lines (263 loc) · 9.36 KB
/
consortium.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package poll
import (
"context"
"math/big"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/action"
"github.com/iotexproject/iotex-core/action/protocol"
"github.com/iotexproject/iotex-core/action/protocol/execution/evm"
"github.com/iotexproject/iotex-core/action/protocol/rolldpos"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/state"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/pkg/errors"
"go.uber.org/zap"
)
var (
consortiumCommitteeContractCreator = address.ZeroAddress
consortiumCommitteeContractNonce = uint64(0)
// this is a special execution that is not signed, set hash = hex-string of "consortiumCommitteeContractHash"
consortiumCommitteeContractHash, _ = hash.HexStringToHash256("00636f6e736f727469756d436f6d6d6974746565436f6e747261637448617368")
)
type contractReader interface {
Read(ctx context.Context, contract string, data []byte) ([]byte, error)
}
type contractReaderFunc func(context.Context, string, []byte) ([]byte, error)
func (f contractReaderFunc) Read(ctx context.Context, contract string, data []byte) ([]byte, error) {
return f(ctx, contract, data)
}
type consortiumCommittee struct {
contractReader contractReader
contract string
abi abi.ABI
bufferEpochNum uint64
bufferResult state.CandidateList
indexer *CandidateIndexer
addr address.Address
getBlockHash evm.GetBlockHash
}
// NewConsortiumCommittee creates a committee for consorium chain
func NewConsortiumCommittee(indexer *CandidateIndexer, readContract ReadContract, getBlockHash evm.GetBlockHash) (Protocol, error) {
abi, err := abi.JSON(strings.NewReader(ConsortiumManagementABI))
if err != nil {
return nil, err
}
if readContract == nil {
return nil, errors.New("failed to create consortium committee: empty read contract callback")
}
h := hash.Hash160b([]byte(protocolID))
addr, err := address.FromBytes(h[:])
if err != nil {
return nil, err
}
return &consortiumCommittee{
contractReader: genContractReaderFromReadContract(readContract, true),
abi: abi,
addr: addr,
indexer: indexer,
getBlockHash: getBlockHash,
}, nil
}
func (cc *consortiumCommittee) Start(ctx context.Context, sr protocol.StateReader) (interface{}, error) {
g := genesis.MustExtractGenesisContext(ctx)
if g.ConsortiumCommitteeContractCode == "" {
return nil, errors.New("cannot find consortium committee contract in gensis")
}
caller, _ := address.FromString(consortiumCommitteeContractCreator)
ethAddr := crypto.CreateAddress(common.BytesToAddress(caller.Bytes()), consortiumCommitteeContractNonce)
iotxAddr, _ := address.FromBytes(ethAddr.Bytes())
cc.contract = iotxAddr.String()
log.L().Info("Loaded consortium committee contract", zap.String("address", iotxAddr.String()))
return nil, nil
}
func (cc *consortiumCommittee) CreateGenesisStates(ctx context.Context, sm protocol.StateManager) error {
g := genesis.MustExtractGenesisContext(ctx)
blkCtx := protocol.MustGetBlockCtx(ctx)
blkCtx.Producer, _ = address.FromString(consortiumCommitteeContractCreator)
blkCtx.GasLimit = g.BlockGasLimit
bytes, err := hexutil.Decode(g.ConsortiumCommitteeContractCode)
if err != nil {
return err
}
execution, err := action.NewExecution(
"",
consortiumCommitteeContractNonce,
big.NewInt(0),
g.BlockGasLimit,
big.NewInt(0),
bytes,
)
if err != nil {
return err
}
actionCtx := protocol.ActionCtx{}
actionCtx.Caller, err = address.FromString(consortiumCommitteeContractCreator)
if err != nil {
return err
}
actionCtx.Nonce = consortiumCommitteeContractNonce
actionCtx.ActionHash = consortiumCommitteeContractHash
actionCtx.GasPrice = execution.GasPrice()
actionCtx.IntrinsicGas, err = execution.IntrinsicGas()
if err != nil {
return err
}
ctx = protocol.WithActionCtx(ctx, actionCtx)
ctx = protocol.WithBlockCtx(ctx, blkCtx)
// deploy consortiumCommittee contract
_, receipt, err := evm.ExecuteContract(
ctx,
sm,
execution,
func(height uint64) (hash.Hash256, error) {
return hash.ZeroHash256, nil
},
func(ctx context.Context, sm protocol.StateManager, amount *big.Int) (*action.TransactionLog, error) {
return nil, nil
},
)
if err != nil {
return err
}
if receipt.Status != uint64(iotextypes.ReceiptStatus_Success) {
return errors.Errorf("error when deploying consortiumCommittee contract, status=%d", receipt.Status)
}
cc.contract = receipt.ContractAddress
r := getContractReaderForGenesisStates(ctx, sm, cc.getBlockHash)
cands, err := cc.readDelegatesWithContractReader(ctx, r)
if err != nil {
return err
}
return setCandidates(ctx, sm, cc.indexer, cands, uint64(1))
}
func (cc *consortiumCommittee) CreatePreStates(ctx context.Context, sm protocol.StateManager) error {
return nil
}
func (cc *consortiumCommittee) CreatePostSystemActions(ctx context.Context, sr protocol.StateReader) ([]action.Envelope, error) {
return createPostSystemActions(ctx, sr, cc)
}
func (cc *consortiumCommittee) Handle(ctx context.Context, act action.Action, sm protocol.StateManager) (*action.Receipt, error) {
return handle(ctx, act, sm, cc.indexer, cc.addr.String())
}
func (cc *consortiumCommittee) Validate(ctx context.Context, act action.Action, sr protocol.StateReader) error {
return validate(ctx, sr, cc, act)
}
func (cc *consortiumCommittee) ReadState(
ctx context.Context,
sm protocol.StateReader,
method []byte,
args ...[]byte,
) ([]byte, uint64, error) {
return nil, uint64(0), nil
}
// Register registers the protocol with a unique ID
func (cc *consortiumCommittee) Register(r *protocol.Registry) error {
return r.Register(protocolID, cc)
}
// ForceRegister registers the protocol with a unique ID and force replacing the previous protocol if it exists
func (cc *consortiumCommittee) ForceRegister(r *protocol.Registry) error {
return r.ForceRegister(protocolID, cc)
}
// Name returns the name of protocol
func (cc *consortiumCommittee) Name() string {
return protocolID
}
func (cc *consortiumCommittee) CalculateCandidatesByHeight(ctx context.Context, _ protocol.StateReader, _ uint64) (state.CandidateList, error) {
return cc.readDelegates(ctx)
}
func (cc *consortiumCommittee) CalculateUnproductiveDelegates(
ctx context.Context,
sr protocol.StateReader,
) ([]string, error) {
return nil, nil
}
func (cc *consortiumCommittee) Delegates(ctx context.Context, _ protocol.StateReader) (state.CandidateList, error) {
return cc.readDelegates(ctx)
}
func (cc *consortiumCommittee) NextDelegates(ctx context.Context, _ protocol.StateReader) (state.CandidateList, error) {
return cc.readDelegates(ctx)
}
func (cc *consortiumCommittee) Candidates(ctx context.Context, _ protocol.StateReader) (state.CandidateList, error) {
return cc.readDelegates(ctx)
}
func (cc *consortiumCommittee) NextCandidates(ctx context.Context, _ protocol.StateReader) (state.CandidateList, error) {
return cc.readDelegates(ctx)
}
func (cc *consortiumCommittee) readDelegates(ctx context.Context) (state.CandidateList, error) {
return cc.readDelegatesWithContractReader(ctx, cc.contractReader)
}
func (cc *consortiumCommittee) readDelegatesWithContractReader(ctx context.Context, r contractReader) (state.CandidateList, error) {
bcCtx := protocol.MustGetBlockchainCtx(ctx)
rp := rolldpos.MustGetProtocol(protocol.MustGetRegistry(ctx))
epochNum := rp.GetEpochNum(bcCtx.Tip.Height)
if cc.bufferEpochNum == epochNum && cc.bufferResult != nil {
return cc.bufferResult, nil
}
data, err := cc.abi.Pack("delegates")
if err != nil {
return nil, err
}
data, err = r.Read(ctx, cc.contract, data)
if err != nil {
return nil, err
}
ret, err := cc.abi.Unpack("delegates", data)
if err != nil {
return nil, err
}
res, err := toEtherAddressSlice(ret[0])
if err != nil {
return nil, err
}
candidates := make([]*state.Candidate, 0, len(res))
for _, ethAddr := range res {
pkhash, err := hexutil.Decode(ethAddr.String())
if err != nil {
return nil, err
}
addr, err := address.FromBytes(pkhash)
if err != nil {
return nil, err
}
candidates = append(candidates, &state.Candidate{
Address: addr.String(),
Votes: big.NewInt(100),
})
}
cc.bufferEpochNum = epochNum
cc.bufferResult = candidates
return candidates, nil
}
func genContractReaderFromReadContract(r ReadContract, setting bool) contractReaderFunc {
return func(ctx context.Context, contract string, data []byte) ([]byte, error) {
return r(ctx, contract, data, setting)
}
}
func getContractReaderForGenesisStates(ctx context.Context, sm protocol.StateManager, getBlockHash evm.GetBlockHash) contractReaderFunc {
return func(ctx context.Context, contract string, data []byte) ([]byte, error) {
gasLimit := uint64(10000000)
ex, err := action.NewExecution(contract, 1, big.NewInt(0), gasLimit, big.NewInt(0), data)
if err != nil {
return nil, err
}
addr, err := address.FromString(address.ZeroAddress)
if err != nil {
return nil, err
}
res, _, err := evm.SimulateExecution(ctx, sm, addr, ex, getBlockHash)
return res, err
}
}
func toEtherAddressSlice(v interface{}) ([]common.Address, error) {
if addr, ok := v.([]common.Address); ok {
return addr, nil
}
return nil, ErrWrongData
}