0

I have pyro4 blockchain class to create transfers and add them to blockchain. This file has the class implementation:

import Pyro4

@Pyro4.expose
class MyBlockChain(object):
  # constructor
  def __init__(self, chainName): 
    self.set_head(None)
    self.chainName = chainName

  def get_head(self):
    return self._head

  def set_head(self, value):
      self._head = value  # Attached behavior



  @Pyro4.expose
  def chainNameGetter(self):
    return self.chainName

  def createAccount(self, amount):
    theHead = self.get_head()
    #check if the head is null
    if theHead == None:
      tupleForBlock = ("createAccount", 1, amount)
      newBlock = MyBlock(tupleForBlock)
      self.set_head(newBlock)
      return 1
    else:
      while theHead.transaction[0] != "createAccount":
          theHead = theHead.next
      tupleForBlock = ("createAccount", theHead.transaction[1]+1, amount)
      newBlock = MyBlock(tupleForBlock)
      newBlock.next = self.get_head()
      self.set_head(newBlock)
      return theHead.transaction[1]+1
    

  def transfer(self, fromm, to, amount):
    if self.checkIfAccountExists(fromm) == -1 or self.checkIfAccountExists(to) == -1:
      return -1
    fromBalance = self.calculateBalance(fromm)
    toBalance = self.calculateBalance(to)
    if amount < 0:
      if toBalance < abs(amount):
        return -1
    else:
      if fromBalance < amount:
        return -1
    tupleForBlock = ("transfer", fromm, to, amount)
    newBlock = MyBlock(tupleForBlock)
    newBlock.next = self.get_head()
    self.set_head(newBlock)

  def exchange(self, fromm, to, toChain, amount):
    theURL = str("PYRONAME:") + str(toChain.chainNameGetter())
    transactionChain = Pyro4.Proxy(theURL)
    if transactionChain.checkIfAccountExists(to) == -1 or self.checkIfAccountExists(fromm) == -1:
      return -1
    fromBalance = self.calculateBalance(fromm)
    toBalance   = transactionChain.calculateBalance(to)
    if amount < 0:
      if toBalance < abs(amount):
        return -1
    else:
      if fromBalance < amount:
        return -1
    
    tupleForCurrentChain   = ("exchange", fromm, to, toChain, amount)
    newBlock               = MyBlock(tupleForCurrentChain)
    newBlock.next          = self.get_head()
    self.set_head(newBlock)            
    
    tupleForRemoteChain    = ("exchange", to, fromm, self.chainName, amount)
    newBlockForRemote      = MyBlock(tupleForRemoteChain)
    newBlockForRemote.next = transactionChain.get_head()
    transactionChain.set_head(newBlockForRemote) 
    
  def checkIfAccountExists(self, accountNumber):
    theHead = self.get_head()
    if theHead == None:
      return -1
    else:
      while theHead:
        if theHead.transaction[0] == "createAccount":
          if theHead.transaction[1] == accountNumber:
            return 1
        theHead = theHead.next
    return -1
  
  def calculateBalance(self, accountNumber):
    accountBalance = 0
    theHead = self.get_head()
    while theHead:
      
      if theHead.transaction[0] == "createAccount":
        if theHead.transaction[1] == accountNumber:
          accountBalance += theHead.transaction[2]
      
      if theHead.transaction[0] == "transfer":
        if theHead.transaction[1] == accountNumber:
          accountBalance -= theHead.transaction[3]
        if theHead.transaction[2] == accountNumber:
          accountBalance += theHead.transaction[3]
      theHead = theHead.next
    return accountBalance
  
  def printChain(self):
    theH = self.get_head()
    while theH:
      print(theH.transaction)
      theH = theH.next


class MyBlock(object):
  def __init__(self, transaction):  
    self.transaction = transaction
    self.next = None

This one has pyro4 server for a blockchain server :

import Pyro4
from MyBlockChain import MyBlockChain

ETH = MyBlockChain("ETH")

def main():
    Pyro4.Daemon.serveSimple(
        {
            MyBlockChain: "ETH"
            #MyBlockChain: None
        },
        ns=True)


if __name__ == "__main__":
    main()

And this is file i run to run the commands:

import Pyro4


BTC = Pyro4.Proxy("PYRONAME:BTC")
acc1 = BTC.createAccount(100)
print(acc1)
print(BTC.chainNameGetter())
bal = BTC.calculateBalance(acc1)
if bal > 20:
    BTC.transfer(acc1, 1, -60)
BTC.printChain()

ETH = Pyro4.Proxy("PYRONAME:ETH")
e1 = ETH.createAccount(30)
print(e1)
bal = ETH.calculateBalance(e1)
if bal > 20:
    ETH.transfer(e1, 1, -20)
ETH.printChain()

BTC.exchange(acc1, e1, ETH, -20)
BTC.printChain()
ETH.printChain()

Now I get this error, can someone please explain, i dont understand?:

  File "firstClient.py", line 5, in <module>
    acc1 = BTC.createAccount(100)
  File "/Users//opt/anaconda3/lib/python3.7/site-packages/Pyro4/core.py", line 185, in __call__
    return self.__send(self.__name, args, kwargs)
  File "/Users//opt/anaconda3/lib/python3.7/site-packages/Pyro4/core.py", line 476, in _pyroInvoke
    raise data  # if you see this in your traceback, you should probably inspect the remote traceback as well
TypeError: __init__() missing 1 required positional argument: 'chainName'
Ahmed Can Unbay
  • 2,694
  • 2
  • 16
  • 33

0 Answers0