James Larisch

I write not from wisdom but out of a desire to achieve it

Page 2


Understanding Bitcoin mining & difficulty for fun and no profit

This post is for understanding the particulars of mining a block from the Bitcoin network. It assumes you have read Ken Shirrif’s post on the subject and have a vague understanding of it. If some of the details behind the Python mining simulator seemed a bit rushed, that’s what this post will attempt to ramify.

I will also attempt to make some of the details in the difficulty wiki article a bit more clear, so it’s worth giving that a skim.

Ken posted a small Python program to simulate how mining works here. He doesn’t really go into the details, so I did, and I’d like to share.

I will go through the code line by line, explaining what it does. After that I plan to convert the code to Ruby, using (I think) better naming conventions and function abstraction.

import hashlib, struct

ver = 2
prev_block = "000000000000000117c80378b8da0e33559b5997f2ad55e2f7d18ec1975b9717"
mrkl_root =

Continue reading →


Fix: Including modules on singleton objects in Ruby

Two weeks ago I posted this.

The idea behind adding methods to a single object still hold, but I made a rather egregious error.

I am pulling a row out of the database, (abstracted by ActiveRecord) and then extending that Ruby object with a module to add methods to it.

I was operating under the false assumption that that object, let’s call it x, will persist those methods after dying.

x is pulled out of the database. Methods a and b are added to that object (row). Then, the code moves on, or the user logs out. The data represented by x still exists in the database, but x itself does not. So, those methods get thrown away.

In my example, whenever a Membership was saved, it was extended. But again, that extends becomes useless after that particular object is no longer referenced.

A solution to this, the one I’m taking, is to extend that module whenever the Coinbase membership is

Continue reading →


Living With Zombie Software

The budding software developer is excited! She has landed her first programming job.

She’s never done programming for other humans before, but she’s eager to learn! She’s read plenty of books and has a plethora of pet projects under her belt.

She’s developing a website for a small business in her town. Things start off great! But soon, assignments pile up, and her code quality diminishes. After weeks of iteration and developing, her code starts to look like a house of toothpicks.

The website works though, and by the end of the summer she’s back to school.

Oh no! One day after calculus, her old boss calls and says there’s a problem! Time to dig through the pile of conditionals and code carbohydrates to fix a bug. Which will probably break something else.

 Forgive yourself

Our hero feels she has an obligation to keep that code up to date, because it gave her a start. But alas

Continue reading →


Mathematical Implication

The way it ties into philosophical implication can be confusing.

This cleared it up for me - forget math! Just remember what you thought when you first heard about implication.

In common sense implication (let’s think of A => B as if A, then B) B must be true if A is true.

Think about it. I tell you the sky will explode if birds chirp.

A => B form is If birds chirp, then the sky will explode.

A = birds chirp

B = sky explodes

If birds do indeed chirp, then the sky MUST EXPLODE.

In other words, if you told me that birds chirped and the sky did not explode (True => False), I would tell you that you are lying.

This explains…

T | F | F

Back to the math. Common sense wise it is impossible for A to be true without B being true. It’s a lie if that’s the case. Always. So, in math, we say that statement is logically FALSE.

But what about the others?

T | T | T

Is easy. It

Continue reading →


Including modules on singleton objects in Ruby

Zenchi‘s relational model has the following relationship:

User has_many memberships

A membership is say, a Coinbase account. This model contains the email associated with this account, among other things of that nature.

Separately, we have a Coinbase module, which has a static method refresh_transactions(user) that updates a User’s Coinbase transactions.

This means a user has to have a Coinbase membership and utilize the Coinbase module. When more services like Coinbase come into the picture, we will add a membership and module for each. Could be better.

My thinking - 'why can’t each membership have the same set of methods, defined differently?’

Solution: dynamically include modules based on the type of membership. This way, Coinbase memberships and Bitstamp memberships will have a different set of methods.

Like so -

class Membership < ActiveRecord::Base

  after_save

Continue reading →


Fitness, and the problem with the ‘fuck it, ship it’ model

GSD (getting shit done) is great. I couldn’t get anything done if I didn’t get anything done.

