Example #1
0
	def test_TCDList(self):
		"Test the TCD list serialization"

		TCDlist = \
		[
		tcd.TCD(1, 2, 7, 'a'*20, 'b'*20, 'c'*20),
		tcd.TCD(3, 4, 8, 'd'*20, 'e'*20, 'f'*20),
		tcd.TCD(5, 6, 9, 'g'*20, 'h'*20, 'i'*20)
		]

		data = tcd.serializeList(TCDlist)
		self.assertEqual(data,
			TCDlist[0].serialize() +
			TCDlist[1].serialize() +
			TCDlist[2].serialize()
			)

		TCDlist2 = tcd.deserializeList(data)
		self.assertEqual(len(TCDlist2), 3)
		for i in range(3):
			self.assertEqual(TCDlist2[i].startTime, TCDlist[i].startTime)
			self.assertEqual(TCDlist2[i].endTime, TCDlist[i].endTime)
			self.assertEqual(TCDlist2[i].amount, TCDlist[i].amount)
			self.assertEqual(TCDlist2[i].tokenHash, TCDlist[i].tokenHash)
			self.assertEqual(TCDlist2[i].commitAddress, TCDlist[i].commitAddress)
			self.assertEqual(TCDlist2[i].rollbackAddress, TCDlist[i].rollbackAddress)

		self.assertRaises(Exception, tcd.deserializeList, data[:-1])
		self.assertRaises(Exception, tcd.deserializeList, data + '\x00')
	def test_serialization(self):
		"Test serialization and deserialization"

		ownKey = crypto.Key()
		ownKey.makeNewKey(compressed=True)
		tx1 = multisigtransaction.MultiSigTransaction.makeNew(
			ownKey, "x"*32, 2000, 10, 1234)

		data = tx1.serialize()

		tx2 = multisigtransaction.MultiSigTransaction.deserialize(data)

		self.assertEqual(
			tx1.transaction.serialize(),
			tx2.transaction.serialize()
			)
		self.assertEqual(
			tcd.serializeList(tx1.TCDlist),
			tcd.serializeList(tx2.TCDlist)
			)
	def test_state(self):
		"Test state loading and saving"

		ownKey = crypto.Key()
		ownKey.makeNewKey(compressed=True)
		tx1 = multisigtransaction.MultiSigTransaction.makeNew(
			ownKey, "x"*32, 2000, 10, 1234)

		state = tx1.getState(forDisplay=True)
		self.assertEqual(state,
			{"ID": tx1.transaction.getTransactionID()[::-1].encode("hex")})

		state = tx1.getState(forDisplay=False)

		tx2 = multisigtransaction.MultiSigTransaction.makeFromState(state)

		self.assertEqual(
			tx1.transaction.serialize(),
			tx2.transaction.serialize()
			)
		self.assertEqual(
			tcd.serializeList(tx1.TCDlist),
			tcd.serializeList(tx2.TCDlist)
			)
	def test_setOutputs(self):
		"Test setOutputs method"

		ownKey = crypto.Key()
		ownKey.makeNewKey(compressed=True)
		txn = multisigtransaction.MultiSigTransaction.makeNew(
			ownKey, "x"*32, 2000, 10, 1234)

		txn.TCDlist = [
			tcd.TCD(1, 2, 3, 'a'*20, 'b'*20, 'c'*20),
			tcd.TCD(4, 5, 6, 'd'*20, 'e'*20, 'f'*20)
			]
		serializedList = tcd.serializeList(txn.TCDlist)

		txn.setOutputs(
			"ownPubKey", "peerPubKey", "escrowPubKey",
			1234, 5678)

		tx = txn.transaction

		self.assertEqual(len(tx.tx_in), 1)
		tx_in = tx.tx_in[0]
		self.assertEqual(tx_in.previousOutputHash, "x"*32)
		self.assertEqual(tx_in.previousOutputIndex, 0)
		self.assertEqual(tx_in.scriptSig.elements, tuple())

		def testTCDOutput(tx_out):
			self.assertEqual(tx_out.amount, 0)
			self.assertEqual(tx_out.scriptPubKey.elements,
				(OP.RETURN, crypto.RIPEMD160(crypto.SHA256(serializedList))))
		def testOwnOutput(tx_out):
			self.assertEqual(tx_out.amount, 1234)
			self.assertEqual(tx_out.scriptPubKey.elements,
				(OP.DUP, OP.HASH160, crypto.RIPEMD160(crypto.SHA256("ownPubKey")), OP.EQUALVERIFY, OP.CHECKSIG))
		def testPeerOutput(tx_out):
			self.assertEqual(tx_out.amount, 5678)
			self.assertEqual(tx_out.scriptPubKey.elements,
				(OP.DUP, OP.HASH160, crypto.RIPEMD160(crypto.SHA256("peerPubKey")), OP.EQUALVERIFY, OP.CHECKSIG))
		def testLockOutput(tx_out):
			self.assertEqual(tx_out.amount, 9)
			self.assertEqual(tx_out.scriptPubKey.elements,
				[OP.TWO, "ownPubKey", "peerPubKey", "escrowPubKey", OP.TWO+1, OP.CHECKMULTISIG])

		tx_out = tx.tx_out
		self.assertEqual(len(tx_out), 4)
		testTCDOutput(tx_out[0])
		testOwnOutput(tx_out[1])
		testPeerOutput(tx_out[2])
		testLockOutput(tx_out[3])


		txn.TCDlist = []
		txn.setOutputs(
			"ownPubKey", "peerPubKey", "escrowPubKey",
			1234, 5678)
		tx = txn.transaction
		tx_out = tx.tx_out
		self.assertEqual(len(tx_out), 2)
		testOwnOutput(tx_out[0])
		testPeerOutput(tx_out[1])

		txn.setOutputs(
			"ownPubKey", "peerPubKey", "escrowPubKey",
			1234, 0)
		tx = txn.transaction
		tx_out = tx.tx_out
		self.assertEqual(len(tx_out), 1)
		testOwnOutput(tx_out[0])

		txn.setOutputs(
			"ownPubKey", "peerPubKey", "escrowPubKey",
			0, 0)
		tx = txn.transaction
		self.assertEqual(len(tx.tx_out), 0)