Skip to content

Commit

Permalink
Merge pull request #110 from qubic/develop
Browse files Browse the repository at this point in the history
Release/v1.203 to main
  • Loading branch information
philippwerner authored May 14, 2024
2 parents 63ddb33 + b6ad419 commit 33822a9
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/network_core/peers.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ static void closePeer(Peer* peer)
}
}

// Add message to sending buffer of specific peer, can only called from main thread (not thread-safe).
static void push(Peer* peer, RequestResponseHeader* requestResponseHeader)
{
// The sending buffer may queue multiple messages, each of which may need to transmitted in many small packets.
Expand All @@ -149,6 +150,7 @@ static void push(Peer* peer, RequestResponseHeader* requestResponseHeader)
}
}

// Add message to sending buffer of random peer, can only called from main thread (not thread-safe).
static void pushToAny(RequestResponseHeader* requestResponseHeader)
{
unsigned short suitablePeerIndices[NUMBER_OF_OUTGOING_CONNECTIONS + NUMBER_OF_INCOMING_CONNECTIONS];
Expand All @@ -166,6 +168,7 @@ static void pushToAny(RequestResponseHeader* requestResponseHeader)
}
}

// Add message to sending buffer of some random peers, can only called from main thread (not thread-safe).
static void pushToSeveral(RequestResponseHeader* requestResponseHeader)
{
unsigned short suitablePeerIndices[NUMBER_OF_OUTGOING_CONNECTIONS + NUMBER_OF_INCOMING_CONNECTIONS];
Expand All @@ -186,6 +189,7 @@ static void pushToSeveral(RequestResponseHeader* requestResponseHeader)
}
}

// Add message to response queue of specific peer. If peer is NULL, it will be sent to random peers. Can be called from any thread.
static void enqueueResponse(Peer* peer, RequestResponseHeader* responseHeader)
{
ACQUIRE(responseQueueHeadLock);
Expand All @@ -207,6 +211,7 @@ static void enqueueResponse(Peer* peer, RequestResponseHeader* responseHeader)
RELEASE(responseQueueHeadLock);
}