But ‘fuck it, ship it’ isn’t right. Ask your local personal trainer.

Any serious fitness-enthusiast will tell beginners that it’s always ALWAYS always better to start at the bottom while focusing on form.

Form is a the biggest and most important pillar in the cathedrals of fitness and martial arts. You start small - you start light - you start slow. You focus on the technique, on doing the fundamentals correctly.

And you build. You slowly improve - increasing speed, power, weight, duration - never forgetting the importance of the fundamentals.

As your journey matures, a rock-solid construction emerges. Acquisition of new skills gets easier, because the fundamentals are second only to breath. Muscles grow, programming gets easier, faster, and cleaner. Drills seem easier. Your stamina

Continue reading →


Day 5-15: Stack, BST, and Linked List (CodeReview is awesome)

I have not abandoned you C. But I get impatient quite easily, and wanted to jump ahead to some greater and more dreadful foes.

For my first, I chose the Stack. A stack is a first on, last off data structure. I discovered there are no generics in C, or, rather, C programmers don’t find generics particularly useful. I created a stack of *int*s. I posted all of my solutions on codereview stackexchange, which is a fantastic place to post working code that you’d like checked for best practices and style.

I think the decision to post on codereview accelerated my learning in the art of C tremendously - I was given immediate feedback for my work and felt like I was part a discussion. Being able to jump into a community like that was refreshingly helpful.

Here is my int stack. I used a pointer to a pointer to represent the stack. This way, the head of the stack can easily be switched out, and

Continue reading →


Late Night Big O Revelations

Big O complexity describes a relationship between the length of the input and the amount of “steps” it will take to complete some operation.

I am observing a function, with no numbers. If there is any multiplication / division of constant numbers that do not rely on anything, delete them. Just delete them. O(1), which is constant time, means the amount of steps (y), relates to n, the size of the input, as a horizontal line!

y = 1 (* whatever) is a horizontal line, right!?

O(n) means a straight, diagonal line, like a linear function. Hence, linear time. y = n.

( I should mention this is all worst case - the actual poly time it takes can be less than any of these lines. )

O(n2) means an polynomial curve! y = n^2!

Now… logarithmic…

O(log n)… the base doesn’t matter. so y = log n, say base 10, says if n is going up like 10, 20, 30 then y is 1, 1.something, 1.somethingbigger

Continue reading →


Day 3 : Pointers

#include stdio.h
char *copy_string(char *source, char *destination) {
  int *start = destination;
  while (*source != '\0') {
    *destination++ = *source++;
  }
  *destination = '\0';
  return start;
}

int main() {
  char a[9] = "hello";
  char b[9];
  char *pA = a;
  char *pB = b;
  copy_string(a, b);
  return 0;
}

Pointers. int x = 12; allocates a block of memory (4 bytes for 32, 8 bytes for 64 bit OSes) and then 12 is placed into that block of memory. If you then do int y = x;, you have taken the value of x, which is 12, and then placed that value also in the allocation for y.

However — I don’t know if this is only because integers are constants - I don’t think this would be the same for objects.

&x is the memory address that the x block exists under. Addresses are stored in “pointer types”. I thought that all things storing addresses should be integers, but that doesn’t seem

Continue reading →


James Learns C: Day 2

Time to compile the program, hopefully.

But I don’t know what does yet. .H files are header files. They are preprocessed by the compiler - compiled first. Everything is linked after.

Standard Input Output. It’s a “standard library” - which means it’s not in the language but every implementer of the language must supply it - stuff everyone needs.

So stdio.h is a header file in the standard library - the input/output library - and it gives us printf! Look at all these things : (https://en.wikipedia.org/wiki/Stdio.h) so fancy.

Just compiled. Compiled with gcc -o 1-hello-world 1-hello-world.c then ran with ./1-hello-world. I think ./ means current directory…?

Cool it worked moving on.

#include stdio.h

int main() {
  int count;
  puts("Please enter a number: ");
  scanf("%d", &count);
  printf("The number is %d \n", count);
} 

Weird what’s that ampersand? Puts puts things to

Continue reading →