<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Developers Forum for XinFin XDC Network: Sina Mahmoodi</title>
    <description>The latest articles on Developers Forum for XinFin XDC Network by Sina Mahmoodi (@sina).</description>
    <link>https://www.xdc.dev/sina</link>
    <image>
      <url>https://www.xdc.dev/images/5BJ-sYWEizgNx9ytW1SSkA-LgSr5wIC2kMC-1Ied-sA/rs:fill:90:90/mb:500000/ar:1/aHR0cHM6Ly93d3cu/eGRjLmRldi91cGxv/YWRzL3VzZXIvcHJv/ZmlsZV9pbWFnZS8z/MTIyLzZkZjI5ZDYz/LWI4YTItNDliZC04/ODAzLTM0MTlkZWVm/N2RkNy5wbmc</url>
      <title>Developers Forum for XinFin XDC Network: Sina Mahmoodi</title>
      <link>https://www.xdc.dev/sina</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://www.xdc.dev/feed/sina"/>
    <language>en</language>
    <item>
      <title>Security analysis of PREVRANDAO opcode (block.prevrandao)</title>
      <dc:creator>Sina Mahmoodi</dc:creator>
      <pubDate>Tue, 17 Dec 2024 11:29:05 +0000</pubDate>
      <link>https://www.xdc.dev/sina/security-analysis-of-prevrandao-opcode-blockprevrandao-6o</link>
      <guid>https://www.xdc.dev/sina/security-analysis-of-prevrandao-opcode-blockprevrandao-6o</guid>
      <description>&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Blockchains are deterministic machines. In an indeterministic world, many real-world use-cases require randomness in their decentralized applications. We would like to warn developers of using &lt;code&gt;block.prevrandao&lt;/code&gt; in Solidity as a source of randomness in application such as lottery.&lt;/p&gt;

&lt;p&gt;This post will highlight the reasoning for this and show results of an analysis into the history of the chain which aims to detect any vulnerable contracts. 11 contracts were detected to have invoked this opcode from block 70300000 to block 80300000 (10 million recent blocks). To the authors relief the contracts detected have no funds at stake.&lt;/p&gt;

&lt;h2&gt;
  
  
  Note on PREVRANDAO
&lt;/h2&gt;

&lt;p&gt;Since block 76321000 the XDC network has activated a version of EIP-4399. It adds the PREVRANDAO opcode to the EVM, or rather replaces the existing opcode DIFFICULTY which was meaningless in the DPoS consensus algorithm of XDC. This new opcode is exposed to developers through &lt;code&gt;block.prevrandao&lt;/code&gt; since Solidity v0.8.18.&lt;/p&gt;

&lt;p&gt;PREVRANDAO essentially returns the keccak256 hash of the current block number. The reader might recognize that block numbers are always incrementing. This means that the value of PREVRANDAO can be precomputed for every future block in advance with negligible amount of computation.&lt;/p&gt;

&lt;p&gt;The authors advise against using &lt;code&gt;block.prevrandao&lt;/code&gt; (or &lt;code&gt;block.difficulty&lt;/code&gt;) as a source of randomness in applications, specially when funds can be moved on a condition based on the random number, as in lottery applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Analysis of existing contracts
&lt;/h2&gt;

&lt;p&gt;The goal here was to do a thorough analysis of the chain, find any contracts that might be using this opcode and assess their risk level, try to contact the maintainers if necessary.&lt;/p&gt;

