<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title>null_radix</title><subtitle>Recent Posts</subtitle><updated>2024-12-13T18:20:36Z</updated><link href="feed.xml" rel="self" /><link href="nullradix.eth.link" /><entry><title>SRFI-89 support of Guile</title><author><name>mjbecze</name></author><updated>2020-08-11T13:45:00Z</updated><link href="./srfi-89-support-of-guile.html" rel="alternate" /><summary type="html">&lt;p&gt;Recently I &lt;a href=&quot;https://srfi.schemers.org/srfi-89/srfi-89.html&quot;&gt;implemented SRFI-89&lt;/a&gt; for Guile. You can check out the code &lt;a href=&quot;https://gitlab.com/mjbecze/guile-srfi-89&quot;&gt;here&lt;/a&gt;. If you use Guix you can install it with &lt;code&gt;guix install guile-srfi-89&lt;/code&gt;&lt;/p&gt;&lt;h2&gt;Why?&lt;/h2&gt;&lt;p&gt;The motivation for me was that I wanted to write &lt;em&gt;more&lt;/em&gt; portable scheme code. Some say Scheme has a problem with a fractured ecosystem. One way to combat a fractured ecosystem is to write portable scheme code. SRFIs help with this.&lt;/p&gt;&lt;h2&gt;SRFI-89 -  Optional positional and named parameters&lt;/h2&gt;&lt;p&gt;Guile has a rather nice extended define know as &lt;a href=&quot;https://www.gnu.org/software/guile/manual/html_node/lambda_002a-and-define_002a.html&quot;&gt;&lt;code&gt;define*&lt;/code&gt;&lt;/a&gt; It is close to the &lt;code&gt;define*&lt;/code&gt; of SRFI-89 but there are some differences. Guile's define* allows you to add optional and named parameters to your procedures. It works like this&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;define*&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;optional-proc&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;syntax-keyword&quot;&gt;#:optional&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here, all parameters after &lt;code&gt;#:optional&lt;/code&gt; - that is &lt;code&gt;c&lt;/code&gt; &lt;code&gt;d&lt;/code&gt; and &lt;code&gt;e&lt;/code&gt; - are optional and don't have to be used when the procedure is called. The parameters a b c d are known as positional parameters, since the position of the values used when calling the produced determines what value is bound to which variable. Parameters can also have names. For example:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;define*&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;sir-yes-sir&lt;/span&gt; &lt;span class=&quot;syntax-keyword&quot;&gt;#:key&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;action&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;how-high&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;action&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;how-high&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;`&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Here, any parameter occurring after &lt;code&gt;#:key&lt;/code&gt; is a key that can be used as follows:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;sir-yes-sir&lt;/span&gt; &lt;span class=&quot;syntax-keyword&quot;&gt;#:action&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;'jump&lt;/span&gt; &lt;span class=&quot;syntax-keyword&quot;&gt;#:how-high&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The order of the keys doesn't matter, so this is equivalent to the above:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;sir-yes-sir&lt;/span&gt; &lt;span class=&quot;syntax-keyword&quot;&gt;#:how-high&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;10&lt;/span&gt;  &lt;span class=&quot;syntax-keyword&quot;&gt;#:action&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;'jump&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Keys can be used in any order and can have default values. This is really convenient when you have a procedure that may have many options.&lt;/p&gt;&lt;h3&gt;Guile and SRFI-89 Differences&lt;/h3&gt;&lt;h4&gt;Named Parameters&lt;/h4&gt;&lt;p&gt;SRFI-89's define* has nearly the same capabilities as Guile but there are a few differences.&lt;/p&gt;&lt;p&gt;Instead of using &lt;code&gt;#:keyword &amp;lt;keywords&amp;gt; ...&lt;/code&gt; SRFI-89 uses the following syntax&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;define*&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;sir-yes-sir&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword&quot;&gt;#:action&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;action&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword&quot;&gt;#:how-high&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;how-high&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;action&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;how-high&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;`&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Each keyword is specified by being a &lt;a href=&quot;https://srfi.schemers.org/srfi-88/srfi-88.html&quot;&gt;keyword object&lt;/a&gt; paired with a variable that it will bind to in the body of the defined procedure.&lt;/p&gt;&lt;h4&gt;Optional Parameters&lt;/h4&gt;&lt;p&gt;Optionals also work differently. For example:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;define*&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;optional-proc&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;#f&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;#f&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; 
  &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;With SRFI-89's every optional must have a corresponding expression. With Guile, optionals always default to #f. This is also true for named parameters.&lt;/p&gt;&lt;h4&gt;Rest&lt;/h4&gt;&lt;p&gt;The &lt;code&gt;. rest&lt;/code&gt; parameter catches any tailing arguments in a procedure. For example:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;define*&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;3&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;syntax-symbol&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;4&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;5&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;When using the rest syntax with named parameters in Guile the named parameters are also bound to the rest variable. For example:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;define*&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;syntax-keyword&quot;&gt;#:key&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;syntax-keyword&quot;&gt;#:b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;'z&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;syntax-symbol&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;'z&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword&quot;&gt;#:b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;'z&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;SRFI-89 on the other hand does not do this. In SRFI-89's world we would have the following:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;define*&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword&quot;&gt;#:b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;syntax-keyword&quot;&gt;#:b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;'z&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;syntax-symbol&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;'z&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h4&gt;Forms&lt;/h4&gt;&lt;p&gt;Guile's &lt;code&gt;define*&lt;/code&gt; has one basic form which is:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;define*&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;proc-name&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;&amp;lt;positional&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;&amp;lt;optionals&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;&amp;lt;named&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;&amp;lt;rest&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;While SRFI-89 has two forms:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;define*&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;proc-name&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;&amp;lt;positional&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;&amp;lt;optionals&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;&amp;lt;named&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;&amp;lt;rest&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;or&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;define*&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;proc-name&lt;/span&gt;  &lt;span class=&quot;syntax-symbol&quot;&gt;&amp;lt;named&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;&amp;lt;positional&amp;gt;&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;&amp;lt;optionals&amp;gt;&lt;/span&gt;  &lt;span class=&quot;syntax-symbol&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;&amp;lt;rest&amp;gt;&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This means you can write a procedure as:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;define*&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a-positional&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;an-optional-positional&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword&quot;&gt;#:named&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;#f&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;rest&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a-positional&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;an-optional-positional&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;rest&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;or&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;define*&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;test&lt;/span&gt;  &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword&quot;&gt;#:named&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;#f&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a-positional&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;an-optional-positional&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;rest&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a-positional&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;an-optional-positional&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;rest&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2&gt;Implementation&lt;/h2&gt;&lt;p&gt;To implement SRFI-89 I wanted to be as lazy as possible, performant and hygienic. The original implementation of SRFI-89 was not so performant on Guile compared to the native &lt;code&gt;define*&lt;/code&gt; and was not hygienic. To start with I didn't know much about macros so to get up to speed  I read &lt;a href=&quot;https://www.gnu.org/software/guile/manual/html_node/Macros.html&quot;&gt;Guile's documention&lt;/a&gt; and &lt;a href=&quot;https://3e8.org/pub/scheme/doc/Writing%20Macros%20with%20Syntax-case.pdf&quot;&gt;Writing Hygienic Macros in Scheme with Syntax-Case&lt;/a&gt; which I highly recommend. The latter has a bunch of nice examples that were very helpful. Also &lt;a href=&quot;http://okmij.org/ftp/Scheme/macros.html&quot;&gt;Oleg Kiselyov's page&lt;/a&gt; on marcos is quite fun.&lt;/p&gt;&lt;p&gt;To accomplish the goals of performance and being lazy I just reused Guile's &lt;code&gt;define*&lt;/code&gt;. Lets see what the macro produces.&lt;/p&gt;&lt;p&gt;Here is an example of the form &amp;lt;positionals&amp;gt;|&amp;lt;named&amp;gt;|&amp;lt;rest&amp;gt;:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;define*&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;test&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;#f&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword&quot;&gt;#:c&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;c'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword&quot;&gt;#:d&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;#t&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Which results in:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;test&lt;/span&gt;
  &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;guile:lambda*&lt;/span&gt;
    &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;syntax-keyword&quot;&gt;#:optional&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;#f&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
       &lt;span class=&quot;syntax-keyword&quot;&gt;#:key&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;guile:error&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;key c is required&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;#t&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
       &lt;span class=&quot;syntax-keyword&quot;&gt;#:rest&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;t-8ae9909742848ae-ae4&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
 
     &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;remove-keywords&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;t-8ae9909742848ae-ae4&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
       &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;c'&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
       &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;d&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
     
       &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;It's a quite straightforward mapping I think. The statement &lt;code&gt;(c (guile:error &amp;quot;key c is required&amp;quot;))&lt;/code&gt; prevents &lt;code&gt;c&lt;/code&gt; from defaulting to #f which is &lt;code&gt;guile:lambda*&lt;/code&gt; default behavior. The &lt;code&gt;let&lt;/code&gt; provides the mapping from keywords to internal variable names. And &lt;code&gt;(remove-keywords t-8ae9909742848ae-ae4)&lt;/code&gt; removes the keywords from rest.&lt;/p&gt;&lt;p&gt;The other form &amp;lt;named&amp;gt;|&amp;lt;positionals&amp;gt;|&amp;lt;rest&amp;gt; is a bit more tricky. Here is an example:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;define*&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;test&lt;/span&gt;  &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword&quot;&gt;#:a&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword&quot;&gt;#:b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;#t&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;#f&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Which results in:&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;test&lt;/span&gt;
  &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;guile:lambda*&lt;/span&gt;
    &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-keyword&quot;&gt;#:key&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;guile:error&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;key&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;required&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;#t&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
     &lt;span class=&quot;syntax-keyword&quot;&gt;#:rest&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;t-8ae9909742848ae-c77&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
       &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;let&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;a'&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
             &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
         &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;apply&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;guile:lambda*&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;syntax-keyword&quot;&gt;#:optional&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;#f&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-keyword&quot;&gt;#:rest&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;list&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;remove-keywords&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;t-8ae9909742848ae-c77&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Since &lt;code&gt;guile:lambda*&lt;/code&gt; doesn't support this form at all we need an extra &lt;code&gt;guile:lambda*&lt;/code&gt;. The outer &lt;code&gt;guile:lambda*&lt;/code&gt; processes the named parameters and leaves the positional parameters which can be accessed the &lt;code&gt;#:rest&lt;/code&gt;. The inner &lt;code&gt;guile:lambda*&lt;/code&gt; the processes the positional arguments after removing the keywords from rest.&lt;/p&gt;</summary></entry><entry><title>Ambients Applied to Ethereum</title><author><name>mjbecze</name></author><updated>2016-02-01T13:22:00Z</updated><link href="./ambients-applied-to-ethereum.html" rel="alternate" /><summary type="html">&lt;h2&gt;Part I&lt;/h2&gt;&lt;blockquote&gt;&lt;p&gt;Sometimes Ethereum is compared to a singleton Virtual Machine.  While this is correct in some sense; I think it is a bit more. First of all what is a singleton in a distributed system? It is merely a set of values that some threshold of participants have come to consensus on.  A Virtual Machine is a computational environment that is isolated from the physical computer and from other environments.&lt;/p&gt;&lt;/blockquote&gt;&lt;hr /&gt;&lt;p&gt;A hypervisor allows the physical machine to be multiplexed into many VMs. According to this definition a common hypervisor is the web browser where webpages are VMs. Another example of a hypervisor would be Ethereum as each contract  gets its own isolated computational environment.&lt;/p&gt;&lt;p&gt;There are many differences between the common web browser and Ethereum, but one of the more interesting ones is how VMs communicate and interact with each other. Web browsers don???t provide a way for VMs to directly interact while Ethereum on the other hand provides some simple mechanism for VM interaction; the opcodes CALL, DELEGATECALL, CALLCODE, CREATE.  In this post will explore the question; What other rules could exist?  Can we generalize VM interactions and provided an abstract framework for these interactions? And from this framework can we reason about distributed hypervisors?&lt;/p&gt;&lt;p&gt;Most of this post will resemble &lt;a href=&quot;https://en.wikipedia.org/wiki/Ambient_calculus&quot;&gt;ambient calculus&lt;/a&gt; but there are several notable differences from ambient calculus and what is presented here. The diagrams can be thought of as &lt;a href=&quot;https://en.wikipedia.org/wiki/Bigraph&quot;&gt;bigraphs&lt;/a&gt; but they should also be self explanatory. Part I will describe the rules of ambients and then apply them to Ethereum. Part II will discuss scaling in the terms of ambients as laid out by part I.&lt;/p&gt;&lt;h3&gt;What is an Ambient?&lt;/h3&gt;&lt;p&gt;&lt;img src=&quot;./images/image09.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;p&gt;An ambient is a bounded place in which computation can occur. A boundary determines what is inside and what is outside an ambient.  For ambients we call this boundary a membrane. The area inside an ambient is hierarchical namespace. Objects can exist inside an ambient. The objects are addressable via the namespace. There are three base elements in ambient calculus. Objects, Namespaces and Messages.&lt;/p&gt;&lt;h4&gt;Hierarchical Namespaces&lt;/h4&gt;&lt;p&gt;One of the most familiar namespace is the file system tree.  Namespaces allow us to identify objects with paths or names. Namespaces here have the following properties&lt;/p&gt;&lt;ul&gt;&lt;li&gt;For every possible path there exists a null or an object&lt;/li&gt;&lt;li&gt;At any point in the namespace you can move up or down. This is what is implied by hierarchical.&lt;/li&gt;&lt;li&gt;Every path has a root associated with it. The root uniquely identifies the content for all the paths below the root. You can think of the root as a pointer to the content of the path.&lt;/li&gt;&lt;li&gt;Paths can be read from or written to&lt;/li&gt;&lt;li&gt;Messages can be sent along paths to objects&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Object Types&lt;/h3&gt;&lt;p&gt;&lt;img src=&quot;./images/image07.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;p&gt;What is an object? It is just a value. In real life computing its just some data.  This data can be interpreted in several different ways. Any Object can be read as data. The pink circle is some data that exists in the grey ambient.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;./images/image12.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;p&gt;Objects can also be interpreted as ambients. This allows ambients to have sub-ambients. Here the orange and grey circles are ambients.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;./images/image16.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;p&gt;Objects can also be interpreted as ports. Two or more ports form a I/O channel. Channels allow messages to be sent to ambients in a different namespaces. Channels can be thought of as tunnels through an ambient???s membrane. Both the entrance and exit ports must exist somewhere in a namespace.  Here the green objects represent ports.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;./images/image06.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;p&gt;Lastly messages can also be considered to be an object. Messages are  special since they are defined as objects in motion or thought of as objects with velocity.&lt;/p&gt;&lt;p&gt;To Recap; Objects can be the following types&lt;/p&gt;&lt;pre&gt;&lt;code&gt;Objects :: =
     Data
     Port
     Ambient
     Message&lt;/code&gt;&lt;/pre&gt;&lt;h3&gt;Messages&lt;/h3&gt;&lt;p&gt;As stated above messages are objects that are in transit. Messages can be sent through a namespace and through channels. Messages have the following properties that are set by the systems message handler. They are not all intrinsically part of the message but as you will see later they make working with messages easier.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;To - The path to the destination of the message. This is immutable.&lt;/li&gt;&lt;li&gt;From - The sender of the message. This is immutable.&lt;/li&gt;&lt;li&gt;Type - The type of message. This is immutable.&lt;/li&gt;&lt;li&gt;Data - The message's body. This is immutable.&lt;/li&gt;&lt;li&gt;Heading - The destination relative to its current position. If &lt;code&gt;Heading&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt; then the message has arrived at its destination and will travel no further. This is not directly encoded in the message but instead set by the systems message handler. This is mutable.&lt;/li&gt;&lt;li&gt;Direction - Which direction the message is traveling. It can either be going ???out??? of the ambient or going ???in??? to the ambient. This is mutable.&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Message Types&lt;/h3&gt;&lt;p&gt;Message have the following types which have corresponding commands used to send them.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;Message :: =
       Set(path, value) - Sets a path to a given value
       Get(path) - Gets a value of the given path
       SetRoot(path, root) - sets the root of `path` to `root`
       GetRoot(path) - Gets the path???s root
       Call(path, data) - Sends a message along the given path
       Connect(to, from, options) - creates a channel between two paths.&lt;/code&gt;&lt;/pre&gt;&lt;h3&gt;Deleting&lt;/h3&gt;&lt;p&gt;It might not be immediately obvious how to delete an ambient or other objects. To do this we use the &lt;code&gt;Set&lt;/code&gt; and &lt;code&gt;SetRoot&lt;/code&gt; message.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;./images/image11.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;p&gt;The &lt;code&gt;Set&lt;/code&gt; message sets the value of a path.  Setting a path to null is equivalent to deleting  the contents of that path. For example &lt;code&gt;Set(???pinkAmbient???, null)&lt;/code&gt; Here the pink ambient is set to null.  Note the the orange ambient was not deleted.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;./images/image01.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;p&gt;The &lt;code&gt;SetRoot&lt;/code&gt; message sets the root of a path. If the root is set to &lt;code&gt;null&lt;/code&gt; all the path values below the root will become null. For example &lt;code&gt;CopyRoot(???pinkAmbient???, null)&lt;/code&gt; will set the pink ambient???s root to null which will also cause the orange ambient be to null.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;./images/image05.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;p&gt;Of course if we did something like SetRoot(???a???, ???pinkAmbientsRoot???) we would copy the pink Ambient and all of it contents to path ???a???&lt;/p&gt;&lt;h3&gt;Iterating the through a Namespace.&lt;/h3&gt;&lt;p&gt;In many cases it useful to iterate through all the ambients in a given namespace. One way we could approach this is to get each path in the namespace. But the problem is that most namespaces are infinite.  A better way would be to provide an explicit iteration method.  Let???s add a message&lt;/p&gt;&lt;pre&gt;&lt;code&gt;Message :: =
   Next(path) - Given a path return the next non-null path in the namespace.&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This implies that namespaces all must have an order.  Also this provides us with a nice way to build more complicated ambient operations like merging two or more ambients.  We also need this to build type checking.&lt;/p&gt;&lt;h3&gt;Membrane computing&lt;/h3&gt;&lt;p&gt;&lt;img src=&quot;./images/image02-1.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;p&gt;The ambient???s border is its membrane. It can filter message coming into and going out of it.  For example the if the grey ambient sends a &lt;code&gt;Set(???blueAmbient???, null)&lt;/code&gt;  message to the path of the ???blueAmbient??? it will go through the membrane of the orange ambient. The orange ambient can decided whether or not to let the message pass through.&lt;/p&gt;&lt;h3&gt;A Membrane API&lt;/h3&gt;&lt;p&gt;Lets walk through a small example of what programming ambients might look like.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;./images/image00.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;p&gt;Ambient A is trying send a message to  ambient B but the message has to go through Ambient C. Since A is a sub-ambient of C, C can control this message. Here is what an api for dealing with messages might look like.  Let say that we have a function ???onMessage??? that gets ran whenever the ambient gets a message.  Here is what C membrane could look like.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;/**
* Allow any message to pass through the membrane except messages from Ambient D
* @method onMessage
* @param message - the message that is leaving the ambient
* @retruns Boolean
*/

