Class: Platon::Ppos

Inherits:
Object
  • Object
show all
Defined in:
lib/platon/ppos.rb

Constant Summary collapse

PARAMS_ORDER =
{
   "1000"=> ['typ', 'benefitAddress', 'nodeId', 'externalId', 'nodeName', 'website', 'details', 'amount', 'rewardPer', 'programVersion', 'programVersionSign', 'blsPubKey', 'blsProof'],
   "1001"=> ['benefitAddress', 'nodeId', 'rewardPer', 'externalId', 'nodeName', 'website', 'details'],
   "1002"=> ['nodeId', 'typ', 'amount'],
   "1003"=> ['nodeId'],
   "1004"=> ['typ', 'nodeId', 'amount'],
   "1005"=> ['stakingBlockNum', 'nodeId', 'amount'],
   "1100"=> [],
   "1101"=> [],
   "1102"=> [],
   "1103"=> ['addr'],
   "1104"=> ['stakingBlockNum', 'delAddr', 'nodeId'],
   "1105"=> ['nodeId'],
   "1200"=> [],
   "1201"=> [],
   "1202"=> [],

   "2000"=> ['verifier', 'pIDID'],
   "2001"=> ['verifier', 'pIDID', 'newVersion', 'endVotingRounds'],
   "2002"=> ['verifier', 'pIDID', 'module', 'name', 'newValue'],
   "2005"=> ['verifier', 'pIDID', 'endVotingRounds', 'tobeCanceledProposalID'],
   "2003"=> ['verifier', 'proposalID', 'option', 'programVersion', 'versionSign'],
   "2004"=> ['verifier', 'programVersion', 'versionSign'],
   "2100"=> ['proposalID'],
   "2101"=> ['proposalID'],
   "2102"=> [],
   "2103"=> [],
   "2104"=> ['module', 'name'],
   "2105"=> ['proposalID', 'blockHash'],
   "2106"=> ['module'],

   "3000"=> ['typ', 'data'],
   "3001"=> ['typ', 'addr', 'blockNumber'],

   "4000"=> ['account', 'plan'],
   "4100"=> ['account'],

   "5000"=> [],
   "5100"=> ['address', 'nodeIDs']
}

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Ppos

Returns a new instance of Ppos.



45
46
47
# File 'lib/platon/ppos.rb', line 45

def initialize(client)
	@client = client
end

Instance Method Details

#add_staking(key, node_id, typ, amount) ⇒ Object

(1002)



179
180
181
182
183
184
185
# File 'lib/platon/ppos.rb', line 179

def add_staking(key,node_id,typ,amount) # (1002)
	send key,[1002,
     Utils.hex_to_bin(node_id),
     typ,
     Platon::Formatter.new.to_von(amount)
   ]
end

#call(params, block_param = "latest") ⇒ Object

注意:个别参数需提前 hex_to_bin



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/platon/ppos.rb', line 103

def call(params,block_param="latest") ##注意:个别参数需提前 hex_to_bin
	begin
		rawTx = {}
		params = obj_to_params(params)

		rawTx["data"] = params_to_data(params)
		rawTx["to"] = funcType_to_bech32(@client.hrp,params[0])
		data = @client.platon_call(rawTx,block_param)

		return ppos_hex_to_obj(data)

	rescue Exception => e
		# puts "ppos call error :#{e}"
		puts e.backtrace
	end
end

#cancel_proposal(key, verifier, pIDID, end_voting_rounds, to_be_canceled_proposal_id, other = nil) ⇒ Object

提交取消提案



252
253
254
# File 'lib/platon/ppos.rb', line 252

def cancel_proposal(key,verifier,pIDID,end_voting_rounds,to_be_canceled_proposal_id,other=nil)#2005
	send key,[2005,Utils.hex_to_bin(verifier), pIDID, end_voting_rounds , to_be_canceled_proposal_id],other
end

#cancel_staking(key, node_id) ⇒ Object

撤销质押(一次性发起全部撤销,多次到账)



188
189
190
191
192
# File 'lib/platon/ppos.rb', line 188

def cancel_staking(key,node_id) #(1003)
	send key,[1003,
     Utils.hex_to_bin(node_id)
   ]
end

#create_restricting_plan(key, account, restricting_plans) ⇒ Object

创建锁仓计划 4000



311
312
313
314
315
316
# File 'lib/platon/ppos.rb', line 311

