def connect(): global bitcoind print "Reading the settings file...", s = settings.Settings("../amikopay.conf") print "done" print "Connecting...", bitcoind = bd.Bitcoind(s) print "done"
def test_recentPayerState(self): 'Test collection of recent payer state information' s = settings.Settings() s.payLogFile = logFile payLog = paylog.PayLog(s) self.assertRaises(KeyError, payLog.getRecentPayerState, '\x01\xde') payLog.writePayer(DummyPay(123, 'abc', 'committed', '\x01\xde', '\xab\xcd')) self.assertEqual(payLog.getRecentPayerState('\x01\xde'), 'committed') payLog.removeRecentPayerState('\x01\xde') self.assertRaises(KeyError, payLog.getRecentPayerState, '\x01\xde')
def test_write(self): 'Test appending to an existing log file' with open(logFile, "wb") as f: f.write("Existing data\n") self.checkLogContents( "Existing data\n" ) s = settings.Settings() s.payLogFile = logFile payLog = paylog.PayLog(s) payLog.writePayer(DummyPay(123, "abc", "committed", "\x01\xde", "\xab\xcd")) self.checkLogContents( "Existing data\n" "-123, 'abc', committed, 01de, abcd\n" ) payLog.writePayee(DummyPay(456, "def", "committed", "\x99\xaf", "\xef\x01")) self.checkLogContents( "Existing data\n" "-123, 'abc', committed, 01de, abcd\n" "456, 'def', committed, 99af, ef01\n" ) payLog.writePayer(DummyPay(123, "abc", "otherState", "\x01\xde", "\xab\xcd")) self.checkLogContents( "Existing data\n" "-123, 'abc', committed, 01de, abcd\n" "456, 'def', committed, 99af, ef01\n" "-123, 'abc', otherState, 01de, \n" ) payLog.writePayee(DummyPay(456, "def", "cancelled", "\x99\xaf", "\xef\x01")) self.checkLogContents( "Existing data\n" "-123, 'abc', committed, 01de, abcd\n" "456, 'def', committed, 99af, ef01\n" "-123, 'abc', otherState, 01de, \n" "456, 'def', cancelled, 99af, \n" ) payLog.close()
def test_newLogFile(self): 'Test creation of a new log file' if os.access(logFile, os.R_OK): os.remove(logFile) self.assertFalse(os.access(logFile, os.R_OK)) s = settings.Settings() s.payLogFile = logFile payLog = paylog.PayLog(s) payLog.writePayer(DummyPay(123, "abc", "committed", "\x01\xde", "\xab\xcd")) payLog.writePayee(DummyPay(456, "def", "committed", "\x99\xaf", "\xef\x01")) payLog.close() self.checkLogContents( "-123, 'abc', committed, 01de, abcd\n" "456, 'def', committed, 99af, ef01\n" )
def makeNodes(linkDefinitions=linkDefinitions_global): ret = [] ports = [4321 + i for i in range(len(linkDefinitions))] for i in range(len(linkDefinitions)): links = linkDefinitions[i] s = settings.Settings() s.name = 'Node %d' % i s.bitcoinRPCURL = "dummy" s.listenHost = "localhost" s.listenPort = ports[i] s.advertizedHost = s.listenHost s.advertizedPort = s.listenPort s.stateFile = "state_%d.dat" % i s.payLogFile = "payments_%d.log" % i s.externalMeetingPoints = ["Node4"] meetingPoints = '{}' if i == 4: meetingPoints = '{"Node4": {"_class": "MeetingPoint", "transactions": {}, "ID": "Node4"}}' linkStates = [] linkConnections = [] for link in links: localID = "link_to_%d" % link remoteID = "link_to_%d" % i linkStates.append(""""%s": { "_class": "Link", "channels": [ { "_class": "PlainChannel", "state": "open", "amountLocal": 1000, "amountRemote": 1000, "transactionsIncomingLocked": {}, "transactionsOutgoingLocked": {}, "transactionsIncomingReserved": {}, "transactionsOutgoingReserved": {} } ], "localID": "%s", "remoteID": "%s" }""" % (localID, localID, remoteID)) linkConnections.append(""""%s": { "_class": "PersistentConnection", "connectMessage": { "_class": "ConnectLink", "ID": "%s", "callbackHost": "localhost", "callbackPort": %d, "callbackID": "%s" }, "messages": [], "lastIndex": -1, "notYetTransmitted": 0, "host": "localhost", "port": %d, "closing": false }""" % (localID, remoteID, ports[i], localID, ports[link])) linkStates = ",\n".join(linkStates) linkConnections = ",\n".join(linkConnections) state = \ """{ "_class": "NodeState", "links": { %s }, "connections": { %s }, "transactions": [], "meetingPoints": %s, "payeeLinks": {}, "payerLink": null, "timeoutMessages": [] }""" % (linkStates, linkConnections, meetingPoints) with open(s.stateFile, "wb") as f: f.write(state) ret.append(s) return ret
def test_construct_with_file(self): "Test constion with file" s = settings.Settings('test_settings.conf') self.checkLoadedValues(s)
def test_construct_without_file(self): "Test constion without file" s = settings.Settings() self.checkDefaultValues(s)
def setUp(self): settings1 = settings.Settings() settings1.name = 'Node 1' settings1.bitcoinRPCURL = 'dummy' settings1.listenHost = 'localhost' settings1.listenPort = 4322 settings1.advertizedHost = settings1.listenHost settings1.advertizedPort = settings1.listenPort settings1.stateFile = 'twonodes_1.dat' settings1.payLogFile = 'payments1.log' settings1.externalMeetingPoints = ['MeetingPoint2'] with open(settings1.stateFile, 'wb') as f: f.write(''' { "_class": "NodeState", "links": { "node1": { "_class": "Link", "channels": [ { "_class": "PlainChannel", "state": "open", "amountLocal": 1000, "amountRemote": 0, "transactionsIncomingLocked": {}, "transactionsOutgoingLocked": {}, "transactionsIncomingReserved": {}, "transactionsOutgoingReserved": {} } ], "localID": "node1", "remoteID": "node2" } }, "connections": { "node1": { "_class": "PersistentConnection", "connectMessage": { "_class": "ConnectLink", "ID": "node2", "callbackHost": "localhost", "callbackPort": 4322, "callbackID": "node1" }, "messages": [], "lastIndex": -1, "notYetTransmitted": 0, "host": "localhost", "port": 4323, "closing": false } }, "transactions": [], "meetingPoints": {}, "payeeLinks": {}, "payerLink": null, "timeoutMessages": [] } ''') self.node1 = node.Node(settings1) self.node1.start() settings2 = settings.Settings() settings2.name = 'Node 2' settings2.bitcoinRPCURL = 'dummy' settings2.listenHost = 'localhost' settings2.listenPort = 4323 settings2.advertizedHost = settings2.listenHost settings2.advertizedPort = settings2.listenPort settings2.stateFile = 'twonodes_2.dat' settings2.payLogFile = 'payments2.log' with open(settings2.stateFile, 'wb') as f: f.write(''' { "_class": "NodeState", "links": { "node2": { "_class": "Link", "channels": [ { "_class": "PlainChannel", "state": "open", "amountLocal": 0, "amountRemote": 1000, "transactionsIncomingLocked": {}, "transactionsOutgoingLocked": {}, "transactionsIncomingReserved": {}, "transactionsOutgoingReserved": {} } ], "localID": "node2", "remoteID": "node1" } }, "connections": { "node2": { "_class": "PersistentConnection", "connectMessage": { "_class": "ConnectLink", "ID": "node1", "callbackHost": "localhost", "callbackPort": 4323, "callbackID": "node2" }, "messages": [], "lastIndex": -1, "notYetTransmitted": 0, "host": "localhost", "port": 4322, "closing": false } }, "transactions": [], "meetingPoints": {"MeetingPoint2": {"_class": "MeetingPoint", "transactions": {}, "ID": "MeetingPoint2"}}, "payeeLinks": {}, "payerLink": null, "timeoutMessages": [] } ''') self.node2 = node.Node(settings2) self.node2.start() #Allow links to connect time.sleep(3)