package blockchain

import "testing"
import "encoding/hex"


func Test_Transaction_Deserialisation_Height_110_first(t *testing.T) {

	// transaction from height 110 txid 7f7bf73e2da48f16f17b2d5c81fe9e581e5a7ac6ed552da3015660baeda9f698
	// this structure has been manually pulled from wireshark stream
	tx_data_hex := "01aa0101ff6e08acbb2702eab03067870349139bee7eab2ca2e030a6bb73d4f68ab6a3b6ca937214054cdac0843d028bbe23b57ea9bae53f12da93bb57bf8a2e40598d9fccd10" +
		"c2921576e987d93cd80b4891302468738e391f07c4f2b356f7957160968e0bfef6e907c3cee2d8c23cbf04b089680c6868f01025a0f41f063e195a966051e3a29e17130a9ce97" +
		"d48f55285b9bb04bdd55a09ae78088aca3cf0202d0f26169290450fe17e08974789c3458910b4db18361cdc564f8f2d0bdd2cf568090cad2c60e02d6f3483ec45505cc3be8410" +
		"46c7a12bf953ac973939bc7b727e54258e1881d4d80e08d84ddcb0102dae6dfb16d3e28aaaf43e00170b90606b36f35f38f8a3dceb5ee18199dd8f17c80c0caf384a30202385d" +
		"7e57a4daba4cdd9e550a92dcc188838386e7581f13f09de796cbed4716a42101c052492a077abf41996b50c1b2e67fd7288bcd8c55cdc657b4e22d0804371f69"
	tx_data_blob, _ := hex.DecodeString(tx_data_hex)

	var tx Transaction

	err := tx.DeserializeHeader(tx_data_blob)

	if err != nil {
		t.Error("DeserializeHeader  transaction from blockheight 110 7f7bf73e2da48f16f17b2d5c81fe9e581e5a7ac6ed552da3015660baeda9f698\n")
		return
	}

	// now serialize once again
	/*
		serialised, _ := lheader.Serialize()
		 if raw_data != hex.EncodeToString(serialised){
		 	t.Error("Serialize Levin_Header Failed")
		 }
	*/

}

func Test_Transaction_Deserialisation_Height_110_second(t *testing.T) {

	// transaction from height 110 txid beb76a82ea17400cd6d7f595f70e1667d2018ed8f5a78d1ce07484222618c3cd
	// this structure has been manually pulled from wireshark stream
	tx_data_hex := "0100010280e08d84ddcb0106010401110701f254220bb50d901a5523eaed438af5d43f8c6d0e54ba0632eb539884f6b7c02008c0a8a50402f9c7cf807ae74e56f4ec84db2bd93" +
		"cfb02c2249b38e306f5b54b6e05d00d543b8095f52a02b6abb84e00f47f0a72e37b6b29392d906a38468404c57db3dbc5e8dd306a27a880d293ad0302cfc40a86723e7d459e90" +
		"e45d47818dc0e81a1f451ace5137a4af8110a89a35ea80b4c4c321026b19c796338607d5a2c1ba240a167134142d72d1640ef07902da64fed0b10cfc8088aca3cf02021f6f655" +
		"254fee84161118b32e7b6f8c31de5eb88aa00c29a8f57c0d1f95a24dd80d0b8e1981a023321af593163cea2ae37168ab926efd87f195756e3b723e886bdb7e618f751c480a094" +
		"a58d1d0295ed2b08d1cf44482ae0060a5dcc4b7d810a85dea8c62e274f73862f3d59f8ed80a0e5b9c2910102dc50f2f28d7ceecd9a1147f7106c8d5b4e08b2ec77150f52dd713" +
		"0ee4f5f50d42101d34f90ac861d0ee9fe3891656a234ea86a8a93bf51a237db65baa00d3f4aa196a9e1d89bc06b40e94ea9a26059efc7ba5b2de7ef7c139831ca62f3fe0bb252" +
		"008f8c7ee810d3e1e06313edf2db362fc39431755779466b635f12f9f32e44470a3e85e08a28fcd90633efc94aa4ae39153dfaf661089d045521343a3d63e8da08d7916753c66" +
		"aaebd4eefcfe8e58e5b3d266b752c9ca110749fa33fce7c44270386fcf2bed4f03dd5dadb2dc1fd4c505419f8217b9eaec07521f0d8963e104603c926745039cf38d31de6ed95" +
		"ace8e8a451f5a36f818c151f517546d55ac0f500e54d07b30ea7452f2e93fa4f60bdb30d71a0a97f97eb121e662006780fbf69002228224a96bff37893d47ec3707b17383906c" +
		"0cd7d9e7412b3e6c8ccf1419b093c06c26f96e3453b424713cdc5c9575f81cda4e157052df11f4c40809edf420f88a3dd1f7909bbf77c8b184a933389094a88e480e900bcdbf6" +
		"d1824742ee520fc0032e7d892a2b099b8c6edfd1123ce58a34458ee20cad676a7f7cfd80a28f0cb0888af88838310db372986bdcf9bfcae2324480ca7360d22bff21fb569a530e"
	tx_data_blob, _ := hex.DecodeString(tx_data_hex)

	var tx Transaction

	err := tx.DeserializeHeader(tx_data_blob)

	if err != nil {
		t.Error("DeserializeHeader  transaction from blockheight 110 beb76a82ea17400cd6d7f595f70e1667d2018ed8f5a78d1ce07484222618c3cd\n")
		return
	}

	// now serialize once again
	/*
		serialised, _ := lheader.Serialize()
		 if raw_data != hex.EncodeToString(serialised){
		 	t.Error("Serialize Levin_Header Failed")
		 }
	*/

}

