I'm a Floridian web engineer who planted his roots in Paris. Some of my favorite past-times are analyzing and optimizing development processes, building solid APIs, mentoring junior devs, and long walks on the beach.

Ethereum Part 1: Test Blockchain Setup 02-16-2016

Ethereum

Ethereum is a cryptocurrency like Bitcoin which allows for transactions to take place that are not limited to leger changes but also allow for the running of code known as smart contracts. Their unit of currency is called ether and it is used whenever you want to push code onto the blockchain or you run code on the blockchain that modifies the underlying data in some way. This article from EtherScripter goes more into detail.

Requirements

The ethereum site says you just need 1-2gb free to start developing. For me, I am using an ubuntu virtualbox with

  • 4 gigs of RAM
  • 2 CPU
  • 8 gigs of HDD // With 2 chains(dev and staging) I’m at 42% capacity

Setup

In order to start playing around with ethereum I used the following things:

  • ubuntu - apt-get all the things
  • go-ethereum - geth command line utility to run your own blockchain
  • truffle - A lightweight framework for developing, compiling, and deploying code
  • vim - A command-line editor
  • tmux - A terminal multiplexer because there is a lot going on

Get go-ethereum and build your blockchain


sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo add-apt-repository -y ppa:ethereum/ethereum-dev
sudo apt-get update
sudo apt-get install -y ethereum
mkdir -p ~/dapps/test/data
mkdir -p ~/dapps/test/config/
touch ~/dapps/test/dev_up   # Startup script for development
chmod +x ~/dapps/test/dev_up
touch ~/dapps/test/up           # Startup script
chmod +x ~/dapps/test/up

Create your startup scripts

~/dapps/test/dev_up:


geth --datadir "data" --trace "data/chaintrace.log" \
     --port 30303 --rpc --rpcport 8101 \
     --rpcaddr 0.0.0.0 --networkid 0 \
     --rpccorsdomain "*" --nodiscover \
     --genesis="config/genesis.json" \
     --rpcapi "eth,web3" \
     --maxpeers 0 --unlock 0 \
     --password ./password \
     console

This file will call geth and specify logging and data directories. It doesn’t look for other network ids to connect with. It is pretty standard for a dev environment. This also attempts to unlock the first account in it’s system and use the password in a local password file. This will be used for development.

~/dapps/test/up:


geth --datadir "testnet/data" --trace "data/chaintrace.log" \
     --port 30303 --rpc --rpcport 8101 \
     --rpcaddr 0.0.0.0 --networkid 0 \
     --rpccorsdomain "*" --nodiscover \
     --genesis="config/genesis.json" \
     --rpcapi "eth,web3" \
     console

It’s the same as the previous file but you can use this one without having any accounts. If you want more information on this setting up a test chain, you can use this as a reference: Ethereum Testing.

Genesis block

The next thing is to create a genesis file to seed the blockchain with. This file is needed for creating the base of the blockchain. A little more information about the production chain can be found on the Bitcoin wiki page and the Ethereum blog.

config/genesis.json


{
    "nonce": "0xdeadbeefdeadbeef",
    "timestamp": "0x0",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x0",
    "gasLimit": "0x8000000",
    "difficulty": "0x400",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "coinbase": "0x3333333333333333333333333333333333333333",
    "alloc": {
    }
}

Create an Account

You can spool up your new test blockchain and access the console with ~/dapps/test/up


> personal.newAccount()
Passphrase:
Repeat passphrase:
"0xYourNewAccount"

Ok. A word to the wise, don’t lose your password or your account hash. When they are gone, they are gone.

In the console, you can find a list of your accounts with the command personal.listAccounts. Now take the password you just entered and put it in ~/dapps/test/password. Now when you run ~/dapps/test/dev_up it will bring up your test chain and unlock the first account in personal.listAccounts with the password in the password file.

Exit the console and start up the dev_up version. Now we need to grab some ether by starting a miner in the console.


