Skip to content

Commit c464cea

Browse files
committed
Prepared default demo
1 parent 955105d commit c464cea

File tree

8 files changed

+354
-277
lines changed

8 files changed

+354
-277
lines changed

app.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,18 @@ app.post('/channels/:channelName/chaincodes/:chaincodeName', async function(req,
330330
return;
331331
}
332332

333-
let message = await invoke.invokeChaincode(peers, channelName, chaincodeName, fcn, args, req.username, req.orgname);
333+
try {
334+
let message = await invoke.invokeChaincode(peers, channelName, chaincodeName, fcn, args, req.username, req.orgname);
335+
res.send(message);
336+
} catch (error) {
337+
var error_message = {};
338+
logger.error('Failed to invoke due to error: ' + error.stack ? error.stack : error);
339+
logger.error(error.toString());
340+
error_message.flag = 1;
341+
error_message.error_message = error.toString();
342+
res.send(error_message);
343+
}
334344
// console.log(message.payload_) ;
335-
res.send(message);
336345
});
337346
// Query on chaincode on target peers
338347
app.get('/channels/:channelName/chaincodes/:chaincodeName', async function(req, res) {

artifacts/src/github.com/lottery_cc/cc_subscribe.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ func (t *SimpleChaincode) subscribe(stub shim.ChaincodeStubInterface, args []str
7373
le.MemberList += "," + args[2]
7474
}
7575

76-
kRegisted, err := strconv.Atoi(le.NumOfRegistered)
77-
le.NumOfRegistered = strconv.Itoa(kRegisted + 1)
76+
// Remove redundant double-commas
77+
strings.Replace(le.MemberList, ",,", "", -1)
78+
kRegisted := strings.Count(le.MemberList, ",") + 1
79+
80+
// kRegisted, err := strconv.Atoi(le.NumOfRegistered)
81+
le.NumOfRegistered = strconv.Itoa(kRegisted)
7882

7983
logger.Info("NumOfRegistered: %s\n", le.NumOfRegistered)
8084

artifacts/src/github.com/lottery_cc/chaincode.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ type lottery_event struct {
5656
RandomKey string `json:"RandomKey"` // This is from input, so it's not non-deterministic
5757
InputHash string `json:"InputHash"` // built from eventname, duedate, num of members, winners and member list, randomkey from server
5858
FutureBlockHeight string `json:"FutureBlockHeight"`
59+
TargetBlockHash string `json:"TargetBlockHash"`
5960
WinnerList string `json:"WinnerList"` // comma seperated winner list
6061
Script string `json:"Script"` // script text for draw()
6162
VerifiableRandomkey string `json:"VerifiableRandomkey"`
@@ -81,6 +82,7 @@ func (l lottery_event) GetAllConcats() string {
8182
l.RandomKey +
8283
l.InputHash +
8384
l.FutureBlockHeight +
85+
l.TargetBlockHash +
8486
l.WinnerList +
8587
l.Script +
8688
l.LotteryNote +
@@ -93,6 +95,7 @@ func (l lottery_event) GetAllConcats() string {
9395
logger.Debug("RandomKey", l.RandomKey)
9496
logger.Debug("InputHash", l.InputHash)
9597
logger.Debug("targetBlock", l.FutureBlockHeight)
98+
logger.Debug("targetBlockHash", l.TargetBlockHash)
9699
logger.Debug("winnerList", l.WinnerList)
97100
logger.Debug("script", l.Script)
98101
logger.Debug("lotteryNote", l.LotteryNote)
@@ -592,7 +595,7 @@ func (t *SimpleChaincode) draw(stub shim.ChaincodeStubInterface, args []string)
592595

593596
// Get the actual winner list
594597
// winnerList, winnerListNames := DoDetermineWinner(le)
595-
winnerIndexes := DoDetermineWinner(le)
598+
winnerIndexes, targetBlockHash := DoDetermineWinner(le)
596599
participantArray := strings.Split(le.MemberList, ",")
597600
// winnerArray
598601

@@ -613,6 +616,8 @@ func (t *SimpleChaincode) draw(stub shim.ChaincodeStubInterface, args []string)
613616
le.WinnerList = winnerConcat
614617
logger.Info("WinnerList ", winnerConcat)
615618

619+
le.TargetBlockHash = targetBlockHash
620+
616621
/* logger.Info("Winners: %s\n", winnerConcat) */
617622
// logger.Info("Winners: %s\n", winnerConcat)
618623
// Get draw txid
Lines changed: 119 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,149 @@
11
package main
22

33
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"
1516
)
1617

1718
// List of draw script
1819
const (
19-
kFisherYatesShuffle = 1
20-
kIndexHashing = 2
20+
kFisherYatesShuffle = 1
21+
kIndexHashing = 2
2122
)
2223

2324
// This function determins winner list
2425
// 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
7272
}
7373

7474
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
7777
}
7878

79-
// Use random source +
79+
// Use random source +
8080
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
119118
}
120119

121120
func drawByFisherYatesShuffle(numOfParticipants int, numOfWinners int, randomSource string) []int {
122-
var participants = make([]int, numOfParticipants)
121+
var participants = make([]int, numOfParticipants)
123122

124-
for i := 0; i < numOfParticipants; i++ {
125-
participants[i] = i
126-
}
123+
for i := 0; i < numOfParticipants; i++ {
124+
participants[i] = i
125+
}
127126

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+
}
132131

133-
var winnerList = make([]int, numOfWinners)
132+
var winnerList = make([]int, numOfWinners)
134133

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+
}
139138

140-
return winnerList
139+
return winnerList
141140
}
142141

143142
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)
147146

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
150149
}

0 commit comments

Comments
 (0)