Changeset 294

Show
Ignore:
Timestamp:
04/30/09 21:15:23 (3 years ago)
Author:
ocjhb
Message:

first redeem, needs error handling and coin picking

Location:
trunk/sandbox/jhb/oc2
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/sandbox/jhb/oc2/coinsplitting.py

    r287 r294  
    2828        if rest > 0 and token <= rest: 
    2929            picked.append(token) 
    30     return picked             
     30    return picked          
     31 
    3132if __name__ == '__main__': 
    3233    dl = [[1,2,5,10,20,50,100],[1,3,9,27],[1,3,5,7,11,13,17,19,23],[1,17,33]] 
  • trunk/sandbox/jhb/oc2/testissuer.py

    r290 r294  
    2323 
    2424    print 'issuer: setup currency discription' 
    25     denominations=[0,1,2,5,10,20] 
     25    denominations=[1,2,5,10,20] 
    2626    cdd = issuer.makeCDD('TestCent','tc',[str(d) for d in denominations],'http://192.168.2.101:%s/' % port,'') 
    2727    mint.setCDD(cdd) 
     
    4747    authorizerstorage.save() 
    4848 
     49 
     50def address_string(self): 
     51    host, port = self.client_address[:2] 
     52    return host 
     53 
    4954print 'Starting up server' 
    5055Handler.issuer = issuer 
     
    5257Handler.authorizer = authorizer 
    5358httpd = BaseHTTPServer.HTTPServer(("", port), Handler) 
     59httpd.address_string = address_string 
    5460httpd.serve_forever() 
  • trunk/sandbox/jhb/oc2/wallet.py

    r290 r294  
    170170        return coinsplitting.tokenizer([int(d) for d in denominations],amount)                    
    171171 
    172          
     172    def pickForSpending(self,amount,coins): 
     173        tmp = [(c.denomination,c) for c in coins] 
     174        tmp.sort() 
     175        tmp.reverse() 
     176        coins = [t[1] for t in tmp] 
     177        picked = [] 
     178        for coin in coins: 
     179            sumpicked = sum([int(c.denomination) for c in picked]) 
     180            if sumpicked < amount: 
     181                if int(coin.denomination) <= (amount - sumpicked): 
     182                    picked.append(coin) 
     183            else: 
     184                break 
     185        return picked                 
     186 
    173187 
    174188#################################higher level############################# 
     
    181195            currency['cdds'].append(cdd) 
    182196 
    183     def buyCoins(self,transport,amount,target): 
     197    def mintCoins(self,transport,amount,target): 
    184198        cdd = self.askLatestCDD(transport) 
    185199        currency = self.getCurrency(cdd.currencyId) 
     
    217231            i += 1 
    218232        self.storage.save() 
     233 
     234    def getAllCoins(self,currencyId): 
     235        currency = self.getCurrency(currencyId) 
     236        return currency['coins'] 
     237 
     238 
     239 
     240    def redeemCoins(self,transport,amount,target): 
     241        cdd = self.askLatestCDD(transport) 
     242        currency = self.getCurrency(cdd.currencyId) 
     243        coins = currency['coins'] 
     244        picked = self.pickForSpending(amount,coins) 
     245        tid = self.makeSerial() 
     246        response = self.requestTransfer(transport,tid,target,[],picked) 
     247        newcoins = [c for c in coins if c not in picked] 
     248        currency['coins'] = newcoins 
     249 
     250