miner.start(10) // Starts 10 miner threads.
I0216 20:13:20.229458 miner/worker.go:349] 🔨  Mined block (#1 / 67af97f2). Wait 5 blocks for confirmation
I0216 20:13:20.230321 miner/worker.go:570] commit new work on block 2 with 0 txs & 0 uncles. Took 358.013µs
I0216 20:13:20.235390 miner/worker.go:349] 🔨  Mined stale block (#1 / 7feeeaa9).
….

This will go on forever unless you stop it. So just run miner.stop(10) to stop the miner threads. You can see your ether balance by running web3.fromWei(eth.getBalance("0xYourAccountHash"), "ether"). With that you will be ready to start making a dapp.

A Controller for Disabled Gamers 02-11-2016

A while back I went to my home city and played some Xbox with a friend. He has one arm and while he was very interested in playing an FPS with me it was heart crushing to see how impossible it was for him. I decided to try to modify an Xbox controller and I started to look into how I could make modifications to it.

Here is my initial plan:

New Xbox controller

The idea was to lay out all of the buttons so that he could use them with the controller sitting down. After taking apart an old Xbox controller, de-soldering all of the buttons and playing around with some alternatives, I decided to trash the idea.

A friend at Le Petit Fablab had recommended Makey Makey as a possible solution, but since it wasn't made for Xbox I didn't want anything to do with it. Later on I decided that the system didn't matter and ended up taking the Makey Makey route after all.

Board Construction

I decided to base the board out of plexiglass, with 6 smaller squares of plexiglass fixed onto it for the buttons. The decision to go with plexiglass was because other materials are a little more harsh.

  • Metal wasn't great since the base board needed to be an insulator and it's harder to work with.
  • Wood wasn't great because over time it might break down or get splintery.

Here is a shot of what I created:

Sorry for the potato quality

I covered the squares with metal tape to make them conductive and attached Makey Makey's alligator clips to the buttons. After everything was assembled, I covered it in as much duct tape as I could get my hands on. Now it really has that prototype feel. :)

The board can then be controlled with your bare feet. While it was a little awkward for me at the start, it was possible to use it to take the place of my other hand. This setup gives you the directional buttons and then two miscelleneous buttons. Makey Makey comes with certain connectors hardwired for certain buttons. WASD, F, G, space, and mouse click being the presets. You can extend what they give you but for my purposes it is just easier to override the buttons in a game.

It is funny that when I started the project, I had thought that my biggest problems would be technical ones. In retrospect, it ended up being more questions of ergonomics and materials.

Tags: hardware xbox pc

Create an Object and Its Associations in Django 04-08-2015

I just added a bit of code for creating an object with an association in Django. I saw quite a few tutorials that mix the setting of associated data directly into the view. In general, this is not recommended as django views should be for handling the passing of of data that will be rendered and the likes. Form processing should be handled in the Form classes. Here is an example of a not so great solution:

view.py


def some_view(request):
    obj_form = ObjectForm.new(request.POST)
    other_obj = OtherObj.objects.find(pk=request.POST['other_obj_id'])
    obj = obj_form.save()
    obj.other_obj = other_obj
    obj.save()
    return render(request, 'object.html')

Association Support

Django supports the setting of associations with ModelChoiceField. However there is a caveat in that you must specify a queryset. I wanted the associated object other_obj to be able to be any row from the table that represents OtherObj, but this table will grow with time. For my needs I don't need to limit my associations to the queryset and what's more, as the table grows, the queryset will become less manageable.

Therefore, I modified the form class as follows:

form.py


class ObjectForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):

        super(ObjectForm, self).__init__(*args, **kwargs)
        self.other_obj_id = self.data['other_obj_id']

    def save(self, *args, **kwargs):
        self.instance.other_obj_id = self.other_obj_id
        return super(ObjectForm, self).save(*args, **kwargs)

In this way, I can keep everything relative to the manipulation of the Object class within ObjectForm instead of having it spread between the view and form classes.

Tags: django

Test a will_paginate Custom Renderer 01-22-2012

I was recently working on a project where my employer wanted me to make the site's current pagination work the way the old site's pagination worked. I had recently switched over using will_paginate, and while there are a number of good resources for making customized link renderers: frivolous, thewebfellas are a few. They don't really mention testing.

While searching through the internets, I eventually stumbled upon this on tsigo's github with some specs for his renderers. For me, I just had to test an override of windowedpagenumbers, and link. Setting up the test can be seen easily enough from the github file, but essentially, you just create an instance of WillPaginate::Collection and pass that into whatever renderer you have created along with any options like so:


@renderer = LinkRenderer.new
@renderer.stubs(:url).returns('')
@collection = WillPaginate::Collection.new(page, per_page, total_entries)
@renderer.prepare(@collection, options, '')

@renderer.method_name.should do_something

You will have noticed that I've stubbed out the url method. That is because we aren't using a real collection of objects and therefore there is no url to generate.

Definition vs Function/Methods Style 10-15-2011

I was recently reading The Rails 3 way by Obie Fernandez when I came across a section of the book that was talking about how Ruby allows for a definition-style as opposed to a funtion-style. That is to say, using a function call without including the open and closing parentheses. Typically, I like using functions without parentheses because I feel that the function looks more clear.

I much prefer to see:


link_to "Some page", some_page_path

instead of:


link_to( "Some page", some_page_path )

Especially in a haml template, the first style looks nicer. However, "The Rails 3 way" mentions that one of the key benefits parenthesis-less function calls offer is macro-like feeling and should be a signifier that this call will affect other things and not act directly. Indeed when you look at the following code:


class Client < ActiveRecord::Base 
  has_many :billing_codes
  has_many :billable_weeks
  has_many :timesheets, :through => :billable_weeks
end

class Client < ActiveRecord::Base
  has_many(:billing_codes)
  has_many(:billable_weeks)
  has_many(:timesheets, :through => :billable_weeks)
end

You can see that the first one looks cleaner. It also looks more like a declaration. Indeed, the has_many function alters the object of the class that calls it giving all of it's instantiated objects a slew of other functions. This theme happens over and over again in the Rails framework. The filters that are added to controllers, such as before_filter and after_filter, will alter the functionality of actions that get called.

Ambiguity

The problem with the definition-style method is when there are nested functions which take parameters. For instance, if the function for generating the page for some_page requires parameters as well:


some_page_path( :user_id => current_user.id, :name => 'derp' ) # calls the path for some page

This will cause problems when the following call is made:


link_to "Some page", some_page_path :user_id => current_user.id, :name => 'derp'

In the link_to call, it can't be determined whether the :name parameter should be part of the params for link_to or part of the params for some_page_path.

Since consistency is always preferred, it is better to always use parentheses with function calls only leave them off only when the function is being used to alter the class in some way.

Tags: style ruby Rails