func TestCommitMarshalUnmarshal(t *testing.T) { blocks := testHelper.CreateFullTestBlockSet() for _, block := range blocks { for _, tx := range block.ECBlock.GetEntries() { h1, err := tx.MarshalBinary() if err != nil { t.Errorf("Error marshalling - %v", err) } var h2 []byte var e interfaces.BinaryMarshallable switch tx.ECID() { case ECIDChainCommit: e = new(CommitChain) break case ECIDEntryCommit: e = new(CommitEntry) break case ECIDBalanceIncrease: e = new(IncreaseBalance) break case ECIDMinuteNumber: e = new(MinuteNumber) break case ECIDServerIndexNumber: e = new(ServerIndexNumber) break default: t.Error("Wrong ECID") break } h2, err = e.UnmarshalBinaryData(h1) if err != nil { t.Logf("Tried to unmarshal %x", h1) t.Errorf("Error unmarshalling - %v", err) continue } if len(h2) > 0 { t.Errorf("Leftovers from unmarshalling - %x", h2) } h2, err = e.MarshalBinary() if err != nil { t.Errorf("Error marshalling2 - %v", err) continue } if primitives.AreBytesEqual(h1, h2) == false { t.Error("ECEntries are not identical - %x vs %x", h1, h2) } } } }
func TestGetDirectoryBlockByHeight(t *testing.T) { state := testHelper.CreateAndPopulateTestState() blocks := testHelper.CreateFullTestBlockSet() for i, block := range blocks { dBlock := state.GetDirectoryBlockByHeight(uint32(i)) if dBlock.GetKeyMR().IsSameAs(block.DBlock.GetKeyMR()) == false { t.Errorf("DBlocks are not the same at height %v", i+1) continue } if dBlock.GetFullHash().IsSameAs(block.DBlock.GetFullHash()) == false { t.Errorf("DBlocks are not the same at height %v", i+1) continue } } }
func TestHandleV2GetTranasction(t *testing.T) { state := testHelper.CreateAndPopulateTestState() blocks := testHelper.CreateFullTestBlockSet() for _, block := range blocks { for _, tx := range block.FBlock.GetTransactions() { hashkey := new(HashRequest) hashkey.Hash = tx.GetFullHash().String() resp, jErr := HandleV2GetTranasction(state, hashkey) if jErr != nil { t.Errorf("%v", jErr) return } r := resp.(*TransactionResponse) if r.ECTranasction != nil { t.Errorf("ECTranasction != nil") } if r.Entry != nil { t.Errorf("Entry != nil") } if r.FactoidTransaction.GetFullHash().String() != hashkey.Hash { t.Errorf("Got wrong hash for FactoidTransaction") } if r.IncludedInTransactionBlock != block.FBlock.DatabasePrimaryIndex().String() { t.Errorf("Invalid IncludedInTransactionBlock") } if r.IncludedInDirectoryBlock != block.DBlock.DatabasePrimaryIndex().String() { t.Errorf("Invalid IncludedInDirectoryBlock") } if r.IncludedInDirectoryBlockHeight != int64(block.DBlock.GetDatabaseHeight()) { t.Errorf("Invalid IncludedInDirectoryBlockHeight") } } for _, h := range block.ECBlock.GetEntryHashes() { hashkey := new(HashRequest) hashkey.Hash = h.String() resp, jErr := HandleV2GetTranasction(state, hashkey) if jErr != nil { t.Errorf("%v", jErr) return } r := resp.(*TransactionResponse) if r.FactoidTransaction != nil { t.Errorf("FactoidTransaction != nil") } if r.Entry != nil { t.Errorf("Entry != nil") } if r.ECTranasction.Hash().String() != hashkey.Hash { t.Errorf("Got wrong hash for ECTranasction") } if r.IncludedInTransactionBlock != block.ECBlock.DatabasePrimaryIndex().String() { t.Errorf("Invalid IncludedInTransactionBlock") } if r.IncludedInDirectoryBlock != block.DBlock.DatabasePrimaryIndex().String() { t.Errorf("Invalid IncludedInDirectoryBlock") } if r.IncludedInDirectoryBlockHeight != int64(block.DBlock.GetDatabaseHeight()) { t.Errorf("Invalid IncludedInDirectoryBlockHeight") } } for _, tx := range block.EBlock.GetEntryHashes() { hashkey := new(HashRequest) hashkey.Hash = tx.String() resp, jErr := HandleV2GetTranasction(state, hashkey) if jErr != nil { t.Errorf("%v", jErr) return } r := resp.(*TransactionResponse) if r.ECTranasction != nil { t.Errorf("ECTranasction != nil") } if r.FactoidTransaction != nil { t.Errorf("FactoidTransaction != nil") } if r.Entry.GetHash().String() != hashkey.Hash { t.Errorf("Got wrong hash for Entry") } if r.IncludedInTransactionBlock != block.EBlock.DatabasePrimaryIndex().String() { t.Errorf("Invalid IncludedInTransactionBlock") } if r.IncludedInDirectoryBlock != block.DBlock.DatabasePrimaryIndex().String() { t.Errorf("Invalid IncludedInDirectoryBlock") } if r.IncludedInDirectoryBlockHeight != int64(block.DBlock.GetDatabaseHeight()) { t.Errorf("Invalid IncludedInDirectoryBlockHeight") } } for _, tx := range block.AnchorEBlock.GetEntryHashes() { hashkey := new(HashRequest) hashkey.Hash = tx.String() resp, jErr := HandleV2GetTranasction(state, hashkey) if jErr != nil { t.Errorf("%v", jErr) return } r := resp.(*TransactionResponse) if r.ECTranasction != nil { t.Errorf("ECTranasction != nil") } if r.FactoidTransaction != nil { t.Errorf("FactoidTransaction != nil") } if r.Entry.GetHash().String() != hashkey.Hash { t.Errorf("Got wrong hash for Entry") } if r.IncludedInTransactionBlock != block.AnchorEBlock.DatabasePrimaryIndex().String() { t.Errorf("Invalid IncludedInTransactionBlock") } if r.IncludedInDirectoryBlock != block.DBlock.DatabasePrimaryIndex().String() { t.Errorf("Invalid IncludedInDirectoryBlock") } if r.IncludedInDirectoryBlockHeight != int64(block.DBlock.GetDatabaseHeight()) { t.Errorf("Invalid IncludedInDirectoryBlockHeight") } } } }
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 } } }
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 } } }
func TestDecodeTransactionToHashes(t *testing.T) { blocks := testHelper.CreateFullTestBlockSet() for _, block := range blocks { for _, tx := range block.Entries { txID := tx.GetHash().String() h, err := tx.MarshalBinary() if err != nil { t.Errorf("%v", err) continue } fullTx := hex.EncodeToString(h) eTxID, ecTxID := DecodeTransactionToHashes(fullTx) if eTxID == "" && ecTxID == "" { t.Error("No TxID returned") continue } if ecTxID != "" { t.Error("Entry mistaken for EC Transaction") } if eTxID != txID { t.Errorf("Returned wrong Entry hash - %v vs %v", eTxID, txID) } } for _, tx := range block.ECBlock.GetEntries() { if tx.ECID() != entryCreditBlock.ECIDChainCommit && tx.ECID() != entryCreditBlock.ECIDEntryCommit { continue } /*if tx.ECID() == entryCreditBlock.ECIDChainCommit { fmt.Println("CC!") } if tx.ECID() == entryCreditBlock.ECIDEntryCommit { fmt.Println("EC!") }*/ txID := tx.GetHash().String() entryHash := tx.GetEntryHash().String() h, err := tx.MarshalBinary() if err != nil { t.Errorf("%v", err) continue } fullTx := hex.EncodeToString(h) eTxID, ecTxID := DecodeTransactionToHashes(fullTx) if eTxID == "" && ecTxID == "" { t.Error("No TxID returned") continue } if eTxID != entryHash { t.Errorf("Returned wrong Entry hash - %v vs %v", eTxID, entryHash) } if ecTxID != txID { t.Errorf("Returned wrong EC TxID - %v vs %v", ecTxID, txID) } } } }
func TestGetEntryType(t *testing.T) { blocks := testHelper.CreateFullTestBlockSet() dbo := testHelper.CreateAndPopulateTestDatabaseOverlay() for _, block := range blocks { eType, err := dbo.GetEntryType(block.DBlock.DatabasePrimaryIndex()) if err != nil { t.Error(err) } if eType == nil { t.Error("eType==nil") } if eType.IsSameAs(block.DBlock.GetChainID()) == false { t.Error("Block type mismatch") } eType, err = dbo.GetEntryType(block.ABlock.DatabasePrimaryIndex()) if err != nil { t.Error(err) } if eType == nil { t.Error("eType==nil") } if eType.IsSameAs(block.ABlock.GetChainID()) == false { t.Error("Block type mismatch") } eType, err = dbo.GetEntryType(block.ECBlock.DatabasePrimaryIndex()) if err != nil { t.Error(err) } if eType == nil { t.Error("eType==nil") } if eType.IsSameAs(block.ECBlock.GetChainID()) == false { t.Error("Block type mismatch") } eType, err = dbo.GetEntryType(block.FBlock.DatabasePrimaryIndex()) if err != nil { t.Error(err) } if eType == nil { t.Error("eType==nil") } if eType.IsSameAs(block.FBlock.GetChainID()) == false { t.Error("Block type mismatch") } eType, err = dbo.GetEntryType(block.AnchorEBlock.DatabasePrimaryIndex()) if err != nil { t.Error(err) } if eType == nil { t.Error("eType==nil") } if eType.IsSameAs(block.AnchorEBlock.GetChainID()) == false { t.Error("Block type mismatch") } eType, err = dbo.GetEntryType(block.EBlock.DatabasePrimaryIndex()) if err != nil { t.Error(err) } if eType == nil { t.Error("eType==nil") } if eType.IsSameAs(block.EBlock.GetChainID()) == false { t.Error("Block type mismatch") } for _, entry := range block.Entries { eType, err = dbo.GetEntryType(entry.GetHash()) if err != nil { t.Error(err) } if eType == nil { t.Error("eType==nil") } if eType.IsSameAs(entry.GetChainID()) == false { t.Error("Entry type mismatch") } } } }