&lt;p&gt;A tool was developed (&lt;a href="https://www.xdc.dev/juklee0ira/using-a-tracer-to-locate-contracts-j3a"&gt;read more&lt;/a&gt;) which is able to crunch through past blocks and transactions and find any invocation of this (or any other) opcode. We ran this tool for the range of blocks between 70300000 to 80300000 and found the following contracts as potentially vulnerable:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://xdcscan.com/address/0x07bcc79ee3bb77b82cf66ffb48dc8fa19b3bd017"&gt;0x07bcc79ee3bb77b82cf66ffb48dc8fa19b3bd017&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://xdcscan.com/address/0x1dba493855dd9ed04be903f3fd30288465bb8301"&gt;0x1dba493855dd9ed04be903f3fd30288465bb8301&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://xdcscan.com/address/0x1eec6f02a5e237184a00adbc7842faabc433944e"&gt;0x1eec6f02a5e237184a00adbc7842faabc433944e&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://xdcscan.com/address/0x28d7c6f15a7151ded8a30913df523652c7f0ece6"&gt;0x28d7c6f15a7151ded8a30913df523652c7f0ece6&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://xdcscan.com/address/0x60df7c8b3892f5cefd388d156230a40a6f6ea51b"&gt;0x60df7c8b3892f5cefd388d156230a40a6f6ea51b&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://xdcscan.com/address/0x7526b430b9d8a6f75da2d21be07fe1b11635cdde"&gt;0x7526b430b9d8a6f75da2d21be07fe1b11635cdde&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://xdcscan.com/address/0x7bf8b41759737957694b57c8f3939a7abfa12ac5"&gt;0x7bf8b41759737957694b57c8f3939a7abfa12ac5&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://xdcscan.com/address/0x9bd9d6c5dc73facfe662a8692f03e844f70b4c0d"&gt;0x9bd9d6c5dc73facfe662a8692f03e844f70b4c0d&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://xdcscan.com/address/0xad749e101f8291418203d5b81eef5f088362e7d5"&gt;0xad749e101f8291418203d5b81eef5f088362e7d5&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://xdcscan.com/address/0xd1110f51da7cc743acce406889cc1897ea5e3a08"&gt;0xd1110f51da7cc743acce406889cc1897ea5e3a08&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://xdcscan.com/address/0xda8474fb954c76bd2cd23d789c3f3b28c832600d"&gt;0xda8474fb954c76bd2cd23d789c3f3b28c832600d&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After assessing there is no funds at risk we have decided to publish this post for the community's benefit. If you have any question about this please reach out.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Analysis of upstream security advisories</title>
      <dc:creator>Sina Mahmoodi</dc:creator>
      <pubDate>Mon, 26 Aug 2024 09:13:36 +0000</pubDate>
      <link>https://www.xdc.dev/sina/analysis-of-upstream-security-advisories-12m0</link>
      <guid>https://www.xdc.dev/sina/analysis-of-upstream-security-advisories-12m0</guid>
      <description>&lt;p&gt;We've gone through all the &lt;a href="https://github.com/ethereum/go-ethereum/security/advisories"&gt;advisories&lt;/a&gt; published by the upstream go-ethereum repository to make sure XDC is not vulnerable to those issues. We can report that all the issues have been either patched in XDC or are not relevant. More details follow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://github.com/ethereum/go-ethereum/security/advisories/GHSA-4xc9-8hmq-j652"&gt;CVE-2024-32972&lt;/a&gt;: XDC is not affected as the issue was in the implementation of eth/68 and XDC is on eth/62 (eth/64 on testnet).&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/ethereum/go-ethereum/security/advisories/GHSA-ppjg-v974-84cm"&gt;CVE-2023-40591&lt;/a&gt;: This has been patched by &lt;a href="https://github.com/XinFinOrg/XDPoSChain/commit/4a333ad63ee96459cede19da1a9b3d2b8227c756"&gt;4a333ad&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/ethereum/go-ethereum/security/advisories/GHSA-wjxw-gh3m-7pm5"&gt;CVE-2022-29177&lt;/a&gt;: Fixed by &lt;a href="https://github.com/XinFinOrg/XDPoSChain/pull/242"&gt;https://github.com/XinFinOrg/XDPoSChain/pull/242&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/ethereum/go-ethereum/security/advisories/GHSA-59hh-656j-3p7v"&gt;CVE-2021-41173&lt;/a&gt;: Not relevant as XDC doesn't implement the snap protocol.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/ethereum/go-ethereum/security/advisories/GHSA-9856-9gg9-qcmq"&gt;CVE-2021-39137&lt;/a&gt;: Fixed as part of &lt;a href="https://github.com/XinFinOrg/XDPoSChain/pull/133"&gt;https://github.com/XinFinOrg/XDPoSChain/pull/133&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/ethereum/go-ethereum/security/advisories/GHSA-xw37-57qp-9mm4"&gt;CVE-2020-26265&lt;/a&gt;: The commit that introduced this issue (&lt;a href="https://github.com/ethereum/go-ethereum/commit/223b950944f494a5b4e0957fd9f92c48b09037ad"&gt;223b95&lt;/a&gt;) was never introduced to XDC.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/ethereum/go-ethereum/security/advisories/GHSA-r33q-22hv-j29q"&gt;CVE-2020-26264&lt;/a&gt;: To my knowledge no XDC LES servers are in production.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/ethereum/go-ethereum/security/advisories/GHSA-m6gx-rhvj-fh52"&gt;CVE-2020-28362&lt;/a&gt;: This has been an issue of XDC nodes built with Go &amp;lt;1.15.5 or &amp;lt;1.14.12. XDC cannot be built with such old versions anymore.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/ethereum/go-ethereum/security/advisories/GHSA-jm5c-rv3w-w83m"&gt;CVE-2020-26242&lt;/a&gt;: XDC is on &lt;a href="https://github.com/XinFinOrg/XDPoSChain/blob/3fb1a015e0500cdc339707ebb26db8392a30aa5d/go.mod#L22"&gt;v1.2.4&lt;/a&gt; of &lt;code&gt;holiman/uint256&lt;/code&gt;. The vulnerability had been resolved in &lt;code&gt;v1.1.1&lt;/code&gt;. Furthermore the precompiles have not been migrated to use uint256.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/ethereum/go-ethereum/security/advisories/GHSA-69v6-xc2j-r2jf"&gt;CVE-2020-26241&lt;/a&gt;: Fixed in &lt;a href="https://github.com/XinFinOrg/XDPoSChain/pull/255"&gt;https://github.com/XinFinOrg/XDPoSChain/pull/255&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/ethereum/go-ethereum/security/advisories/GHSA-v592-xf75-856p"&gt;CVE-2020-26240&lt;/a&gt;: Not relevant as XDC is not on Ethash PoW algorithm.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>xdc</category>
      <category>security</category>
    </item>
    <item>
      <title>[Proposal] Tracing bottleneck analysis</title>
      <dc:creator>Sina Mahmoodi</dc:creator>
      <pubDate>Tue, 04 Jun 2024 14:19:55 +0000</pubDate>
      <link>https://www.xdc.dev/sina/tracing-bottleneck-analysis-15ag</link>
      <guid>https://www.xdc.dev/sina/tracing-bottleneck-analysis-15ag</guid>
      <description>&lt;h2&gt;
  
  
  XDC tracing bottleneck
