# Towards Blockchain-Based Federated Machine Learning: Smart Contract for Model Inference
## 1 Introduction
As the model is public, and the history is recorded, the blockchain stores not only the current model but all the evolutions, which can be traced back. Some blockchains also support smart contracts which could expand capabilities to develop an infrastructure for hosting heterogenous ML models, as well as to customize the workflow of some parts of the machine learning process. Smart contracts not only facilitate iterative development but also provide flexibility to implement custom FL solutions. Federated learning can benefit from blockchain infrastructure since decentralization is inherent in both. While ML and blockchain can be combined, the solutions still come with few key concerns that need to be addressed such as scalability, smart contract vulnerabilities, and complicated testing. Blockchain-based ML solutions could further be customized by integrating external ML libraries implemented using blockchain oracles [11; 12; 13]. Oracles act as decentralized web services, which extend the capabilities of blockchain smart contract implementations. Oracles introduce flexibility in blockchain-based ML implementations by enabling the usage of virtually any programming language, which does not need to be compatible with the language supported by the smart contract. This way the existing solutions for ML can be reused in blockchain while simultaneously benefiting from DLT functionality, which allows storing transactions, keeping a distributed exchange of ML models, and data transparent and auditable. The paper is related to research on the specification of blockchain-based scientific processes [14] in terms of smart contract development for federated learning decentralization. Since blockchain technology solutions are quite innovative and complex, visual representation techniques can help describe the implementation logic. Both in [14] and in this paper, unified modelling language (UML) [15] diagrams are used to outline the proposed solution details and to demonstrate the behaviour and structure of the implemented smart contract. In our research, a blockchain architecture for the ML model inference is implemented for experimental evaluation. We use Hyperledger Fabric blockchain [16] as a platform of choice, as Hyperledger supports smart contract calls to external APIs, and features a modular infrastructure by adopting the microservice ecosystem. Hyperledger Fabric enables the implementation of smart contracts, which are referred to as chaincodes in the Hyperledger infrastructure. It also supports existing programming languages such as Go, Javascript, Java, and service-based architecture. In our experiments, we implement a chaincode dedicated to the logistic regression model inference using Go. The logistic regression was chosen as a proof of concept due to its simplicity and popularity for the ML detection task [17]. We develop two experimental implementations: A model inference chaincode, and a model inference oracle service. The execution logic implemented in the chaincode and the oracle service is identical. To test the performance of these blockchain implementations we conduct two experiments where we benchmark the ML model validation performance on generated synthetic 2D dataset [18] and real-world EEG (electroencephalogram) eye state data [19]. In the experiments, we compare the runtime of functions which were executed using either the chaincode or oracle service. As existing approaches implementing the FL using blockchain are limited in scope with respect to the available models, the goal of our work is to expand the set of ML models which could benefit from decentralization using blockchain technologies and to facilitate the development of such solutions. Therefore, we have to determine whether there is a substantial difference in the model inference runtime between chaincode variants, where the code is encapsulated entirely into a smart contract, and a standalone microservice, which is used as a trusted oracle inside a smart contract.