|
1 | 1 | package main
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "crypto/sha256" |
5 |
| - // "crypto/hmac" |
6 |
| - "fmt" |
7 |
| - "sort" |
8 |
| - // "encoding/binary" |
9 |
| - // "encoding/hex" |
10 |
| - // "time" |
11 |
| - // gjson "./lib/tidwall/gjson" |
12 |
| - "strconv" |
13 |
| - // "strings" |
14 |
| - // "math" |
| 4 | + "crypto/sha256" |
| 5 | + // "crypto/hmac" |
| 6 | + "fmt" |
| 7 | + "sort" |
| 8 | + |
| 9 | + // "encoding/binary" |
| 10 | + // "encoding/hex" |
| 11 | + // "time" |
| 12 | + // gjson "./lib/tidwall/gjson" |
| 13 | + "strconv" |
| 14 | + // "strings" |
| 15 | + // "math" |
15 | 16 | )
|
16 | 17 |
|
17 | 18 | // List of draw script
|
18 | 19 | const (
|
19 |
| - kFisherYatesShuffle = 1 |
20 |
| - kIndexHashing = 2 |
| 20 | + kFisherYatesShuffle = 1 |
| 21 | + kIndexHashing = 2 |
21 | 22 | )
|
22 | 23 |
|
23 | 24 | // This function determins winner list
|
24 | 25 | // Returns winner list, its encrypted version, nonce(might be used for verifiable randomkey)
|
25 |
| -func DoDetermineWinner(le lottery_event) ([]int) { |
26 |
| - var block Block |
27 |
| - var blockHash string |
28 |
| - var numOfParticipants int |
29 |
| - var numOfWinners int |
30 |
| - |
31 |
| - numOfParticipants, _ = strconv.Atoi(le.NumOfRegistered) |
32 |
| - numOfWinners, _ = strconv.Atoi(le.NumOfWinners) |
33 |
| - fmt.Printf("numOfParticipants:%d, numOfWinners:%d\n", numOfParticipants, numOfWinners) |
34 |
| - |
35 |
| - // 없으면 그냥 최신(디폴트) |
36 |
| - if le.FutureBlockHeight == "UNDEFINED" { |
37 |
| - fmt.Printf("FutureBlockHeight is UNDEFINED\nGetting latest block...\n") |
38 |
| - block = get_latest_block() |
39 |
| - blockHash = block.hash |
40 |
| - } |
41 |
| - |
42 |
| - if le.InputHash == "UNDEFINED" { |
43 |
| - panic("InputHash or FutureBlockHeight is missing\n") |
44 |
| - } |
45 |
| - |
46 |
| - // FTB: Future Target Block |
47 |
| - FTB, _ := strconv.ParseInt(le.FutureBlockHeight,10,64) |
48 |
| - block = get_block_by_height(FTB) |
49 |
| - |
50 |
| - blockHash = block.hash |
51 |
| - fmt.Printf("Block Number: %d\n", FTB) |
52 |
| - fmt.Printf("Block hash: %s\n", blockHash) |
53 |
| - |
54 |
| - if block.hash == "" { |
55 |
| - panic("Future block not published\nShutting down program") |
56 |
| - } |
57 |
| - |
58 |
| - |
59 |
| - if numOfParticipants == 0 { |
60 |
| - panic("Number of registered member should be greater than zero") |
61 |
| - } |
62 |
| - |
63 |
| - if numOfWinners > numOfParticipants { |
64 |
| - panic("Number of registered member should be greater than the number of winner") |
65 |
| - } |
66 |
| - |
67 |
| - // Canlculate winner lists |
68 |
| - // winnerIndexes := drawbyIndexHashing(numOfParticipants, numOfWinners, blockHash) |
69 |
| - winnerIndexes := drawByFisherYatesShuffle(numOfParticipants, numOfWinners, blockHash) |
70 |
| - |
71 |
| - return winnerIndexes |
| 26 | +func DoDetermineWinner(le lottery_event) ([]int, string) { |
| 27 | + var block Block |
| 28 | + var targetBlockHash string |
| 29 | + var numOfParticipants int |
| 30 | + var numOfWinners int |
| 31 | + |
| 32 | + numOfParticipants, _ = strconv.Atoi(le.NumOfRegistered) |
| 33 | + numOfWinners, _ = strconv.Atoi(le.NumOfWinners) |
| 34 | + fmt.Printf("numOfParticipants:%d, numOfWinners:%d\n", numOfParticipants, numOfWinners) |
| 35 | + |
| 36 | + // 없으면 그냥 최신(디폴트) |
| 37 | + if le.FutureBlockHeight == "UNDEFINED" { |
| 38 | + fmt.Printf("FutureBlockHeight is UNDEFINED\nGetting latest block...\n") |
| 39 | + block = get_latest_block() |
| 40 | + targetBlockHash = block.hash |
| 41 | + } |
| 42 | + |
| 43 | + if le.InputHash == "UNDEFINED" { |
| 44 | + panic("InputHash or FutureBlockHeight is missing\n") |
| 45 | + } |
| 46 | + |
| 47 | + // targetBlockHeight |
| 48 | + targetBlockHeight, _ := strconv.ParseInt(le.FutureBlockHeight, 10, 64) |
| 49 | + block = get_block_by_height(targetBlockHeight) |
| 50 | + |
| 51 | + targetBlockHash = block.hash |
| 52 | + fmt.Printf("Block Number: %d\n", targetBlockHeight) |
| 53 | + fmt.Printf("Block hash: %s\n", targetBlockHash) |
| 54 | + |
| 55 | + if block.hash == "" { |
| 56 | + panic("Future block not published\nShutting down program") |
| 57 | + } |
| 58 | + |
| 59 | + if numOfParticipants == 0 { |
| 60 | + panic("Number of registered member should be greater than zero") |
| 61 | + } |
| 62 | + |
| 63 | + if numOfWinners > numOfParticipants { |
| 64 | + panic("Number of registered member should be greater than the number of winner") |
| 65 | + } |
| 66 | + |
| 67 | + // Canlculate winner lists |
| 68 | + // winnerIndexes := drawbyIndexHashing(numOfParticipants, numOfWinners, targetBlockHash) |
| 69 | + winnerIndexes := drawByFisherYatesShuffle(numOfParticipants, numOfWinners, targetBlockHash) |
| 70 | + |
| 71 | + return winnerIndexes, targetBlockHash |
72 | 72 | }
|
73 | 73 |
|
74 | 74 | func gen_random_bit_array(random_key uint64) string {
|
75 |
| - random_bits_str := strconv.FormatUint(random_key, 2) |
76 |
| - return random_bits_str |
| 75 | + random_bits_str := strconv.FormatUint(random_key, 2) |
| 76 | + return random_bits_str |
77 | 77 | }
|
78 | 78 |
|
79 |
| -// Use random source + |
| 79 | +// Use random source + |
80 | 80 | func drawByIndexHashing(numOfParticipants int, numOfWinners int, randomSource string) []int {
|
81 |
| - var winner_list []int |
82 |
| - var winner_listNames string = "" |
83 |
| - var concat string = "" |
84 |
| - var lucky_map map[int]string |
85 |
| - lucky_map = make(map[int]string) |
86 |
| - |
87 |
| - |
88 |
| - for idx := 0; idx < numOfParticipants; idx++ { |
89 |
| - concat = randomSource + strconv.Itoa(idx) |
90 |
| - |
91 |
| - hash := sha256.New() |
92 |
| - hash.Write([]byte(concat)) |
93 |
| - index_hash := fmt.Sprintf("%x", hash.Sum(nil)) |
94 |
| - fmt.Printf("hash %d: %s\n", idx, concat) |
95 |
| - |
96 |
| - lucky_map[idx] = index_hash |
97 |
| - } |
98 |
| - |
99 |
| - // Sort by value. References follwing link |
100 |
| - // http://ispycode.com/GO/Sorting/Sort-map-by-value |
101 |
| - hack := make(map[string]int) |
102 |
| - hackkeys := []string{} |
103 |
| - |
104 |
| - for key, val := range lucky_map { |
105 |
| - hack[val]=key |
106 |
| - hackkeys = append(hackkeys, val) |
107 |
| - } |
108 |
| - |
109 |
| - sort.Strings(hackkeys) |
110 |
| - |
111 |
| - // print winners |
112 |
| - for i := 0; i < numOfWinners; i++ { |
113 |
| - fmt.Printf("%dth: %s\n", hack[hackkeys[i]], hackkeys[i]) |
114 |
| - winner_list = append(winner_list, hack[hackkeys[i]]) |
115 |
| - winner_listNames += hackkeys[i] + "," |
116 |
| - } |
117 |
| - |
118 |
| - return winner_list |
| 81 | + var winner_list []int |
| 82 | + var winner_listNames string = "" |
| 83 | + var concat string = "" |
| 84 | + var lucky_map map[int]string |
| 85 | + lucky_map = make(map[int]string) |
| 86 | + |
| 87 | + for idx := 0; idx < numOfParticipants; idx++ { |
| 88 | + concat = randomSource + strconv.Itoa(idx) |
| 89 | + |
| 90 | + hash := sha256.New() |
| 91 | + hash.Write([]byte(concat)) |
| 92 | + index_hash := fmt.Sprintf("%x", hash.Sum(nil)) |
| 93 | + fmt.Printf("hash %d: %s\n", idx, concat) |
| 94 | + |
| 95 | + lucky_map[idx] = index_hash |
| 96 | + } |
| 97 | + |
| 98 | + // Sort by value. References follwing link |
| 99 | + // http://ispycode.com/GO/Sorting/Sort-map-by-value |
| 100 | + hack := make(map[string]int) |
| 101 | + hackkeys := []string{} |
| 102 | + |
| 103 | + for key, val := range lucky_map { |
| 104 | + hack[val] = key |
| 105 | + hackkeys = append(hackkeys, val) |
| 106 | + } |
| 107 | + |
| 108 | + sort.Strings(hackkeys) |
| 109 | + |
| 110 | + // print winners |
| 111 | + for i := 0; i < numOfWinners; i++ { |
| 112 | + fmt.Printf("%dth: %s\n", hack[hackkeys[i]], hackkeys[i]) |
| 113 | + winner_list = append(winner_list, hack[hackkeys[i]]) |
| 114 | + winner_listNames += hackkeys[i] + "," |
| 115 | + } |
| 116 | + |
| 117 | + return winner_list |
119 | 118 | }
|
120 | 119 |
|
121 | 120 | func drawByFisherYatesShuffle(numOfParticipants int, numOfWinners int, randomSource string) []int {
|
122 |
| - var participants = make([]int, numOfParticipants) |
| 121 | + var participants = make([]int, numOfParticipants) |
123 | 122 |
|
124 |
| - for i := 0; i < numOfParticipants; i++ { |
125 |
| - participants[i] = i |
126 |
| - } |
| 123 | + for i := 0; i < numOfParticipants; i++ { |
| 124 | + participants[i] = i |
| 125 | + } |
127 | 126 |
|
128 |
| - for j := len(participants) - 1; j > 0; j-- { |
129 |
| - k := randomOracle(strconv.Itoa(j) + randomSource + strconv.Itoa(j)) % uint32(numOfParticipants) |
130 |
| - participants[j], participants[k] = participants[k], participants[j] |
131 |
| - } |
| 127 | + for j := len(participants) - 1; j > 0; j-- { |
| 128 | + k := randomOracle(strconv.Itoa(j)+randomSource+strconv.Itoa(j)) % uint32(numOfParticipants) |
| 129 | + participants[j], participants[k] = participants[k], participants[j] |
| 130 | + } |
132 | 131 |
|
133 |
| - var winnerList = make([]int, numOfWinners) |
| 132 | + var winnerList = make([]int, numOfWinners) |
134 | 133 |
|
135 |
| - for i := 0; i < numOfWinners; i++ { |
136 |
| - winnerList[i] = participants[i] |
137 |
| - logger.Debug("winnerList", winnerList[i]) |
138 |
| - } |
| 134 | + for i := 0; i < numOfWinners; i++ { |
| 135 | + winnerList[i] = participants[i] |
| 136 | + logger.Debug("winnerList", winnerList[i]) |
| 137 | + } |
139 | 138 |
|
140 |
| - return winnerList |
| 139 | + return winnerList |
141 | 140 | }
|
142 | 141 |
|
143 | 142 | func randomOracle(randomSource string) uint32 {
|
144 |
| - hash := sha256.New() |
145 |
| - hash.Write([]byte(randomSource)) |
146 |
| - expectedHash := hash.Sum(nil) |
| 143 | + hash := sha256.New() |
| 144 | + hash.Write([]byte(randomSource)) |
| 145 | + expectedHash := hash.Sum(nil) |
147 | 146 |
|
148 |
| - var random uint32 = uint32(expectedHash[0]) + uint32(expectedHash[1]) + uint32(expectedHash[2]) + uint32(expectedHash[3]) |
149 |
| - return random |
| 147 | + var random uint32 = uint32(expectedHash[0]) + uint32(expectedHash[1]) + uint32(expectedHash[2]) + uint32(expectedHash[3]) |
| 148 | + return random |
150 | 149 | }
|
0 commit comments