Пример #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
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
		}
	}
}
Пример #3
0
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())
		}
	}
}
Пример #4
0
func (m *Miner) Join() {
	m.write(bitcoin.NewJoin())
}