github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/core/ppos/ticket_state_test.go (about) 1 package pposm_test 2 3 // 4 /*func TestTicketProcess(t *testing.T) { 5 var ( 6 db = ethdb.NewMemDatabase() 7 genesis = new(core.Genesis).MustCommit(db) 8 ) 9 fmt.Println("genesis", genesis) 10 // Initialize a fresh chain with only a genesis block 11 ppos_storage.NewPPosTemp(db) 12 blockchain, _ := core.NewBlockChain(db, nil, params.AllEthashProtocolChanges, nil, vm.Config{}, nil) 13 14 configs := params.PposConfig{ 15 CandidateConfig: ¶ms.CandidateConfig{ 16 Threshold: "100", 17 DepositLimit: 10, 18 MaxChair: 1, 19 MaxCount: 3, 20 RefundBlockNumber: 1, 21 }, 22 TicketConfig: ¶ms.TicketConfig{ 23 TicketPrice: "1", 24 MaxCount: 10000, 25 ExpireBlockNumber: 4, 26 }, 27 } 28 29 candidatePoolContext := pposm.NewCandidatePoolContext(&configs) 30 31 ticketPoolContext := pposm.NewTicketPoolContext(&configs) 32 33 var state *state.StateDB 34 if statedb, err := blockchain.State(); nil != err { 35 fmt.Println("reference statedb failed", err) 36 } else { 37 state = statedb 38 } 39 40 candidate := &types.Candidate{ 41 Deposit: new(big.Int).SetUint64(100), 42 BlockNumber: new(big.Int).SetUint64(7), 43 CandidateId: discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"), 44 TxIndex: 6, 45 Host: "10.0.0.1", 46 Port: "8548", 47 Owner: common.HexToAddress("0x12"), 48 } 49 50 fmt.Println("seting new k-v \n", candidate) 51 // test SetCandidate 52 if err := candidatePoolContext.SetCandidate(state, candidate.CandidateId, candidate); nil != err { 53 fmt.Println("SetCandidate err:", err) 54 } 55 56 // set ownerList 57 ownerList := []common.Address{common.HexToAddress("0x20"), common.HexToAddress("0x21")} 58 var count uint32 = 0 59 var blockNumber = new(big.Int).SetUint64(10) 60 voteNum := 100 61 timeMap := make(map[uint32]int64) 62 var releaseTime int64 = 0 63 for i := 0; i < voteNum; i++ { 64 //go func() { 65 startTime := time.Now().UnixNano() / 1e6 66 voteOwner := ownerList[rand.Intn(2)] 67 deposit := new(big.Int).SetUint64(1) 68 var voteNum uint32 = 2 69 state.SubBalance(voteOwner, new(big.Int).Mul(deposit, new(big.Int).SetUint64(uint64(voteNum)))) 70 state.AddBalance(common.TicketPoolAddr, new(big.Int).Mul(deposit, new(big.Int).SetUint64(uint64(voteNum)))) 71 tempBlockNumber := new(big.Int).SetUint64(blockNumber.Uint64()) 72 if i < 2 { 73 tempBlockNumber.SetUint64(6) 74 t.Logf("vote blockNumber[%v]", tempBlockNumber.Uint64()) 75 } 76 77 txHash := common.Hash{} 78 txHash.SetBytes(crypto.Keccak256([]byte(strconv.Itoa(time.Now().Nanosecond() + i)))) 79 state.Prepare(txHash, common.Hash{}, 1) 80 81 _, err := ticketPoolContext.VoteTicket(state, voteOwner, voteNum, deposit, candidate.CandidateId, tempBlockNumber) 82 if nil != err { 83 fmt.Println("vote ticket error:", err) 84 } 85 if i == 1 { 86 var tempBlockNumber uint64 = 6 87 for i := 0; i < 4; i++ { 88 ticketPoolContext.Notify(state, new(big.Int).SetUint64(tempBlockNumber)) 89 tempBlockNumber++ 90 } 91 } 92 atomic.AddUint32(&count, 1) 93 timeMap[count] = (time.Now().UnixNano() / 1e6) - startTime 94 //}() 95 } 96 97 candidate = candidatePoolContext.GetCandidate(state, candidate.CandidateId, blockNumber) 98 99 100 ticketIds := ticketPoolContext.GetCandidateTicketIds(state, candidate.CandidateId) 101 ticketList := ticketPoolContext.GetTicketList(state, ticketIds) 102 t.Logf("ticketListSize=%d\n", len(ticketList)) 103 expireTicketIds := ticketPoolContext.GetExpireTicketIds(state, blockNumber) 104 //expireTickets, err := ticketPool.GetTicketList(state, expireTicketIds) 105 106 surplusQuantity := ticketPoolContext.GetPoolNumber(state) 107 epoch := ticketPoolContext.GetCandidateEpoch(state, candidate.CandidateId) 108 109 t.Logf("ticketPoolSize:[%d],expireTicketListSize:[%d],candidate.TicketPool:[%d],tcount:[%d],epoch:[%d]\n", 110 surplusQuantity, len(expireTicketIds), len(ticketIds), state.GetPPOSCache().GetCandidateTicketCount(candidate.CandidateId), epoch) 111 t.Logf("ticketPoolBalance[%v]", state.GetBalance(common.TicketPoolAddr)) 112 fmt.Println("------all ticket-----") 113 //for _, ticket := range ticketList { 114 // fmt.Printf("ticket:%+v,ticketId:[%v]\n", ticket, ticket.TicketId.Hex()) 115 //} 116 117 candidate = candidatePoolContext.GetCandidate(state, candidate.CandidateId, blockNumber) 118 ticketIds = ticketPoolContext.GetCandidateTicketIds(state, candidate.CandidateId) 119 120 121 if err := ticketPoolContext.Notify(state, blockNumber); err != nil { 122 t.Error("Execute HandleExpireTicket error", err) 123 } 124 expireTicketIds = ticketPoolContext.GetExpireTicketIds(state, blockNumber) 125 surplusQuantity = ticketPoolContext.GetPoolNumber(state) 126 epoch = ticketPoolContext.GetCandidateEpoch(state, candidate.CandidateId) 127 t.Logf("ticketPoolSize:[%d],expireTicketListSize:[%d],candidate.TicketPool:[%d],tcount:[%d],epoch:[%d]\n", 128 surplusQuantity, len(expireTicketIds), len(ticketIds), ticketPoolContext.GetCandidateTicketCount(state, candidate.CandidateId), epoch) 129 t.Logf("ticketPoolBalance[%v]", state.GetBalance(common.TicketPoolAddr)) 130 131 blockHash := common.Hash{} 132 blockHash.SetBytes([]byte("3b41e0aee38c1a1f959a6aaae678d86f1e6af59617d2f667bb2ef5527779c861")) 133 luckyTicketId, err := ticketPoolContext.SelectionLuckyTicket(state, candidate.CandidateId, blockHash) 134 if nil != err { 135 t.Error("SelectionLuckyTicket error", err) 136 } 137 //selectedTicketIndex := rand.Intn(len(ticketList)) 138 //selectedTicketId := ticketList[selectedTicketIndex].TicketId 139 t.Logf("-----------Start releasing a ticket 【%v】-----------\n", luckyTicketId.Hex()) 140 tempTime := time.Now().UnixNano() / 1e6 141 err = ticketPoolContext.ReturnTicket(state, candidate.CandidateId, luckyTicketId, blockNumber) 142 if nil != err { 143 t.Error("ReleaseSelectedTicket error", err) 144 } 145 releaseTime = (time.Now().UnixNano() / 1e6) - tempTime 146 ticket := ticketPoolContext.GetTicket(state, luckyTicketId) 147 t.Logf("lucky ticket :%+v", ticket) 148 149 expireTicketIds = ticketPoolContext.GetExpireTicketIds(state, blockNumber) 150 surplusQuantity = ticketPoolContext.GetPoolNumber(state) 151 epoch = ticketPoolContext.GetCandidateEpoch(state, candidate.CandidateId) 152 t.Logf("After processing the expired ticket block height:[%d]", blockNumber) 153 t.Logf("ticketPoolSize:[%d],expireTicketListSize:[%d],candidate.TicketPool:[%d],tcount:[%d],epoch:[%d]\n", 154 surplusQuantity, len(expireTicketIds), len(ticketIds), ticketPoolContext.GetCandidateTicketCount(state, candidate.CandidateId), epoch) 155 t.Log("expireTicketList info", "blockNumber", ) 156 t.Logf("ticketPoolBalance[%v]", state.GetBalance(common.TicketPoolAddr)) 157 158 fmt.Println("It takes time to release a vote:", releaseTime, "ms") 159 fmt.Println("When the 10,000th ticket is voted, it takes time:", timeMap[10000], "ms") 160 fmt.Println("When the 5000th ticket is used, the voting takes time:", timeMap[5000], "ms") 161 fmt.Println("When the 1000th ticket is taken, the time taken for voting:", timeMap[1000], "ms") 162 fmt.Println("When the 500th ticket is taken, the time taken for voting:", timeMap[500], "ms") 163 fmt.Println("When the 100th ticket is taken, the time taken for voting:", timeMap[100], "ms") 164 fmt.Println("When voting for the 50th ticket, it takes time to vote.:", timeMap[50], "ms") 165 fmt.Println("When the 10th ticket is taken, the voting takes time:", timeMap[10], "ms") 166 fmt.Println("When the first ticket is taken, the time taken for voting:", timeMap[1], "ms") 167 }*/ 168 // 169 //func initParam() (*state.StateDB, *pposm.CandidatePoolContext, *pposm.TicketPoolContext) { 170 // var ( 171 // db = ethdb.NewMemDatabase() 172 // genesis = new(core.Genesis).MustCommit(db) 173 // ) 174 // fmt.Println("genesis", genesis) 175 // // Initialize a fresh chain with only a genesis block 176 // blockchain, _ := core.NewBlockChain(db, nil, params.AllEthashProtocolChanges, nil, vm.Config{}, nil) 177 // ticketcache.NewTicketIdsCache(db) 178 // configs := params.PposConfig{ 179 // CandidateConfig: ¶ms.CandidateConfig{ 180 // Threshold: "100", 181 // DepositLimit: 10, 182 // MaxChair: 1, 183 // MaxCount: 3, 184 // RefundBlockNumber: 1, 185 // }, 186 // TicketConfig: ¶ms.TicketConfig{ 187 // TicketPrice: "1", 188 // MaxCount: 10000, 189 // ExpireBlockNumber: 100, 190 // }, 191 // } 192 // 193 // candidatePoolContextContext := pposm.NewCandidatePoolContext(&configs) 194 // 195 // ticketPoolContext := pposm.NewTicketPoolContext(&configs) 196 // 197 // var state *state.StateDB 198 // if statedb, err := blockchain.State(); nil != err { 199 // fmt.Println("reference statedb failed", "err", err) 200 // } else { 201 // state = statedb 202 // } 203 // txHash := common.Hash{} 204 // txHash.SetBytes(crypto.Keccak256([]byte(strconv.Itoa(time.Now().Second())))) 205 // state.Prepare(txHash, common.Hash{}, 1) 206 // return state, candidatePoolContextContext, ticketPoolContext 207 //} 208 // 209 //func TestTicketPool_VoteTicket(t *testing.T) { 210 // state, candidatePoolContext, ticketPool := initParam() 211 // 212 // candidate := &types.Candidate{ 213 // Deposit: new(big.Int).SetUint64(100), 214 // BlockNumber: new(big.Int).SetUint64(7), 215 // CandidateId: discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"), 216 // TxIndex: 6, 217 // Host: "10.0.0.1", 218 // Port: "8548", 219 // Owner: common.HexToAddress("0x12"), 220 // } 221 // 222 // if err := candidatePoolContext.SetCandidate(state, candidate.CandidateId, candidate); nil != err { 223 // t.Error("SetCandidate fail", "err", err) 224 // } 225 // 226 // successNumber, err := ticketPool.VoteTicket(state, common.HexToAddress("0x20"), 10, new(big.Int).SetUint64(100), candidate.CandidateId, new(big.Int).SetUint64(10)) 227 // if nil != err { 228 // t.Error("vote ticket fail", "err", err) 229 // } else { 230 // t.Log("vote ticket success", "successNumber", successNumber) 231 // } 232 //} 233 // 234 ///*func TestTicketPool_GetExpireTicketIds(t *testing.T) { 235 // state, _, ticketPool := initParam() 236 // 237 // list := ticketPool.GetExpireTicketIds(state, new(big.Int).SetUint64(10)) 238 // t.Logf("Execute TestTicketPool_GetExpireTicketIds success, listSize=%d", len(list)) 239 //}*/ 240 // 241 //func TestTicketPool_GetTicketList(t *testing.T) { 242 // state, candidatePoolContext, ticketPool := initParam() 243 // 244 // candidate := &types.Candidate{ 245 // Deposit: new(big.Int).SetUint64(100), 246 // BlockNumber: new(big.Int).SetUint64(7), 247 // CandidateId: discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"), 248 // TxIndex: 6, 249 // Host: "10.0.0.1", 250 // Port: "8548", 251 // Owner: common.HexToAddress("0x12"), 252 // } 253 // 254 // if err := candidatePoolContext.SetCandidate(state, candidate.CandidateId, candidate); nil != err { 255 // t.Error("SetCandidate fail", "err", err) 256 // } 257 // 258 // _, err := ticketPool.VoteTicket(state, common.HexToAddress("0x20"), 10, new(big.Int).SetUint64(100), candidate.CandidateId, new(big.Int).SetUint64(10)) 259 // if nil != err { 260 // t.Error("vote ticket fail", "err", err) 261 // } 262 // 263 // list := ticketPool.GetCandidateTicketIds(state, candidate.CandidateId) 264 // t.Logf("Execute TestTicketPool_GetTicketList success, list=%d", len(list)) 265 //} 266 // 267 //func TestTicketPool_GetTicket(t *testing.T) { 268 // state, candidatePoolContext, ticketPool := initParam() 269 // 270 // candidate := &types.Candidate{ 271 // Deposit: new(big.Int).SetUint64(100), 272 // BlockNumber: new(big.Int).SetUint64(7), 273 // CandidateId: discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"), 274 // TxIndex: 6, 275 // Host: "10.0.0.1", 276 // Port: "8548", 277 // Owner: common.HexToAddress("0x12"), 278 // } 279 // 280 // if err := candidatePoolContext.SetCandidate(state, candidate.CandidateId, candidate); nil != err { 281 // t.Error("SetCandidate fail", "err", err) 282 // } 283 // 284 // _, err := ticketPool.VoteTicket(state, common.HexToAddress("0x20"), 10, new(big.Int).SetUint64(100), candidate.CandidateId, new(big.Int).SetUint64(10)) 285 // if nil != err { 286 // t.Error("vote ticket fail", "err", err) 287 // } 288 // 289 // ticket := ticketPool.GetTicket(state, state.TxHash()) 290 // if ticket == nil { 291 // t.Error("getTicket fail", "err", err) 292 // } 293 //} 294 // 295 //func TestTicketPool_DropReturnTicket(t *testing.T) { 296 // state, candidatePoolContext, ticketPool := initParam() 297 // 298 // candidate := &types.Candidate{ 299 // Deposit: new(big.Int).SetUint64(100), 300 // BlockNumber: new(big.Int).SetUint64(7), 301 // CandidateId: discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"), 302 // TxIndex: 6, 303 // Host: "10.0.0.1", 304 // Port: "8548", 305 // Owner: common.HexToAddress("0x12"), 306 // } 307 // 308 // if err := candidatePoolContext.SetCandidate(state, candidate.CandidateId, candidate); nil != err { 309 // t.Error("SetCandidate fail", "err", err) 310 // } 311 // 312 // _, err := ticketPool.VoteTicket(state, common.HexToAddress("0x20"), 10, new(big.Int).SetUint64(100), candidate.CandidateId, new(big.Int).SetUint64(6)) 313 // if nil != err { 314 // t.Error("vote ticket fail", "err", err) 315 // } 316 // 317 // err = ticketPool.DropReturnTicket(state, candidate.BlockNumber, candidate.CandidateId) 318 // if nil != err { 319 // t.Error("dropReturnTicket fail", "err", err) 320 // } 321 //} 322 // 323 //func TestTicketPool_ReturnTicket(t *testing.T) { 324 // state, candidatePoolContext, ticketPool := initParam() 325 // 326 // candidate := &types.Candidate{ 327 // Deposit: new(big.Int).SetUint64(100), 328 // BlockNumber: new(big.Int).SetUint64(7), 329 // CandidateId: discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"), 330 // TxIndex: 6, 331 // Host: "10.0.0.1", 332 // Port: "8548", 333 // Owner: common.HexToAddress("0x12"), 334 // } 335 // 336 // if err := candidatePoolContext.SetCandidate(state, candidate.CandidateId, candidate); nil != err { 337 // t.Error("SetCandidate fail", "err", err) 338 // } 339 // 340 // _, err := ticketPool.VoteTicket(state, common.HexToAddress("0x20"), 10, new(big.Int).SetUint64(100), candidate.CandidateId, new(big.Int).SetUint64(10)) 341 // if nil != err { 342 // t.Error("vote ticket fail", "err", err) 343 // } 344 // 345 // err = ticketPool.ReturnTicket(state, candidate.CandidateId, state.TxHash(), new(big.Int).SetUint64(10)) 346 // if nil != err { 347 // t.Error("returnTicket fail", "err", err) 348 // } 349 //} 350 // 351 //func TestTicketPool_Notify(t *testing.T) { 352 // state, candidatePoolContext, ticketPool := initParam() 353 // 354 // candidate := &types.Candidate{ 355 // Deposit: new(big.Int).SetUint64(100), 356 // BlockNumber: new(big.Int).SetUint64(7), 357 // CandidateId: discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"), 358 // TxIndex: 6, 359 // Host: "10.0.0.1", 360 // Port: "8548", 361 // Owner: common.HexToAddress("0x12"), 362 // } 363 // 364 // if err := candidatePoolContext.SetCandidate(state, candidate.CandidateId, candidate); nil != err { 365 // t.Error("SetCandidate fail", "err", err) 366 // } 367 // 368 // _, err := ticketPool.VoteTicket(state, common.HexToAddress("0x20"), 10, new(big.Int).SetUint64(100), candidate.CandidateId, new(big.Int).SetUint64(6)) 369 // if nil != err { 370 // t.Error("vote ticket fail", "err", err) 371 // } 372 // 373 // err = ticketPool.Notify(state, new(big.Int).SetUint64(10)) 374 // if nil != err { 375 // t.Error("notify fail", "err", err) 376 // } 377 //} 378 // 379 //func TestTicketPool_SelectionLuckyTicket(t *testing.T) { 380 // state, candidatePoolContext, ticketPool := initParam() 381 // 382 // candidate := &types.Candidate{ 383 // Deposit: new(big.Int).SetUint64(100), 384 // BlockNumber: new(big.Int).SetUint64(7), 385 // CandidateId: discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345"), 386 // TxIndex: 6, 387 // Host: "10.0.0.1", 388 // Port: "8548", 389 // Owner: common.HexToAddress("0x12"), 390 // } 391 // 392 // if err := candidatePoolContext.SetCandidate(state, candidate.CandidateId, candidate); nil != err { 393 // t.Error("SetCandidate fail", "err", err) 394 // } 395 // 396 // _, err := ticketPool.VoteTicket(state, common.HexToAddress("0x20"), 10, new(big.Int).SetUint64(100), candidate.CandidateId, new(big.Int).SetUint64(6)) 397 // if nil != err { 398 // t.Error("vote ticket fail", "err", err) 399 // } 400 // 401 // blockHash := common.Hash{} 402 // blockHash.SetBytes([]byte("3b41e0aee38c1a1f959a6aaae678d86f1e6af59617d2f667bb2ef5527779c861")) 403 // _, err = ticketPool.SelectionLuckyTicket(state, candidate.CandidateId, blockHash) 404 // if nil != err { 405 // t.Error("selectionLuckyTicket fail", "err", err) 406 // } 407 //} 408 // 409 //func TestTicketPool_GetPoolNumber(t *testing.T) { 410 // state, _, ticketPool := initParam() 411 // 412 // sum := ticketPool.GetPoolNumber(state) 413 // t.Log("getPoolNumber success", "sum", sum) 414 //} 415 // 416 //func TestTicketPool_GetCandidateTicketIds(t *testing.T) { 417 // state, _, ticketPool := initParam() 418 // 419 // list := ticketPool.GetCandidateTicketIds(state, discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")) 420 // t.Logf("Execute TestTicketPool_GetCandidateTicketIds success, listSize=%d", len(list)) 421 //} 422 // 423 //func TestTicketPool_GetCandidateEpoch(t *testing.T) { 424 // state, _, ticketPool := initParam() 425 // 426 // epoch := ticketPool.GetCandidateEpoch(state, discover.MustHexID("0x01234567890121345678901123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345")) 427 // t.Logf("Execute TestTicketPool_GetCandidateTicketIds success, epoch=%d", epoch) 428 //} 429 // 430 //func TestTicketPool_GetTicketPrice(t *testing.T) { 431 // state, _, ticketPool := initParam() 432 // 433 // price := ticketPool.GetTicketPrice(state) 434 // if price.Cmp(big.NewInt(0)) <= 0 { 435 // t.Error("getTicketPrice fail") 436 // } 437 //}