From c3198c1928225292f071bc9fe377e1d6f5bc6112 Mon Sep 17 00:00:00 2001 From: Sasha Frolov Date: Wed, 22 Nov 2017 11:15:21 -0500 Subject: [PATCH] Implement getNonce/getVersionNo + Tests --- contracts/BTCRelay.sol | 16 ++++++++++++++++ test/TestBTCRelay.sol | 14 ++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/contracts/BTCRelay.sol b/contracts/BTCRelay.sol index cbdb6a2..16e4e41 100644 --- a/contracts/BTCRelay.sol +++ b/contracts/BTCRelay.sol @@ -87,4 +87,20 @@ contract BTCRelay { return tmp >> 224; } + function getVersionNo(bytes header) public constant returns (uint){ + uint tmp; + assembly { + tmp := mload(add(header,32)) + } + return tmp >> 224; + } + + function getNonce(bytes header) public constant returns(uint){ + uint tmp; + assembly{ + tmp := mload(add(header, 108)) + } + return tmp >> 224; + } + } diff --git a/test/TestBTCRelay.sol b/test/TestBTCRelay.sol index b2bfa35..8d91a47 100644 --- a/test/TestBTCRelay.sol +++ b/test/TestBTCRelay.sol @@ -89,4 +89,18 @@ contract TestBTCRelay { uint res = relay.getTimestamp(header); Assert.equal(res, 0xabcdef12, "getTimestamp"); } + + function testGetVersionNo(){ + BTCRelay relay = BTCRelay(DeployedAddresses.BTCRelay()); + bytes memory header = hex"f01dab1e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f01dab1e"; + uint version = relay.getVersionNo(header); + Assert.equal(version, 0xf01dab1e, "getVersionNo"); + } + + function testGetNonce(){ + BTCRelay relay = BTCRelay(DeployedAddresses.BTCRelay()); + bytes memory header = hex"000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c105e7edc105e7ed"; + uint nonce = relay.getNonce(header); + Assert.equal(nonce, 0xc105e7ed, "getNonce"); + } }