def create_restricting_plan(key,,restricting_plans)
  send key,[4000,
    Utils.bech32_to_bin(),
    restricting_plans
  ]
end

#create_staking(key, typ, benefit_address, node_id, external_id, node_name, website, details, amount, reward_per, program_version, program_version_sign, bls_pub_key, bls_proof) ⇒ Object

(1000)



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/platon/ppos.rb', line 149

def create_staking(key,typ,benefit_address,node_id,external_id,node_name,website,details,amount,reward_per,program_version,program_version_sign,bls_pub_key,bls_proof)  #(1000)
   send key,[1000,
       typ,
       Utils.bech32_to_bin(benefit_address),
       Utils.hex_to_bin(node_id),
       Utils.hex_to_bin(external_id),
       node_name,
       website,
       details,
       Platon::Formatter.new.to_von(amount),
       reward_per,
       program_version,  #client.admin_get_program_version 获取
       Utils.hex_to_bin(program_version_sign), #client.admin_get_program_version 获取
       Utils.hex_to_bin(bls_pub_key), 
       Utils.hex_to_bin(bls_proof)  ## client.admin_get_schnorr_nizk_prove获取
   ]
end

#declare_version(key, verifier, program_version, version_sign) ⇒ Object

版本声明



262
263
264
# File 'lib/platon/ppos.rb', line 262

def declare_version(key,verifier,program_version,version_sign) #2004
	send key,[2004,Utils.hex_to_bin(verifier),program_version,Utils.hex_to_bin(version_sign)],other
end

#delegate(key, typ, node_id, amount) ⇒ Object

发起委托,委托已质押节点,委托给某个节点增加节点权重来获取收入



195
196
197
# File 'lib/platon/ppos.rb', line 195

def delegate(key,typ,node_id,amount) #(1004)
	send key,[1004,typ,Utils.hex_to_bin(node_id),amount]
end

#funcType_to_address(funcType) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/platon/ppos.rb', line 64

def funcType_to_address(funcType)
 	case funcType
 	when 1000...2000
 		return "0x1000000000000000000000000000000000000002"
 	when 2000...3000
 		return "0x1000000000000000000000000000000000000005"
 	when 3000...4000
 		return "0x1000000000000000000000000000000000000004"
 	when 4000...5000
 		return "0x1000000000000000000000000000000000000001"
 	when 5000...6000
 		return "0x1000000000000000000000000000000000000006"
 	end
end

#funcType_to_bech32(hrp, funcType) ⇒ Object

TODO!!!



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/platon/ppos.rb', line 79

def funcType_to_bech32(hrp,funcType)  ### TODO!!!
	case funcType
 	when 1000...2000
 		return Utils.to_bech32_address(hrp,"0x1000000000000000000000000000000000000002")
 	when 2000...3000
 		return Utils.to_bech32_address(hrp,"0x1000000000000000000000000000000000000005")
 	when 3000...4000
 		return Utils.to_bech32_address(hrp,"0x1000000000000000000000000000000000000004")
 	when 4000...5000
 		return Utils.to_bech32_address(hrp,"0x1000000000000000000000000000000000000001")
 	when 5000...6000
 		return Utils.to_bech32_address(hrp,"0x1000000000000000000000000000000000000006")
 	end
end

#get_address_delegate_info(staking_block_num, address, node_id, block_param = "latest") ⇒ Object

查询单个委托信息



224
225
226
# File 'lib/platon/ppos.rb', line 224

def get_address_delegate_info(staking_block_num, address, node_id, block_param="latest")
	call [1104, staking_block_num, Utils.bech32_to_bin(address), Utils.hex_to_bin(node_id)],block_param
end

#get_block_reward(block_param = "latest") ⇒ Object



234
235
236
# File 'lib/platon/ppos.rb', line 234

def get_block_reward(block_param="latest")
  call [1200],block_param
end

#get_current_candidates(block_param = "latest") ⇒ Object

查询实时候选人列表,返回全部。包含当前验证人



215
216
217
# File 'lib/platon/ppos.rb', line 215

def get_current_candidates(block_param="latest") #(1102)
	call [1102],block_param
end

#get_delegate_nodeids_by_addr(address, block_param = "latest") ⇒ Object

(1103)



219
220
221
# File 'lib/platon/ppos.rb', line 219

def get_delegate_nodeids_by_addr(address,block_param="latest") #(1103)
	call [1103, Utils.bech32_to_bin(address)],block_param
end

#get_delegate_reward(address, node_ids = [], block_param = "latest") ⇒ Object

