<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dylan Sale on Medium]]></title>
        <description><![CDATA[Stories by Dylan Sale on Medium]]></description>
        <link>https://medium.com/@dylansale?source=rss-9f49ad9c3e6a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*H9iaLpfE4vVgGjlr.jpg</url>
            <title>Stories by Dylan Sale on Medium</title>
            <link>https://medium.com/@dylansale?source=rss-9f49ad9c3e6a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 03:19:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dylansale/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Breaking down the IOTA IRI Source]]></title>
            <link>https://medium.com/@dylansale/breaking-down-the-iota-iri-source-49f79db4811e?source=rss-9f49ad9c3e6a------2</link>
            <guid isPermaLink="false">https://medium.com/p/49f79db4811e</guid>
            <category><![CDATA[tangle]]></category>
            <category><![CDATA[open-source]]></category>
            <category><![CDATA[blockchain]]></category>
            <category><![CDATA[iota]]></category>
            <dc:creator><![CDATA[Dylan Sale]]></dc:creator>
            <pubDate>Sat, 02 Sep 2017 11:20:02 GMT</pubDate>
            <atom:updated>2017-09-04T01:22:39.314Z</atom:updated>
            <content:encoded><![CDATA[<p>I decided I wanted to know more about how <a href="https://iota.org/">IOTA</a> works, so I figured the best way was to “Use the Source Luke” and just look at the code. Like most software projects, it is desperately lacking in comments, so I’m going to have to make some guesses at how it works.</p><p>The IRI (IOTA Reference Implementation) is written in Java and is hosted on <a href="https://github.com/iotaledger/iri">Github</a> as an open source project under the GPLv3 License. The README has a pretty good getting started guide, so that won’t be covered here, I’ll just jump right in.</p><h3>The Entrypoint</h3><p>The main function is the entry point and can be found in <a href="https://github.com/iotaledger/iri/blob/master/src/main/java/com/iota/iri/IRI.java">src/main/java/com/iota/iri/IRI.java</a></p><p>The first thing that happens is parsing the command line arguments as defined in the README.</p><p>Next it configures the main business logic classes: Iota, IXI and API and initialises them. It also sets up a shutdown hook that is run when the process exists which seems to just do some cleanup on these classes.</p><pre>iota = new Iota(configuration);        <br>ixi = new IXI(iota);        <br>api = new API(iota, ixi);<br>...             <br>iota.init();<br>api.init();            ixi.init(configuration.string(Configuration.DefaultConfSettings.IXI_DIR));</pre><p>Interestingly the main function ends here without handing off to some sort of loop, so I assume that a separate thread or another process is running as a side effect of the init calls, which keeps the process alive. (Looks like this is done by the API Server, see below).</p><h3>Breaking down the classes</h3><p>As you can see from the code snippet above, the organisation of the code seems to be: the API depends on IXI and Iota classes, and IXI also depends on Iota, so I’ll break these down in the following order: Iota, IXI, API.</p><h4><a href="https://github.com/iotaledger/iri/blob/master/src/main/java/com/iota/iri/Iota.java">Iota</a></h4><p>This class creates and initialises all of the core database and message passing functionality of the IOTA node. It uses ZMQ for message passing (seemingly this is used for analytics, but that is not obvious) and RocksDB to persistently store the transactions in the tangle. Mostly this is just further delegating initialisation to subsystems. It seems that this class just holds a bunch of other objects as public parameters and acts as a “<a href="https://en.wikipedia.org/wiki/God_object">God object</a>” — a singleton that collects all the various services and managers that the rest of the program requires, such as configuration settings etc.</p><h4><a href="https://github.com/iotaledger/iri/blob/master/src/main/java/com/iota/iri/IXI.java">IXI</a></h4><p>IXI stands for <a href="https://blog.iota.org/iota-development-roadmap-74741f37ed01">IOTA eXtension Interface</a>. It seems to be setting up a Javascript engine for scripting purposes. It looks like it creates a thread that watches the file system and reloads the scripts into the Javascript context when they change.</p><p>The attach method is doing the work of loading the script.</p><pre>private void attach(Path pathToMain, String moduleName) {<br>        Reader ixiModuleReader;<br>        try {<br>            ixiModuleReader = new FileReader(pathToMain.toFile());<br>        } catch (FileNotFoundException e) {<br>            log.error(&quot;Could not load &quot; + pathToMain);<br>            return;<br>        }<br>        log.info(&quot;Starting script: &quot; + pathToMain);<br>        Map&lt;String, CallableRequest&lt;AbstractResponse&gt;&gt; ixiMap = new HashMap&lt;&gt;();<br>        Map&lt;String, Runnable&gt; startStop = new HashMap&lt;&gt;();</pre><pre>Bindings bindings = scriptEngine.createBindings();<br>        bindings.put(&quot;API&quot;, ixiMap);<br>        bindings.put(&quot;IXICycle&quot;, startStop);<br>        bindings.put(&quot;IOTA&quot;, iota);</pre><pre>ixiAPI.put(moduleName, ixiMap);<br>        ixiLifetime.put(moduleName, startStop);<br>        try {<br>            scriptEngine.eval(ixiModuleReader, bindings);<br>        } catch (ScriptException e) {<br>            log.error(&quot;Script error&quot;);<br>        }<br>        try {<br>            ixiModuleReader.close();<br>        } catch (IOException e) {<br>            log.error(&quot;Could not close &quot; + pathToMain);<br>        }<br>    }</pre><p>The Iota object is added to the module’s binding (global scope?) when the script is loaded, along with a map called “API” to register commands (functions) against a command name (presumably from the API class), and a map called “IXICycle” for lifecycle functions which are called when the module has lifecycle changes (only “shutdown” is called at the moment).</p><pre>private void detach(String moduleName) {<br>        Map&lt;String, Runnable&gt; ixiMap = ixiLifetime.get(moduleName);<br>        if(ixiMap != null) {<br>            Runnable stop = ixiMap.get(&quot;shutdown&quot;);<br>            if (stop != null) stop.run();<br>        }<br>        ixiLifetime.remove(moduleName);<br>    }</pre><p>The processCommand method looks up the command in the ixAPI map and calls it if it matches by module name, then subcommand. For example, if you have a module called “foo” loaded with a command “bar” registered in its API function, the command foo bar would execute that. The command is also given a Map&lt;String,Object&gt;which it uses as the argument to the function call.</p><pre>public AbstractResponse processCommand(final String command, Map&lt;String, Object&gt; request) {<br>        Map&lt;String, CallableRequest&lt;AbstractResponse&gt;&gt; ixiMap;<br>        AbstractResponse res;<br>        String substring;<br>        for (String key: ixiAPI.keySet()) {<br>            substring = command.substring(0, key.length());<br>            if(substring.equals(key)) {<br>                String subCmd = command.substring(key.length()+1);<br>                ixiMap = ixiAPI.get(key);<br>                res = ixiMap.get(subCmd).call(request);<br>                if(res != null) return res;<br>            }<br>        }<br>        return null;<br>    }</pre><p>Interestingly, the code doesn’t check that the subcommand exists in the API mapping, so passing a command that doesn’t exist could cause an error, maybe that is expected though.</p><h4><a href="https://github.com/iotaledger/iri/blob/master/src/main/java/com/iota/iri/service/API.java">API</a></h4><p>This manages the JSON API for the node, utilising the <a href="http://undertow.io/">Undertow </a>HTTP server. The full API documentation can be found <a href="https://iota.readme.io/docs/getting-started">here</a>.</p><p>Messages are sent as a JSON object to the / path of the server with a command key to specify the command to be executed and the remaining keys acting as parameters for the request. These are parsed in the process method to ensure that the required parameters are provided, and that they are the correct format, then the appropriate controller method is called. Mostly this consists of just calling methods on the Iota class’ various public member variables (most notably the Node one).</p><p>Any commands that are not recognised are passed to the IXI to be processed.</p><pre>default: {<br>                    AbstractResponse response = ixi.processCommand(command, request);<br>                    return response == null ? ErrorResponse.create(&quot;Command [&quot; + command + &quot;] is unknown&quot;) : response;<br>                }</pre><h4>Other Notable Classes</h4><p>The above seem to be the main “entrypoints” into the code, with the API class being the core of the business logic to get the Node to do things, but I noted some other important classes.</p><h4><a href="https://github.com/iotaledger/iri/blob/master/src/main/java/com/iota/iri/network/Node.java">Node</a></h4><p>Handles neighbour connections and messaging parsing/broadcasting. When a new transaction comes in via the API, it is broadcast to the neighbours using the Node class. Additionally, the neighbours will send transactions they receive, which this node then stores and re-broadcasts to its neighbours.</p><h4>Tangle</h4><p>Maps interaction such as adding transactions etc to a set of PersistenceProviders. There is a RocksDB, FileBased and ZeroMQ persistence provider. The RocksDB one seems to be the only one that actually provides persistence however, the other two just write out update logs to a file and a ZeroMQ topic.</p><h4>TipsManager</h4><p>Creates a thread that scans the Tangle every x seconds to find the current set of Tips (transactions that have no approvers). Marks tips that now have approvers as solid once they are found. Provides methods for selecting random tips (presumably for finding transactions to approve when creating a new transaction).</p><h3>General Comments</h3><p>The code is what I would consider “okay”. It utilises a God object instead of Dependency Injection and uses a lot of thread synchronisation instead of utilising serial worker queues and asynchronous code, which would improve the thread safety of the code. It doesn’t seem to use a lot of interfaces or even getter/setter methods, and some methods in the API class are doing too much and could be refactored into method calls on controller classes. There are also some pretty big issues around the imperative nature of the API. For example, the “attachToTangle” command does the computation required to hash a transaction, but stores the hasher computation (PearlDiver) in a member variable — the “ interruptAttachingToTangle” command then cancels this object’s computation, but if you have called “attachToTangle” again in the meantime then there’s no way to stop the previous hashing computation. Really each hashing computation job should be given an ID that can be referenced in the “interruptAttachingToTangle” command.</p><p>Ideally the server would use a RESTful JSON interface instead of this adhoc RPC one.</p><h3>Next Time</h3><p>I plan on looking deeper into the node-to-node communication protocol, database layer and the intended purpose of the ZeroMQ server.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=49f79db4811e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>