&lt;/h2&gt;

&lt;p&gt;It was reported that tracing a block will often take a long time and time out in the end which causes issue for indexers such as elliptic. Here we investigate the bottleneck and propose a solution.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cause of issue
&lt;/h3&gt;

&lt;p&gt;To trace a block/transaction the state needs to be prepared and it would be re-executed via EVM in addition to extra processing for the tracing. It is easily demonstrated via a few queries that the issue lies in the re-execution. Let's observe:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ time curl -X POST -H "Content-Type: application/json" --data '{"method":"debug_traceTransaction","params":["0x52316c4e59d7db74801c8370044eff4a7eb58b79daec81bb9c0e147762cb44ba", { "tracer": "noopTracer", "timeout": "600s" }],"id":1,"jsonrpc":"2.0"}' https://rpc.xinfin.network
{"jsonrpc":"2.0","id":1,"result":{}}
curl -X POST -H "Content-Type: application/json" --data    0.02s user 0.01s system 4% cpu 0.474 total
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This re-executes a simple transfer in the middle of the block. Time taken is 0.5s.&lt;/p&gt;

&lt;p&gt;The following transaction executes a contract and uses 40k gas:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ time curl -X POST -H "Content-Type: application/json" --data '{"method":"debug_traceTransaction","params":["0x7204e1f9339ee3fc9ec7bf75bf077470a2326e6e314f3b5e8222b038d0314db4", { "tracer": "noopTracer", "timeout": "600s" }],"id":1,"jsonrpc":"2.0"}' https://rpc.xinfin.network
{"jsonrpc":"2.0","id":1,"result":{}}
curl -X POST -H "Content-Type: application/json" --data    0.02s user 0.01s system 5% cpu 0.463 total
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again around 0.5s.&lt;/p&gt;