查询未提取的委托奖励



331
332
333
# File 'lib/platon/ppos.rb', line 331

def get_delegate_reward(address,node_ids=[],block_param="latest")
  call [5100,Utils.bech32_to_bin(address),node_ids.map{|item| Utils.hex_to_bin(item)}],block_param
end

#get_epoch_validators(block_param = "latest") ⇒ Object

查询当前结算周期的验证人队列,返回101个



205
206
207
# File 'lib/platon/ppos.rb', line 205

def get_epoch_validators(block_param="latest") #(1100)
	call [1100],block_param
end

#get_govern_param_value(module_name, name, block_param = "latest") ⇒ Object

查询当前块高的治理参数值



297
298
299
# File 'lib/platon/ppos.rb', line 297

def get_govern_param_value(module_name, name,block_param="latest")  #2104
  call [2104,module_name,name],block_param
end

#get_govern_params(module_name, block_param = "latest") ⇒ Object

查询治理参数列表



292
293
294
# File 'lib/platon/ppos.rb', line 292

def get_govern_params(module_name,block_param="latest")  # 2106
  call [2106, module_name],block_param
end

#get_node_delegate_info(node_id, block_param = "latest") ⇒ Object



229
230
231
232
# File 'lib/platon/ppos.rb', line 229

def get_node_delegate_info(node_id,block_param="latest")
	res = call([1105,Utils.hex_to_bin(node_id)],block_param)
	# format_res_values res,["Shares","Released","ReleasedHes","RestrictingPlan","RestrictingPlanHes","DelegateTotal","DelegateTotalHes","DelegateRewardTotal"]
end

#get_node_oversign(type, node_id, block_number, block_param = "latest") ⇒ Object

查询节点被举报情况



306
307
308
# File 'lib/platon/ppos.rb', line 306

def get_node_oversign(type,node_id,block_number,block_param="latest")  #3001
	call [3001,type,Utils.hex_to_bin(node_id),block_number],block_param
end

#get_proposal_info(proposal_id, block_param = "latest") ⇒ Object

提案查询



272
273
274
# File 'lib/platon/ppos.rb', line 272

def get_proposal_info(proposal_id,block_param="latest") #2100
	call [2100,Utils.hex_to_bin(proposal_id)],block_param
end

#get_proposal_result(proposal_id, block_param = "latest") ⇒ Object

查询提案结果



277
278
279
# File 'lib/platon/ppos.rb', line 277

def get_proposal_result(proposal_id,block_param="latest") #2101
	call [2101,Utils.hex_to_bin(proposal_id)],block_param
end

#get_proposals(block_param = "latest") ⇒ Object

查询提案列表



267
268
269
# File 'lib/platon/ppos.rb', line 267

def get_proposals(block_param="latest") #2102
	call [2102],block_param
end

#get_restricting_info(account, block_param = "latest") ⇒ Object

获取锁仓信息 4100



319
320
321
# File 'lib/platon/ppos.rb', line 319

def get_restricting_info(,block_param="latest")
  call [4100, Utils.bech32_to_bin()],block_param
end

#get_round_validators(block_param = "latest") ⇒ Object

查询当前共识周期的验证人队列,返回25个



210
211
212
# File 'lib/platon/ppos.rb', line 210

def get_round_validators(block_param="latest") #(1101)
	call [1101],block_param
end

#get_staking_reward(block_param = "latest") ⇒ Object



238
239
240
# File 'lib/platon/ppos.rb', line 238

def get_staking_reward(block_param="latest")
  call [1201],block_param
end

#get_version_in_effect(block_param = "latest") ⇒ Object

查询节点的链生效版本



282
283
284
# File 'lib/platon/ppos.rb', line 282

def get_version_in_effect(block_param="latest")  #2103
	call [2103],block_param
end

#get_votes_number(proposal_id, block_hash, block_param = "latest") ⇒ Object

查询提案的投票人数



287
288
289
# File 'lib/platon/ppos.rb', line 287

def get_votes_number(proposal_id,block_hash,block_param="latest")  #2105
	call [2105, Utils.hex_to_bin(proposal_id),Utils.hex_to_bin(block_hash)]
end

#obj_to_params(params) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/platon/ppos.rb', line 54

def obj_to_params(params)
	return params if params.instance_of?(Array)
	pars = [params["funcType"]]
	order = PARAMS_ORDER[params["funcType"].to_s]
	order.each do |key|
		pars << params[key]
	end
	return pars