function onMessage(message) {
  if(Message.sender != ???A??? &amp;amp;&amp;amp; Message.direction == ???out???){
    Message.heading = ???D???
  }
}&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;C&lt;/code&gt; filters any messages coming from the path ???A??? that are going out of it.  Instead of letting the message go to its intended location C  reroutes the message to location ???D???.  Notice how C set the heading on the message. If &lt;code&gt;C&lt;/code&gt; set Message.heading to &lt;code&gt;null&lt;/code&gt; then the message would stop there.  C can only decide where to forward the message or to stop it.&lt;/p&gt;&lt;p&gt;The ability of ambients to filter and decide which message can travel through them is an important one.   This is also known as Membrane computing. It will allow you to build flexible and easily composable contracts. Especially when it comes to administration of sub-contracts.&lt;/p&gt;&lt;h3&gt;Mapping ambients to a Ethereum&lt;/h3&gt;&lt;p&gt;Now that we have the basics of ambients let???s apply them to a one of our favorite data structures, the &lt;a href=&quot;https://github.com/ethereum/wiki/wiki/Patricia-Tree&quot;&gt;merkle tree&lt;/a&gt;.  To start you might have already recognized the fact that a contract in Ethereum is like an ambient and the namespace is provided by the merkle tree.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;Ambients ::= contracts
Namespace ::=the merkle tree&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This could be visualized like this&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;./images/image17.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;p&gt;In Ethereum each ambient has an address that is 20 bytes long and looks like the following 0x1158c3c9a70e85d8358972810ed984c8e6ffcf0f.   Ethereum ambients have storage that allow them store store arbitrary values permanently.  Storage is accessed and manipulated with the SSTORE and SLOAD opcodes.  The equivalent to these  are the set and get messages. Also command Call is equivalent.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;Set ::= SSTORE
Get ::= SLOAD
Call :: = CALL&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;SetRoot, GetRoot and Connect do not have equivalents in Ethereum currently. SetRoot and GetRoot would read from and manipulate the underlying mekle trie&lt;/p&gt;&lt;p&gt;Now we are going to deviate from  current Ethereum  to Ethereum + Ambients.  Let us say the contract 0x1158c3c9a70e85d8358972810ed984c8e6ffcf0f sets the value ???doge??? at the addresses ???coin???  which is 636f696e in hex.  The address 0x1158c3c9a70e85d8358972810ed984c8e6ffcf0f/636f696e would then contain the value  ???doge???.   Also ???doge??? could also be interpreted as code if a Call was made to that path.&lt;/p&gt;&lt;h3&gt;Personal Accounts&lt;/h3&gt;&lt;p&gt;&lt;img src=&quot;./images/image10.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;p&gt;Lets use a personal Ethereum account as an example.  For convenience we are going to say the address of the account is ???accountA??? which will be represented as the grey ambient.  This ambient would hold the basic signature validation code as seen in the &lt;a href=&quot;https://github.com/ethereum/EIPs/issues/28&quot;&gt;currency and crypto abstraction&lt;/a&gt;. If the user wanted to place a spending limits on herself then she could create a ???Savings Account??? which would only permit a certain amount of ether to be spent per day.  Furthermore the user could create her own custom Name Reg or other financial apps. The hierarchical nature of the ambients allows you to build up administrative ???zone???. They can make code very modular since the ???saving account??? and other contracts don???t  need to have any code dedicated to checking  if the user is an admin or checking other credential since that could be done by the accountA???s ambient.&lt;/p&gt;&lt;h2&gt;Part II - Scalability&lt;/h2&gt;&lt;p&gt;In this section we will explore some ideas about scalability in terms of ambients. The basic idea of scalability is fairly simple. Most methods proposed so far involve these properties:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Separating some part of the state into a shard that is processed independent of the other shards&lt;/li&gt;&lt;li&gt;Some sort of cross validation; where some portion of a shard???s work is checked by other shards which is usually triggered by cross shard communication.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;We are also assuming we have a Proof of Stake algorithm like Casper and this algorithm is implemented in a set of ambients. Along with casper we have a currency ambient that tracks the amount of ether each account ambient has. These ambients are grouped together into the system ambient. There maybe many more ambients in the system ambient but for now we will just consider these.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;./images/image14.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;p&gt;For now we will simply assume that casper works and produces the correct state for the ???Ethereum Ambient???.&lt;/p&gt;&lt;h3&gt;Sharding&lt;/h3&gt;&lt;p&gt;If Ethereum is successful, the volume of transaction will increase over time.  After a while a high volume of transactions will cause the price of gas to increase. At a certain threshold determined by a Threshold function the Casper ambient will  produce a shard.  It should be noted that only from the casper ambient???s perspective is Ethereum sharded. Everyone else sees Ethereum as one continued namespace extending through many ambients.&lt;/p&gt;&lt;p&gt;There is some threshold that is needed to create a shard in Casper. This is not the focus of this post but we can image some of the parameters it might be based off of. It could use gasPrice to transaction ratio. Or could it use a voting system or a bidding system or combination of all them.&lt;/p&gt;&lt;p&gt;Besides the Threshold function we will assume the following about Casper:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Anyone can contest a state transition.&lt;/li&gt;&lt;li&gt;Validators are randomly assigned to shards. These form a validation group that run Casper for that shard.&lt;/li&gt;&lt;li&gt;Validator may be assigned to more than one shard&lt;/li&gt;&lt;li&gt;New shards must be initially validated by all validators&lt;/li&gt;&lt;li&gt;The total amount in bond in a validation group of a shard should be equivalent to what the shard is worth.&lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Creation of Shards&lt;/h3&gt;&lt;ol&gt;&lt;li&gt;For now we will assume that new shards will start out as an empty ambient.  But keep in mind this might not always be the case- for example a particularly successfully dapp could perhaps pay the Casper contract enough to make it worthwhile for the validator to create a shard out of it.  But for now it is empty.&lt;/li&gt;&lt;li&gt;The new shard would send a &lt;code&gt;getRoot&lt;/code&gt; to the abstract system. Then it would use &lt;code&gt;setRoot&lt;/code&gt; internally to copy the abstract system its namespace
&lt;img src=&quot;./images/image15.png&quot; alt=&quot;&quot; /&gt;&lt;/li&gt;&lt;li&gt;Part of the threshold function might be pledges from other ambients to move to a new shard once it is created. When the new shard is created, all the accounts that pledged to move are automatically moved to the new shard. This is done after the system ambient is in place. The accounts are also copied with the &lt;code&gt;CopyRoot&lt;/code&gt; command.&lt;/li&gt;&lt;li&gt;After they have been copied their original address is replaced by a port (created by the ???Connect??? command) creating a channel to their new account on the new shard.&lt;/li&gt;&lt;li&gt;The currency contract then sets the amount of ether that the shard has to the sum of the accounts that pledge to move.&lt;/li&gt;&lt;li&gt;Lastly the in the new shards currency, the contract is populated by the values of the copied accounts.
&lt;img src=&quot;./images/image03.png&quot; alt=&quot;&quot; /&gt;&lt;/li&gt;&lt;/ol&gt;&lt;h2&gt;Fractal chains?&lt;/h2&gt;&lt;p&gt;&lt;img src=&quot;./images/image08.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;p&gt;The end result will be that the top level ambients no longer ???see??? the individual accounts that are in the new shard, instead it only see the value of the sum of the account on the new shard ($82 in the diagram). While the new shard???s currency contract keeps track of the individual accounts in the shard. This resembles a fractal in the way that part of the whole is encoded in every section of the structure.&lt;/p&gt;&lt;p&gt;Also if anyone uses the old address of an ambient that moved, their messages will be forwarded to them via the channels. There are some disadvantages to using the channels; 1) its will be more costly 2) there will be higher latency.&lt;/p&gt;&lt;h4&gt;Financial Isolation - Counterfeiting Attacks&lt;/h4&gt;&lt;p&gt;The shards can be seen forming a hierarchy; each shard ambient keeping track of its accounts and the sum of the accounts in its children shards.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;./images/image04.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;p&gt;This creates a strong guarantee of the correctness of account balances. No shard can create counterfeit currency and send it to another shard. Furthermore the security is additive. Meaning that the more shards that a message crosses the stronger the guarantee that it is correct. We are assuming that every validation group will check that transaction going through it. If a transaction is going from shard C to C.A.B then shards C, C.A and C.A.B all will check the transaction and ask the shard C for merkle proof of the sender???s account. If the transaction was  found to be invalid after the validator???s approved it then the validators in all three groups would lose their deposits. If accounts were defrauded they would first be refunded from the validators deposits.&lt;/p&gt;&lt;p&gt;Let???s consider a long range counterfeit attack. This is where a validation group on a shard creates an account with an invalid amount of currency associated with it and then they just leave it in the shard. If they ever try to move it from the shard the parent validation group will request a complete transaction log that shows how the accounts got its money. At this point the attack would fail unless the parent validation group was also compromised. And in a long range attack the attackers wait until the parent validation group is compromised. The best way to counter this is to make each validation group responsible for the complete history of its shard and not to release the bonds to unbonded validators after several epochs. This gives the  current validation group an incentive to check the previous validation groups work.&lt;/p&gt;&lt;p&gt;One way in which a validation group can check the previous validation group work quickly is to just sum the transaction graph. We can think of all messages that transfer currency as forming a directed graph. Since we know the global amount of currency that the shard has, a validation group just needs to sum up the total amount the accounts had for each block in the previous epoch and check it against the known global amount.&lt;/p&gt;&lt;p&gt;To recap, several properties that can increase security are:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Give the Parent Validation group an incentive to check the work of their children.&lt;/li&gt;&lt;li&gt;Give validator an incentive to check previous work&lt;/li&gt;&lt;/ul&gt;&lt;h4&gt;Validation Group Groups (Hierarchical validation groups)&lt;/h4&gt;&lt;p&gt;Validators may have to put up a very high bond to participate in validation.  The amount of bond needed is a function of the target number of validators which is a function of the number of shards that exists.&lt;/p&gt;&lt;p&gt;But this poses a problem since if there were a higher number of validators it would be harder to coordinate a bribe attack on a shard but on the other hand Casper can become inefficient when there are large number of validators. One way this might be solved is to have validators themselves composed of validation groups. The validation group would run in a separate ambient on a separate blockchain from Ethereum.&lt;/p&gt;&lt;p&gt;In the validation group ambient, work is further subdivided into smaller chunks. Each individual validator would get assigned several ambients from the shard that validator group was assigned to. This should effectively allow even a small device to participate in validation increasing the total number of participants that briber would have to potentially coordinate with.&lt;/p&gt;&lt;h4&gt;Channels outside the Ethereum ambient&lt;/h4&gt;&lt;p&gt;To do this the validation group would create a new ambient that was connected by a channel to the validator group???s ambient. You might wonder how it is possible to link to an ambient outside of Ethereum. But underneath its straightforward.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;./images/image13.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;p&gt;Initially there would only be a validators account controlled by multisig on the Ethereum blockchain. Then the validators would create their own blockchain (represented as an ambient) which would have the same system ambients and Casper ambients as Ethereum. After creation, the validator group would connect the two ambients with a channel. Any message entering or exiting the ports the must be agreed upon by all the validators, so the channel should also be protected by a multisig. The code for the multisig would exist in the ports message handler. The channel could only be followed by those running both sets of ambients. Nodes running just the Ethereum ambient would see the channel but would not be able to follow it.&lt;/p&gt;&lt;p&gt;This provides a pattern that could be elsewhere as it provides a generic way to connect arbitrary ambients to the Ethereum blockchain. These ambients could stand for the state of your personal computer or an arbitrary feed of data. Beyond the examples given here, there are many other design patterns that make thinking in ambients useful. While there are still many lacunae ambients could be a useful model for computational environments.  Ambients adds a new dimension to Ethereum???s hypervisor. Quite literally too. It allows for contract to be even more modular and provides for a convenient way to create  administrative domains and model many everyday situations.&lt;/p&gt;&lt;h4&gt;NOTES and PROBLEMS&lt;/h4&gt;&lt;ul&gt;&lt;li&gt;SetRoot would have to fail if the root didn???t exist in the current namespace. If SetRoot was explicitly used the parent namespace (../&amp;lt;root&amp;gt;) then that tree would be copied to the namespace. If this happened between shards the tree would be serialized into a transaction.&lt;/li&gt;&lt;li&gt;&lt;p&gt;Message&lt;/p&gt;&lt;ul&gt;&lt;li&gt;All messages are assumed to be async. messages can timeout.&lt;/li&gt;&lt;li&gt;Messages all have a response. The response need to be recoded as transaction on requesting shard and the responding shard.&lt;/li&gt;&lt;li&gt;Blocks would need two parts; in transaction and out transactions.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;Capture and delete -  The sibling ambient sets a value to a path above another sibling with code for to create an ambient that deletes all of its sub-ambients.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Solution 1 any action that might affect a sibling ambient must go through its message handler&lt;/li&gt;&lt;li&gt;Solution 2 an ambient could define a message handle for all internal message that explicitly disallowed certain types of messages.&lt;/li&gt;&lt;li&gt;Solution 3 reintroduce capabilities as presented in ambient calculus&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;</summary></entry><entry><title>Merklizing ASTs</title><author><name>mjbecze</name></author><updated>2015-12-29T13:22:00Z</updated><link href="./merklizing-asts.html" rel="alternate" /><summary type="html">&lt;p&gt;&lt;em&gt;Special thanks to &lt;a href=&quot;http://juan.benet.ai/&quot;&gt;Juan Benet&lt;/a&gt; for mentioning this idea at Devcon 1.&lt;/em&gt;&lt;/p&gt;&lt;p&gt;Recently I wrote a draft of &lt;a href=&quot;https://github.com/ethereum/EIPs/issues/48&quot;&gt;EIP 105&lt;/a&gt; which propose using a subset of webassembly as Ethereum???s VM. If you aren???t aware webassembly  ???is a new, portable, size- and load-time-efficient format suitable for compilation to the web.??? One interesting note about Webassembly doesn???t compile to linear byte code. Instead it uses an  &lt;a href=&quot;https://en.wikipedia.org/wiki/Abstract_syntax_tree&quot;&gt;Abstract Syntax Tree (AST)&lt;/a&gt;. This might not surprise you if you have an experience with LLVM IR. But for me it was a new concept to have bytecode in this form.&lt;/p&gt;&lt;h2&gt;What is an AST?&lt;/h2&gt;&lt;p&gt;It is just a tree repesentation of some code. Each node of the AST represents an expression. Each function body consists of exactly one expression. Compared to the source code, an AST does not include certain elements, such as inessential punctuation and delimiters (braces, semicolons, parentheses, etc.).&lt;/p&gt;&lt;p&gt;To give you a better idea here is a textual representation of some webassembly code using &lt;a href=&quot;https://en.wikipedia.org/wiki/S-expression&quot;&gt;s-expressions&lt;/a&gt;.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;  &lt;span class=&quot;syntax-comment&quot;&gt;;; Recursive factorial
&lt;/span&gt;  &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;$factorial&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;param&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;$i&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;result&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;i64&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;if_else&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;i64.eq&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;get_local&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;i64.const&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;i64.const&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;i64.mul&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;get_local&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;call&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;$factorial&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;i64.sub&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;get_local&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;$i&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;i64.const&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The AST of the above could be displayed like&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;images/Merklizing%20ASTs.svg&quot; alt=&quot;AST example&quot; /&gt;&lt;br /&gt;figure 1 - an AST&lt;/p&gt;&lt;p&gt;This can then be &lt;a href=&quot;https://github.com/WebAssembly/design/blob/master/BinaryEncoding.md#serialized-ast&quot;&gt;serialized&lt;/a&gt; and sent across the wire.&lt;/p&gt;&lt;h2&gt;Why use an AST?&lt;/h2&gt;&lt;p&gt;The &lt;a href=&quot;https://github.com/WebAssembly/design/blob/master/Rationale.md&quot;&gt;rationale that webassembly&lt;/a&gt; gives is&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;p&gt;Trees allow a smaller binary encoding: &lt;a href=&quot;https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.108.1711&quot;&gt;JSZap&lt;/a&gt;[].&lt;/p&gt;&lt;/li&gt;&lt;li&gt;&lt;p&gt;&lt;a href=&quot;https://github.com/WebAssembly/polyfill-prototype-1&quot;&gt;Polyfill prototype&lt;/a&gt; shows simple and efficient translation to asm.js.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Some auxiliary reason might be:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Effective to JIT&lt;/li&gt;&lt;li&gt;Code Deduplication&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;The idea has a bit of an interesting history. It appears that &lt;a href=&quot;http://www.michaelfranz.com/&quot;&gt;Michael Franz&lt;/a&gt; first used the idea to compress java bytecode in a paper on Slim Binaries. The slim binaries were also implemented in the &lt;a href=&quot;https://en.wikipedia.org/wiki/Oberon_(operating_system)&quot;&gt;Oberon OS&lt;/a&gt;&lt;/p&gt;&lt;p&gt;In addition to the Slim Binaries paper here are some more papers if you are interested in the subject.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&quot;ftp://ftp.cis.upenn.edu/pub/cis700/public_html/papers/Franz97b.pdf&quot;&gt;Adaptive Compression of Syntax Trees andIterative Dynamic Code Optimization:Two Basic Technologies for Mobile-Object Systems&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&quot;ftp://ftp.cis.upenn.edu/pub/cis700/public_html/papers/Kistler96.pdf&quot;&gt;A Tree-Based Alternative to Java Byte-Codes&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;Merklized ASTs&lt;/h2&gt;&lt;p&gt;So another fun thing to do with AST is to merklize them! To review; a &lt;a href=&quot;https://en.wikipedia.org/wiki/Merkle_tree&quot;&gt;merkle tree&lt;/a&gt; is just a tree which links its nodes together by using the cryptographic hashes of the nodes.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;images/640px-Hash_Tree.svg.png&quot; alt=&quot;a merkle tree&quot; /&gt;&lt;/p&gt;&lt;p&gt;The result is one root hash that points to the root node. This allows for efficient and secure verification of the contents of large data structures.  To turn a AST into Merkle AST all you have walk from the leaf nodes up to the root hashing each node along the path.&lt;/p&gt;&lt;h2&gt;Efficiency&lt;/h2&gt;&lt;p&gt;If you look at figure one you can count 19 nodes. This is a fairly small program and the number of nodes in a larger program can add up fast. The computation time needed to merklize larger AST might start to add up rather fast. One method to increase efficiency would be to store entire subroutines in a single node and only create merkle tree branches at the point where the AST has a branch condition. For example see figure 3.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;images/Merklizing%20ASTs-grouping.svg&quot; alt=&quot;&quot; /&gt;&lt;br /&gt;figure 3 - nodes contain entire subroutines&lt;/p&gt;&lt;p&gt;In figure 3 the green blocks represent subroutines that are in a single node. Note how the &lt;code&gt;if else&lt;/code&gt; still forms a block by itself since it is a branch condition. This also provide an nice opportunity for parallelization. The interpreter or JIT can run the first branch while the next branch is still being fetched.&lt;/p&gt;&lt;h2&gt;Why Merkle ASTs?&lt;/h2&gt;&lt;p&gt;One reason is code security. It would make DLL hijack impossible. Of course how you would use DLL???s would be a bit different. Instead of open a dynamic link by a name you would reference the root hash of the routine you wanted to use. This way you have 100% confidence that you're getting the code that you want.&lt;/p&gt;&lt;p&gt;Have you ever had a software problem that you googled and found that 100s of forum posts with the same problem as you but none of their solutions worked for you? This is maybe in part caused by the fact you computer is in a different configuration or ???state??? then thiers. If we had secure merklized code this would a lot less common since the state of any given piece of software could be immediately determined or set by the root hash.&lt;/p&gt;&lt;p&gt;Perhaps one the most convincing reason is bandwidth saving and massive code deduplication. How many nearly-the-same versions of libraries you have on your harddrive? Or how many time the same function and routine is duplicated through programs?&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;images/Merklizing%20ASTs-bandwidth.svg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;p&gt;Let's say you have you already have the green nodes since they are common subroutines. You would only have to download the orange nodes. Where this bandwidth saving could be very important is things like Ethereum light clients but also for general computation. As Web Pages begin to more and more resemble apps the larger their code size becomes. In the age of ephemeral webapps code is repeatedly downloaded many times. How many times do you think you have downloaded jquery? Couple this with a peer-to-peer distribution method like &lt;a href=&quot;https://ipfs.io/&quot;&gt;IPFS&lt;/a&gt; and I think you would have a very efficient system.&lt;/p&gt;</summary></entry><entry><title>How to run contracts and create traces with Node.js</title><author><name>mjbecze</name></author><updated>2014-08-12T00:00:00Z</updated><link href="./how-to-run-contracts-and-create-traces-with-nodejs.html" rel="alternate" /><summary type="html">&lt;p&gt;&lt;img src=&quot;images/8jug5Cc.jpg&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;&lt;blockquote&gt;
A valid state transition is one which comes about through a transaction. Formally:
??&lt;sub&gt;t+1&lt;/sub&gt; ??? ??(??&lt;sub&gt;t&lt;/sub&gt; , T )
where ?? is the Ethereum state transition function. In ???thereum, ??, together with ?? are &lt;b&gt;considerably more powerful then any existing comparable system;&lt;/b&gt; ?? allows components to carry out arbitrary computation, while ?? allows components to store arbitrary state between transactions.
- The Yellow Paper
&lt;/blockquote&gt;&lt;p&gt;At the core of Etheruem is the state transition function.  Within the this function lies the ???thereum ???irtual Machine which upon ??????M code runs. In this post we will be running transactions thourgh the VM and looking at the results. A note on Terminology: the VM as descibed in the yellow paper is a subset of the state transition function. With an executioner that manipulates accounts and sets up the enviorment for the VM. Here we will just refer to the state transition function as the VM although this may not be techicanally correct. I did not like having an executioner in my code. It's a bit too barbaric for my taste.  Altogether the VM in this sense handles all changes done to the state, which wholly resided in a trie.&lt;/p&gt;&lt;p&gt;This post will explore creating transactions and processing them with the VM. You can find all the code for this post &lt;a href=&quot;https://github.com/ethereum/ethereumjs-lib/blob/master/examples/vm.js&quot;&gt;here&lt;/a&gt;;&lt;/p&gt;&lt;h2&gt;Running Contracts&lt;/h2&gt;&lt;p&gt;To get stated we will be using two libraries. &lt;code&gt;async&lt;/code&gt; and &lt;a href=&quot;https://github.com/ethereum/ethereumjs-lib&quot;&gt;&lt;code&gt;ethereum-lib&lt;/code&gt;&lt;/a&gt;&lt;br /&gt;&lt;code&gt;npm install async&lt;/code&gt;&lt;br /&gt;&lt;code&gt;npm install ethereum-lib&lt;/code&gt;&lt;/p&gt;&lt;p&gt;First import the necessary libraries and initailize some varibles.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;async&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'async'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;Ethereum&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'ethereum-lib'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;VM&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Ethereum&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;VM&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;Account&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Ethereum&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;Account&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;Transaction&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Ethereum&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;Transaction&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;Trie&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Ethereum&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;Trie&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;rlp&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Ethereum&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;rlp&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;utils&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Ethereum&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;utils&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment&quot;&gt;//creating a trie that just resides in memory
&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;stateTrie&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Trie&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment&quot;&gt;//create a new VM instance
&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;vm&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;VM&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;stateTrie&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment&quot;&gt;//we will use this later
&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;storageRoot&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment&quot;&gt;//the private/public key pare. used to sign the transactions and generate the addresses
&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;secretKey&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511'&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;publicKey&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'0406cc661590d48ee972944b35ad13ff03c7876eae3fd191e8a2f77311b0a3c6613407b5005e63d7d8d76b89d5f900cde691497688bb281e07a5052ff61edebdc0'&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Lets set up the state trie. We need to give the sender's account enougth wei to send the transaction and run the code.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-comment&quot;&gt;//sets up the initial state and runs  the callback when complete
&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;cb&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-comment&quot;&gt;//the address we are sending from
&lt;/span&gt;  &lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;address&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;utils&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;pubToAddress&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Buffer&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;publicKey&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'hex'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment&quot;&gt;//create a new account
&lt;/span&gt;  &lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;account&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Account&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment&quot;&gt;//give the account some wei. 
&lt;/span&gt;  &lt;span class=&quot;syntax-comment&quot;&gt;//This needs to be a `Buffer` or a string. all strings need to be in hex.
&lt;/span&gt;  &lt;span class=&quot;syntax-symbol&quot;&gt;account&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;balance&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'f00000000000000000'&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment&quot;&gt;//store in the trie
&lt;/span&gt;  &lt;span class=&quot;syntax-symbol&quot;&gt;stateTrie&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;address&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;account&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;serialize&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;cb&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Next we will create a transaction and process it. If you want to know more about generating transaction see this &lt;a href=&quot;https://wanderer.github.io/ethereum/2014/06/14/creating-and-verifying-transaction-with-node/&quot;&gt;post&lt;/a&gt; In this example we will forgo generating a transaction from scratch and just use the some json I generated earlier. &lt;code&gt;Transaction&lt;/code&gt; have a &lt;code&gt;toJSON&lt;/code&gt; method if you want to do the same. This transaction contains the initializtion code for the &lt;a href=&quot;https://github.com/ethereum/dapp-bin/blob/master/namereg/namereg.lll&quot;&gt;name register&lt;/a&gt;.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;rawTx&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;nonce&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'00'&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;gasPrice&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'09184e72a000'&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;gasLimit&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'2710'&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'7f4e616d65526567000000000000000000000000000000000000000000000000003055307f4e616d6552656700000000000000000000000000000000000000000000000000557f436f6e666967000000000000000000000000000000000000000000000000000073661005d2720d855f1d9976f88bb10c1a3398c77f5573661005d2720d855f1d9976f88bb10c1a3398c77f7f436f6e6669670000000000000000000000000000000000000000000000000000553360455560df806100c56000396000f3007f726567697374657200000000000000000000000000000000000000000000000060003514156053576020355415603257005b335415603e5760003354555b6020353360006000a233602035556020353355005b60007f756e72656769737465720000000000000000000000000000000000000000000060003514156082575033545b1560995733335460006000a2600033545560003355005b60007f6b696c6c00000000000000000000000000000000000000000000000000000000600035141560cb575060455433145b1560d25733ff5b6000355460005260206000f3'&lt;/span&gt;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Since running a transaction in the VM is asynchronous lets wrap this in a function which we will use later. Running this transaction should create a new name register contract. It is important to note that &lt;code&gt;vm.runTx()&lt;/code&gt; would also need a &lt;code&gt;Block&lt;/code&gt; a the second parameter if the EVM code where to use any opcodes that accessed the block propeties.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-comment&quot;&gt;//runs a transaction through the vm
&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;runTx&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;cb&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-comment&quot;&gt;//create a new transaction out of the json
&lt;/span&gt;  
  &lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;tx&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Transaction&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;raw&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
  
  &lt;span class=&quot;syntax-symbol&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;sign&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Buffer&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;secretKey&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'hex'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment&quot;&gt;//run the tx
