def set_policy_update_txs_weight( self, policy_update_txs_weight_name, policy_update_txs_weight_func): self.__policy_update_txs_weight = MetaFunc( policy_update_txs_weight_name, policy_update_txs_weight_func )
def set_policy_snapshot( self, policy_snapshot_name, policy_snapshot_func): self.__policy_snapshot = MetaFunc( policy_snapshot_name, policy_snapshot_func )
def __init__( self, genesis_transaction: Transaction, policy_update_txs_weight_name: str = None, policy_update_txs_weight_func: callable = None, policy_snapshot_name: str = None, policy_snapshot_func: callable = None, *args): self.genesis_transaction = genesis_transaction self.__policy_update_txs_weight = MetaFunc( policy_update_txs_weight_name, policy_update_txs_weight_func ) self.__policy_snapshot = MetaFunc( policy_snapshot_name, policy_snapshot_func ) self.transactions = dict() self.add_transaction(genesis_transaction)
def __init__( self, id: str, # unique ID (Address) flmodel: Flmodel = None, # NN model x_train=None, y_train=None, x_test=None, y_test=None, reputation: dict = dict(), policy_update_reputation_name: str = None, policy_update_reputation_func: callable = None, policy_update_model_weights_name: str = None, policy_update_model_weights_func: callable = None, policy_replace_model_name: str = None, policy_replace_model_func: callable = None): self.id = id self.__flmodel = flmodel self.__x_train, self.__y_train = x_train, y_train self.__x_test, self.__y_test = x_test, y_test self.__reputation = reputation self.__policy_update_reputation = MetaFunc( policy_update_reputation_name, policy_update_reputation_func) self.__policy_update_model_weights = MetaFunc( policy_update_model_weights_name, policy_update_model_weights_func) self.__policy_replace_model = MetaFunc(policy_replace_model_name, policy_replace_model_func)
class Blockchain: def __init__( self, genesis_transaction: Transaction, policy_update_txs_weight_name: str = None, policy_update_txs_weight_func: callable = None, policy_snapshot_name: str = None, policy_snapshot_func: callable = None, *args): self.genesis_transaction = genesis_transaction self.__policy_update_txs_weight = MetaFunc( policy_update_txs_weight_name, policy_update_txs_weight_func ) self.__policy_snapshot = MetaFunc( policy_snapshot_name, policy_snapshot_func ) self.transactions = dict() self.add_transaction(genesis_transaction) def print(self): print("") print("tx_num :\t", len(self.transactions.keys())) print("tx_weights:\t", { tx.id: tx.weight for tx in self.transactions.values()}) print("policies :\t", "update_txs_weight: ", self.get_policy_update_txs_weight_name()) print("policies :\t", "snapshot: ", self.get_policy_snapshot_name()) """transaction""" def get_transaction_by_id(self, id: str): return self.transactions[id] def get_latest_transaction_by_owner(self, owner: str): txs = self.get_all_transactions_by_owner(owner) times = [tx.timestamp for tx in txs] return txs[times.index(max(times))] def get_all_transactions_by_owner(self, owner: str): res = list() for tx in self.transactions.values(): if tx.owner == owner: res.append(tx) return res def get_all_predecessors_by_id(self, id: str): references_id = self.get_transaction_by_id(id).get_references_ids() predecessors = set() for r_id in references_id: predecessors.add(r_id) grands = self.get_all_predecessors_by_id(r_id) predecessors = predecessors.union(grands) return predecessors def add_transaction(self, tx: Transaction, *args): """invalid tx cases""" # TODO: Tx requires at least one refer. except the genesis one. # already exist if tx.id in self.transactions: raise ValueError # invalid references predecessors_ids = tx.get_references_ids() for p_id in predecessors_ids: if p_id not in self.transactions: raise ValueError # invalid timestamp predecessor = self.get_transaction_by_id(p_id) if predecessor.timestamp > tx.timestamp: raise ValueError # add transaction in blockchain self.transactions[tx.id] = tx # update predecessors' weight self.update_txs_weight(self, tx.id, *args) # TODO: snapshot if needed # self.snapshot() """policies""" def update_txs_weight(self, id: str, *args): return self.__policy_update_txs_weight.func(id, *args) def snapshot(self, *args): return self.__policy_snapshot.func(*args) def get_policy_update_txs_weight_name(self): return self.__policy_update_txs_weight.name def get_policy_snapshot_name(self): return self.__policy_snapshot.name def set_policy_update_txs_weight( self, policy_update_txs_weight_name, policy_update_txs_weight_func): self.__policy_update_txs_weight = MetaFunc( policy_update_txs_weight_name, policy_update_txs_weight_func ) def set_policy_snapshot( self, policy_snapshot_name, policy_snapshot_func): self.__policy_snapshot = MetaFunc( policy_snapshot_name, policy_snapshot_func )
class Node: def __init__( self, id: str, # unique ID (Address) flmodel: Flmodel = None, # NN model x_train=None, y_train=None, x_test=None, y_test=None, reputation: dict = dict(), policy_update_reputation_name: str = None, policy_update_reputation_func: callable = None, policy_update_model_weights_name: str = None, policy_update_model_weights_func: callable = None, policy_replace_model_name: str = None, policy_replace_model_func: callable = None): self.id = id self.__flmodel = flmodel self.__x_train, self.__y_train = x_train, y_train self.__x_test, self.__y_test = x_test, y_test self.__reputation = reputation self.__policy_update_reputation = MetaFunc( policy_update_reputation_name, policy_update_reputation_func) self.__policy_update_model_weights = MetaFunc( policy_update_model_weights_name, policy_update_model_weights_func) self.__policy_replace_model = MetaFunc(policy_replace_model_name, policy_replace_model_func) def print(self): print("") print("id :\t", self.id) print("weight :\t", cal_weights_hash(self.get_model_weights())) print("train :\t", self.get_train_size()) print("test :\t", self.get_test_size()) print("reputation:\t", self.get_reputation()) print("policies :\t", "update_reputation: ", self.get_policy_update_reputation_name()) print("policies :\t", "update_model_weights: ", self.get_policy_update_model_weights_name()) print("policies :\t", "replace_model: ", self.get_policy_replace_model_name()) """reputation""" def set_reputation(self, reputation: dict): self.__reputation = reputation # (id: str => amount: float) def get_reputation(self): return self.__reputation """data""" def set_train_data(self, x_train, y_train): self.__x_train, self.__y_train = x_train, y_train def set_test_data(self, x_test, y_test): self.__x_test, self.__y_test = x_test, y_test def get_train_data(self): return self.__x_train, self.__y_train def get_test_data(self): return self.__x_test, self.__y_test def get_train_size(self): return len(self.__x_train) if self.__x_train is not None else 0 def get_test_size(self): return len(self.__x_test) if self.__x_test is not None else 0 """Flmodel""" def set_model(self, flmodel: Flmodel): self.__flmodel = flmodel def get_model(self): return self.__flmodel def fit_model(self, epochs=1, callbacks=[], verbose=0): self.__flmodel.fit(self.__x_train, self.__y_train, epochs=epochs, callbacks=callbacks, verbose=verbose) def evaluate_model(self, verbose=0): return self.__flmodel.evaluate(self.__x_test, self.__y_test, verbose=verbose) def get_model_metrics(self): return self.__flmodel.get_metrics() def get_model_weights(self): return self.__flmodel.get_weights() def set_model_weights(self, new_weights): self.__flmodel.set_weights(new_weights) def predict_model(self, x_input): return self.__flmodel.predict(x_input) """policies""" def update_reputation(self, *args): return self.__policy_update_reputation.func(*args) def update_model_weights(self, *args): return self.__policy_update_model_weights.func(*args) def replace_model(self, *args): return self.__policy_replace_model.func(*args) def get_policy_update_reputation_name(self): return self.__policy_update_reputation.name def get_policy_update_model_weights_name(self): return self.__policy_update_model_weights.name def get_policy_replace_model_name(self): return self.__policy_replace_model.name def set_policy_update_reputation(self, policy_update_reputation_name, policy_update_reputation_func): self.__policy_update_reputation = MetaFunc( policy_update_reputation_name, policy_update_reputation_func) def set_policy_update_model_weights(self, policy_update_model_weights_name, policy_update_model_weights_func): self.__policy_update_model_weights = MetaFunc( policy_update_model_weights_name, policy_update_model_weights_func) def set_policy_replace_model(self, policy_replace_model_name, policy_replace_model_func): self.__policy_replace_model = MetaFunc(policy_replace_model_name, policy_replace_model_func)
def set_policy_replace_model(self, policy_replace_model_name, policy_replace_model_func): self.__policy_replace_model = MetaFunc(policy_replace_model_name, policy_replace_model_func)
def set_policy_update_model_weights(self, policy_update_model_weights_name, policy_update_model_weights_func): self.__policy_update_model_weights = MetaFunc( policy_update_model_weights_name, policy_update_model_weights_func)
def set_policy_update_reputation(self, policy_update_reputation_name, policy_update_reputation_func): self.__policy_update_reputation = MetaFunc( policy_update_reputation_name, policy_update_reputation_func)