func TestServerFaultUpdateState(t *testing.T) {
	sigs := 10
	sf := new(ServerFault)

	sf.Timestamp = primitives.NewTimestampNow()
	sf.ServerID = testHelper.NewRepeatingHash(1)
	sf.AuditServerID = testHelper.NewRepeatingHash(2)

	sf.VMIndex = 0x33
	sf.DBHeight = 0x44556677
	sf.Height = 0x88990011

	core, err := sf.MarshalCore()
	if err != nil {
		t.Errorf("%v", err)
	}
	for i := 0; i < sigs; i++ {
		priv := testHelper.NewPrimitivesPrivateKey(uint64(i))
		sig := priv.Sign(core)
		sf.SignatureList.List = append(sf.SignatureList.List, sig)
	}
	sf.SignatureList.Length = uint32(len(sf.SignatureList.List))

	s := testHelper.CreateAndPopulateTestState()
	idindex := s.CreateBlankFactomIdentity(primitives.NewZeroHash())
	s.Identities[idindex].ManagementChainID = primitives.NewZeroHash()
	for i := 0; i < sigs; i++ {
		//Federated Server
		index := s.AddAuthorityFromChainID(testHelper.NewRepeatingHash(byte(i)))
		s.Authorities[index].SigningKey = *testHelper.NewPrimitivesPrivateKey(uint64(i)).Pub
		s.Authorities[index].Status = 1

		s.AddFedServer(s.GetLeaderHeight(), testHelper.NewRepeatingHash(byte(i)))

		//Audit Server
		index = s.AddAuthorityFromChainID(testHelper.NewRepeatingHash(byte(i + sigs)))
		s.Authorities[index].SigningKey = *testHelper.NewPrimitivesPrivateKey(uint64(i + sigs)).Pub
		s.Authorities[index].Status = 0

		s.AddFedServer(s.GetLeaderHeight(), testHelper.NewRepeatingHash(byte(i+sigs)))
	}

	err = sf.UpdateState(s)
	if err != nil {
		t.Errorf("%v", err)
	}

}
func TestAddFederatedServerSigningKeyMarshalUnmarshal(t *testing.T) {
	identity := testHelper.NewRepeatingHash(0xAB)
	priv := testHelper.NewPrimitivesPrivateKey(1)
	pub := priv.Pub
	var keyPriority byte = 3

	afsk := NewAddFederatedServerSigningKey(identity, keyPriority, *pub, 0)
	if afsk.Type() != constants.TYPE_ADD_FED_SERVER_KEY {
		t.Errorf("Invalid type")
	}
	if afsk.IdentityChainID.IsSameAs(identity) == false {
		t.Errorf("Invalid IdentityChainID")
	}
	if afsk.KeyPriority != keyPriority {
		t.Errorf("Invalid KeyPriority")
	}
	if afsk.PublicKey.String() != pub.String() {
		t.Errorf("Invalid PublicKey")
	}
	tmp2, err := afsk.MarshalBinary()
	if err != nil {
		t.Error(err)
	}

	afsk = new(AddFederatedServerSigningKey)
	err = afsk.UnmarshalBinary(tmp2)
	if err != nil {
		t.Error(err)
	}
	if afsk.IdentityChainID.IsSameAs(identity) == false {
		t.Errorf("Invalid IdentityChainID")
	}
	if afsk.KeyPriority != keyPriority {
		t.Errorf("Invalid KeyPriority")
	}
	if afsk.PublicKey.String() != pub.String() {
		t.Errorf("Invalid PublicKey")
	}
}
func TestServerFaultMarshalUnmarshal(t *testing.T) {
	sf := new(ServerFault)

	sf.Timestamp = primitives.NewTimestampNow()
	sf.ServerID = testHelper.NewRepeatingHash(1)
	sf.AuditServerID = testHelper.NewRepeatingHash(2)

	sf.VMIndex = 0x33
	sf.DBHeight = 0x44556677
	sf.Height = 0x88990011

	core, err := sf.MarshalCore()
	if err != nil {
		t.Errorf("%v", err)
	}
	for i := 0; i < 10; i++ {
		priv := testHelper.NewPrimitivesPrivateKey(uint64(i))
		sig := priv.Sign(core)
		sf.SignatureList.List = append(sf.SignatureList.List, sig)
	}
	sf.SignatureList.Length = uint32(len(sf.SignatureList.List))

	bin, err := sf.MarshalBinary()
	if err != nil {
		t.Errorf("%v", err)
	}

	sf2 := new(ServerFault)
	rest, err := sf2.UnmarshalBinaryData(bin)
	if err != nil {
		t.Errorf("%v", err)
	}
	if len(rest) > 0 {
		t.Errorf("Unexpected extra piece of data - %x", rest)
	}
	t.Logf("%v", sf.String())
	t.Logf("%v", sf2.String())

	if sf.Timestamp.GetTimeMilliUInt64() !=
		sf2.Timestamp.GetTimeMilliUInt64() {
		t.Errorf("Invalid Timestamp")
	}
	if sf.ServerID.IsSameAs(sf2.ServerID) == false {
		t.Errorf("Invalid ServerID")
	}
	if sf.AuditServerID.IsSameAs(sf2.AuditServerID) == false {
		t.Errorf("Invalid AuditServerID")
	}
	if sf.VMIndex != sf2.VMIndex {
		t.Errorf("Invalid VMIndex")
	}
	if sf.DBHeight != sf2.DBHeight {
		t.Errorf("Invalid DBHeight")
	}
	if sf.Height != sf2.Height {
		t.Errorf("Invalid Height")
	}

	if sf.SignatureList.Length != sf2.SignatureList.Length {
		t.Errorf("Invalid SignatureList.Length")
	}
	if len(sf.SignatureList.List) != len(sf2.SignatureList.List) {
		t.Errorf("Invalid len of SignatureList.List")
	} else {
		for i := range sf.SignatureList.List {
			if sf.SignatureList.List[i].IsSameAs(sf2.SignatureList.List[i]) == false {
				t.Errorf("Invalid SignatureList.List at %v", i)
			}
		}
	}
}