Skip to content

Commit e121c8f

Browse files
axicekpyron
authored andcommitted
Store compiler version in CBOR metadata
1 parent 4037da9 commit e121c8f

File tree

8 files changed

+34
-41
lines changed

8 files changed

+34
-41
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Compiler Features:
88
* SMTChecker: Support inherited state variables.
99
* SMTChecker: Support tuples and function calls with multiple return values.
1010
* SMTChecker: Support ``delete``.
11+
* Assembler: Encode the compiler version in the deployed bytecode.
1112

1213

1314
Bugfixes:

cmake/EthBuildInfo.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ function(create_build_info NAME)
3737
-DETH_BUILD_COMPILER="${ETH_BUILD_COMPILER}"
3838
-DETH_BUILD_PLATFORM="${ETH_BUILD_PLATFORM}"
3939
-DPROJECT_VERSION="${PROJECT_VERSION}"
40+
-DPROJECT_VERSION_MAJOR="${PROJECT_VERSION_MAJOR}"
41+
-DPROJECT_VERSION_MINOR="${PROJECT_VERSION_MINOR}"
42+
-DPROJECT_VERSION_PATCH="${PROJECT_VERSION_PATCH}"
4043
-P "${ETH_SCRIPTS_DIR}/buildinfo.cmake"
4144
)
4245
include_directories("${PROJECT_BINARY_DIR}/include")

cmake/templates/BuildInfo.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#pragma once
22

33
#define ETH_PROJECT_VERSION "@PROJECT_VERSION@"
4+
#define ETH_PROJECT_VERSION_MAJOR @PROJECT_VERSION_MAJOR@
5+
#define ETH_PROJECT_VERSION_MINOR @PROJECT_VERSION_MINOR@
6+
#define ETH_PROJECT_VERSION_PATCH @PROJECT_VERSION_PATCH@
47
#define SOL_COMMIT_HASH "@SOL_COMMIT_HASH@"
58
#define ETH_BUILD_TYPE "@ETH_BUILD_TYPE@"
69
#define ETH_BUILD_OS "@ETH_BUILD_OS@"

libsolidity/interface/CompilerStack.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,10 @@ bytes CompilerStack::createCBORMetadata(string const& _metadata, bool _experimen
11771177
encoder.pushBytes("bzzr0", dev::swarmHash(_metadata).asBytes());
11781178
if (_experimentalMode)
11791179
encoder.pushBool("experimental", true);
1180+
if (VersionIsRelease)
1181+
encoder.pushBytes("solc", VersionCompactBytes);
1182+
else
1183+
encoder.pushString("solc", VersionStringStrict);
11801184
return encoder.serialise();
11811185
}
11821186

libsolidity/interface/Version.cpp

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,10 @@ string const dev::solidity::VersionStringStrict =
4444
(string(SOL_VERSION_PRERELEASE).empty() ? "" : "-" + string(SOL_VERSION_PRERELEASE)) +
4545
(string(SOL_VERSION_COMMIT).empty() ? "" : "+" + string(SOL_VERSION_COMMIT));
4646

47-
bytes dev::solidity::binaryVersion()
48-
{
49-
bytes ret{0};
50-
size_t i = 0;
51-
auto parseDecimal = [&]()
52-
{
53-
size_t ret = 0;
54-
solAssert('0' <= VersionString[i] && VersionString[i] <= '9', "");
55-
for (; i < VersionString.size() && '0' <= VersionString[i] && VersionString[i] <= '9'; ++i)
56-
ret = ret * 10 + (VersionString[i] - '0');
57-
return ret;
58-
};
59-
ret.push_back(uint8_t(parseDecimal()));
60-
solAssert(i < VersionString.size() && VersionString[i] == '.', "");
61-
++i;
62-
ret.push_back(uint8_t(parseDecimal()));
63-
solAssert(i < VersionString.size() && VersionString[i] == '.', "");
64-
++i;
65-
ret.push_back(uint8_t(parseDecimal()));
66-
solAssert(i < VersionString.size() && (VersionString[i] == '-' || VersionString[i] == '+'), "");
67-
++i;
68-
size_t commitpos = VersionString.find("commit.");
69-
solAssert(commitpos != string::npos, "");
70-
i = commitpos + 7;
71-
solAssert(i + 7 < VersionString.size(), "");
72-
bytes commitHash = fromHex(VersionString.substr(i, 8));
73-
solAssert(!commitHash.empty(), "");
74-
ret += commitHash;
75-
solAssert(ret.size() == 1 + 3 + 4, "");
76-
77-
return ret;
78-
}
47+
bytes const dev::solidity::VersionCompactBytes = {
48+
ETH_PROJECT_VERSION_MAJOR,
49+
ETH_PROJECT_VERSION_MINOR,
50+
ETH_PROJECT_VERSION_PATCH
51+
};
7952

