Skip to content

Commit

Permalink
rename binary to bytes for accuracy
Browse files Browse the repository at this point in the history
  • Loading branch information
ethers committed Sep 22, 2015
1 parent 2f8dbea commit e2760d4
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 68 deletions.
6 changes: 3 additions & 3 deletions btcBulkStoreHeaders.se
@@ -1,17 +1,17 @@
inset('btcrelay.se')

# store 'count' number of Bitcoin blockheaders represented as one
# continuous 'headersBinary' (which should have length 80*count
# continuous 'headersBytes' (which should have length 80*count
# since a single Bitcoin block header is 80 bytes)
def bulkStoreHeader(headersBinary:str, count):
def bulkStoreHeader(headersBytes:str, count):
HEADER_SIZE = 80

offset = 0
endIndex = HEADER_SIZE

i = 0
while i < count:
currHeader = slice(headersBinary, chars=offset, chars=endIndex)
currHeader = slice(headersBytes, chars=offset, chars=endIndex)
res = self.storeBlockHeader(currHeader)

offset += HEADER_SIZE
Expand Down
10 changes: 5 additions & 5 deletions btcrelay.se
Expand Up @@ -90,14 +90,14 @@ def setInitialParent(blockHash, height, cumulativeDifficulty):


# store a Bitcoin block header that must be provided in
# binary format 'blockHeaderBinary'
def storeBlockHeader(blockHeaderBinary:str):
# bytes format 'blockHeaderBytes'
def storeBlockHeader(blockHeaderBytes:str):
hashPrevBlock = flip32Bytes(~calldataload(72)) # 68 (header start) + 4 (offset for hashPrevBlock)

scorePrevBlock = m_getScore(hashPrevBlock)
assert scorePrevBlock # assert prev block exists

blockHash = m_hashBlockHeader(blockHeaderBinary)
blockHash = m_hashBlockHeader(blockHeaderBytes)

scoreBlock = m_getScore(blockHash)
if scoreBlock != 0: # block already stored/exists
Expand All @@ -111,7 +111,7 @@ def storeBlockHeader(blockHeaderBinary:str):
if blockHash > 0 && blockHash < target:
m_saveAncestors(blockHash, hashPrevBlock)

save(self.block[blockHash]._blockHeader[0], blockHeaderBinary, chars=80)
save(self.block[blockHash]._blockHeader[0], blockHeaderBytes, chars=80)

difficulty = 0x00000000FFFF0000000000000000000000000000000000000000000000000000 / target # https://en.bitcoin.it/wiki/Difficulty
m_setScore(blockHash, scorePrevBlock + difficulty)
Expand Down Expand Up @@ -160,7 +160,7 @@ def verifyTx(txHash, txIndex, sibling:arr, txBlockHash):
# is logged on both this contract and target contract
#
# TODO txHash can eventually be computed (dbl sha256 then flip32Bytes) when
# txStr becomes txBinary
# txStr becomes txBytes
def relayTx(txStr:str, txHash, txIndex, sibling:arr, txBlockHash, contract):
if self.verifyTx(txHash, txIndex, sibling, txBlockHash, value=msg.value) == 1:
res = contract.processTransaction(txStr, txHash)
Expand Down
2 changes: 1 addition & 1 deletion example-btc-eth/test/test_btcTx.py
Expand Up @@ -43,7 +43,7 @@ def test_2ndTxBlock100K(self):
# rawTx = rawTx.decode('hex')
outNum = 0

