Ejemplo n.º 1
0
func establishQuorum() {
	var port int
	print("Port number: ")
	fmt.Scanf("%d", &port)
	networkServer, err := network.NewRPCServer(port)
	if err != nil {
		println(err)
		return
	}
	s, err := quorum.CreateState(networkServer)
	s.JoinSia()
	select {}
}
Ejemplo n.º 2
0
func TestNetworkedQuorum(t *testing.T) {
	// create a MessageRouter and 4 participants
	rpcs, err := network.NewRPCServer(9988)
	if err != nil {
		println("message sender creation failed")
	}

	s0, err := quorum.CreateState(rpcs)
	if err != nil {
		println("s0 creation failed")
	}
	s1, err := quorum.CreateState(rpcs)
	if err != nil {
		println("s1 creation failed")
	}
	s2, err := quorum.CreateState(rpcs)
	if err != nil {
		println("s2 creation failed")
	}
	s3, err := quorum.CreateState(rpcs)
	if err != nil {
		println("s3 creation failed")
	}

	s0.JoinSia()
	s1.JoinSia()
	s2.JoinSia()
	s3.JoinSia()

	// Basically checking for errors up to this point
	if testing.Short() {
		t.Skip()
	}

	time.Sleep(3 * common.StepDuration * time.Duration(common.QuorumSize))

	// if no seg faults, no errors
	// there needs to be a s0.ParticipantStatus() call returning a function with public information about the participant
	// there needs to be a s0.QuorumStatus() call returning public information about the quorum
	// 		all participants in a public quorum should return the same information
}
Ejemplo n.º 3
0
// TestRPCUploadSector tests the NewRPCServer and uploadFile functions.
// NewRPCServer must properly initialize a RPC server.
// uploadSector must succesfully distribute a Sector among a quorum.
// The uploaded Sector must be successfully reconstructed.
func TestRPCuploadSector(t *testing.T) {
	SectorDB = make(map[crypto.Hash]*common.RingHeader)

	// create RPCServer
	var err error
	router, err = network.NewRPCServer(9985)
	if err != nil {
		t.Fatal("Failed to initialize RPCServer:", err)
	}
	defer router.Close()

	// create quorum
	var q [common.QuorumSize]common.Address
	var shs [common.QuorumSize]Server
	for i := 0; i < common.QuorumSize; i++ {
		q[i] = common.Address{0, "localhost", 9000 + i}
		qrpc, err := network.NewRPCServer(9000 + i)
		defer qrpc.Close()
		if err != nil {
			t.Fatal("Failed to initialize RPCServer:", err)
		}
		q[i].ID = qrpc.RegisterHandler(&shs[i])
	}

	// create sector
	secData, err := crypto.RandomByteSlice(70000)
	if err != nil {
		t.Fatal("Could not generate test data:", err)
	}

	sec, err := common.NewSector(secData)
	if err != nil {
		t.Fatal("Failed to create sector:", err)
	}

	// add sector to database
	k := common.QuorumSize / 2
	SectorDB[sec.Hash] = &common.RingHeader{
		Hosts:  q,
		Params: sec.CalculateParams(k),
	}

	// upload sector to quorum
	err = uploadSector(sec)
	if err != nil {
		t.Fatal("Failed to upload file:", err)
	}

	// rebuild file from first k segments
	var newRing []common.Segment
	for i := 0; i < k; i++ {
		newRing = append(newRing, shs[i].seg)
	}

	sec, err = erasure.RebuildSector(newRing, SectorDB[sec.Hash].Params)
	if err != nil {
		t.Fatal("Failed to rebuild file:", err)
	}

	// check hash
	rebuiltHash, err := crypto.CalculateHash(sec.Data)
	if err != nil {
		t.Fatal("Failed to calculate hash:", err)
	}

	if sec.Hash != rebuiltHash {
		t.Fatal("Failed to recover file: hashes do not match")
	}
}
Ejemplo n.º 4
0
// TestRPCdownloadSector tests the NewRPCServer and downloadSector functions.
// NewRPCServer must properly initialize a RPC server.
// downloadSector must successfully retrieve a Sector from a quorum.
// The downloaded Sector must match the original Sector.
func TestRPCdownloadSector(t *testing.T) {
	SectorDB = make(map[crypto.Hash]*common.RingHeader)

	// create sector
	secData, err := crypto.RandomByteSlice(70000)
	if err != nil {
		t.Fatal("Could not generate test data:", err)
	}

	sec, err := common.NewSector(secData)
	if err != nil {
		t.Fatal("Failed to create sector:", err)
	}

	k := common.QuorumSize / 2
	params := sec.CalculateParams(k)

	// encode sector
	ring, err := erasure.EncodeRing(sec, params)
	if err != nil {
		t.Fatal("Failed to encode sector data:", err)
	}

	// create RPCServer
	router, err = network.NewRPCServer(9985)
	if err != nil {
		t.Fatal("Failed to initialize RPCServer:", err)
	}
	defer router.Close()

	// create quorum
	var q [common.QuorumSize]common.Address
	for i := 0; i < common.QuorumSize; i++ {
		q[i] = common.Address{0, "localhost", 9000 + i}
		qrpc, err := network.NewRPCServer(9000 + i)
		if err != nil {
			t.Fatal("Failed to initialize RPCServer:", err)
		}
		sh := new(Server)
		sh.seg = ring[i]
		q[i].ID = qrpc.RegisterHandler(sh)
	}

	// add sector to database
	SectorDB[sec.Hash] = &common.RingHeader{
		Hosts:  q,
		Params: params,
	}

	// download file from quorum
	sec, err = downloadSector(sec.Hash)
	if err != nil {
		t.Fatal("Failed to download file:", err)
	}

	// check hash
	rebuiltHash, err := crypto.CalculateHash(sec.Data)
	if err != nil {
		t.Fatal("Failed to calculate hash:", err)
	}

	if sec.Hash != rebuiltHash {
		t.Fatal("Failed to recover file: hashes do not match")
	}
}
Ejemplo n.º 5
0
func main() {
	router, _ = network.NewRPCServer(9989)
	defer router.Close()
	SectorDB = make(map[crypto.Hash]*common.RingHeader)
	var (
		input string
		q     common.Quorum
		s     *common.Sector
		h     crypto.Hash
		err   error
	)
	for {
		fmt.Print("Please enter a command: ")
		fmt.Scanln(&input)

		switch input {
		default:
			fmt.Println("unrecognized command")
		case "j":
			fmt.Println("joining quorum")
			q = readQuorumAddresses()
			fmt.Println("connected to quorum")
		case "g":
			fmt.Println("generating Sector")
			s, err = generateSector(q)
			if err != nil {
				fmt.Println("error:", err)
				fmt.Println("failed to generate Sector")
				break
			}
			h = s.Hash
			fmt.Println("created Sector with hash", h[:10])
		case "u":
			fmt.Println("uploading file")
			err = uploadSector(s)
			if err != nil {
				fmt.Println("error:", err)
				fmt.Println("upload failed")
				break
			}
			fmt.Println("upload successful")
		case "d":
			fmt.Println("downloading file")
			rs, err := downloadSector(h)
			if err != nil {
				fmt.Println("error:", err)
				fmt.Println("download failed")
				break
			}
			rh, err := crypto.CalculateHash(rs.Data)
			if err != nil {
				fmt.Println("error:", err)
				break
			}
			fmt.Println("download successful")
			fmt.Println("hash:", rh[:10])
		case "q":
			return
		}
	}
}