-
Notifications
You must be signed in to change notification settings - Fork 22
/
validator.go
111 lines (98 loc) · 3.21 KB
/
validator.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
/**
* Copyright (c) 2018, 2019 National Digital ID COMPANY LIMITED
*
* This file is part of NDID software.
*
* NDID is the free software: you can redistribute it and/or modify it under
* the terms of the Affero GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or any later
* version.
*
* NDID is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Affero GNU General Public License for more details.
*
* You should have received a copy of the Affero GNU General Public License
* along with the NDID source code. If not, see https://www.gnu.org/licenses/agpl.txt.
*
* Please contact info@ndid.co.th for any further questions
*
*/
package app
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"strings"
"github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/ndidplatform/smart-contract/v7/abci/code"
)
const (
ValidatorSetChangePrefix string = "val:"
)
func isValidatorTx(tx []byte) bool {
return strings.HasPrefix(string(tx), ValidatorSetChangePrefix)
}
func (app *ABCIApplication) Validators() (validators []types.Validator) {
app.logger.Infof("Validators")
itr, err := app.state.db.Iterator(nil, nil)
if err != nil {
panic(err)
}
defer itr.Close()
for ; itr.Valid(); itr.Next() {
key := itr.Key()
validator := new(types.Validator)
err := types.ReadMessage(bytes.NewBuffer(key), validator)
if err != nil {
panic(err)
}
validators = append(validators, *validator)
}
return
}
// add, update, or remove a validator
func (app *ABCIApplication) updateValidator(v types.ValidatorUpdate) types.ResponseDeliverTx {
pubKeyBase64 := base64.StdEncoding.EncodeToString(v.PubKey.GetEd25519())
key := []byte("val:" + pubKeyBase64)
if v.Power == 0 {
// remove validator
hasKey, err := app.state.Has(key, false)
if err != nil {
panic(err)
}
if !hasKey {
return app.ReturnDeliverTxLog(code.Unauthorized, fmt.Sprintf("Cannot remove non-existent validator %X", key), "")
}
app.state.Delete(key)
} else {
// add or update validator
value := bytes.NewBuffer(make([]byte, 0))
if err := types.WriteMessage(&v, value); err != nil {
return app.ReturnDeliverTxLog(code.EncodingError, fmt.Sprintf("Error encoding validator: %v", err), "")
}
app.state.Set(key, value.Bytes())
}
app.valUpdates[pubKeyBase64] = v
return app.ReturnDeliverTxLog(code.OK, "success", "")
}
type SetValidatorParam struct {
PublicKey string `json:"public_key"`
Power int64 `json:"power"`
}
func (app *ABCIApplication) setValidator(param string, nodeID string) types.ResponseDeliverTx {
app.logger.Infof("SetValidator, Parameter: %s", param)
var funcParam SetValidatorParam
err := json.Unmarshal([]byte(param), &funcParam)
if err != nil {
return app.ReturnDeliverTxLog(code.UnmarshalError, err.Error(), "")
}
pubKey, err := base64.StdEncoding.DecodeString(string(funcParam.PublicKey))
if err != nil {
return app.ReturnDeliverTxLog(code.DecodingError, err.Error(), "")
}
return app.updateValidator(types.UpdateValidator(pubKey, funcParam.Power, ed25519.KeyType))
}