Redid RLP

This commit is contained in:
Vitalik Buterin 2013-12-25 08:26:43 -05:00
parent 0d729e1219
commit a3af9af23e
3 changed files with 83 additions and 56 deletions

View File

@ -13,10 +13,10 @@ class Block():
if re.match('^[0-9a-fA-F]*$',data): if re.match('^[0-9a-fA-F]*$',data):
data = data.decode('hex') data = data.decode('hex')
header, transaction_list, self.siblings = rlp.decode(data) header, transaction_list, self.uncles = rlp.decode(data)
[ number, [ number,
self.prevhash, self.prevhash,
self.siblings_root, self.uncles_root,
self.coinbase, self.coinbase,
state_root, state_root,
self.transactions_root, self.transactions_root,
@ -36,11 +36,8 @@ class Block():
raise Exception("State Merkle root not found in database!") raise Exception("State Merkle root not found in database!")
if bin_sha256(transaction_list) != transactions_root: if bin_sha256(transaction_list) != transactions_root:
raise Exception("Transaction list root hash does not match!") raise Exception("Transaction list root hash does not match!")
if bin_sha256(sibling_list) != sibling_root: if bin_sha256(uncle_list) != uncle_root:
raise Exception("Transaction list root hash does not match!") raise Exception("Uncle root hash does not match!")
for sibling in self.siblings:
if sibling[0] != self.prevhash:
raise Exception("Sibling's parent is not my parent!")
# TODO: check POW # TODO: check POW
def send(self,tx): def send(self,tx):
@ -65,7 +62,7 @@ class Block():
self.state.update(tx.to,receiver_state) self.state.update(tx.to,receiver_state)
# Create a new contract # Create a new contract
else: else:
addr = tx.hash()[:20] addr = tx.hash()[-20:]
contract = block.get_contract(addr) contract = block.get_contract(addr)
if contract.root != '': return False if contract.root != '': return False
for i in range(len(tx.data)): for i in range(len(tx.data)):
@ -122,7 +119,7 @@ class Block():
txlist = [x.serialize() for x in self.transactions] txlist = [x.serialize() for x in self.transactions]
header = [ encode(self.number,256), header = [ encode(self.number,256),
self.prevhash, self.prevhash,
bin_sha256(rlp.encode(self.siblings)), bin_sha256(rlp.encode(self.uncles)),
self.coinbase, self.coinbase,
self.state.root, self.state.root,
bin_sha256(rlp.encode(self.txlist)), bin_sha256(rlp.encode(self.txlist)),
@ -130,7 +127,7 @@ class Block():
encode(self.timestamp,256), encode(self.timestamp,256),
encode(self.nonce,256), encode(self.nonce,256),
self.extra ] self.extra ]
return rlp.encode([header, txlist, self.siblings ]) return rlp.encode([header, txlist, self.uncles ])
def hash(self): def hash(self):
return bin_sha256(self.serialize()) return bin_sha256(self.serialize())

View File

@ -44,12 +44,13 @@ scriptcode_map = {
} }
params = { params = {
'stepfee': 2**60 * 4096, 'stepfee': 2**64 / 64,
'txfee': 2**60 * 524288, 'txfee': 2**64,
'memoryfee': 2**60 * 262144, 'newcontractfee': 2**64,
'datafee': 2**60 * 16384, 'memoryfee': 2**64 / 4,
'cryptofee': 2**60 * 65536, 'datafee': 2**64 / 16,
'extrofee': 2**60 * 65536, 'cryptofee': 2**64 / 16,
'extrofee': 2**64 / 16,
'blocktime': 60, 'blocktime': 60,
'period_1_reward': 2**80 * 1024, 'period_1_reward': 2**80 * 1024,
'period_1_duration': 57600, 'period_1_duration': 57600,
@ -65,9 +66,11 @@ def eval(block,transactions,timestamp,coinbase):
# Process all transactions # Process all transactions
while len(transactions) > 0: while len(transactions) > 0:
tx = transactions.pop(0) tx = transactions.pop(0)
fee = params['txfee'] + len(tx.data) * params['datafee'] fee = 0
if tx.to = '\x00'*20: if tx.to = '\x00'*20:
fee += len(tx.data) * params['memoryfee'] fee += params['newcontractfee'] + len(tx.data) * params['memoryfee']
else:
fee += params['txfee']
# Insufficient fee, do nothing # Insufficient fee, do nothing
if fee > tx.fee: continue if fee > tx.fee: continue
# Too much data, do nothing # Too much data, do nothing
@ -90,10 +93,10 @@ def eval(block,transactions,timestamp,coinbase):
else: else:
reward = params['period_4_reward'] reward = params['period_4_reward']
miner_balance += reward miner_balance += reward
for sibling in block.siblings: for uncle in block.uncles:
sib_miner_state = rlp_decode(self.state.get(sibling[3])) sib_miner_state = rlp_decode(self.state.get(uncle[3]))
sib_miner_state[1] = encode(decode(sib_miner_state[1],256)+reward*7/8,256) sib_miner_state[1] = encode(decode(sib_miner_state[1],256)+reward*7/8,256)
self.state.update(sibling[3],sib_miner_state) self.state.update(uncle[3],sib_miner_state)
miner_balance += reward/8 miner_balance += reward/8
miner_state[1] = encode(miner_balance,256) miner_state[1] = encode(miner_balance,256)
self.state.update(self.coinbase,miner_state) self.state.update(self.coinbase,miner_state)
@ -108,7 +111,7 @@ def eval(block,transactions,timestamp,coinbase):
block.prevhash = h block.prevhash = h
block.coinbase = coinbase block.coinbase = coinbase
block.transactions = [] block.transactions = []
block.siblings = [] block.uncles = []
return block return block
def eval_contract(block,transaction_list,tx): def eval_contract(block,transaction_list,tx):

95
rlp.py
View File

@ -16,42 +16,69 @@ def from_binary(b):
if len(b) == 0: return 0 if len(b) == 0: return 0
else: return from_binary(b[:-1]) * 256 + ord(b[-1]) else: return from_binary(b[:-1]) * 256 + ord(b[-1])
def num_to_var_int(n): def __decode(s,pos=0):
if n < 253: s = [n] if s == '':
elif n < 2**16: s = [253] + list(to_binary_array(n,2)) return (None, 0)
elif n < 2**32: s = [254] + list(to_binary_array(n,4)) else:
elif n < 2**64: s = [255] + list(to_binary_array(n,8)) fchar = ord(s[pos])
else: raise Exception("number too big") if fchar <= 24:
return ''.join([chr(x) for x in s]) return (ord(s[pos]), pos+1)
elif fchar < 56:
def __decode(s): b = ord(s[pos]) - 24
if s == '': return None return (from_binary(s[pos+1:pos+1+b]), pos+1+b)
o = [] elif fchar < 64:
index = [0] b = ord(s[pos]) - 55
def read_var_int(): b2 = from_binary(s[pos+1:pos+1+b])
si = ord(s[index[0]]) return (from_binary(s[pos+1+b:pos+1+b+b2]), pos+1+b+b2)
index[0] += 1 elif fchar < 120:
if si < 253: return si b = ord(s[pos]) - 64
elif si == 253: read = 2 return (s[pos+1:pos+1+b], pos+1+b)
elif si == 254: read = 4 elif fchar < 128:
elif si == 255: read = 8 b = ord(s[pos]) - 119
index[0] += read b2 = from_binary(s[pos+1:pos+1+b])
return from_binary(s[index[0]-read:index[0]]) return (s[pos+1+b:pos+1+b+b2], pos+1+b+b2)
while index[0] < len(s): elif fchar < 184:
tp = s[index[0]] b = ord(s[pos]) - 128
index[0] += 1 o, pos = [], pos+1
L = read_var_int() for i in range(b):
item = s[index[0]:index[0]+L] obj, pos = __decode(s,pos)
if tp == '\x00': o.append(item) o.append(obj)
else: o.append(__decode(item)) return (o,pos)
index[0] += L elif fchar < 192:
return o b = ord(s[pos]) - 183
b2 = from_binary(s[pos+1:pos+1+b])
o, pos = [], pos+1+b
for i in range(b):
obj, pos = __decode(s,pos)
o.append(obj)
return (o,pos)
else:
raise Exception("byte not supported: "+fchar)
def decode(s): return __decode(s)[0] def decode(s): return __decode(s)[0]
def encode(s): def encode(s):
if isinstance(s,(int,long)): return encode(to_binary(s)) if isinstance(s,(int,long)):
if isinstance(s,str): return '\x00'+num_to_var_int(len(s))+s if s <= 24:
return chr(s)
elif s <= 2**256:
b = to_binary(s)
return chr(len(b) + 24) + b
else:
b = to_binary(s)
b2 = to_binary(len(b))
return chr(len(b2) + 55) + b2 + b
elif isinstance(s,str):
if len(s) < 56:
return chr(len(s) + 64) + s
else:
b2 = to_binary(len(s))
return chr(len(b2) + 119) + b2 + s
elif isinstance(s,list):
if len(s) < 56:
return chr(len(s) + 128) + ''.join([encode(x) for x in s])
else:
b2 = to_binary(len(s))
return chr(len(b2) + 183) + b2 + ''.join([encode(x) for x in s])
else: else:
x = ''.join([encode(x) for x in s]) raise Exception("Encoding for "+s+" not yet implemented")
return '\x01'+num_to_var_int(len(x))+x