# expHashOfOutputScript is by hashing the string (not binary), in this case it is:
# expHashOfOutputScript is by hashing the string (not bytes), in this case it is:
# >>> j
# '76a914c398efa9c392ba6013c5e04ee729755ef7f58b3288ac'
# >>> sha256(j)
Expand Down
2 changes: 1 addition & 1 deletion examples/btcrelayWithBulkStoreContractInfo.json
@@ -1 +1 @@
{"source":"","language":"","languageVersion":"","compilerVersion":"","abiDefinition":[{"inputs":[{"name":"headersBinary","type":"bytes"},{"name":"count","type":"int256"}],"name":"bulkStoreHeader(bytes,int256)","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[{"name":"txHash","type":"int256"},{"name":"txIndex","type":"int256"},{"name":"sibling","type":"int256[]"}],"name":"computeMerkle(int256,int256,int256[])","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[],"name":"getAverageBlockDifficulty()","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[],"name":"getBlockchainHead()","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[],"name":"getCumulativeDifficulty()","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[],"name":"getLastBlockHeight()","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[{"name":"txBlockHash","type":"int256"}],"name":"inMainChain(int256)","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[{"name":"txStr","type":"bytes"},{"name":"txHash","type":"int256"},{"name":"txIndex","type":"int256"},{"name":"sibling","type":"int256[]"},{"name":"txBlockHash","type":"int256"},{"name":"contract","type":"int256"}],"name":"relayTx(bytes,int256,int256,int256[],int256,int256)","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[{"name":"blockHash","type":"int256"},{"name":"height","type":"int256"},{"name":"cumulativeDifficulty","type":"int256"}],"name":"setInitialParent(int256,int256,int256)","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[{"name":"blockHeaderBinary","type":"bytes"}],"name":"storeBlockHeader(bytes)","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[{"name":"txHash","type":"int256"},{"name":"txIndex","type":"int256"},{"name":"sibling","type":"int256[]"},{"name":"txBlockHash","type":"int256"}],"name":"verifyTx(int256,int256,int256[],int256)","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[{"name":"txBlockHash","type":"int256"}],"name":"within6Confirms(int256)","outputs":[{"name":"out","type":"int256"}],"type":"function"}],"userDoc":null,"developerDoc":null}
{"source":"","language":"","languageVersion":"","compilerVersion":"","abiDefinition":[{"inputs":[{"name":"headersBytes","type":"bytes"},{"name":"count","type":"int256"}],"name":"bulkStoreHeader(bytes,int256)","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[{"name":"txHash","type":"int256"},{"name":"txIndex","type":"int256"},{"name":"sibling","type":"int256[]"}],"name":"computeMerkle(int256,int256,int256[])","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[],"name":"getAverageBlockDifficulty()","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[],"name":"getBlockchainHead()","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[],"name":"getCumulativeDifficulty()","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[],"name":"getLastBlockHeight()","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[{"name":"txBlockHash","type":"int256"}],"name":"inMainChain(int256)","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[{"name":"txStr","type":"bytes"},{"name":"txHash","type":"int256"},{"name":"txIndex","type":"int256"},{"name":"sibling","type":"int256[]"},{"name":"txBlockHash","type":"int256"},{"name":"contract","type":"int256"}],"name":"relayTx(bytes,int256,int256,int256[],int256,int256)","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[{"name":"blockHash","type":"int256"},{"name":"height","type":"int256"},{"name":"cumulativeDifficulty","type":"int256"}],"name":"setInitialParent(int256,int256,int256)","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[{"name":"blockHeaderBytes","type":"bytes"}],"name":"storeBlockHeader(bytes)","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[{"name":"txHash","type":"int256"},{"name":"txIndex","type":"int256"},{"name":"sibling","type":"int256[]"},{"name":"txBlockHash","type":"int256"}],"name":"verifyTx(int256,int256,int256[],int256)","outputs":[{"name":"out","type":"int256"}],"type":"function"},{"inputs":[{"name":"txBlockHash","type":"int256"}],"name":"within6Confirms(int256)","outputs":[{"name":"out","type":"int256"}],"type":"function"}],"userDoc":null,"developerDoc":null}
4 changes: 2 additions & 2 deletions examples/js/btcRelayAbi.js
@@ -1,7 +1,7 @@
window.btcRelayAbi = [{
"name": "bulkStoreHeader(bytes,int256)",
"type": "function",
"inputs": [{ "name": "headersBinary", "type": "bytes" }, { "name": "count", "type": "int256" }],
"inputs": [{ "name": "headersBytes", "type": "bytes" }, { "name": "count", "type": "int256" }],
"outputs": [{ "name": "out", "type": "int256" }]
},
{
Expand Down Expand Up @@ -49,7 +49,7 @@ window.btcRelayAbi = [{
{
"name": "storeBlockHeader(bytes)",
"type": "function",
"inputs": [{ "name": "blockHeaderBinary", "type": "bytes" }],
"inputs": [{ "name": "blockHeaderBytes", "type": "bytes" }],
"outputs": [{ "name": "out", "type": "int256" }]
},
{
Expand Down
14 changes: 7 additions & 7 deletions fetchd/fetchd.py
Expand Up @@ -180,25 +180,25 @@ def fetchHeaders(chunkStartNum, chunkSize, numChunk, network=BITCOIN_TESTNET):
chunkStartNum += chunkSize


def storeHeaders(bhBinary, chunkSize):
def storeHeaders(bhBytes, chunkSize):

txCount = instance.transaction_count(defaultBlock='pending')
logger.info('----------------------------------')
logger.info('txCount: %s' % txCount)

hashOne = blockHashHex(int(bin_dbl_sha256(bhBinary[:80])[::-1].encode('hex'), 16))
hashLast = blockHashHex(int(bin_dbl_sha256(bhBinary[-80:])[::-1].encode('hex'), 16))
hashOne = blockHashHex(int(bin_dbl_sha256(bhBytes[:80])[::-1].encode('hex'), 16))
hashLast = blockHashHex(int(bin_dbl_sha256(bhBytes[-80:])[::-1].encode('hex'), 16))
logger.info('hashOne: %s' % hashOne)
logger.info('hashLast: %s' % hashLast)

firstH = bhBinary[:80].encode('hex')
lastH = bhBinary[-80:].encode('hex')
firstH = bhBytes[:80].encode('hex')
lastH = bhBytes[-80:].encode('hex')
logger.info('firstH: %s' % firstH)
logger.info('lastH: %s' % lastH)

sig = 'bulkStoreHeader:[bytes,int256]:int256'

data = [bhBinary, chunkSize]
data = [bhBytes, chunkSize]

gas = 900000
value = 0
Expand All @@ -224,7 +224,7 @@ def storeHeaders(bhBinary, chunkSize):
txHash = instance.transact(instance.relayContract, sig=sig, data=data, gas=gas, value=value)

chainHead = getBlockchainHead()
expHead = int(bin_dbl_sha256(bhBinary[-80:])[::-1].encode('hex'), 16)
expHead = int(bin_dbl_sha256(bhBytes[-80:])[::-1].encode('hex'), 16)

if chainHead != expHead:
logger.info('@@@@@ MISMATCH chainHead: {0} expHead: {1}'.format(blockHashHex(chainHead), blockHashHex(expHead)))
Expand Down
54 changes: 27 additions & 27 deletions test/test_btcrelay.py
Expand Up @@ -5,7 +5,7 @@
import pytest
slow = pytest.mark.slow

from utilRelay import getBlockHeaderBinary, dblSha256Flip, disablePyethLogging
from utilRelay import getHeaderBytes, dblSha256Flip, disablePyethLogging

disablePyethLogging()

Expand Down Expand Up @@ -41,9 +41,9 @@ def testHeadersFrom100K(self):
"0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196367291b4d4c86041b8fa45d63",
"0100000045dc58743362fe8d8898a7506faa816baed7d391c9bc0b13b0da00000000000021728a2f4f975cc801cb3c672747f1ead8a946b2702b7bd52f7b86dd1aa0c975c02a1b4d4c86041b7b47546d"
]
blockHeaderBinary = map(lambda x: x.decode('hex'), headers)
blockHeaderBytes = map(lambda x: x.decode('hex'), headers)
for i in range(7):
res = self.c.storeBlockHeader(blockHeaderBinary[i])
res = self.c.storeBlockHeader(blockHeaderBytes[i])
# print('@@@@ real chain score: ' + str(self.c.getCumulativeDifficulty()))
assert res == i+100000

Expand Down Expand Up @@ -103,9 +103,9 @@ def testHeadersFrom100K(self):
hashPrevBlock = block100kPrev
for i in range(7):
nonce = 1 if (i in [4,5]) else 0
blockHeaderBinary = getBlockHeaderBinary(version, hashPrevBlock, hashMerkleRoot, time, bits, nonce)
res = self.c.storeBlockHeader(blockHeaderBinary)
hashPrevBlock = dblSha256Flip(blockHeaderBinary)
blockHeaderBytes = getHeaderBytes(version, hashPrevBlock, hashMerkleRoot, time, bits, nonce)
res = self.c.storeBlockHeader(blockHeaderBytes)
hashPrevBlock = dblSha256Flip(blockHeaderBytes)

# print('@@@@ fake chain score: ' + str(self.c.getCumulativeDifficulty()))
assert res == i+100000 # fake blocks are stored since there is possibility they can become the main chain
Expand Down Expand Up @@ -161,9 +161,9 @@ def testReorg(self):
hashPrevBlock = block100kPrev
for i in range(7):
nonce = 1 if (i in [4,5]) else 0
blockHeaderBinary = getBlockHeaderBinary(version, hashPrevBlock, hashMerkleRoot, time, bits, nonce)
res = self.c.storeBlockHeader(blockHeaderBinary)
hashPrevBlock = dblSha256Flip(blockHeaderBinary)
blockHeaderBytes = getHeaderBytes(version, hashPrevBlock, hashMerkleRoot, time, bits, nonce)
res = self.c.storeBlockHeader(blockHeaderBytes)
hashPrevBlock = dblSha256Flip(blockHeaderBytes)
assert res == i+100000

# testingonlySetHeaviest is needed because the difficulty from
Expand All @@ -188,10 +188,10 @@ def testReorg(self):
"0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196367291b4d4c86041b8fa45d63",
"0100000045dc58743362fe8d8898a7506faa816baed7d391c9bc0b13b0da00000000000021728a2f4f975cc801cb3c672747f1ead8a946b2702b7bd52f7b86dd1aa0c975c02a1b4d4c86041b7b47546d"
]
blockHeaderBinary = map(lambda x: x.decode('hex'), headers)
blockHeaderBytes = map(lambda x: x.decode('hex'), headers)
block100k = 0x000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506
for i in range(7):
res = self.c.storeBlockHeader(blockHeaderBinary[i])
res = self.c.storeBlockHeader(blockHeaderBytes[i])
assert res == i+100000

# firstEasyBlock should no longer verify since it is no longer on the main chain
Expand All @@ -218,9 +218,9 @@ def testWithin6Confirms(self):
"0100000079cda856b143d9db2c1caff01d1aecc8630d30625d10e8b4b8b0000000000000b50cc069d6a3e33e3ff84a5c41d9d3febe7c770fdcc96b2c3ff60abe184f196367291b4d4c86041b8fa45d63",
"0100000045dc58743362fe8d8898a7506faa816baed7d391c9bc0b13b0da00000000000021728a2f4f975cc801cb3c672747f1ead8a946b2702b7bd52f7b86dd1aa0c975c02a1b4d4c86041b7b47546d"
]
blockHeaderBinary = map(lambda x: x.decode('hex'), headers)
blockHeaderBytes = map(lambda x: x.decode('hex'), headers)
for i in range(7):
res = self.c.storeBlockHeader(blockHeaderBinary[i])
res = self.c.storeBlockHeader(blockHeaderBytes[i])
assert res == i+100000


Expand All @@ -245,15 +245,15 @@ def storeGenesisBlock(self):
self.c.setInitialParent(0, 0, 1)

blockHeaderStr = ("0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c")
bhBinary = blockHeaderStr.decode('hex')
res = self.c.storeBlockHeader(bhBinary, profiling=True)
bhBytes = blockHeaderStr.decode('hex')
res = self.c.storeBlockHeader(bhBytes, profiling=True)
return res

def storeBlock1(self):
# block 1
blockHeaderStr = ("010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e61bc6649ffff001d01e36299")
bhBinary = blockHeaderStr.decode('hex')
res = self.c.storeBlockHeader(bhBinary, profiling=True)
bhBytes = blockHeaderStr.decode('hex')
res = self.c.storeBlockHeader(bhBytes, profiling=True)
print('GAS: '+str(res['gas']))
assert res['output'] == 2 # genesis block is at height 1

Expand All @@ -279,24 +279,24 @@ def testStoreNewHead(self):
self.c.setInitialParent(parent, height, cumulDifficulty)

orphanStr = '03000000c269930b6793b7d3b01ca6b8c56863ca4f00b075a7bd150a0000000000000000391c579bd59cb0199baf96dc1bc1066de0dc202bbe18f062a20aa25f78729376ba6f5d55f586161826f45178'
bhBinary = orphanStr.decode('hex')
bhBytes = orphanStr.decode('hex')

orphanHash = 0x00000000000000000db6ab0aa23c28fc707f05f1646d25dba684ffe316bcf24d
assert dblSha256Flip(bhBinary) == orphanHash
assert dblSha256Flip(bhBytes) == orphanHash

assert self.c.storeBlockHeader(bhBinary) == height + 1
assert self.c.storeBlockHeader(bhBytes) == height + 1
assert self.c.getBlockchainHead() == orphanHash
assert self.c.getLastBlockHeight() == height + 1
assert self.c.getCumulativeDifficulty() == 1981795846593359

# real 357369
headerStr = '03000000c269930b6793b7d3b01ca6b8c56863ca4f00b075a7bd150a00000000000000004bdc09e5405944a6319baf5e90335f221d5b91d44f5212c05bb1e751b997cc74db6f5d55f5861618351ec186'
bhBinary = headerStr.decode('hex')
bhBytes = headerStr.decode('hex')

hash357369 = 0x000000000000000007f379bc159a38fa5ccec4689336f32eba9d148b5c190439
assert dblSha256Flip(bhBinary) == hash357369
assert dblSha256Flip(bhBytes) == hash357369

assert self.c.storeBlockHeader(bhBinary) == height + 1
assert self.c.storeBlockHeader(bhBytes) == height + 1
assert self.c.getBlockchainHead() == hash357369
assert self.c.getLastBlockHeight() == height + 1
assert self.c.getCumulativeDifficulty() == 1981795846593359
Expand Down Expand Up @@ -327,16 +327,16 @@ def testStoreBlockHeader(self):
# nonce = 796195988
# blockNumber = 333001
blockHeaderStr = '0200000059c786bb379b65487f373279354f8ccc91ffcea2200c36080000000000000000dd9d7757a736fec629ab0ed0f602ba23c77afe7edec85a7026f641fd90bcf8f658ca8154747b1b1894fc742f'
bhBinary = blockHeaderStr.decode('hex')
res = self.c.storeBlockHeader(bhBinary, profiling=True, sender=tester.k1)
bhBytes = blockHeaderStr.decode('hex')
res = self.c.storeBlockHeader(bhBytes, profiling=True, sender=tester.k1)
print('GAS: %s' % res['gas'])
assert res['output'] == 300000

# was converted to macro
# def testFastHashBlock(self):
# blockHeaderStr = "0100000050120119172a610421a6c3011dd330d9df07b63616c2cc1f1cd00200000000006657a9252aacd5c0b2940996ecff952228c3067cc38d4885efb5a4ac4247e9f337221b4d4c86041b0f2b5710"
# bhBinary = blockHeaderStr.decode('hex')
# res = self.c.fastHashBlock(bhBinary, profiling=True)
# bhBytes = blockHeaderStr.decode('hex')
# res = self.c.fastHashBlock(bhBytes, profiling=True)
# print('GAS: '+str(res['gas']))
# exp = 0x000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506
# assert res['output'] == exp
Expand Down

0 comments on commit e2760d4

Please sign in to comment.