def solveData (hexData, target, ok): """ Solve a block header given as hex in getwork's 'data' format (or not). This uses Neoscrypt for hashing, since that is what we use in Xaya for stand-alone (getwork) blocks. """ data = codecs.decode (hexData, 'hex_codec') data = auxpow.getworkByteswap (data[:80]) def neoscrypt (hexStr): rawData = codecs.decode (hexStr, 'hex_codec') rawHash = bytearray (powhash.forHeader ('neoscrypt', rawData)) rawHash.reverse () return codecs.encode (rawHash, 'hex_codec') hexSolved, h = mineBlock (codecs.encode (data, 'hex_codec'), target, ok, hashFcn=neoscrypt) solved = codecs.decode (hexSolved, 'hex_codec') solved = auxpow.getworkByteswap (solved) return codecs.decode (codecs.encode (solved, 'hex_codec'), 'ascii')
def test_common(self, create, submit): """ Common test code that is shared between the tests for getwork and the creatework / submitwork method pair. """ # Verify data that can be found in another way. work = create() assert_equal(work['algo'], 'neoscrypt') assert_equal(work['height'], self.nodes[0].getblockcount() + 1) assert_equal(work['previousblockhash'], self.nodes[0].getblockhash(work['height'] - 1)) # Invalid format for data. assert_raises_rpc_error(-8, None, submit, "", "x") assert_raises_rpc_error(-8, None, submit, "", "00") # Compute invalid work. target = reverseHex(work['target']) solved = solveData(work['data'], target, False) res = submit(work['hash'], solved) assert not res # Compute and submit valid work. solved = solveData(work['data'], target, True) res = submit(work['hash'], solved) assert res # Make sure that the block is indeed accepted. height = self.nodes[0].getblockcount() assert_equal(height, work['height']) # Call getblock and verify the powdata field. data = self.nodes[0].getblock(work['hash']) assert 'powdata' in data data = data['powdata'] assert_equal(data['algo'], 'neoscrypt') assert_equal(data['mergemined'], False) assert_equal(data['bits'], '207fffff') assert 'difficulty' in data fakeHeader = codecs.decode(solved, 'hex_codec') fakeHeader = getworkByteswap(fakeHeader) fakeHeader = codecs.encode(fakeHeader, 'hex_codec') fakeHeader = codecs.decode(fakeHeader, 'ascii') assert_equal(data['fakeheader'], fakeHeader) # Also previous blocks should have 'powdata', since all blocks (also # those generated by "generate") are mined with it. oldHash = self.nodes[0].getblockhash(100) data = self.nodes[0].getblock(oldHash) assert 'powdata' in data # Check that it paid correctly to the first node. t = self.nodes[0].listtransactions("*", 1) assert_equal(len(t), 1) t = t[0] assert_equal(t['category'], "immature") assert t['generated'] assert_greater_than_or_equal(t['amount'], Decimal("1")) assert_equal(t['confirmations'], 1) # Mine a block using the hash-less form of submit. work = create() target = reverseHex(work['target']) solved = solveData(work['data'], target, True) res = submit(solved) assert res assert_equal(self.nodes[0].getbestblockhash(), work['hash'])