func Test_Transaction_Deserialisation_Height_345_second(t *testing.T) {

	// transaction from height 345 txid 9ecdadf8b95aa0a7b754559f5fd7a9bb0f5014c4107b3dc6b3331430b5ea984d
	tx_data_hex := "01000502ccfd1b01002c03aa742e881a41588827ea8680f68ea3e6988917c832767e951f3ff7457b170280a0d9e61d010856cfc2d32b7742ac434740a684503e0c61125e71ad8" +
		"590e6e7a5887a6ec8ad3a0280c0fc82aa020143c8db95b9af6de4a73036748acca2e3d0bfaa817d465517b98e57797c9b455d15028090cad2c60e0190025210cecc11becf8d0f" +
		"8299b01674bdf86bceb2f78c5a30c8970e04f4a317d2820280e08d84ddcb01019002b705639c46e64a8d2f61454e63c633dea9163eda0621d7c4ef88bac4e3b06ff608c0a8a50" +
		"4023a8dd1006ef4752f8af5e4021a5e09168f78333f3920b1d52a8d2fd8a0dd0a688095f52a021687f0553eac19137c4e9544082c4103e40108be40459deed7cd29887786f3cc" +
		"80d293ad030242751f4fef4a8c939f2e495dc68ad9d6a0757ae73a60840af570901cfefe49ca808cee891a026692debc763112bd5e9d2fa3881af58c89cba3f6a8550faac92d7" +
		"ebc82afcffd80c0fc82aa0202a7b5bce3e2e5017ab9c4a0917494b03ce108fd7a6930ed16399456d221ddaa828090cad2c60e0200630a66763c211bc964e6f2a279144a911289" +
		"e4992c995a5bf87b89b64ac93180a094a58d1d024930687802aec5a6c594e64571ddd31ecc371f52d7d10ad869acdb9af730ead180c0f9decfae0102dd96078cd9a9ef9044397" +
		"cfde225abe9ab24166b55479bec6ebb7c7f49c5b2d92101353bbda344eeaa6cd0225a414e83d1a59cbb91bbf36dbc49fda6f532fa9286ac8a040a050c9179a1e88ac96370fc6c" +
		"0ad7e5fdaee4c9b0a440af56d5dcd159034c572e6bf5f7be8036e1f1349d2e895253956a55c30fd3dc0c6646512aac5a05e608faaf2186192b4e403d9da0dd211e81fef866b94" +
		"807b9deb1195de6800a0512bece8d12f5e3815d69036bafaec7c95025bc4dcc4656e911a673559bfd6b01394bdbfb69bb44665dc26c321db3cf4f174077d14c9cee2339893beb7ac2f50d2c6a1875d0e100e0ee230e52e3e33553a1fc06502ddaba7d13cfc314d1eded04347d7e4fc115185b32bbab7483099187acfad1393417795b7348a66f1801b906f2aaf8284e12abb4d651755b587bc6b840e785f5376416c264d48c5dbb32190a999b4cbfd90c86b1eda8e2e943f094a064690c8e5a7daa87f55335aeac576b0af5cc6789a97b34ba17dcda54382ea8c72e392be6e2850ee348fa5b5b1e0f2002"
	tx_data_blob, _ := hex.DecodeString(tx_data_hex)

	var tx Transaction

	err := tx.DeserializeHeader(tx_data_blob)

	if err != nil {
		t.Error("DeserializeHeader  transaction from blockheight 110 beb76a82ea17400cd6d7f595f70e1667d2018ed8f5a78d1ce07484222618c3cd\n")
		return
	}

	// now serialize once again
	/*
		serialised, _ := lheader.Serialize()
		 if raw_data != hex.EncodeToString(serialised){
		 	t.Error("Serialize Levin_Header Failed")
		 }
	*/

}

