Bits<256> Block::getSeedHash(Epoch epochs) { if(Block::epochToSeedMap.size() <= 0 || Block::seedToEpochMap.size() <= 0) { createSeedMapCache(); } if(epochs < 2048) { return Block::epochToSeedMap[epochs]; } else { Bits<256> seedHash = Block::epochToSeedMap[2047]; for (uint64_t i = 2047; i < epochs; ++i) { SHA3_256(seedHash.ptr(), seedHash.ptr(), 32); } return seedHash; } }
void Block::createSeedMapCache() { uint64_t seedMapFileSize = 2048*(256/8); uint8_t seedMap[seedMapFileSize]; std::ifstream inputFile( "seedmap", std::ios::binary ); if(inputFile.fail()) { Bits<256> seedHash; for(size_t i=0 ; i<2048 ; i++) { uint8_t* ptr = &seedMap[i*(256/8)]; memcpy(ptr,seedHash.ptr(),32); Block::epochToSeedMap[i] = Bits<256>(seedHash); Block::seedToEpochMap[seedHash] = i; SHA3_256(seedHash.ptr(), seedHash.ptr(), 32); } std::ofstream outputFile( "seedmap", std::ofstream::out ); outputFile.write((char*)seedMap, seedMapFileSize); outputFile.flush(); outputFile.close(); } else { inputFile.read((char*)seedMap,seedMapFileSize); inputFile.close(); for(size_t i=0 ; i<2048 ; i++) { uint8_t* ptr = &seedMap[i*(256/8)]; Block::epochToSeedMap[i] = Bits<256>(ptr); Block::seedToEpochMap[Bits<256>(ptr)] = i; } } }