Skip to content

Commit

Permalink
feat(traffictile.h): Adds versioning checks
Browse files Browse the repository at this point in the history
Allocates one of the spare fields in the TrafficTileHeader
for versioning data, with the version being made up of
the first four bytes of the MD5 checksum of baldr/traffictile.h
  • Loading branch information
purew committed Jul 22, 2020
1 parent 014c017 commit d60dac1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
16 changes: 16 additions & 0 deletions scripts/build_traffic_tile_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
# Builds the traffic_tile_version.h header

set -o errexit -o pipefail -o nounset

readonly VALHALLA_SOURCE_DIR="${1:-NONE}"

if [ "$VALHALLA_SOURCE_DIR" = NONE ]; then
echo "Must specify path to root folder"
exit 1
fi

printf "const uint32_t TRAFFIC_TILE_VERSION = 0x%s;" $(
# Grab only four bytes because space constraints
sha1sum ${VALHALLA_SOURCE_DIR}/valhalla/baldr/traffictile.h | head -c4
)
15 changes: 13 additions & 2 deletions src/baldr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,22 @@ add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/date_time_windows_zones.h

file(GLOB headers ${VALHALLA_SOURCE_DIR}/valhalla/baldr/*.h)

# Builds a checksum of the traffictile header and store it in `traffic_tile_version.h`
set(GENERATED_VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/traffic_tile_version.h")
add_custom_command(OUTPUT "${GENERATED_VERSION_HEADER}"
COMMENT "Generating the traffic-tile version"
COMMAND ${VALHALLA_SOURCE_DIR}/scripts/build_traffic_tile_version.sh ${VALHALLA_SOURCE_DIR} > "${GENERATED_VERSION_HEADER}"
DEPENDS ${VALHALLA_SOURCE_DIR}/valhalla/baldr/traffictile.h
)

set(includes
${VALHALLA_SOURCE_DIR}
${VALHALLA_SOURCE_DIR}/valhalla
$<$<BOOL:${MSVC}>:${VALHALLA_SOURCE_DIR}/third_party/dirent/include>
${VALHALLA_SOURCE_DIR}/third_party/rapidjson/include
${VALHALLA_SOURCE_DIR}/third_party/date/include)
${VALHALLA_SOURCE_DIR}/third_party/date/include
${CMAKE_CURRENT_BINARY_DIR}/src/baldr
)

set(sources
accessrestriction.cc
Expand Down Expand Up @@ -69,8 +79,9 @@ set(sources
verbal_text_formatter_us_tx.cc
verbal_text_formatter_factory.cc)

#basic timezone stuff
list(APPEND sources
${GENERATED_VERSION_HEADER}
#basic timezone stuff
${CMAKE_CURRENT_BINARY_DIR}/date_time_africa.h
${CMAKE_CURRENT_BINARY_DIR}/date_time_antarctica.h
${CMAKE_CURRENT_BINARY_DIR}/date_time_asia.h
Expand Down
4 changes: 3 additions & 1 deletion test/gurka/test_traffic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ void update_all_edges_but_bd(const valhalla::gurka::map& map, uint16_t new_speed
update_all_edges_but_bd(map, ts);
}

void blank_traffic(const valhalla::gurka::map& map) {
void blank_traffic(const valhalla::gurka::map& map,
uint32_t traffic_tile_version = TRAFFIC_TILE_VERSION) {
const auto& traffic_extract = map.config.get<std::string>("mjolnir.traffic_extract");
mtar_t tar;
auto tar_open_result = mtar_open(&tar, traffic_extract.c_str(), "w");
Expand All @@ -109,6 +110,7 @@ void blank_traffic(const valhalla::gurka::map& map) {
std::stringstream buffer;
baldr::TrafficTileHeader header = {};
header.tile_id = tile_id;
header.traffic_tile_version = traffic_tile_version;
std::vector<baldr::TrafficSpeed> speeds;
header.directed_edge_count = tile->header()->directededgecount();
buffer.write(reinterpret_cast<char*>(&header), sizeof(header));
Expand Down
8 changes: 8 additions & 0 deletions test/traffictile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ TEST(Traffic, TileConstruction) {
TestTile testdata = {};

testdata.header.directed_edge_count = 3;
testdata.header.traffic_tile_version = TRAFFIC_TILE_VERSION;
testdata.speed3.overall_speed = 98 >> 1;
testdata.speed3.speed1 = 98 >> 1;
testdata.speed3.speed2 = UNKNOWN_TRAFFIC_SPEED_RAW;
Expand All @@ -32,6 +33,13 @@ TEST(Traffic, TileConstruction) {
EXPECT_EQ(speed.get_overall_speed(), 98);
EXPECT_EQ(speed.get_speed(0), 98);
EXPECT_EQ(speed.get_speed(1), UNKNOWN_TRAFFIC_SPEED_RAW << 1);

// Verify the version (Expect this value to change when traffictile.h is updated)
EXPECT_EQ(0x4c91, TRAFFIC_TILE_VERSION);
// Test with an invalid version
testdata.header.traffic_tile_version = 78;
auto const volatile& invalid_speed = tile.trafficspeed(2);
EXPECT_FALSE(invalid_speed.valid());
}

TEST(Traffic, NullTileConstruction) {
Expand Down
9 changes: 7 additions & 2 deletions valhalla/baldr/traffictile.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#else
#include <stdint.h>
#endif
#include "baldr/traffic_tile_version.h"

#ifndef C_ONLY_INTERFACE
namespace valhalla {
Expand Down Expand Up @@ -69,6 +70,7 @@ struct TrafficSpeed {
assert(false);
}
}

/// Returns overall speed in kph across edge
inline uint8_t get_overall_speed() const volatile {
return overall_speed << 1;
Expand Down Expand Up @@ -102,7 +104,7 @@ struct TrafficTileHeader {
uint64_t tile_id;
uint64_t last_update; // seconds since epoch
uint32_t directed_edge_count;
uint32_t spare1;
uint32_t traffic_tile_version;
uint32_t spare2;
uint32_t spare3;
};
Expand Down Expand Up @@ -149,8 +151,11 @@ class TrafficTile {
}

const volatile TrafficSpeed& trafficspeed(const uint32_t directed_edge_offset) const {
if (header == nullptr)
if (header == nullptr) {
return INVALID_SPEED;
} else if (header->traffic_tile_version != TRAFFIC_TILE_VERSION) {
return INVALID_SPEED;
}
if (directed_edge_offset >= header->directed_edge_count)
throw std::runtime_error("TrafficSpeed requested for edgeid beyond bounds of tile (offset: " +
std::to_string(directed_edge_offset) +
Expand Down

0 comments on commit d60dac1

Please sign in to comment.