Created
September 27, 2013 12:22
-
-
Save alcides/6727813 to your computer and use it in GitHub Desktop.
BitCoin cheap clone proof-of-concept
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fibCache = [1, 1] | |
fibMax = 0; | |
def isFib(n): | |
global fibMax | |
if n <= 1: | |
return True | |
else: | |
while fibMax < n: | |
fibMax = sum(fibCache[-2:]) | |
fibCache.append(fibMax) | |
return n in fibCache | |
def isSolution(n): | |
return isFib(n) | |
def mining(s=0, n=100): | |
return [ i for i in range(s, n) if isSolution(i) ] | |
# Server | |
def encode(n, i, passw): | |
return ("Token", n, i, passw) | |
serverSolutions = [1] | |
serverUpdates = {} | |
def getMax(): | |
return serverSolutions[-1] | |
def isValid(n, passw): | |
if n in serverSolutions: | |
return None | |
else: | |
serverSolutions.append(n) | |
serverUpdates[n] = 0 | |
return encode(n, 0, passw) | |
def isUpdated(n, i): | |
if n not in serverUpdates: | |
return False | |
if serverUpdates[n] > i: | |
return False | |
else: | |
serverUpdates[n] = i | |
return True | |
def update(n, i, passw): | |
if serverUpdates[n] < i: | |
serverUpdates[n] = i | |
return encode(n, i, passw) | |
return False | |
# Client | |
def decode(token, passw): | |
if token[3] == passw: | |
return token[1:3] | |
else: | |
return (0,0) | |
def check(token, pass_f): | |
n, i = decode(token, pass_f) | |
if n == 0 and i == 0: | |
return False | |
return isUpdated(n, i) | |
def transaction(goods, pass_f, pass_t, annonymous=False): | |
print "Transferring %s" % str(goods) | |
n, i = decode(goods, pass_f) | |
if not isUpdated(n, i): | |
print "Owner does not own the goods" | |
return False | |
ni = i + 1 | |
if annonymous: | |
return encode(n, ni, pass_t) | |
else: | |
return update(n, ni, pass_t) | |
pass_alcides = "alcides" | |
tries = mining(getMax(), getMax() + 100) | |
keychain_alcides = filter(lambda x: x, map(lambda x: isValid(x, pass_alcides), tries)) | |
print "\n".join(map(str,keychain_alcides)) | |
print ".........." | |
pass_raul = "raul" | |
tries = mining(getMax(), getMax() + 200) | |
keychain_raul = filter(lambda x: x, map(lambda x: isValid(x, pass_raul), tries)) | |
print "\n".join(map(str,keychain_raul)) | |
print ".........." | |
goods = keychain_alcides[1] | |
new_token = transaction(goods, pass_alcides, pass_raul) | |
keychain_raul.append(new_token) | |
print "Raul got ", new_token | |
if check(goods, pass_alcides) != False or check(new_token, pass_raul) != True: | |
print "Transaction failed!" | |
else: | |
print "Transaction successful!" | |
pass_joaquim = 'joaquim' | |
keychain_joaquim = [] | |
goods = keychain_raul[-1] | |
new_token = transaction(goods, pass_raul, pass_joaquim, annonymous=True) | |
keychain_joaquim.append(new_token) | |
print "Joaquim got unverified ", new_token | |
pass_maria = 'maria' | |
keychain_maria = [] | |
goods = keychain_raul[-1] | |
new_token = transaction(goods, pass_raul, pass_maria, annonymous=True) | |
keychain_maria.append(new_token) | |
print "Maria got unverified ", new_token | |
goods = keychain_joaquim[-1] | |
new_token = transaction(goods, pass_joaquim, pass_alcides) | |
keychain_alcides.append(new_token) | |
print "Alcides got unverified ", new_token | |
if check(goods, pass_joaquim) != False or check(new_token, pass_alcides) != True: | |
print "Transaction failed!" | |
else: | |
print "Transaction successful!" | |
if check(keychain_maria[-1], pass_maria) != False: | |
print "Maria still has the old key" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment