示例#1
0
func main() {
	var err error

	const numArgs = 2
	if len(os.Args) != numArgs {
		fmt.Println("Usage: ./miner <hostport>")
		return
	}

	hostport := os.Args[1]
	fmt.Println(hostport)
	params := lsp.NewParams()
	lspClient, err = lsp.NewClient(hostport, params)
	if err != nil {
		fmt.Println("cannot craete client or connect to the server")
		return
	}

	// send Join message to the server
	joinMsg := bitcoin.NewJoin()
	err = sendMessage(joinMsg)
	if err != nil {
		fmt.Println(err)
		return
	}

	for {
		buf, err := lspClient.Read()
		if err != nil {
			fmt.Println("connection lost when reading from server")
			return
		}

		reqMsg := &bitcoin.Message{}
		err = json.Unmarshal(buf, reqMsg)
		if err != nil {
			fmt.Println("error when unmarshalling message")
			return
		}

		// calculate the minhash and nonce and send the result back to server
		fmt.Println("calculating... ", reqMsg.Data, reqMsg.Lower, reqMsg.Upper)
		hash, nonce := calculateMinHash(reqMsg.Data, reqMsg.Lower, reqMsg.Upper)
		resultMsg := bitcoin.NewResult(hash, nonce)
		fmt.Println("sending back result: ", hash, nonce)
		sendMessage(resultMsg)
	}
}
示例#2
0
文件: miner.go 项目: more-free/p1
func (m *Miner) handleRequest(req *bitcoin.Message) {
	go func() {
		nonce := req.Lower
		minHash := bitcoin.Hash(req.Data, nonce)

		for n := req.Lower + 1; n <= req.Upper; n++ {
			hash := bitcoin.Hash(req.Data, n)
			if hash < minHash {
				minHash = hash
				nonce = n
			}
		}

		m.write(bitcoin.NewResult(minHash, nonce))
	}()
}
func main() {
	const numArgs = 2
	if len(os.Args) != numArgs {
		fmt.Println("Usage: ./miner <hostport>")
		return
	}

	c, neterr := lsp.NewClient(os.Args[1], lsp.NewParams())
	if neterr != nil {
		fmt.Println(neterr)
	}
	join := bitcoin.NewJoin()
	buf, _ := json.Marshal(join)

	err := c.Write(buf)
	if err != nil {
		c.Close()
	}

	for {
		buf, err := c.Read()
		if err != nil {
			c.Close()
			return
		}

		var request bitcoin.Message
		json.Unmarshal(buf, &request)
		var minValue uint64 = math.MaxUint64
		var minIndex uint64
		for i := request.Lower; i <= request.Upper; i++ {
			res := bitcoin.Hash(request.Data, i)
			if res < minValue {
				minValue = res
				minIndex = i
			}
		}

		result := bitcoin.NewResult(minValue, minIndex)
		buffer, _ := json.Marshal(result)
		error := c.Write(buffer)
		if error != nil {
			c.Close()
			return
		}
	}
}
示例#4
0
func NewReducer() Reducer {
	return func(partial []*TaskPartialResult, id RequestID) *TaskResult {
		hash := partial[0].PartialResult.Hash
		nonce := partial[0].PartialResult.Nonce
		for _, pr := range partial {
			if pr.PartialResult.Hash < hash {
				hash = pr.PartialResult.Hash
				nonce = pr.PartialResult.Nonce
			}
		}

		return &TaskResult{
			ID:     id,
			Result: bitcoin.NewResult(hash, nonce),
		}
	}
}
示例#5
0
文件: server.go 项目: wdbaruni/dbm
func main() {
	const numArgs = 2
	if len(os.Args) != numArgs {
		fmt.Println("Usage: ./server <port>")
		return
	}
	file, err := os.OpenFile(name, flag, perm)
	if err != nil {
		return
	}
	mlx := log.New(file, "", log.Lshortfile|log.Lmicroseconds)
	port, _ := strconv.Atoi(os.Args[1])
	s, err := lsp.NewServer(port, lsp.NewParams())

	if err != nil {
		mlx.Println("cant start server in the port")
		file.Close()
		return
	}
	yy := NewScheduler()
	for {
		select {
		case <-yy.waitans:
			if yy.jobgiveup == false {
				res := bitcoin.NewResult(yy.ans1, yy.ans2)
				buf, _ := json.Marshal(res)
				mlx.Println("send ans to client: ", yy.curclient, res.String())
				errr := s.Write(yy.curclient, buf)
				if errr != nil {
					mlx.Println("server  write ans to client timeout")
				}
			}
			yy.run = false
			yy.currid += 1
			if len(yy.pendjobs) != 0 && yy.currid < yy.reqct {
				mlx.Println("there is jobs left begin to assign", yy.currid)
				mlx.Println("move pend miner to miner")
				yy.resize(mlx, s)
				mlx.Println("-----")
				yy.assign(mlx, s)
			}
		default:
			id, v, err := s.Read()
			if err != nil {
				//mlx.Println(err)
				_, ok1 := yy.miner[id]
				if ok1 {
					if yy.run {
						mlx.Println("lost miner: ", id, " when running the job")
						if yy.miner[id] != nil {
							yy.pendrange[id] = yy.miner[id]
							yy.minerfail += 1
							delete(yy.miner, id)
							yy.resize(mlx, s)
							if len(yy.miner) != 0 {
								yy.reassign(mlx, s)
							}
							continue
						}
					} else {
						mlx.Println("lost miner: ", id, " when there is no job and miner wait for request")
						delete(yy.miner, id)
						continue
					}
				}
				_, ok2 := yy.pendminer[id]
				if ok2 {
					mlx.Println("lost miner: ", id, " before assign job")
					delete(yy.pendminer, id)
					continue
				}
				if id == yy.curclient {
					mlx.Println("lost the running client: ", id, " before return results, give up current job")
					yy.jobgiveup = true
					continue
				} /*else {
					if id < yy.curclient {
						continue
					}
					mlx.Println("lost the unruning client: ", id, "before start its job")
					pos := 0
					for i, v := range yy.pendclients {
						if pos == 0 {
							if i == id {
								pos = i
								delete(yy.pendclients, i)
							}
						} else {
							if i > pos {
								delete(yy.pendclients, i)
								yy.pendclients[i-1] = v
							}
						}
					}
					for i, v := range yy.pendjobs {
						if i > pos {
							delete(yy.pendjobs, i)
							yy.pendjobs[i-1] = v
						}
					}
					yy.reqct -= 1
				}*/
			} else {
				msg := &bitcoin.Message{}
				err = json.Unmarshal(v[:], msg)
				mlx.Println(id, msg.String())
				switch msg.Type {
				case bitcoin.Join:
					if yy.run {
						yy.pendminer[id] = 0
						yy.resize(mlx, s)
						if len(yy.pendrange) != 0 {
							yy.reassign(mlx, s)
						}
					} else {
						yy.miner[id] = nil
						if len(yy.pendrange) != 0 {
							mlx.Println("request wait for miner for left job, miner come")
							yy.reassign(mlx, s)
							continue
						}
						if len(yy.pendclients) != 0 {
							mlx.Println("request wait for miner, miner come")
							yy.assign(mlx, s)
						}
					}
				case bitcoin.Request:
					mlx.Println("receive request form client:", id, msg.String())
					if yy.run {
						yy.pendclients[yy.reqct] = id
						yy.pendjobs[yy.reqct] = msg
						yy.reqct += 1
					} else {
						if len(yy.miner) != 0 {
							yy.pendclients[yy.reqct] = id
							yy.pendjobs[yy.reqct] = msg
							yy.reqct += 1
							mlx.Println("miner wait for request, client come")

							yy.assign(mlx, s)
						} else {
							yy.pendclients[yy.reqct] = id
							yy.pendjobs[yy.reqct] = msg
							yy.reqct += 1
						}
					}
				case bitcoin.Result:
					mlx.Println("receive ans: ", msg.Hash, msg.Nonce)
					if yy.run {
						for i, v := range yy.miner {
							if v == nil {
								continue
							}
							mlx.Println("check miner: ", i, v.Lower, v.Upper)
							if v != nil && msg.Nonce >= v.Lower && msg.Nonce <= v.Upper {
								if msg.Hash < yy.ans1 {
									yy.ans1 = msg.Hash
									yy.ans2 = msg.Nonce
								} else if msg.Hash == yy.ans1 {
									if msg.Nonce < yy.ans2 {
										yy.ans2 = msg.Nonce
									}
								}
								yy.miner[i] = nil
								yy.block -= 1
								mlx.Println("left ", yy.block, " answers, now answer: ", yy.ans1, yy.ans2)
								break
							}
						}
						if yy.block == 0 {
							//yy.ans1 = msg.Hash
							//yy.ans2 = msg.Nonce
							yy.waitans <- 1
							continue
						}
						if yy.minerfail > 0 {
							yy.reassign(mlx, s)
						}
					}
				}
			}

		}
	}
}
示例#6
0
文件: miner.go 项目: wdbaruni/dbm
func main() {
	const numArgs = 2
	if len(os.Args) != numArgs {
		fmt.Println("Usage: ./miner <hostport>")
		return
	}

	file, err := os.OpenFile(name, flag, perm)
	if err != nil {
		return
	}

	mlx := log.New(file, "", log.Lshortfile|log.Lmicroseconds)

	min, err := lsp.NewClient(os.Args[1], lsp.NewParams())

	if err != nil {
		mlx.Println("cant connect the server")
		file.Close()
		return
	}
	msg := bitcoin.NewJoin()
	buf, _ := json.Marshal(msg)
	hold := min.ConnID()
	mlx.Println("get id: ", hold)
	errr := min.Write(buf)
	if errr != nil {
		mlx.Println("write join fail")
		min.Close()
		file.Close()
		return
	} else {
		mlx.Println("miner: ", hold, " write join done")
		mlx.Println(msg.String())
	}
	for {
		v, er := min.Read()
		//mlx.Println("+++++")
		if er != nil {
			mlx.Println(er)
			file.Close()
			//min.Close()
			return
		}
		msgg := &bitcoin.Message{}

		err = json.Unmarshal(v[:], msgg)
		mlx.Println("miner: ", hold, " read request->", msgg.String())
		mes := msgg.Data
		l := msgg.Lower
		r := msgg.Upper
		var hold, ans1, ans2 uint64
		ans2 = 1<<64 - 1
		ans1 = 1<<64 - 1
		for i := l; i <= r; i++ {
			hold = bitcoin.Hash(mes, i)
			if hold < ans1 {
				ans1 = hold
				ans2 = i
			} else if hold == ans1 {
				if i < ans2 {
					ans2 = i
				}
			}
		}
		res := bitcoin.NewResult(ans1, ans2)
		buf, _ := json.Marshal(res)
		errr := min.Write(buf)
		if errr != nil {
			mlx.Println("miner: ,", hold, " write ans timeout fail")
			min.Close()
			file.Close()
			return
		} else {
			mlx.Println("miner: ", hold, " write ans done")
			mlx.Println(msg.String())
		}
	}
}