Skip to content

Commit

Permalink
Added support for transactions.
Browse files Browse the repository at this point in the history
Also a .gitignore file and various tweaks.
  • Loading branch information
petertdavies committed Mar 3, 2011
1 parent 3ac1050 commit f30a4ac
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.pyc
9 changes: 9 additions & 0 deletions bserialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ def tobinary(obj):
return VarInt.tobinary(len(obj)) + b"".join((ty.tobinary(x) for x in obj))
return _

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

class IPv4Inv6():
def frombinary(bdata):
import ipaddr
Expand Down
4 changes: 3 additions & 1 deletion jserialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ def f(cls, *args):
return self
return f

class Hash():
class Bytes():
def tojson(obj):
return binascii.hexlify(obj).decode("ascii")
def fromjson(json):
return binascii.unhexlify(json.encode("ascii"))

Hash = Bytes

class Entity():
def tojson(self):
retval = {}
Expand Down
57 changes: 54 additions & 3 deletions msgs.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,32 +198,82 @@ class Getdata(js.Entity, bs.Entity):
def make(self, objs):
self.objs = objs

class TxOutpoint(js.Entity, bs.Entity):
fields = {
"tx":js.Hash,
"index":js.Int,
}
bfields = [
("tx", bs.Hash),
("index", bs.structfmt("<I")),
]

class TxInput(js.Entity, bs.Entity):
fields = {
"outpoint":TxOutpoint,
"script":js.Bytes,
"sequence":js.Int,
}
bfields = [
("outpoint", TxOutpoint),
("script", bs.VarBytes),
("sequence", bs.structfmt("<I")),
]

class TxOutput(js.Entity, bs.Entity):
fields = {
"amount":js.Int,
"script":js.Bytes,
}
bfields = [
("amount", bs.structfmt("<Q")),
("script", bs.VarBytes),
]

class Tx(js.Entity, bs.Entity):
type = "tx"
fields = {
"version":js.Int,
"inputs":js.List(TxInput),
"outputs":js.List(TxOutput),
"locktime":js.Int,
}
bfields = [
("version", bs.structfmt("<I")),
("inputs", bs.VarList(TxInput)),
("outputs", bs.VarList(TxOutput)),
("locktime", bs.structfmt("<I")),
]


class Block(js.Entity, bs.Entity):
fields = {
"version":js.Int,
"prev":js.Hash,
"merkle":js.Hash,
"time":js.Int,
"bits":js.Int,# FIXME its a bytestring
"bits":js.Bytes,
"nonce":js.Int,
}
bfields = [
("version", bs.structfmt("<I")),
("prev", bs.Hash),
("merkle", bs.Hash),
("time", bs.structfmt("<I")),
("bits", bs.structfmt("<I")), # FIXME bytestring
("bits", bs.structfmt("<4s")),
("nonce", bs.structfmt("<I")),
]

class Blockmsg(js.Entity, bs.Entity): # FIXME no tx's yet
class Blockmsg(js.Entity, bs.Entity):
type = "block"
fields = {
"type":js.Str,
"block":Block,
"txs":js.List(Tx)
}
bfields = [
("block", Block),
("txs", bs.VarList(Tx)),
]

msgtable = {
Expand All @@ -235,4 +285,5 @@ class Blockmsg(js.Entity, bs.Entity): # FIXME no tx's yet
'inv':Inv,
'getdata':Getdata,
'block':Blockmsg,
'tx':Tx,
}
6 changes: 4 additions & 2 deletions network.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,16 @@ def handle_version(self, msg):
self.sendmsg(msgs.Verack.make())
def handle_verack(self, msg):
self.active = True
self.sendmsg(msgs.Getaddr.make())
#self.sendmsg(msgs.Getaddr.make())
self.sendmsg(msgs.Getblocks.make([status.genesisblock]))
def handle_addr(self, msg):
storage.storeaddrs(msg.addrs)
def handle_inv(self, msg):
self.sendmsg(msgs.Getdata.make([msg.objs[0]]))
def handle_block(self, msg):
exit(0)
pass
def handle_tx(self, msg):
pass

class NodeDisconnected(BaseException): pass

Expand Down

0 comments on commit f30a4ac

Please sign in to comment.