forked from PlatONnetwork/client-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpip.py
247 lines (228 loc) · 11 KB
/
pip.py
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
import json
import rlp
from client_sdk_python.module import (
Module,
)
from client_sdk_python.utils.transactions import send_obj_transaction, call_obj
def parse_data(raw_data):
data = str(raw_data, encoding="utf-8")
if data == "":
return ""
return json.loads(data)
class Pip(Module):
# If you want to get the result of the transaction, please set it to True,
# if you only want to get the transaction hash, please set it to False
need_analyze = True
def submitText(self, verifier, pip_id, pri_key, transaction_cfg=None):
"""
Submit a text proposal
:param verifier: The certified submitting the proposal
:param pip_id: PIPID
:param pri_key: Private key for transaction
:param transaction_cfg: Transaction basic configuration
type: dict
example:cfg = {
"gas":100000000,
"gasPrice":2000000000000,
"nonce":1,
}
:return: if is need analyze return transaction result dict
if is not need analyze return transaction hash
"""
data = rlp.encode([rlp.encode(int(2000)), rlp.encode(bytes.fromhex(verifier)), rlp.encode(pip_id)])
return send_obj_transaction(self, data, self.web3.pipAddress, pri_key, transaction_cfg)
def submitVersion(self, verifier, pip_id, new_version, end_voting_rounds, pri_key, transaction_cfg=None):
"""
Submit an upgrade proposal
:param verifier: The certified submitting the proposal
:param pip_id: PIPID
:param new_version: upgraded version
:param end_voting_rounds: The number of voting consensus rounds.
Explanation: Assume that the transaction submitted by the proposal is rounded when the consensus round
number of the package is packed into the block, then the proposal voting block is high,
which is the 230th block height of the round of the round1 + endVotingRounds
(assuming a consensus round out of block 250, ppos The list is 20 blocks high in advance,
250, 20 are configurable), where 0 < endVotingRounds <= 4840 (about 2 weeks, the actual discussion
can be calculated according to the configuration), and is an integer)
:param pri_key: Private key for transaction
:param transaction_cfg: Transaction basic configuration
type: dict
example:cfg = {
"gas":100000000,
"gasPrice":2000000000000,
"nonce":1,
}
:return: if is need analyze return transaction result dict
if is not need analyze return transaction hash
"""
data = rlp.encode([rlp.encode(int(2001)), rlp.encode(bytes.fromhex(verifier)), rlp.encode(pip_id),
rlp.encode(int(new_version)), rlp.encode(int(end_voting_rounds))])
return send_obj_transaction(self, data, self.web3.pipAddress, pri_key, transaction_cfg)
def submitParam(self, verifier, url, end_voting_block, param_name, current_value, new_value,
pri_key, transaction_cfg=None):
"""
todo fill
:param verifier:
:param url:
:param end_voting_block:
:param param_name:
:param current_value:
:param new_value:
:param pri_key: Private key for transaction
:param transaction_cfg: Transaction basic configuration
type: dict
example:cfg = {
"gas":100000000,
"gasPrice":2000000000000,
"nonce":1,
}
:return: if is need analyze return transaction result dict
if is not need analyze return transaction hash
"""
data = rlp.encode([rlp.encode(int(2002)), rlp.encode(bytes.fromhex(verifier)), rlp.encode(url), rlp.encode(int(end_voting_block)),
rlp.encode(param_name), rlp.encode(str(current_value)), rlp.encode(str(new_value))])
return send_obj_transaction(self, data, self.web3.pipAddress, pri_key, transaction_cfg)
def submitCancel(self, verifier, pip_id, end_voting_rounds, tobe_canceled_proposal_id, pri_key, transaction_cfg=None):
"""
Submit cancellation proposal
:param verifier: The certified submitting the proposal
:param pip_id: PIPID
:param end_voting_rounds:
The number of voting consensus rounds. Refer to the instructions for submitting the upgrade proposal.
At the same time, the value of this parameter in this interface
cannot be greater than the value in the corresponding upgrade proposal.
:param tobe_canceled_proposal_id: Upgrade proposal ID to be cancelled
:param pri_key: Private key for transaction
:param transaction_cfg: Transaction basic configuration
type: dict
example:cfg = {
"gas":100000000,
"gasPrice":2000000000000,
"nonce":1,
}
:return: if is need analyze return transaction result dict
if is not need analyze return transaction hash
"""
if tobe_canceled_proposal_id[:2] == '0x':
tobe_canceled_proposal_id = tobe_canceled_proposal_id[2:]
data = rlp.encode([rlp.encode(int(2005)), rlp.encode(bytes.fromhex(verifier)), rlp.encode(pip_id),
rlp.encode(int(end_voting_rounds)), rlp.encode(bytes.fromhex(tobe_canceled_proposal_id))])
return send_obj_transaction(self, data, self.web3.pipAddress, pri_key, transaction_cfg)
def vote(self, verifier, proposal_id, option, program_version, version_sign, pri_key, transaction_cfg=None):
"""
Vote for proposal
:param verifier: The certified submitting the proposal
:param proposal_id: Proposal ID
:param option: Voting option
:param program_version: Node code version, obtained by rpc getProgramVersion interface
:param version_sign: Code version signature, obtained by rpc getProgramVersion interface
:param pri_key: Private key for transaction
:param transaction_cfg: Transaction basic configuration
type: dict
example:cfg = {
"gas":100000000,
"gasPrice":2000000000000,
"nonce":1,
}
:return: if is need analyze return transaction result dict
if is not need analyze return transaction hash
"""
if proposal_id[:2] == '0x':
proposal_id = proposal_id[2:]
if version_sign[:2] == '0x':
version_sign = version_sign[2:]
data = rlp.encode([rlp.encode(int(2003)), rlp.encode(bytes.fromhex(verifier)), rlp.encode(bytes.fromhex(proposal_id)),
rlp.encode(option), rlp.encode(int(program_version)), rlp.encode(bytes.fromhex(version_sign))])
return send_obj_transaction(self, data, self.web3.pipAddress, pri_key, transaction_cfg)
def declareVersion(self, active_node, program_version, version_sign, pri_key, transaction_cfg=None):
"""
Version statement
:param active_node: The declared node can only be a verifier/candidate
:param program_version: The declared version, obtained by rpc's getProgramVersion interface
:param version_sign: The signed version signature, obtained by rpc's getProgramVersion interface
:param pri_key: Private key for transaction
:param transaction_cfg: Transaction basic configuration
type: dict
example:cfg = {
"gas":100000000,
"gasPrice":2000000000000,
"nonce":1,
}
:return: if is need analyze return transaction result dict
if is not need analyze return transaction hash
"""
if version_sign[0:2] == '0x':
version_sign = version_sign[2:]
data = rlp.encode([rlp.encode(int(2004)), rlp.encode(bytes.fromhex(active_node)), rlp.encode(int(program_version)),
rlp.encode(bytes.fromhex(version_sign))])
return send_obj_transaction(self, data, self.web3.pipAddress, pri_key, transaction_cfg)
def getProposal(self, proposal_id, from_address=None):
"""
Query proposal
:param proposal_id: proposal id
:param from_address: Used to call the rpc call method
:return:
todo fill
"""
if proposal_id[:2] == '0x':
proposal_id = proposal_id[2:]
data = rlp.encode([rlp.encode(int(2100)), rlp.encode(bytes.fromhex(proposal_id))])
return parse_data(call_obj(self, from_address, self.web3.pipAddress, data))
def getTallyResult(self, proposal_id, from_address=None):
"""
Query proposal results
:param proposal_id: proposal id
:param from_address: Used to call the rpc call method
:return:
todo fill
"""
if proposal_id[:2] == '0x':
proposal_id = proposal_id[2:]
data = rlp.encode([rlp.encode(int(2101)), rlp.encode(bytes.fromhex(proposal_id))])
return parse_data(call_obj(self, from_address, self.web3.pipAddress, data))
def getAccuVerifiersCount(self, proposal_id, block_hash, from_address=None):
"""
Query the cumulative number of votes for the proposal
:param proposal_id: proposal id
:param block_hash: block hash
:param from_address: Used to call the rpc call method
:return:
todo fill
"""
if proposal_id[:2] == '0x':
proposal_id = proposal_id[2:]
if block_hash[:2] == '0x':
block_hash = block_hash[2:]
data = rlp.encode(
[rlp.encode(int(2105)), rlp.encode(bytes.fromhex(proposal_id)), rlp.encode(bytes.fromhex(block_hash))])
return parse_data(call_obj(self, from_address, self.web3.pipAddress, data))
def listProposal(self, from_address=None):
"""
Query proposal list
:param from_address: Used to call the rpc call method
:return:
todo fill
"""
data = rlp.encode([rlp.encode(int(2102))])
return parse_data(call_obj(self, from_address, self.web3.pipAddress, data))
def getActiveVersion(self, from_address=None):
"""
Query the chain effective version of the node
:param from_address: Used to call the rpc call method
:return:
todo fill
"""
data = rlp.encode([rlp.encode(int(2103))])
return parse_data(call_obj(self, from_address, self.web3.pipAddress, data))
# def getProgramVersion(self, from_address=None):
# data = rlp.encode([rlp.encode(int(2104))])
# return parse_data(call_obj(self, from_address, self.web3.pipAddress, data))
def listParam(self, from_address=None):
"""
todo fill
:param from_address: Used to call the rpc call method
:return:
todo fill
"""
data = rlp.encode([rlp.encode(int(2105))])
return parse_data(call_obj(self, from_address, self.web3.pipAddress, data))