53+
bool const dev::solidity::VersionIsRelease = string(SOL_VERSION_PRERELEASE).empty();

libsolidity/interface/Version.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,8 @@ namespace solidity
3333
extern char const* VersionNumber;
3434
extern std::string const VersionString;
3535
extern std::string const VersionStringStrict;
36-
37-
/// @returns a binary form of the version string, where A.B.C-HASH is encoded such that
38-
/// the first byte is zero, the following three bytes encode A B and C (interpreted as decimals)
39-
/// and HASH is interpreted as 8 hex digits and encoded into the last four bytes.
40-
bytes binaryVersion();
36+
extern bytes const VersionCompactBytes;
37+
extern bool const VersionIsRelease;
4138

4239
}
4340
}

test/Metadata.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ bytes onlyMetadata(bytes const& _bytecode)
4141
size_t metadataSize = (_bytecode[size - 2] << 8) + _bytecode[size - 1];
4242
if (size < (metadataSize + 2))
4343
return bytes{};
44-
// Sanity check: assume the first byte is a fixed-size CBOR array with either 1 or 2 entries
44+
// Sanity check: assume the first byte is a fixed-size CBOR array with either 2 or 3 entries
4545
unsigned char firstByte = _bytecode[size - metadataSize - 2];
46-
if (firstByte != 0xa1 && firstByte != 0xa2)
46+
if (firstByte != 0xa2 && firstByte != 0xa3)
4747
return bytes{};
4848
return bytes(_bytecode.end() - metadataSize - 2, _bytecode.end() - 2);
4949
}

test/libsolidity/Metadata.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <test/Metadata.h>
2323
#include <test/Options.h>
2424
#include <libsolidity/interface/CompilerStack.h>
25+
#include <libsolidity/interface/Version.h>
2526
#include <libdevcore/SwarmHash.h>
2627
#include <libdevcore/JSON.h>
2728

@@ -69,7 +70,12 @@ BOOST_AUTO_TEST_CASE(metadata_stamp)
6970
bytes hash = dev::swarmHash(metadata).asBytes();
7071
BOOST_REQUIRE(hash.size() == 32);
7172
auto const cborMetadata = requireParsedCBORMetadata(bytecode);
72-
BOOST_CHECK(cborMetadata.size() == 1);
73+
BOOST_CHECK(cborMetadata.size() == 2);
74+
BOOST_CHECK(cborMetadata.count("solc") == 1);
75+
if (VersionIsRelease)
76+
BOOST_CHECK(cborMetadata.at("solc") == toHex(VersionCompactBytes));
77+
else
78+
BOOST_CHECK(cborMetadata.at("solc") == VersionStringStrict);
7379
BOOST_CHECK(cborMetadata.count("bzzr0") == 1);
7480
BOOST_CHECK(cborMetadata.at("bzzr0") == toHex(hash));
7581
}
@@ -95,7 +101,12 @@ BOOST_AUTO_TEST_CASE(metadata_stamp_experimental)
95101
bytes hash = dev::swarmHash(metadata).asBytes();
96102
BOOST_REQUIRE(hash.size() == 32);
97103
auto const cborMetadata = requireParsedCBORMetadata(bytecode);
98-
BOOST_CHECK(cborMetadata.size() == 2);
104+
BOOST_CHECK(cborMetadata.size() == 3);
105+
BOOST_CHECK(cborMetadata.count("solc") == 1);
106+
if (VersionIsRelease)
107+
BOOST_CHECK(cborMetadata.at("solc") == toHex(VersionCompactBytes));
108+
else
109+
BOOST_CHECK(cborMetadata.at("solc") == VersionStringStrict);
99110
BOOST_CHECK(cborMetadata.count("bzzr0") == 1);
100111
BOOST_CHECK(cborMetadata.at("bzzr0") == toHex(hash));
101112
BOOST_CHECK(cborMetadata.count("experimental") == 1);

0 commit comments

Comments
 (0)