func TestRevealMatryoshkaHashMarshalUnmarshal(t *testing.T) {
	identity := testHelper.NewRepeatingHash(0xAB)
	mhash := testHelper.NewRepeatingHash(0xCD)

	rmh := NewRevealMatryoshkaHash(identity, mhash)
	if rmh.Type() != constants.TYPE_REVEAL_MATRYOSHKA {
		t.Errorf("Invalid type")
	}
	if rmh.IdentityChainID.IsSameAs(identity) == false {
		t.Errorf("Invalid IdentityChainID")
	}
	if rmh.MHash.IsSameAs(mhash) == false {
		t.Errorf("Invalid MHash")
	}
	tmp2, err := rmh.MarshalBinary()
	if err != nil {
		t.Error(err)
	}

	rmh = new(RevealMatryoshkaHash)
	err = rmh.UnmarshalBinary(tmp2)
	if err != nil {
		t.Error(err)
	}
	if rmh.Type() != constants.TYPE_REVEAL_MATRYOSHKA {
		t.Errorf("Invalid type")
	}
	if rmh.IdentityChainID.IsSameAs(identity) == false {
		t.Errorf("Invalid IdentityChainID")
	}
	if rmh.MHash.IsSameAs(mhash) == false {
		t.Errorf("Invalid MHash")
	}
}
func TestAddFederatedServerBitcoinAnchorKeyMarshalUnmarshal(t *testing.T) {
	identity := testHelper.NewRepeatingHash(0xAB)
	pub := new(primitives.ByteSlice20)
	err := pub.UnmarshalBinary(testHelper.NewRepeatingHash(0xCD).Bytes())
	if err != nil {
		t.Error(err)
	}
	var keyPriority byte = 3
	var keyType byte = 1

	afsk := NewAddFederatedServerBitcoinAnchorKey(identity, keyPriority, keyType, *pub)
	if afsk.Type() != constants.TYPE_ADD_BTC_ANCHOR_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.KeyType != keyType {
		t.Errorf("Invalid KeyType")
	}
	if afsk.ECDSAPublicKey.String() != pub.String() {
		t.Errorf("Invalid ECDSAPublicKey")
	}
	tmp2, err := afsk.MarshalBinary()
	if err != nil {
		t.Error(err)
	}

	afsk = new(AddFederatedServerBitcoinAnchorKey)
	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.KeyType != keyType {
		t.Errorf("Invalid KeyType")
	}
	if afsk.ECDSAPublicKey.String() != pub.String() {
		t.Errorf("Invalid ECDSAPublicKey")
	}
}
func TestRemoveFederatedServerMarshalUnmarshal(t *testing.T) {
	identity := testHelper.NewRepeatingHash(0xAB)
	var dbHeight uint32 = 0xAABBCCDD

	rfs := NewRemoveFederatedServer(identity, dbHeight)
	if rfs.Type() != constants.TYPE_REMOVE_FED_SERVER {
		t.Errorf("Invalid type")
	}
	if rfs.DBHeight != dbHeight {
		t.Errorf("Invalid DBHeight")
	}
	if rfs.IdentityChainID.IsSameAs(identity) == false {
		t.Errorf("Invalid IdentityChainID")
	}
	tmp2, err := rfs.MarshalBinary()
	if err != nil {
		t.Error(err)
	}

	rfs = new(RemoveFederatedServer)
	err = rfs.UnmarshalBinary(tmp2)
	if err != nil {
		t.Error(err)
	}
	if rfs.Type() != constants.TYPE_REMOVE_FED_SERVER {
		t.Errorf("Invalid type")
	}
	if rfs.DBHeight != dbHeight {
		t.Errorf("Invalid DBHeight")
	}
	if rfs.IdentityChainID.IsSameAs(identity) == false {
		t.Errorf("Invalid IdentityChainID")
	}
}
Ejemplo n.º 4
0
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")
	}
}
Ejemplo n.º 6
0
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)
			}
		}
	}
}
Ejemplo n.º 7
0
func TestHandleV2FactoidACK(t *testing.T) {
	state := testHelper.CreateAndPopulateTestState()
	blocks := testHelper.CreateFullTestBlockSet()

	for _, block := range blocks {
		for _, tx := range block.FBlock.GetTransactions() {
			req := AckRequest{}
			txID := tx.GetSigHash().String()
			req.TxID = txID

			r, jError := HandleV2FactoidACK(state, req)

			if jError != nil {
				t.Errorf("%v", jError)
				continue
			}

			resp, ok := r.(*FactoidTxStatus)
			if ok == false {
				t.Error("Invalid response type returned")
				continue
			}

			if resp.TxID != txID {
				t.Error("Invalid TxID returned")
			}
			if resp.Status != AckStatusDBlockConfirmed {
				t.Error("Invalid status returned")
			}

			req = AckRequest{}
			h, err := tx.MarshalBinary()
			if err != nil {
				t.Errorf("%v", err)
				continue
			}
			req.FullTransaction = hex.EncodeToString(h)

			r, jError = HandleV2FactoidACK(state, req)

			if jError != nil {
				t.Errorf("%v", jError)
				continue
			}

			resp, ok = r.(*FactoidTxStatus)
			if ok == false {
				t.Error("Invalid response type returned")
				continue
			}

			if resp.TxID != txID {
				t.Error("Invalid TxID returned")
			}
			if resp.Status != AckStatusDBlockConfirmed {
				t.Error("Invalid status returned")
			}
		}
	}

	for i := 0; i < 10; i++ {
		h := testHelper.NewRepeatingHash(byte(i))

		req := AckRequest{}
		req.TxID = h.String()

		r, jError := HandleV2FactoidACK(state, req)

		if jError != nil {
			t.Errorf("%v", jError)
			continue
		}

		resp, ok := r.(*FactoidTxStatus)
		if ok == false {
			t.Error("Invalid response type returned")
			continue
		}

		if resp.TxID != h.String() {
			t.Error("Invalid TxID returned")
		}
		if resp.Status != AckStatusUnknown {
			t.Error("Invalid status returned")
		}

		req = AckRequest{}
		req.FullTransaction = h.String()

		_, jError = HandleV2FactoidACK(state, req)

		if jError == nil {
			t.Error("Invalid transaciton not caught")
			continue
		}
	}
}
Ejemplo n.º 8
0
func TestHandleV2EntryACK(t *testing.T) {
	state := testHelper.CreateAndPopulateTestState()
	blocks := testHelper.CreateFullTestBlockSet()

	for _, block := range blocks {
		for _, tx := range block.Entries {
			req := AckRequest{}
			txID := tx.GetHash().String()
			req.TxID = txID

			r, jError := HandleV2EntryACK(state, req)

			if jError != nil {
				t.Errorf("%v", jError)
				continue
			}

			resp, ok := r.(*EntryStatus)
			if ok == false {
				t.Error("Invalid response type returned")
				continue
			}

			if resp.EntryHash != txID {
				t.Errorf("Invalid EntryHash returned - %v vs %v", resp.EntryHash, txID)
			}
			if resp.CommitTxID == "" {
				t.Errorf("Invalid CommitTxID returned - %v", resp.CommitTxID)
			}
			if resp.CommitData.Status != AckStatusDBlockConfirmed {
				t.Errorf("Invalid status returned - %v vs %v", resp.CommitData.Status, AckStatusDBlockConfirmed)
			}
			if resp.EntryData.Status != AckStatusDBlockConfirmed {
				t.Errorf("Invalid status returned - %v vs %v", resp.EntryData.Status, AckStatusDBlockConfirmed)
			}

			req = AckRequest{}
			h, err := tx.MarshalBinary()
			if err != nil {
				t.Errorf("%v", err)
				continue
			}
			req.FullTransaction = hex.EncodeToString(h)

			r, jError = HandleV2EntryACK(state, req)

			if jError != nil {
				t.Errorf("%v", jError)
				continue
			}

			resp, ok = r.(*EntryStatus)
			if ok == false {
				t.Error("Invalid response type returned")
				continue
			}

			if resp.EntryHash != txID {
				t.Errorf("Invalid EntryHash returned - %v vs %v", resp.EntryHash, txID)
			}
			if resp.CommitTxID == "" {
				t.Errorf("Invalid CommitTxID returned - %v", resp.CommitTxID)
			}
			if resp.CommitData.Status != AckStatusDBlockConfirmed {
				t.Errorf("Invalid status returned - %v vs %v", resp.CommitData.Status, AckStatusDBlockConfirmed)
			}
			if resp.EntryData.Status != AckStatusDBlockConfirmed {
				t.Errorf("Invalid status returned - %v vs %v", resp.EntryData.Status, AckStatusDBlockConfirmed)
			}
		}

		for _, tx := range block.ECBlock.GetEntries() {
			if tx.ECID() != entryCreditBlock.ECIDChainCommit && tx.ECID() != entryCreditBlock.ECIDEntryCommit {
				continue
			}
			req := AckRequest{}

			txID := tx.GetSigHash().String()
			entryHash := tx.GetEntryHash().String()
			req.TxID = txID

			r, jError := HandleV2EntryACK(state, req)

			if jError != nil {
				t.Errorf("%v", jError)
				continue
			}

			resp, ok := r.(*EntryStatus)
			if ok == false {
				t.Error("Invalid response type returned")
				continue
			}
			t.Logf("resp - %v", resp)

			if resp.CommitTxID != txID {
				t.Errorf("Invalid CommitTxID returned - %v vs %v", resp.CommitTxID, txID)
			}
			if resp.EntryHash != entryHash {
				t.Errorf("Invalid EntryHash returned - %v vs %v", resp.EntryHash, entryHash)
			}
			if resp.CommitData.Status != AckStatusDBlockConfirmed {
				t.Errorf("Invalid status returned - %v vs %v", resp.CommitData.Status, AckStatusDBlockConfirmed)
			}
			if resp.EntryData.Status != AckStatusDBlockConfirmed {
				t.Errorf("Invalid status returned - %v vs %v", resp.EntryData.Status, AckStatusDBlockConfirmed)
			}

			req = AckRequest{}
			h, err := tx.MarshalBinary()
			if err != nil {
				t.Errorf("%v", err)
				continue
			}
			req.FullTransaction = hex.EncodeToString(h)

			r, jError = HandleV2EntryACK(state, req)

			if jError != nil {
				t.Errorf("%v", jError)
				continue
			}

			resp, ok = r.(*EntryStatus)
			if ok == false {
				t.Error("Invalid response type returned")
				continue
			}
			t.Logf("resp - %v", resp)

			if resp.CommitTxID != txID {
				t.Errorf("Invalid CommitTxID returned - %v vs %v", resp.CommitTxID, txID)
			}
			if resp.EntryHash != entryHash {
				t.Errorf("Invalid EntryHash returned - %v vs %v", resp.EntryHash, entryHash)
			}
			if resp.CommitData.Status != AckStatusDBlockConfirmed {
				t.Errorf("Invalid status returned - %v vs %v", resp.CommitData.Status, AckStatusDBlockConfirmed)
			}
			if resp.EntryData.Status != AckStatusDBlockConfirmed {
				t.Errorf("Invalid status returned - %v vs %v", resp.EntryData.Status, AckStatusDBlockConfirmed)
			}
		}
	}

	for i := 0; i < 10; i++ {
		h := testHelper.NewRepeatingHash(byte(i))

		req := AckRequest{}
		req.TxID = h.String()

		r, jError := HandleV2EntryACK(state, req)

		if jError != nil {
			t.Errorf("%v", jError)
			continue
		}

		resp, ok := r.(*EntryStatus)
		if ok == false {
			t.Error("Invalid response type returned")
			continue
		}

		if resp.CommitTxID != "" {
			t.Error("Invalid CommitTxID returned")
		}
		if resp.EntryHash != "" {
			t.Error("Invalid EntryHash returned")
		}
		if resp.CommitData.Status != AckStatusUnknown {
			t.Error("Invalid status returned")
		}
		if resp.EntryData.Status != AckStatusUnknown {
			t.Error("Invalid status returned")
		}

		req = AckRequest{}
		req.FullTransaction = h.String()

		_, jError = HandleV2EntryACK(state, req)

		if jError == nil {
			t.Error("Invalid transaciton not caught")
			continue
		}
	}
}
Ejemplo n.º 9
0
func TestAddServerFault(t *testing.T) {
	block := createTestAdminBlock().(*AdminBlock)

	for i := 0; i < 5; i++ {
		block.ABEntries = append(block.ABEntries, new(AddFederatedServer))
		block.ABEntries = append(block.ABEntries, new(DBSignatureEntry))
		block.ABEntries = append(block.ABEntries, new(EndOfMinuteEntry))
		block.ABEntries = append(block.ABEntries, new(IncreaseServerCount))
		block.ABEntries = append(block.ABEntries, new(AddFederatedServerBitcoinAnchorKey))
	}

	for i := 0; i < 10; i++ {
		sf := new(ServerFault)

		sf.Timestamp = primitives.NewTimestampFromMinutes(uint32(i * 2))
		sf.ServerID = testHelper.NewRepeatingHash(1)
		sf.AuditServerID = testHelper.NewRepeatingHash(2)

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

		block.AddServerFault(sf)
	}

	for i := 0; i < 10; i++ {
		sf := new(ServerFault)

		sf.Timestamp = primitives.NewTimestampFromMinutes(uint32(i*2 + 1))
		sf.ServerID = testHelper.NewRepeatingHash(1)
		sf.AuditServerID = testHelper.NewRepeatingHash(2)

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

		block.AddServerFault(sf)
	}

	for i := 0; i < 20; i++ {
		sf := new(ServerFault)

		sf.Timestamp = primitives.NewTimestampFromMinutes(uint32(i))
		sf.ServerID = testHelper.NewRepeatingHash(1)
		sf.AuditServerID = testHelper.NewRepeatingHash(2)

		sf.VMIndex = byte(i)
		sf.DBHeight = 0x44556677
		sf.Height = 0x88990011

		block.AddServerFault(sf)
	}

	if len(block.ABEntries) != 5*5+10+10+20 {
		t.Errorf("Wrong length of ABEntries - %v", len(block.ABEntries))
	}

	sfFound := false
	for i := range block.ABEntries {
		if block.ABEntries[i].Type() != constants.TYPE_SERVER_FAULT {
			if sfFound {
				t.Errorf("Non-SF entry between SF entries at position %v", i)
			}
			continue
		}
		if i == 0 {
			t.Errorf("SF entry is at position 0 when it shouldn't be")
			continue
		}
		if block.ABEntries[i-1].Type() != constants.TYPE_SERVER_FAULT {
			continue
		}

		prev := block.ABEntries[i-1].(*ServerFault)
		cur := block.ABEntries[i].(*ServerFault)

		if prev.Timestamp.GetTimeMilliUInt64() > cur.Timestamp.GetTimeMilliUInt64() {
			t.Errorf("Wrong order by Timestamp")
			continue
		}
		if prev.Timestamp.GetTimeMilliUInt64() < cur.Timestamp.GetTimeMilliUInt64() {
			continue
		}
		if prev.VMIndex > cur.VMIndex {
			t.Errorf("Wrong order by VMIndex")
			continue
		}
	}
}