// Add message to response queue of specific peer. If peer is NULL, it will be sent to random peers. Can be called from any thread.
static void enqueueResponse(Peer* peer, unsigned int dataSize, unsigned char type, unsigned int dejavu, const void* data)
{
ACQUIRE(responseQueueHeadLock);
Expand Down
18 changes: 18 additions & 0 deletions src/network_messages/special_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,26 @@ struct UtcTime

#define SPECIAL_COMMAND_QUERY_TIME 12ULL // send this to node to query time, responds with time read from clock
#define SPECIAL_COMMAND_SEND_TIME 13ULL // send this to node to set time, responds with time read from clock after setting

struct SpecialCommandSendTime
{
unsigned long long everIncreasingNonceAndCommandType;
UtcTime utcTime;
};

#define SPECIAL_COMMAND_GET_MINING_SCORE_RANKING 14ULL
#pragma pack( push, 1)
template<unsigned int maxNumberOfMiners>
struct SpecialCommandGetMiningScoreRanking
{
struct ScoreRankingEntry {
m256i minerPublicKey;
unsigned int minerScore;
};

unsigned long long everIncreasingNonceAndCommandType;
unsigned int numberOfRankings;
ScoreRankingEntry rankings[maxNumberOfMiners];
};

#pragma pack(pop)
10 changes: 5 additions & 5 deletions src/public_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
// Config options that should NOT be changed by operators

#define VERSION_A 1
#define VERSION_B 202
#define VERSION_B 203
#define VERSION_C 0

// Epoch and initial tick for node startup
#define EPOCH 108
#define TICK 13820000
#define EPOCH 109
#define TICK 13960000

#define ARBITRATOR "AFZPUAIYVPNUYGJRQVLUKOPPVLHAZQTGLYAAUUNBXFTVTAMSBKQBLEIEPCVJ"

Expand All @@ -53,8 +53,8 @@ static unsigned short CONTRACT_FILE_NAME[] = L"contract????.???";

#define DATA_LENGTH 256
#define INFO_LENGTH 128
#define NUMBER_OF_INPUT_NEURONS 8192
#define NUMBER_OF_OUTPUT_NEURONS 8192
#define NUMBER_OF_INPUT_NEURONS 16384
#define NUMBER_OF_OUTPUT_NEURONS 16384
#define MAX_INPUT_DURATION 256
#define MAX_OUTPUT_DURATION 256
#define NEURON_VALUE_LIMIT 1LL
Expand Down
35 changes: 34 additions & 1 deletion src/qubic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ static bool competitorComputorStatuses[(NUMBER_OF_COMPUTORS - QUORUM) * 2];
static unsigned int minimumComputorScore = 0, minimumCandidateScore = 0;
static int solutionThreshold[MAX_NUMBER_EPOCH] = { -1 };
static unsigned long long solutionTotalExecutionTicks = 0;
static volatile char minerScoreArrayLock = 0;
static SpecialCommandGetMiningScoreRanking<MAX_NUMBER_OF_MINERS> requestMiningScoreRanking;

BroadcastFutureTickData broadcastedFutureTickData;

Expand Down Expand Up @@ -603,6 +605,7 @@ static void processBroadcastComputors(Peer* peer, RequestResponseHeader* header)
if (request->computors.epoch == system.epoch)
{
numberOfOwnComputorIndices = 0;
ACQUIRE(minerScoreArrayLock);
for (unsigned int i = 0; i < NUMBER_OF_COMPUTORS; i++)
{
minerPublicKeys[i] = request->computors.publicKeys[i];
Expand All @@ -618,6 +621,7 @@ static void processBroadcastComputors(Peer* peer, RequestResponseHeader* header)
}
}
}
RELEASE(minerScoreArrayLock);
}
}
}
Expand Down Expand Up @@ -1215,6 +1219,28 @@ static void processSpecialCommand(Peer* peer, RequestResponseHeader* header)
enqueueResponse(peer, sizeof(SpecialCommandSendTime), SpecialCommand::type, header->dejavu(), &response);
}
break;
case SPECIAL_COMMAND_GET_MINING_SCORE_RANKING:
{
requestMiningScoreRanking.everIncreasingNonceAndCommandType =
(request->everIncreasingNonceAndCommandType & 0xFFFFFFFFFFFFFF) | (SPECIAL_COMMAND_GET_MINING_SCORE_RANKING << 56);

ACQUIRE(minerScoreArrayLock);
requestMiningScoreRanking.numberOfRankings = numberOfMiners;
for (unsigned int i = 0; i < requestMiningScoreRanking.numberOfRankings; ++i)
{
requestMiningScoreRanking.rankings[i].minerPublicKey = minerPublicKeys[i];
requestMiningScoreRanking.rankings[i].minerScore = minerScores[i];
}
RELEASE(minerScoreArrayLock);
enqueueResponse(peer,
sizeof(requestMiningScoreRanking.everIncreasingNonceAndCommandType)
+ sizeof(requestMiningScoreRanking.numberOfRankings)
+ sizeof(requestMiningScoreRanking.rankings[0]) * requestMiningScoreRanking.numberOfRankings,
SpecialCommand::type,
header->dejavu(),
&requestMiningScoreRanking);
}
break;
}
}
}
Expand Down Expand Up @@ -2319,6 +2345,7 @@ static void processTick(unsigned long long processorNumber)
}
}

ACQUIRE(minerScoreArrayLock);
unsigned int minerIndex;
for (minerIndex = 0; minerIndex < numberOfMiners; minerIndex++)
{
Expand Down Expand Up @@ -2365,6 +2392,7 @@ static void processTick(unsigned long long processorNumber)
}
competitorComputorStatuses[i + (NUMBER_OF_COMPUTORS - QUORUM)] = false;
}
RELEASE(minerScoreArrayLock);

// bubble sorting -> top 225 from competitorPublicKeys have computors and candidates which are the best from that subset
for (unsigned int i = NUMBER_OF_COMPUTORS - QUORUM; i < (NUMBER_OF_COMPUTORS - QUORUM) * 2; i++)
Expand Down Expand Up @@ -2401,10 +2429,13 @@ static void processTick(unsigned long long processorNumber)
minimumCandidateScore = minimumComputorScore;
}

ACQUIRE(minerScoreArrayLock);
for (unsigned int i = 0; i < QUORUM; i++)
{
system.futureComputors[i] = minerPublicKeys[i];
}
RELEASE(minerScoreArrayLock);

