From 9d0fb51be3e75f5322b60545e5ad7016ad92a381 Mon Sep 17 00:00:00 2001 From: Sasha Date: Fri, 17 Nov 2017 12:50:41 -0500 Subject: [PATCH] Skeleton for Store Block Header --- README.md | 5 ----- contracts/BTCRelay.sol | 49 +++++++++++++++++++++++++++++++++++++----- package.json | 2 +- test/TestBTCRelay.sol | 24 +++++++++++++++++++++ 4 files changed, 69 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 2e66ffb..32ce95e 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,3 @@ # BTCRelay-Solidity This project implements BTC relay, a medium of exchange for Bitcoin and Ethereum, in solidity - -## Testing: - - testrpc -l 40000000 & - truffle test diff --git a/contracts/BTCRelay.sol b/contracts/BTCRelay.sol index 490a449..e3e71d0 100644 --- a/contracts/BTCRelay.sol +++ b/contracts/BTCRelay.sol @@ -15,20 +15,59 @@ contract BTCRelay { // storeBlockHeader(header) pareses a length 80 bytes and stores the resulting // Header struct in the blockHeaders mapping, where the index is the blockhash - function storeBlockHeader(bytes header){} + function storeBlockHeader(bytes header){ + require(header.length >= 80); + uint32 version_ = uint32(uint(bytesToBytes4(header, 0))); + bytes32 prevBlock_ = bytesToBytes32(header, 4); + bytes32 merkleRoot_ = bytesToBytes32(header, 36); + uint32 time_ = uint32(uint(bytesToBytes4(header, 68))); + uint32 nBits_ = uint32(uint(bytesToBytes4(header, 72))); + uint32 nonce_ = uint32(uint(bytesToBytes4(header, 76))); + + bytes32 index = dblShaFlip(header); + blockHeaders[index] = Header(version_, prevBlock_, merkleRoot_, time_, nBits_, nonce_); + } + + + function bytesToBytes32(bytes b, uint offset) private pure returns (bytes32) { + bytes32 out; + + for (uint i = 0; i < 32; i++) { + out |= bytes32(b[offset + i] & 0xFF) >> (i * 8); + } + return out; + } + + function bytesToBytes4(bytes b, uint offset) private pure returns (bytes4) { + bytes4 out; + + for (uint i = 0; i < 4; i++) { + out |= bytes4(b[offset + i] & 0xFF) >> (i * 8); + } + return out; + } // computeMerkle(txHash, txIndex, siblings) computes the Merkle root of the // block that the transaction corresponding to txHash was included in. - function computeMerkle(bytes32 txHash, uint txIndex, bytes32[] siblings) pure returns (bytes32 merkleRoot){} + function computeMerkle(bytes32 txHash, uint txIndex, bytes32[] siblings) pure returns (bytes32 merkleRoot){ + + } // Computes the target from the compressed "bits" form // https://bitcoin.org/en/developer-reference#target-nbits - function targetFromBits(uint32 nBits) pure returns (bytes32 target){} + function targetFromBits(uint32 nBits) pure returns (bytes32 target){ + + } // Converts the input to the opposite endianness - function flip32(bytes32) pure returns (bytes32) {} + function flip32(bytes32) pure returns (bytes32) { + + } // BTC-style reversed double sha256 - function dblShaFlip(bytes data) pure returns (bytes32){} + function dblShaFlip(bytes data) pure returns (bytes32){ + bytes32 headerPt1 = 0x020000007ef055e1674d2e6551dba41cd214debbee34aeb544c7ec6700000000; + return headerPt1; //dummy function, delet this. + } } diff --git a/package.json b/package.json index 3dbf340..ae50571 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "truffle": "^4.0.1" }, "scripts": { - "test": "truffle test", + "test": "testrpc -l 40000000 & truffle test && kill %1", "coverage": "./node_modules/.bin/solidity-coverage" }, "repository": { diff --git a/test/TestBTCRelay.sol b/test/TestBTCRelay.sol index d8522e1..f1a7397 100644 --- a/test/TestBTCRelay.sol +++ b/test/TestBTCRelay.sol @@ -12,5 +12,29 @@ contract TestBTCRelay { Assert.equal(relay.flip32(le), be, "flip32 should convert LE <-> BE"); } + function testStoreBlockHeader(){ + BTCRelay relay = BTCRelay(DeployedAddresses.BTCRelay()); + + bytes32 headerPt1 = 0x020000007ef055e1674d2e6551dba41cd214debbee34aeb544c7ec6700000000; + bytes32 headerPt2 = 0x00000000d3998963f80c5bab43fe8c26228e98d030edf4dcbe48a666f5c39e2d; + bytes16 headerPt3 = 0x7a885c9102c86d536c890019593a470d; + bytes memory testHeader = new bytes(160); + for(uint x = 0; x < 32; x ++){ + testHeader[x] = headerPt1[x]; + } //I have no clue how to declare an 80 bytes literal in one go so I did it like this. + + + relay.storeBlockHeader(testHeader); + uint32 version; + bytes32 prevBlock; + bytes32 merkleRoot; + uint32 time; + uint32 nBits; + uint32 nonce; + (version, prevBlock, merkleRoot, time, nBits, nonce) = relay.blockHeaders(0x020000007ef055e1674d2e6551dba41cd214debbee34aeb544c7ec6700000000); + uint version_ = uint(version); + uint nonce_ = uint(nonce); + Assert.equal(prevBlock, merkleRoot, "I cannot figure this part out"); + } }