&lt;/span&gt;  &lt;span class=&quot;syntax-symbol&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;runTx&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;createdAddress&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;createdAddress&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-comment&quot;&gt;//log some results 
&lt;/span&gt;    &lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'gas used: '&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;results&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;gasUsed&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-special&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;createdAddress&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'address created: '&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;createdAddress&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'hex'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;cb&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;vm.runTx&lt;/code&gt; 1) checks if the sending &lt;code&gt;account&lt;/code&gt; has enough wei to run then 2) runs the code and lastly 3) saves the changed accounts in the  trie. It uses a callback to returns the results of running the transaction. We are only going to log some of the results here but if you are curious george you can look at the &lt;a href=&quot;https://github.com/ethereum/ethereumjs-lib/blob/master/docs/VM.md#vmruntxtx-block-cb&quot;&gt;docs&lt;/a&gt;&lt;/p&gt;&lt;p&gt;Now lets actully run this!&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-comment&quot;&gt;//run the asynchronous functions in series
&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;series&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;syntax-special&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;runTx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;rawTx&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;syntax-close&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So you should see how much gas the transaction used and the addrres of the created contract &lt;code&gt;c8b97e77d29c5ccb5e9298519c707da5fb83c442&lt;/code&gt;.  But not too exciting yet. Lets make sure that things worked. The VM should have created a new account for the contract in the trie. Lets make sure.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-special&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;checkResults&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;cb&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;createdAddress&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Buffer&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'692a70d2e424a56d2c6c27aa97d1a86395877b3a'&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'hex'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment&quot;&gt;//fetch the new account from the trie.
&lt;/span&gt;  &lt;span class=&quot;syntax-symbol&quot;&gt;stateTrie&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;createdAddress&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;

    &lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;account&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Account&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment&quot;&gt;//we will use this later! :)