func Test_Transaction_Deserialisation_Height_1302238_second(t *testing.T) {

	// transaction from height 1302238 txid be9d2cf9b473dbbb2c59ffb07b5d812516f94d64121d87ad61956386a4bc3843
	// prefix hash 1bbfda600fa6affc80dae05b1124bf05ed0e20890aa42601441dbe0f6fa81f4b
	tx_data_hex := "[encoded data, 39122 characters]"
	tx_data_blob, _ := hex.DecodeString(tx_data_hex)

	var tx Transaction

	err := tx.DeserializeHeader(tx_data_blob)

	if err != nil {
		t.Error("DeserializeHeader  transaction from blockheight 1302238 beb76a82ea17400cd6d7f595f70e1667d2018ed8f5a78d1ce07484222618c3cd\n")
		return
	}

	// verify prefix hash
	calculated_prefix_hash := tx.GetPrefixHash()

	if hex.EncodeToString([]byte(calculated_prefix_hash[:])) != "1bbfda600fa6affc80dae05b1124bf05ed0e20890aa42601441dbe0f6fa81f4b" {
		t.Error("Prefix hash mismatched")
	}

	// be9d2cf9b473dbbb2c59ffb07b5d812516f94d64121d87ad61956386a4bc3843
	calculated_hash := tx.GetHash()

	if hex.EncodeToString([]byte(calculated_hash[:])) != "be9d2cf9b473dbbb2c59ffb07b5d812516f94d64121d87ad61956386a4bc3843" {
		t.Error("transaction hash mismatched")
	}

	// now serialize once again
	//
	serialised := tx.SerializeHeader()
	_ = serialised

	tx.Clear()

	err = tx.DeserializeHeader(serialised)

	/*if err != nil {
		t.Error("DeserializeHeader  transaction from blockheight 1302238 beb76a82ea17400cd6d7f595f70e1667d2018ed8f5a78d1ce07484222618c3cd\n")
		return
	}*/

	if hex.EncodeToString(serialised) != hex.EncodeToString(tx.SerializeHeader()) {

		t.Logf("serialized  %s\n", hex.EncodeToString(serialised))
		t.Logf("reserialized %s\n", hex.EncodeToString(tx.SerializeHeader()))
		t.Error("Serialize TX  Failed")
	}

}

// test taken from  derod project block 72 txid 2c4738d3856e8e3e8f9fc4818a9197d4911af3010e067ec56d08c264627cb547
//transaction_test.go
func Test_Transaction_Deserialisation_Height_72_second(t *testing.T) {

	// transaction from height 72 txid 2c4738d3856e8e3e8f9fc4818a9197d4911af3010e067ec56d08c264627cb547

	tx_data_hex := "[encoded data, 26098 characters]"
	tx_data_blob, _ := hex.DecodeString(tx_data_hex)

	var tx Transaction

	err := tx.DeserializeHeader(tx_data_blob)

	if err != nil {
		t.Error("DeserializeHeader  transaction from project dero blockheight 72 2c4738d3856e8e3e8f9fc4818a9197d4911af3010e067ec56d08c264627cb547\n")
		return
	}

	// now serialize once again
	/*
		serialised, _ := lheader.Serialize()
		 if raw_data != hex.EncodeToString(serialised){
		 	t.Error("Serialize Levin_Header Failed")
		 }
	*/

}
