Example #1
0
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))
	}()
}
Example #2
0
// calculate the minimum hash number and corresponding nonce in the given range
func calculateMinHash(data string, lower, upper uint64) (minHash, nonce uint64) {
	minHash = math.MaxUint64
	nonce = 0
	for i := lower; i <= upper; i++ {
		hash := bitcoin.Hash(data, i)
		if hash < minHash {
			minHash = hash
			nonce = i
		}
	}
	fmt.Println(minHash, nonce)
	return
}
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
		}
	}
}
Example #4
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())
		}
	}
}