&lt;p&gt;Now here's a transaction that's burning more than 6 million gas. First we use the noopTracer which returns after roughly 9s. And &lt;code&gt;callTracer&lt;/code&gt; which actually does processing of the data fails with timeout.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;❯ time curl -X POST -H "Content-Type: application/json" --data '{"method":"debug_traceTransaction","params":["0x4b5f5dc7c86fdc275fe5466c58ffca3ae6a76eded952a73c6d88b836183fa4a4", { "tracer": "noopTracer", "timeout": "600s" }],"id":1,"jsonrpc":"2.0"}' https://rpc.xinfin.network
{"jsonrpc":"2.0","id":1,"result":{}}
curl -X POST -H "Content-Type: application/json" --data    0.02s user 0.01s system 0% cpu 9.397 total

~ 9s
❯ time curl -X POST -H "Content-Type: application/json" --data '{"method":"debug_traceTransaction","params":["0x4b5f5dc7c86fdc275fe5466c58ffca3ae6a76eded952a73c6d88b836183fa4a4", { "tracer": "callTracer", "timeout": "600s" }],"id":1,"jsonrpc":"2.0"}' https://rpc.xinfin.network
{"error":"http server timeout"}curl -X POST -H "Content-Type: application/json" --data    0.02s user 0.01s system 0% cpu 10.387 total
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells us tracing bottleneck is (not surprisingly) in execution. To understand that better keep in mind the difference between &lt;code&gt;noopTracer&lt;/code&gt; and &lt;code&gt;callTracer&lt;/code&gt; used above. They are both Javascript codes that are invoked by the client on various events (most notably opcode execution). The difference is tho that &lt;code&gt;noopTracer&lt;/code&gt; does no processing whatsoever. However &lt;code&gt;callTracer&lt;/code&gt; actually has JS code to infer the structure of a tx (it's calls and subcalls) from those opcodes. Executing JS code from Go is expensive, hence this takes a long time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Proposal
&lt;/h3&gt;

&lt;p&gt;As mentioned above the EVM will call into the JS &lt;code&gt;step()&lt;/code&gt; function of the &lt;code&gt;callTracer&lt;/code&gt; for &lt;em&gt;every&lt;/em&gt; opcode executed. When we have 6 million gas, that is potentially a lot of opcodes. This issue has been largely mitigated in upstream go-ethereum:&lt;/p&gt;

&lt;h4&gt;
  
  
  Improving the JS callTracer
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Better JS engine
&lt;/h5&gt;

&lt;p&gt;Upstream go-ethereum has migrated from duktape to &lt;a href="https://pkg.go.dev/github.com/dop251/goja"&gt;Goja&lt;/a&gt; as its JS interpretation engine. The large difference is duktape is written via cgo. That means any time a JS code is executed (once per opcode) the runtime has to cross the Go-C barrier. On the other hand, Goja is written in native Go. It performs faster. This is a low-hanging fruit and will result in a 2x improvement. The code for the &lt;a href="https://github.com/ethereum/go-ethereum/blob/master/eth/tracers/js/goja.go"&gt;goja tracer&lt;/a&gt;.&lt;/p&gt;

&lt;h5&gt;
  
  
  OnEnter/OnExit events
&lt;/h5&gt;

&lt;p&gt;&lt;code&gt;callTracer&lt;/code&gt; gathers information about all the EVM calls within a transaction. In the current version of XDC, this is done via meticuluosly checking every opcode, seeing if it is a CALL/DELEGATECALL/STATICCALL or CREATE/CREATE2 and marking the beginning of the call. Then on a RETURN or REVERT note the end of that call. However this means this &lt;code&gt;step()&lt;/code&gt; function will be processed for every other unrelated opcode such as PUSH* LOG* DUP*, etc. too.&lt;/p&gt;

&lt;p&gt;In upstream go-ethereum new event hooks have been added to the EVM to capture calls directly. After adopting them it will be possible to update the JS callTracer to use that information instead of needing to parse that info from the opcodes. This will result in an order of magnitude improvement.&lt;/p&gt;

&lt;h4&gt;
  
  
  Native tracers FTW
&lt;/h4&gt;

&lt;p&gt;Interpreting JS code from Go is very expensive. Specially that XDC client is still using duktape as its JS engine which has to cross the Go-C barrier as well. Upstream go-ethereum has migrated to a native Go &lt;code&gt;callTracer&lt;/code&gt; which is possibly 1-2 orders of magnitude faster. This is definitely the way to go long-term. Here's the &lt;a href="https://github.com/ethereum/go-ethereum/blob/master/eth/tracers/native/call.go"&gt;code&lt;/a&gt;. This would require a larger refactor of the tracing infrastructure.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