end

#params_to_data(params) ⇒ Object

params should be Array



49
50
51
52
# File 'lib/platon/ppos.rb', line 49

def params_to_data(params)  #params should be Array
	arr =params.map{|param| RLP.encode(param)}
	rlpData = "0x"+RLP.encode(arr).unpack("H*").first
end

#ppos_hex_to_obj(hex_str) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/platon/ppos.rb', line 94

def ppos_hex_to_obj(hex_str)
	hex_str = hex_str.downcase.start_with?("0x") ? hex_str[2..-1] : hex_str
	str = JSON.parse([hex_str].pack("H*")) rescue ""
	if(str["Data"].instance_of?(String))
		str["Data"] = JSON.parse(str["Data"]) rescue ""
	end
	return str
end

#reduce_delegate(key, staking_block_num, node_id, amount) ⇒ Object

减持/撤销委托(全部减持就是撤销)



200
201
202
# File 'lib/platon/ppos.rb', line 200

def reduce_delegate(key,staking_block_num,node_id,amount) #(1005)
	send key,[1005,staking_block_num,Utils.hex_to_bin(node_id),amount]
end

#report_duplicate_sign(key, duplicate_sign_type, data, other = nil) ⇒ Object

举报双签



302
303
304
# File 'lib/platon/ppos.rb', line 302

def report_duplicate_sign(key,duplicate_sign_type,data,other=nil)#3000
	send key,[3000,duplicate_sign_type,data],other
end

#send(key, params, other = nil) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/platon/ppos.rb', line 120

def send(key,params,other=nil)
	raise ArgumentError, "key or chain_id is not exist." if key == nil || @client.chain_id == nil

   begin

     rawTx = {
       from:key.address,
       to: funcType_to_address(params[0]),
       data: params_to_data(obj_to_params(params)),
       nonce: @client.get_nonce(key.bech32_address(hrp:@client.hrp)),
       gas_limit: (other && other[:gas_limit])|| @client.gas_limit,
       gas_price: (other && other[:gas_price]) || @client.gas_price,
       chain_id: @client.chain_id,
       value:0
     }
     # p rawTx
     tx = Platon::Tx.new(rawTx)
     tmp = tx.sign key
     # p tmp
     @client.platon_send_raw_transaction(tx.hex)
  
   rescue Exception => e
     # puts "ppos send error :#{e}"
     puts e.backtrace
   end


end

#submit_proposal(key, verifier, pIDID, other = nil) ⇒ Object

提交文本提案



243
244
245
# File 'lib/platon/ppos.rb', line 243

def submit_proposal(key,verifier,pIDID,other=nil)#2000 
	send key,[2000,Utils.hex_to_bin(verifier),pIDID],other
end

#update_proposal(key, verifier, pIDID, new_version, end_voting_rounds, other = nil) ⇒ Object

提交升级提案



247
248
249
# File 'lib/platon/ppos.rb', line 247

def update_proposal(key, verifier,pIDID, new_version,end_voting_rounds,other=nil) #2001
	send key,[2001,Utils.hex_to_bin(verifier), pIDID, new_version,end_voting_rounds],other
end

#update_staking_info(key, benefit_address, node_id, reward_per, external_id, node_name, website, details) ⇒ Object

(1001)



167
168
169
170
171
172
173
174
175
176
177
# File 'lib/platon/ppos.rb', line 167

def update_staking_info(key,benefit_address,node_id,reward_per,external_id,node_name,website,details) #(1001)
	send key,[1001,
     Utils.bech32_to_bin(benefit_address),
     Utils.hex_to_bin(node_id),
     reward_per,
     Utils.hex_to_bin(external_id),
     node_name,
     website,
     details
   ]
end

#vote_proposal(key, verifier, proposal_id, option, program_version, version_sign, other = nil) ⇒ Object

给提案投票



257
258
259
# File 'lib/platon/ppos.rb', line 257

def vote_proposal(key,verifier,proposal_id,option,program_version,version_sign,other=nil)#2003
	send key,[2003,Utils.hex_to_bin(verifier),Utils.hex_to_bin(proposal_id),option,program_version,Utils.hex_to_bin(version_sign)],other
end

#withdraw_delegate_reward(key) ⇒ Object

奖励接口 提取委托奖励 5000



326
327
328
# File 'lib/platon/ppos.rb', line 326

def withdraw_delegate_reward(key)
  send key,[5000]
end