Skip to content

Commit

Permalink
heavy modifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Rudloff committed Jul 22, 2011
1 parent 8f8e580 commit f3c3f98
Show file tree
Hide file tree
Showing 20 changed files with 486 additions and 2,702 deletions.
1 change: 1 addition & 0 deletions CREDITS
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Peter Davies <[email protected]>
Jonas Rudloff <pcfreck "AT" gmail "DOT" com>
1 change: 0 additions & 1 deletion UNLICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,3 @@ OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <http://unlicense.org/>

The file "ipaddr.py" is licensed separately, see that file for details.
58 changes: 58 additions & 0 deletions blockchain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from utils import *
import genesisblock
import msgs
import jserialize as js
import bserialize as bs
class BlockAux(js.Entity, bs.Entity):
fields = {
"block":msgs.Block,
"txs":js.List(js.Hash),
"number":js.Int,
"totaldiff":js.Int,
"succ": js.Hash,
}
bfields = [
("block", msgs.Block),
("txs", bs.VarList(bs.Hash)),
("number", bs.structfmt("<Q")),
("totaldiff", bs.structfmt("<Q")),
("succ", bs.Hash),
]
@property
def hash(self):
return self.block.hash
@constructor
def make(self, blockmsg):
self.block, self.txs, self.number = blockmsg.block, [tx.hash for tx in blockmsg.txs], 2**64-1
self.totaldiff = 0
self.succ = nullhash

class BlockList(js.Entity, bs.Entity):
fields = {
"blocks":js.List(js.Hash),
}
bfields = [
("blocks", bs.VarList(bs.Hash)),
]
class BlockChain:
def __init__(self, db, server, txdb):
self._db
self.server = server
self.txdb = txdb
try:
self.mainchain = self._db["mainchain"]
except:
self.add(genesisblock.blockmsg)
def put_aux(aux):
self._db[aux.hash] = aux.tobinary()

def add(self, blockmsg):
if blockmsg.block.hash in self._db:
return
aux = BlockAux.make(blockmsg)
if blockmsg.block.prev == nullhash:
aux.number = 1
aux.totaldiff = bits_to_diff(aux.block.bits)
self.put_aux(aux)


33 changes: 22 additions & 11 deletions bserialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
from utils import ProtocolViolation

import struct

class Entity():
from socket import inet_ntoa, inet_aton
class Entity(object):
"""
Each Entity subclass has a class variable called bfields, which is list of
tuples contained as fieldname and a serialization class.
Expand Down Expand Up @@ -48,8 +48,10 @@ def structfmt(fmt):
"""Produce a serialization object for a value understood by struct.
e.g. structfmt("<I") for 4-byte integers"""
class Foo():
@staticmethod
def tobinary(obj):
return struct.pack(fmt, obj)
@staticmethod
def frombinary(bdata):
try:
return struct.unpack(fmt, bdata[:struct.calcsize(fmt)])[0], bdata[struct.calcsize(fmt):]
Expand All @@ -58,8 +60,10 @@ def frombinary(bdata):
return Foo

class Str():
@staticmethod
def tobinary(obj):
return obj.encode("ascii") + b'\0'
@staticmethod
def frombinary(bdata):
bytes, ch, bdata = bdata.partition(b'\0')
if ch == b'':
Expand All @@ -72,18 +76,20 @@ def frombinary(bdata):
Hash = structfmt("<32s")

class VarInt():
@staticmethod
def frombinary(bdata):
try:
if bdata[0] <= 0xfc:
return bdata[0], bdata[1:]
if bdata[0] == 0xfd:
if ord(bdata[0]) <= 0xfc:
return ord(bdata[0]), bdata[1:]
if ord(bdata[0]) == 0xfd:
return struct.unpack("<xH", bdata[:3])[0], bdata[3:]
if bdata[0] == 0xfe:
if ord(bdata[0]) == 0xfe:
return struct.unpack("<xI", bdata[:5])[0], bdata[5:]
if bdata[0] == 0xff:
if ord(bdata[0]) == 0xff:
return struct.unpack("<xQ", bdata[:9])[0], bdata[9:]
except (struct.error, IndexError):
raise ProtocolViolation
@staticmethod
def tobinary(int):
if int <= 0xfc:
return struct.pack("<B", int)
Expand All @@ -96,33 +102,38 @@ def tobinary(int):

def VarList(ty):
class _():
@staticmethod
def frombinary(bdata):
num, bdata = VarInt.frombinary(bdata)
retval = []
for _ in range(num):
item, bdata = ty.frombinary(bdata)
retval.append(item)
return retval, bdata
@staticmethod
def tobinary(obj):
return VarInt.tobinary(len(obj)) + b"".join((ty.tobinary(x) for x in obj))
return _

class VarBytes():
@staticmethod
def frombinary(bdata):
num, bdata = VarInt.frombinary(bdata)
if len(bdata) < num:
raise ProtocolViolation
return bdata[:num], bdata[num:]
@staticmethod
def tobinary(obj):
return VarInt.tobinary(len(obj)) + obj

class IPv4Inv6():
@staticmethod
def frombinary(bdata):
import ipaddr
try:
obj, bdata = structfmt("!12xI").frombinary(bdata)
obj, bdata = structfmt("!12x4s").frombinary(bdata)
except struct.error:
raise ProtocolViolation
return ipaddr.IPv4Address(obj), bdata
return inet_ntoa(obj), bdata
@staticmethod
def tobinary(object):
return struct.pack("!10xHI", 2**16-1, int(object))
return struct.pack("!10xH4s", 2**16-1, inet_aton(object))
7 changes: 6 additions & 1 deletion genesisblock.py
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
blockmsg = {"type": "block", "txs": [{"inputs": [{"script": "04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73", "outpoint": {"index": 4294967295, "tx": "0000000000000000000000000000000000000000000000000000000000000000"}, "sequence": 4294967295}], "locktime": 0, "version": 1, "outputs": [{"amount": 5000000000, "script": "4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac"}]}], "block": {"nonce": 2083236893, "version": 1, "time": 1231006505, "merkle": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b", "bits": 0x1d00ffff, "prev": "0000000000000000000000000000000000000000000000000000000000000000"}}
import msgs
blockmsg = msgs.Blockmsg.fromjson({"type": "block", "txs": [{"inputs": [{"script": "04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73", "outpoint": {"index": 4294967295, "tx": "0000000000000000000000000000000000000000000000000000000000000000"}, "sequence": 4294967295}], "locktime": 0, "version": 1, "outputs": [{"amount": 5000000000, "script": "4104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac"}]}], "block": {"nonce": 2083236893, "version": 1, "time": 1231006505, "merkle": "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b", "bits": 0x1d00ffff, "prev": "0000000000000000000000000000000000000000000000000000000000000000"}})
block = blockmsg.block
hash = block.hash


Loading

0 comments on commit f3c3f98

Please sign in to comment.