def Rebuild(self): super(UserWallet, self).Rebuild() for c in Coin.select(): c.delete_instance() for tx in Transaction.select(): tx.delete_instance() self.__log.debug("deleted coins and transactions %s %s " % (Coin.select().count(), Transaction.select().count()))
def Rebuild(self): try: super(UserWallet, self).Rebuild() logger.debug("wallet rebuild: deleting %s coins and %s transactions" % (Coin.select().count(), Transaction.select().count())) for c in Coin.select(): c.delete_instance() for tx in Transaction.select(): tx.delete_instance() except Exception as e: print("Could not rebuild %s " % e)
def DeleteAddress(self, script_hash): success, coins_toremove = super(UserWallet, self).DeleteAddress(script_hash) for coin in coins_toremove: try: c = Coin.get(TxId=bytes(coin.Reference.PrevHash.Data), Index=coin.Reference.PrevIndex) c.delete_instance() except Exception as e: logger.error("Could not delete coin %s %s " % (coin, e)) todelete = bytes(script_hash.ToArray()) for c in Contract.select(): address = c.Address if address.ScriptHash == todelete: c.delete_instance() address.delete_instance() try: address = Address.get(ScriptHash=todelete) address.delete_instance() except Exception as e: pass return True, coins_toremove
def Rebuild(self): self._lock.acquire() try: super(UserWallet, self).Rebuild() logger.debug("wallet rebuild: deleting %s coins and %s transactions" % (Coin.select().count(), Transaction.select().count())) for c in Coin.select(): c.delete_instance() for tx in Transaction.select(): tx.delete_instance() finally: self._lock.release() logger.debug("wallet rebuild complete")
def OnCoinsChanged(self, added, changed, deleted): for coin in added: addr_hash = bytes(coin.Output.ScriptHash.Data) try: address = Address.get(ScriptHash=addr_hash) c = Coin( TxId=bytes(coin.Reference.PrevHash.Data), Index=coin.Reference.PrevIndex, AssetId=bytes(coin.Output.AssetId.Data), Value=coin.Output.Value.value, ScriptHash=bytes(coin.Output.ScriptHash.Data), State=coin.State, Address=address ) c.save() logger.debug("saved coin %s " % c) except Exception as e: logger.error("COULDN'T SAVE!!!! %s " % e) for coin in changed: for hold in self._holds: if hold.Reference == coin.Reference and coin.State & CoinState.Spent > 0: hold.IsComplete = True hold.save() try: c = Coin.get(TxId=bytes(coin.Reference.PrevHash.Data), Index=coin.Reference.PrevIndex) c.State = coin.State c.save() except Exception as e: logger.error("Coulndn't change coin %s %s (coin to change not found)" % (coin, e)) for coin in deleted: for hold in self._holds: if hold.Reference == coin.Reference: hold.IsComplete = True hold.save() try: c = Coin.get(TxId=bytes(coin.Reference.PrevHash.Data), Index=coin.Reference.PrevIndex) c.delete_instance() except Exception as e: logger.error("could not delete coin %s %s " % (coin, e))
def LoadCoins(self): coins = {} try: for coin in Coin.select(): reference = CoinReference(prev_hash=UInt256(coin.TxId), prev_index=coin.Index) output = TransactionOutput(UInt256(coin.AssetId), Fixed8(coin.Value), UInt160(coin.ScriptHash)) walletcoin = WalletCoin.CoinFromRef(reference, output, coin.State) coins[reference] = walletcoin except Exception as e: logger.error("could not load coins %s " % e) return coins
def DeleteAddress(self, script_hash): success, coins_toremove = super(UserWallet, self).DeleteAddress(script_hash) for coin in coins_toremove: try: c = Coin.get(TxId=bytes(coin.Reference.PrevHash.Data), Index=coin.Reference.PrevIndex) c.delete_instance() print("deleted coin!!!") except Exception as e: print("Couldnt delete coin %s %s " % (e, coin)) self.__log.debug("could not delete coin %s %s " % (coin, e)) address = Address.get(ScriptHash=bytes(script_hash.ToArray())) address.delete_instance() return True, coins_toremove
def OnCoinsChanged(self, added, changed, deleted): for coin in added: addr_hash = bytes(coin.Output.ScriptHash.Data) try: address = Address.get(ScriptHash=addr_hash) c = Coin( TxId=bytes(coin.Reference.PrevHash.Data), Index=coin.Reference.PrevIndex, AssetId=bytes(coin.Output.AssetId.Data), Value=coin.Output.Value.value, ScriptHash=bytes(coin.Output.ScriptHash.Data), State=coin.State, Address=address ) c.save() except Exception as e: logger.error("[Path: %s ] Could not create coin: %s " % (self._path, e)) for coin in changed: for hold in self._holds: if hold.Reference == coin.Reference and coin.State & CoinState.Spent > 0: hold.IsComplete = True hold.save() try: c = Coin.get(TxId=bytes(coin.Reference.PrevHash.Data), Index=coin.Reference.PrevIndex) c.State = coin.State c.save() except Exception as e: logger.error("[Path: %s ] could not change coin %s %s (coin to change not found)" % (self._path, coin, e)) for coin in deleted: for hold in self._holds: if hold.Reference == coin.Reference: hold.IsComplete = True hold.save() try: c = Coin.get(TxId=bytes(coin.Reference.PrevHash.Data), Index=coin.Reference.PrevIndex) c.delete_instance() except Exception as e: logger.error("[Path: %s] could not delete coin %s %s " % (self._path, coin, e))
def OnCoinsChanged(self, added, changed, deleted): if len(added) > 0 or len(changed) > 0 or len(deleted) > 0: pass for coin in added: addr_hash = bytes(coin.Output.ScriptHash.Data) address = Address.get(ScriptHash=addr_hash) try: c = Coin(TxId=bytes(coin.Reference.PrevHash.Data), Index=coin.Reference.PrevIndex, AssetId=bytes(coin.Output.AssetId.Data), Value=coin.Output.Value.value, ScriptHash=bytes(coin.Output.ScriptHash.Data), State=coin.State, Address=address) c.save() self.__log.debug("saved coin %s " % c) except Exception as e: print("COLUDNT SAVE!!!! %s " % e) for coin in changed: try: c = Coin.get(TxId=bytes(coin.Reference.PrevHash.Data), Index=coin.Reference.PrevIndex) c.State = coin.State c.save() except Exception as e: print("Coulndnt change coin %s %s" % (coin, e)) self.__log.debug("coin to change not found! %s %s " % (coin, e)) for coin in deleted: try: c = Coin.get(TxId=bytes(coin.Reference.PrevHash.Data), Index=coin.Reference.PrevIndex) c.delete_instance() except Exception as e: print("Couldnt delete coin %s %s " % (e, coin)) self.__log.debug("could not delete coin %s %s " % (coin, e))
def OnCoinsChanged(self, added, changed, deleted): for coin in added: addr_hash = bytes(coin.Output.ScriptHash.Data) try: address = Address.get(ScriptHash=addr_hash) c = Coin(TxId=bytes(coin.Reference.PrevHash.Data), Index=coin.Reference.PrevIndex, AssetId=bytes(coin.Output.AssetId.Data), Value=coin.Output.Value.value, ScriptHash=bytes(coin.Output.ScriptHash.Data), State=coin.State, Address=address) c.save() logger.debug("saved coin %s " % c) except Exception as e: logger.error("COULDN'T SAVE!!!! %s " % e) for coin in changed: try: c = Coin.get(TxId=bytes(coin.Reference.PrevHash.Data), Index=coin.Reference.PrevIndex) c.State = coin.State c.save() except Exception as e: logger.error( "Coulndn't change coin %s %s (coin to change not found)" % (coin, e)) for coin in deleted: try: c = Coin.get(TxId=bytes(coin.Reference.PrevHash.Data), Index=coin.Reference.PrevIndex) c.delete_instance() except Exception as e: logger.error("could not delete coin %s %s " % (coin, e))