for (unsigned int i = QUORUM; i < NUMBER_OF_COMPUTORS; i++)
{
system.futureComputors[i] = competitorPublicKeys[i - QUORUM];
Expand Down Expand Up @@ -2704,7 +2735,7 @@ static void beginEpoch1of2()
CONTRACT_FILE_NAME[sizeof(CONTRACT_FILE_NAME) / sizeof(CONTRACT_FILE_NAME[0]) - 3] = (system.epoch % 100) / 10 + L'0';
CONTRACT_FILE_NAME[sizeof(CONTRACT_FILE_NAME) / sizeof(CONTRACT_FILE_NAME[0]) - 2] = system.epoch % 10 + L'0';

bs->SetMem(score, sizeof(*score), 0);
score->initMemory();
score->resetTaskQueue();
bs->SetMem(minerSolutionFlags, NUMBER_OF_MINER_SOLUTION_FLAGS / 8, 0);
bs->SetMem((void*)minerPublicKeys, sizeof(minerPublicKeys), 0);
Expand Down Expand Up @@ -3948,6 +3979,7 @@ static bool initialize()
logStatusToConsole(L"EFI_BOOT_SERVICES.AllocatePool() fails", status, __LINE__);
return false;
}
setMem(score, sizeof(*score), 0);

bs->SetMem(solutionThreshold, sizeof(int) * MAX_NUMBER_EPOCH, 0);
if (status = bs->AllocatePool(EfiRuntimeServicesData, NUMBER_OF_MINER_SOLUTION_FLAGS / 8, (void**)&minerSolutionFlags))
Expand Down Expand Up @@ -5139,6 +5171,7 @@ EFI_STATUS efi_main(EFI_HANDLE imageHandle, EFI_SYSTEM_TABLE* systemTable)
}
}

// Add messages from response queue to sending buffer
const unsigned short responseQueueElementHead = ::responseQueueElementHead;
if (responseQueueElementTail != responseQueueElementHead)
{
Expand Down
33 changes: 30 additions & 3 deletions src/score.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ struct ScoreFunction
static constexpr unsigned int maxAllNeuronLength = dataLength + numberOfNeuronsMaxInputOutput + infoLength;
long long miningData[dataLength];

struct
struct synapseStruct
{
char inputLength[(numberOfInputNeurons + infoLength) * (dataLength + numberOfInputNeurons + infoLength)];
char outputLength[(numberOfOutputNeurons + dataLength) * (infoLength + numberOfOutputNeurons + dataLength)];
} _synapses[solutionBufferCount];
} * _synapses;

struct queueItem {
short tick;
Expand Down Expand Up @@ -74,7 +74,7 @@ struct ScoreFunction
unsigned char _maxIndexBuffer[allParamsCount * 2][32];
static_assert(maxInputDuration <= 256 && maxOutputDuration <= 256, "Need to increase size of _maxIndexBuffer");
short buffer[256];
} _computeBuffer[solutionBufferCount];
} * _computeBuffer;
static_assert(maxInputDuration <= 256 && maxOutputDuration <= 256, "Need to regenerate mod num table");
// _totalModNum[i]: total of divisible numbers of i
unsigned char _totalModNum[257];
Expand Down Expand Up @@ -111,6 +111,33 @@ struct ScoreFunction
}
}

bool initMemory()
{
// TODO: call freePool() for buffers allocated below
if (_synapses == nullptr) {
if (!allocatePool(sizeof(synapseStruct) * solutionBufferCount, (void**)&_synapses))
{
logToConsole(L"Failed to allocate memory for score solution buffer!");
return false;
}
}
if (_computeBuffer == nullptr) {
if (!allocatePool(sizeof(computeBuffer) * solutionBufferCount, (void**)&_computeBuffer))
{
logToConsole(L"Failed to allocate memory for score solution buffer!");
return false;
}
}
for (int i = 0; i < solutionBufferCount; i++) {
setMem(&_synapses[i], sizeof(synapseStruct), 0);
setMem(&_computeBuffer[i], sizeof(computeBuffer), 0);
solutionEngineLock[i] = 0;
}
scoreCacheLock = 0;
setMem(&scoreCache, sizeof(scoreCache), 0);
return true;
}

// Save score cache to SCORE_CACHE_FILE_NAME
void saveScoreCache(int epoch)
{
Expand Down
1 change: 1 addition & 0 deletions test/score.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ struct ScoreTester
score_ref_impl = new ScoreFuncRef;
memset(score, 0, sizeof(ScoreFuncOpt));
memset(score_ref_impl, 0, sizeof(ScoreFuncRef));
EXPECT_TRUE(score->initMemory());
score->initMiningData(_mm256_setzero_si256());
score_ref_impl->initMiningData();
}
Expand Down

0 comments on commit 33822a9

Please sign in to comment.