&lt;/span&gt;    &lt;span class=&quot;syntax-symbol&quot;&gt;storageRoot&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;account&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;stateRoot&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'------results------'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'nonce: '&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;account&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;nonce&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'hex'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'blance in wei: '&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;account&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;balance&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'hex'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'stateRoot: '&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;storageRoot&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'hex'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'codeHash:'&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;account&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;codeHash&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'hex'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'-------------------'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;cb&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Add this to &lt;code&gt;async.series&lt;/code&gt; like so&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-special&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;series&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;syntax-special&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;runTx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;rawTx&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;checkResults&lt;/span&gt;
&lt;span class=&quot;syntax-close&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And run. We should see the details of the new created contract. Next thing we can do is send a message to our newly created contract. Lets create another transaction to do so. This transaction should register the sending address as &amp;quot;null_radix&amp;quot;. The data field here is the RLP of  pad_32_bytes('register') and  pad_32_bytes('null_radix')&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-comment&quot;&gt;//This transaction should register the sending address as &amp;quot;null_radix&amp;quot;
&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;rawTx2&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;nonce&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'01'&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;gasPrice&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'09184e72a000'&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;gasLimit&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'2710'&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'692a70d2e424a56d2c6c27aa97d1a86395877b3a'&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'72656769737465720000000000000000000000000000000000000000000000006e756c6c5f726164697800000000000000000000000000000000000000000000'&lt;/span&gt;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment&quot;&gt;//run everything
&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;series&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;[&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;setup&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;syntax-special&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;runTx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;rawTx&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;syntax-special&quot;&gt;async&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;runTx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;rawTx2&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;checkResults&lt;/span&gt;
&lt;span class=&quot;syntax-close&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;So if everything went right we should have  &amp;quot;null_radix&amp;quot; stored at &amp;quot;0x9bdf9e2cc4dfa83de3c35da792cdf9b9e9fcfabd&amp;quot;. To see this we need to print out the name register's storage trie.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;//reads and prints the storage of a contract
function readStorage(cb) {
  //we need to create a copy of the state root
  var storageTrie = stateTrie.copy();

  //Since we are using a copy we won't change the root of `stateTrie` 
  storageTrie.root = storageRoot;

  var stream = storageTrie.createReadStream();

  console.log('------Storage------');

  //prints all of the keys and values in the storage trie
  stream.on('data', function(data) {
    console.log('key: ' + data.key.toString('hex'));
    //remove the 'hex' if you want to see the ascii values
    console.log('Value: ' + rlp.decode(data.value).toString('hex'));
  });

  stream.on('end', cb);
}

//and finally 
//run everything
async.series([
    setup,
    async.apply(runTx, rawTx),
    async.apply(runTx, rawTx2),
    checkResults,
    readStorage
]);&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;After running you should see the contents of the contract's storage.&lt;/p&gt;&lt;h2&gt;Debugging with traces&lt;/h2&gt;&lt;p&gt;Lastly lets create a trace of the EMV code. This can be very usefully for debugging (and deherbing, don't smoke and code m'kay?) contracts.&lt;/p&gt;&lt;p&gt;The VM provides a simple hook for each step the VM takes while running EVM code.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-comment&quot;&gt;//runs on each opcode
&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;vm&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;onStep&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-comment&quot;&gt;//prints the program counter, the current opcode and the amount of gas left 
&lt;/span&gt;    &lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'[vm] '&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;pc&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;' Opcode: '&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;opcode&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;' Gas: '&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;gasLeft&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;syntax-comment&quot;&gt;//prints out the current stack
&lt;/span&gt;    &lt;span class=&quot;syntax-symbol&quot;&gt;info&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;stack&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;forEach&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'[vm]    '&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;item&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'hex'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;syntax-comment&quot;&gt;//important! call `done` when your done messing around
&lt;/span&gt;    &lt;span class=&quot;syntax-symbol&quot;&gt;done&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now when you run you should see a complete trace. &lt;code&gt;onStep&lt;/code&gt; provides an object that contians all the information on the current state of the &lt;code&gt;VM&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;the &lt;code&gt;vm&lt;/code&gt; also provides a &lt;code&gt;runBlock&lt;/code&gt; function that will process a &lt;code&gt;block&lt;/code&gt; as well as a few other usefull functions.
And now I'm out of stuff to say.&lt;/p&gt;</summary></entry><entry><title>Creating Ethereum contracts and verifying messages with node.js</title><author><name>mjbecze</name></author><updated>2014-06-14T23:39:00Z</updated><link href="./creating-ethereum-contracts-and-verifying-messages-with-nodejs.html" rel="alternate" /><summary type="html">&lt;p&gt;In this article I'll be covering ethereum transaction creation and verifying transactions. First of all; whats the rational for creating transaction with with javascript? Well I, as always just want to explore and play around with ethereum. But I imagine this would be usefull in testing compilers and even more usefull, create contracts programmatically. So lets get started! :D&lt;/p&gt;&lt;p&gt;First of all we will be using &lt;a href=&quot;https://github.com/ethereum/ethereumjs-lib&quot;&gt;ethereumjs-lib&lt;/a&gt; in this post. You can install it from npm &lt;code&gt;npm install ethereumjs-lib&lt;/code&gt; or directly from git &lt;code&gt;npm install git+https://github.com/ethereum/ethereumjs-lib&lt;/code&gt;&lt;/p&gt;&lt;h2&gt;Creating a transaction&lt;/h2&gt;&lt;p&gt;After that let create a new file and put the following in it. For the those how are cut and paste handicap here is the &lt;a href=&quot;https://github.com/ethereum/ethereumjs-lib/blob/master/examples/transactions.js&quot;&gt;full example&lt;/a&gt;.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Ethereum&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;ethereum-lib&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Transaction&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Ethereum&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;Transaction&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment&quot;&gt;//create a blank transaction
&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;tx&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Transaction&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment&quot;&gt;// So now we have created a blank transaction but Its not quiet valid yet. We
&lt;/span&gt;&lt;span class=&quot;syntax-comment&quot;&gt;// need to add some things to it. Lets start with 
&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;nonce&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-symbol&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;gasPrice&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-symbol&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;gasLimit&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;1000&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-symbol&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;to&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;syntax-comment&quot;&gt;//Sending to 0, therefore we are creating a contract
&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;value&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-symbol&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;data&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;7f4e616d65526567000000000000000000000000000000000000000000000000003057307f4e616d6552656700000000000000000000000000000000000000000000000000573360455760415160566000396000f20036602259604556330e0f600f5933ff33560f601e5960003356576000335700604158600035560f602b590033560f60365960003356573360003557600035335700&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Hopefull this is pretty self explanatory. &lt;code&gt;nonce&lt;/code&gt; is the number of transaction that the sending account has sent. &lt;code&gt;gasPrice&lt;/code&gt; is the amount you are will to pay for gas. &lt;code&gt;gasLimit&lt;/code&gt; if the amount of gas you are will to spend. &lt;code&gt;to&lt;/code&gt; is the address we are sending to, in this case the zero address which creates a new contract. If we where to put a valid address here this account would become a message instead of a contract. To check which type of transaction you have, you can do&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;message&amp;quot;&lt;/span&gt;
&lt;span class=&quot;syntax-comment&quot;&gt;//or
&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;contract&amp;quot;&lt;/span&gt;
&lt;span class=&quot;syntax-comment&quot;&gt;//type updates every time you change tx.to&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;&lt;code&gt;value&lt;/code&gt; is the is the amount you are sending and &lt;code&gt;data&lt;/code&gt; is data that you are sending in this case it is initialization code for a contract.
The data is a Name Registrar contract. You can use one of the compilers to generate the code. The two main ones are lllc which is compiled with &lt;a href=&quot;https://github.com/ethereum/cpp-ethereum&quot;&gt;cpp-ethereum&lt;/a&gt; and &lt;a href=&quot;https://github.com/ethereum/serpent&quot;&gt;serpent&lt;/a&gt;. Ok now we have a transaction with data. Now we need to sign it. To do this we will need a private key. There are several way to acquire one but for now we are just going to steal one from &lt;a href=&quot;https://github.com/ethereum/cpp-ethereum&quot;&gt;AlethZero&lt;/a&gt;. That way I know that I'm create valid transaction that actually has the ether that this transaction is says that it has. If you have AlethZero running. You can select &lt;code&gt;tools&amp;gt;export selected key&lt;/code&gt;, and the copy the private key out. Here is mine.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/N0S4q3l.png&quot; alt=&quot;exporting private key&quot; /&gt;&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;privateKey&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Buffer&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-symbol&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;sign&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;privateKey&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now we have a signed transaction, but for it to be valid, the account that we signed it with needs to have a certain amount of wei in to. To see how much that this account needs we can use  &lt;code&gt;tx.getUpFrontCost&lt;/code&gt;&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;Total Amount of wei needed:&amp;quot;&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;getUpFrontCost&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;this gives the amount in wei that the account needs to have. If your wondering how this is caculated it is&lt;/p&gt;&lt;ul&gt;&lt;li&gt;data lenght in bytes * 5&lt;/li&gt;&lt;li&gt;+ 500 Default transaction fee&lt;/li&gt;&lt;li&gt;+ gasLimit * gasPrice&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Lets serialize the transaction&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;---Serialized TX----&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;serialize&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;hex&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;syntax-comment&quot;&gt;//serialize returns a buffer
&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;--------------------&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Now that we have the serialized transaction we can get AlethZero to except it by selecting &lt;code&gt;debug&amp;gt;inject transaction&lt;/code&gt; and pasting in the transaction serialization. It should then show up in the pending transaction pane.&lt;/p&gt;&lt;p&gt;&lt;img src=&quot;https://i.imgur.com/YPEkMTx.png&quot; alt=&quot;inject transaction&quot; /&gt;&lt;/p&gt;&lt;h2&gt;Parsing &amp;amp; Validating transactions&lt;/h2&gt;&lt;p&gt;If you have a transaction that you want to verify you can parse it. If you got it directly from the network it will be rlp encoded. You can decode it using the &lt;a href=&quot;https://github.com/wanderer/rlp&quot;&gt;rlp module&lt;/a&gt;. After decoding it that you should have something like this.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;rawTx&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt;  &lt;span class=&quot;syntax-open&quot;&gt;[&lt;/span&gt;
        &lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;00&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;09184e72a000&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;2710&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;0000000000000000000000000000000000000000&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;00&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;7f7465737432000000000000000000000000000000000000000000000000000000600057&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;1c&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;5e1d3a76fbf824220eafc8c79ad578ad2b67d01b0c2425eb1f1347e8f50882ab&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;5bd428537f05f9830e93792f90ea6a3e2d1ee84952dd96edbae9f658f831ab13&amp;quot;&lt;/span&gt;
    &lt;span class=&quot;syntax-close&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;tx&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Transaction&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;rawTx&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Note: &lt;code&gt;rlp.decode&lt;/code&gt; will actully produce an array of buffers &lt;code&gt;new Transaction&lt;/code&gt; will take either and array of buffers or and array of hex strings. So assuming that you were able to parse the transaction, we will now get the sender's address&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;Senders Address&amp;quot;&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;getSenderAddress&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Cool now we know who sent the tx! Lets verify the signature to make sure it not some poser.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-special&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;tx&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;verifySignature&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;Signature Checks out!&amp;quot;&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;And hopefull its verified. For the transaction to be total valid we would also need to check the account of the sender and see if they have at least &lt;code&gt;tx.getUpFrontCost()&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;And that is all for now I hope you have enjoyed the read. You can view the documentation for transaction &lt;a href=&quot;https://github.com/ethereum/ethereumjs-lib/blob/master/docs/transaction.md&quot;&gt;here&lt;/a&gt; to see all the exciting things it can do.  Let me know if you have any questions or comments.&lt;/p&gt;</summary></entry><entry><title>Validating GeoJson from the command line</title><author><name>mjbecze</name></author><updated>2014-05-26T13:22:00Z</updated><link href="./validating-geojson-from-the-command-line.html" rel="alternate" /><summary type="html">&lt;p&gt;This week I add a CLI to &lt;a href=&quot;https://gitlab.com/mjbecze/GeoJSON-Validation&quot;&gt;geojson validation module&lt;/a&gt; and will be going over how to use it in this post.  To start, install it &lt;code&gt;npm install geojson-validation -g&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;Next you need some geoJSON. We will use &lt;a href=&quot;https://raw.githubusercontent.com/wanderer/Detroit-Farm-Map/master/data/map.geojson&quot;&gt;this&lt;/a&gt; for the example. Now to check if it is valid simple run &lt;code&gt;curl https://raw.githubusercontent.com/wanderer/Detroit-Farm-Map/master/data/map.geojson | gjv&lt;/code&gt;. If it was invalid &lt;code&gt;gjv&lt;/code&gt; would print out the errors else it will just tell you its valid.&lt;/p&gt;&lt;p&gt;You can also validate local files like this &lt;code&gt;gjv file1 files2 ...&lt;/code&gt;&lt;/p&gt;</summary></entry><entry><title>Exploring Ethereum's state trie with Node.js</title><author><name>mjbecze</name></author><updated>2014-05-21T13:22:00Z</updated><link href="./exploring-ethereums-state-trie-with-nodejs.html" rel="alternate" /><summary type="html">&lt;p&gt;If you not familiar with ethereum start &lt;a href=&quot;https://www.ethereum.org/&quot;&gt;here&lt;/a&gt;. In this post I will attempt to read &lt;a href=&quot;https://github.com/ethereum/cpp-ethereum&quot;&gt;cpp-ethereum's&lt;/a&gt; state db give a root using Node.js and the &lt;a href=&quot;https://github.com/wanderer/merkle-patricia-tree&quot;&gt;merkle-patricia-tree&lt;/a&gt; module. The merkle-patricia-tree module is hot off the press so there might be bugs. If you find any please let me know. The State DB is like a global ledger; It stores balances of all the accounts was well as the code for all of the contracts and their respective balances.&lt;/p&gt;&lt;p&gt;The global state is stored in a modified merkle patricia tree (trie), which you can read more about in the &lt;a href=&quot;http://gavwood.com/Paper.pdf&quot;&gt;yellow paper&lt;/a&gt; or on the &lt;a href=&quot;https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-Patricia-Tree&quot;&gt;wiki&lt;/a&gt;. The important part is understanding what it is suppose to do, which is &amp;quot;The core of the trie, and its sole requirement in terms of the protocol specification is to provide a single 32-byte value that identifies a given set of key-value pairs.&amp;quot;&lt;/p&gt;&lt;h2&gt;Reading the db&lt;/h2&gt;&lt;p&gt;cpp-ethereum stores the trie in a level db. On linux the path to the state db is ~/.ethereum/state. Let try just opening the database and seeing what we get. To do this we are going to need install the levelup module &lt;code&gt;npm install levelup&lt;/code&gt;&lt;/p&gt;&lt;p&gt;We are going to look up one of the state roots. You can get them from althezero. In the example I'm using the genesis root hash from the latest cpp code from the dev branch. Looking up the root should give the top node in the trie.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;levelup&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'levelup'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;db&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;levelup&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'./.ethereum/state'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment&quot;&gt;//the genesis state root
&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;root&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'12582945fc5ad12c3e7b67c4fc37a68fc0d52d995bb7f7291ff41a2739a7ca16'&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment&quot;&gt;//Note: we are doing everything using binary encoding.
&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Buffer&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'hex'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'binary'&lt;/span&gt;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;We should get a print out of a Buffer... not necessarily usefully. Each node in the trie is &lt;code&gt;rlp&lt;/code&gt; encoded, so let decoded the node. To do that we are going to need to install the &lt;a href=&quot;https://github.com/wanderer/rlp&quot;&gt;rlp module&lt;/a&gt; &lt;code&gt;npm install rlp&lt;/code&gt;. and add it the code&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;rlp&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'rlp'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;syntax-symbol&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Buffer&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'hex'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;encoding&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'binary'&lt;/span&gt;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;decoded&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;rlp&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;decoded&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Yay we got one node! (hopefully). Now the node decoded should print out an array of buffers. In our case we should get a branch node with is an array of the length 17. 1-16 are more nodes and 17 is a value.&lt;/p&gt;&lt;h2&gt;Looking up Values in the Trie&lt;/h2&gt;&lt;p&gt;Now lets try to look up a key. After looking up the root node finding a key is as simple as following the key path to the right leaf. I'm skimming over the details, but you can read the specs &lt;a href=&quot;https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-Patricia-Tree&quot;&gt;here&lt;/a&gt;. First install &lt;code&gt;npm install merkle-patricia-tree&lt;/code&gt;. This module allows you to read and manipulate the trie. Lets try looking up Gav's, our patron saint of cpp programming, account. His address is &amp;quot;8a40bfaa73256b60764c1bf40675a99083efb075&amp;quot;.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Trie&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'merkle-patricia-tree'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;rlp&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'rlp'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;levelup&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'levelup'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;db&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;levelup&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'/home/null/.ethereum/state'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment&quot;&gt;//the genesis state root
&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;root&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'12582945fc5ad12c3e7b67c4fc37a68fc0d52d995bb7f7291ff41a2739a7ca16'&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;trie&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Trie&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment&quot;&gt;//gav's address
&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;gav&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Buffer&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'8a40bfaa73256b60764c1bf40675a99083efb075'&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;'hex'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-symbol&quot;&gt;trie&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;gav&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;decoded&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;rlp&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;decoded&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The accounts are rlp encoded before they are stored so we need to decoded them after we retrieve them. After decoding them we should get an array of the length 4. Which are the four fields in an account, &lt;code&gt;balance&lt;/code&gt;, &lt;code&gt;nonce&lt;/code&gt;, &lt;code&gt;stateRoot&lt;/code&gt; and &lt;code&gt;codeHash&lt;/code&gt;&lt;/p&gt;&lt;h2&gt;Streaming the Trie&lt;/h2&gt;&lt;p&gt;The Last thing we want to do is to read out the entire trie. This might be handy for debugging or just poking around for fun. To do this we are going to use &lt;code&gt;streams&lt;/code&gt;. If your not familiar with node streams &lt;a href=&quot;http://nodeschool.io/#stream-adventure&quot;&gt;here&lt;/a&gt; is a really fun place to get stated. Here the on &lt;code&gt;data&lt;/code&gt; event returns an &lt;code&gt;object&lt;/code&gt;that has two propeties; the &lt;code&gt;key&lt;/code&gt; and the &lt;code&gt;value&lt;/code&gt;. Both should be buffers.&lt;/p&gt;&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Trie&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'merkle-patricia-tree'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;rlp&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'rlp'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;levelup&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;require&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'levelup'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;db&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;levelup&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'/home/null/.ethereum/state'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;root&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-string&quot;&gt;&amp;quot;12582945fc5ad12c3e7b67c4fc37a68fc0d52d995bb7f7291ff41a2739a7ca16&amp;quot;&lt;/span&gt;
&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;trie&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;Trie&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;db&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;root&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-comment&quot;&gt;//get a read stream object
&lt;/span&gt;&lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;stream&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;trie&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;createReadStream&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-symbol&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'data'&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'key:'&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;key&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;toString&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'hex'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

  &lt;span class=&quot;syntax-comment&quot;&gt;//accouts are rlp encoded
&lt;/span&gt;  &lt;span class=&quot;syntax-special&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;decodedVal&lt;/span&gt; &lt;span class=&quot;syntax-operator&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;syntax-symbol&quot;&gt;rlp&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;decode&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;decodedVal&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;syntax-symbol&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;on&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'end'&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;syntax-special&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;val&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;syntax-open&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;syntax-symbol&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;syntax-symbol&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;syntax-open&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;syntax-string&quot;&gt;'done reading!'&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;syntax-close&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;syntax-close&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;syntax-operator&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;This should read out all the account's address and the account's contents in the trie.&lt;/p&gt;&lt;h2&gt;The End?&lt;/h2&gt;&lt;p&gt;&lt;a href=&quot;https://github.com/wanderer/merkle-patricia-tree&quot;&gt;merkle-patricia-tree&lt;/a&gt; module can do more than what I went over here. It can create, update and delete values too! amazing! And if you actually read all the way to the bottom, thanks for reading.&lt;/p&gt;</summary></entry></feed>