Month: February 2021

  • Mining Ethereum on M1 Mac GPU

    Mining Ethereum on M1 Mac GPU

    TL;DR: It’s possible to mine Ethereum on a M1 Mac GPU. Hashrate is about 2Mh/s.

    Mining on a M1 Mac

    I’ve had my M1 MacBook Air for a bit of time now, and I also recently started mining Ethereum. I can’t help asking myself: What’s Ethereum mining performance like on a M1 Mac?

    The obvious thing to do first is to run the off-the-shelf ethminer, which gives the following error:

    ethminer 0.19.0-alpha.0
    Build: darwin/release/appleclang
    
    Unrecognized platform Apple
    Error: No usable mining devices found

    Not good. Apparently Apple GPUs are not whitelisted in ethminer. That should be easy to fix. Relevant lines are in libethash-cl/CLMiner.cpp, and I added Apple GPUs to the whitelist, pretending it’s an Intel GPU.

    Then boost won’t compile since it’s trying to compile with a -fcoalesce-templates argument, which doesn’t exist in recent clang versions. So I have to update boost to the latest version, and fix relevant asio code since ethminer was using deprecated asio APIs.

    I also need to upgrade OpenSSL to the latest version to have it support darwin + arm64.

    After getting everything to compile. Here’s the result:

    ethminer 0.19.0-17+commit.ce52c740.dirty
    Build: darwin/release/appleclang
    
     i 19:51:36          Configured pool eth-us-east1.nanopool.org:9999
     i 19:51:36          Selected pool eth-us-east1.nanopool.org:9999
     i 19:51:36          Connection remotely closed by eth-us-east1.nanopool.org
     i 19:51:36          Stratum mode : EthereumStratum/1.0.0 (NiceHash)
     i 19:51:36          Established connection to eth-us-east1.nanopool.org [144.217.14.139:9999]
     i 19:51:36          Spinning up miners...
    cl 19:51:36 cl-0     Using Device : Intel GPU 0.0 Apple M1 OpenCL 1.2  Memory : 10.67 GB (11453251584 B)
     i 19:51:36          Extranonce set to 778d
     i 19:51:36          Extranonce set to 778d
     i 19:51:36          Authorized worker [REDACTED]
     i 19:51:36          Epoch : 397 Difficulty : 10.00 Gh
     i 19:51:36          Job: c7fc5311… eth-us-east1.nanopool.org [144.217.14.139:9999]
    cl 19:51:38 cl-0     Generating split DAG + Light (total): 4.10 GB
     i 19:51:38          Job: 40a57756… eth-us-east1.nanopool.org [144.217.14.139:9999]
    cl 19:51:38 cl-0     OpenCL kernel
    cl 19:51:38 cl-0     Creating DAG buffer, size: 4.10 GB, free: 6.57 GB
    cl 19:51:38 cl-0     Creating light cache buffer, size: 65.62 MB
    cl 19:51:38 cl-0     Loading kernels
    cl 19:51:38 cl-0     Creating buffer for header.
    cl 19:51:38 cl-0     Creating mining buffer
     m 19:51:41          0:00 A0 0.00 h - cl0 0.00
     i 19:51:42          Job: 077b62f6… eth-us-east1.nanopool.org [144.217.14.139:9999]
     m 19:51:46          0:00 A0 0.00 h - cl0 0.00
     i 19:51:46          Job: 2835839e… eth-us-east1.nanopool.org [144.217.14.139:9999]
     m 19:51:51          0:00 A0 0.00 h - cl0 0.00
     m 19:51:56          0:00 A0 0.00 h - cl0 0.00
     i 19:51:57          Job: 97f724e7… eth-us-east1.nanopool.org [144.217.14.139:9999]
     m 19:52:01          0:00 A0 0.00 h - cl0 0.00
     m 19:52:06          0:00 A0 0.00 h - cl0 0.00
     m 19:52:11          0:00 A0 0.00 h - cl0 0.00
     m 19:52:16          0:00 A0 0.00 h - cl0 0.00
     i 19:52:16          Job: 54df0504… eth-us-east1.nanopool.org [144.217.14.139:9999]
     m 19:52:21          0:00 A0 0.00 h - cl0 0.00
    cl 19:52:22 cl-0     4.10 GB of DAG data generated in 44,060 ms.
     m 19:52:26          0:00 A0 184.16 Kh - cl0 184.16
     m 19:52:31          0:00 A0 1.96 Mh - cl0 1.96
     m 19:52:36          0:01 A0 1.98 Mh - cl0 1.98
     i 19:52:39          Job: d3b1da5e… eth-us-east1.nanopool.org [144.217.14.139:9999]
     m 19:52:41          0:01 A0 1.99 Mh - cl0 1.99
    cl 19:52:43 cl-0     Job: 54df0504… Sol: 0x778d000001d14c71
     i 19:52:43          **Accepted 150 ms. eth-us-east1.nanopool.org [144.217.14.139:9999]
     m 19:52:46          0:01 A1 1.95 Mh - cl0 1.95
     m 19:52:51          0:01 A1 2.07 Mh - cl0 2.07
     m 19:52:56          0:01 A1 2.00 Mh - cl0 2.00
     m 19:53:01          0:01 A1 1.98 Mh - cl0 1.98
     i 19:53:01          Job: ccc2b97f… eth-us-east1.nanopool.org [144.217.14.139:9999]
     m 19:53:06          0:01 A1 1.97 Mh - cl0 1.97
     i 19:53:07          Job: 23919d82… eth-us-east1.nanopool.org [144.217.14.139:9999]
    ^C i 19:53:10 main     Got interrupt ...
     i 19:53:10 main     Disconnected from eth-us-east1.nanopool.org [144.217.14.139:9999]
     i 19:53:10 main     Shutting down miners...
     i 19:53:16 main     Terminated!

    Code is available at https://github.com/gyf304/ethminer-m1

    Is it worth it?

    Um. Not really. At current Ethereum prices (2021-02-26), it generates $0.14 of profit per day. It’s still a profit, but very miniscule.

  • LAN Party VPN – My experience with WinTun, Windows Firewall, NSIS and Windows Defender

    I’ve recently made LAN Party VPN. It makes playing LAN games with friends on Discord effortless. LPVPN is written in C++, mostly because Discord SDK is only available in C, C++ and C#. So I didn’t have much of an option here.

    The idea of LPVPN is rather simple, get IP packets from WinTun (the Windows implementation of TUN by WireGuard), and send those using the Discord SDK, and of course the other way around. There were still a few things left on the table: IP assignment and package routing. Both of these were implemented in the most straight-forward way possible. A minimal GUI is slapped on to LPVPN to make everything a little bit more user friendly.

    I did ran into a few problems though:

    Problem 1: Windows Firewall

    Windows by default sets a network as “Public” with no easy way to reconfigure. A network interface being set to the “Public” profile gives Windows PCs a little bit more safety when connected to public networks. But in this case it totally destroys the intended use-case: LAN gaming. When a network is set to “Public”, other parties cannot connect to services on that interface, even if it listens to 0.0.0.0. This means that if you will not be able to find and connect to games other people are hosting. I’m able to work around this using this API, which is able to set a network as “Private”.

    Problem 2: Packing everything into a single binary

    I want users to download a standalone exe file, double click it, and have the VPN running. This proved to be a little bit hard since I need to bundle 2 dlls with the main binary: one for WinTun, and one for Discord. I want to package everything into a single binary since I don’t want users to download a ZIP file and manually unzip. For this I used NSIS, which is usually used for creating software installer. In this case I used it as a way to automatically unpack the dlls and exe and then execute in a temporary directory. This works on paper until I run into problem 3.

    Problem 3: Windows Defender False Positives

    Unfortunately, as soon as I have the NSIS executable packaged. Windows Defender promptly deleted it from my computer and gave me a stern warning that it is a Trojan. (It’s kind of funny to see Windows do that.) This would not fly if I plan on distributing the software.

    Problem 4: Problem 2, Electric Boogaloo

    Since there’s really no easy way to fix problem 3. I have think of another way to solve problem 2. Fortunately I found MemoryModule, which is a reimplementation of LoadLibrary and its friends in Windows. This means I can load dlls from memory, instead of from files. I ended up packing the dlls as resources using cmrc, and then loading them using MemoryModule. This fixed the false positives from Windows Defender.