Skip to content

Commit

Permalink
Merge pull request #1669 from cyrossignol/beacon
Browse files Browse the repository at this point in the history
Overhaul the contract handling system
  • Loading branch information
jamescowens authored May 13, 2020
2 parents 56e67bb + a77bb70 commit 982491c
Show file tree
Hide file tree
Showing 38 changed files with 3,913 additions and 620 deletions.
14 changes: 11 additions & 3 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ GRIDCOIN_CORE_H = \
compat.h \
compat/byteswap.h \
compat/endian.h \
contract/cpid.h \
contract/message.h \
contract/polls.h \
contract/contract.h \
contract/rain.h \
crypter.h \
db.h \
fs.h \
Expand All @@ -99,7 +101,9 @@ GRIDCOIN_CORE_H = \
neuralnet/accrual/null.h \
neuralnet/accrual/research_age.h \
neuralnet/accrual/snapshot.h \
neuralnet/beacon.h \
neuralnet/claim.h \
neuralnet/contract.h \
neuralnet/cpid.h \
neuralnet/magnitude.h \
neuralnet/project.h \
Expand All @@ -119,7 +123,7 @@ GRIDCOIN_CORE_H = \
scheduler.h \
scraper_net.h \
scraper/fwd.h \
scraper/http.h \
scraper/http.h \
scraper/scraper.h \
script.h \
scrypt.h \
Expand Down Expand Up @@ -160,8 +164,10 @@ GRIDCOIN_CORE_CPP = addrdb.cpp \
block.cpp \
boinc.cpp \
checkpoints.cpp \
contract/cpid.cpp \
contract/message.cpp \
contract/polls.cpp \
contract/contract.cpp \
contract/rain.cpp \
crypter.cpp \
db.cpp \
fs.cpp \
Expand All @@ -173,7 +179,9 @@ GRIDCOIN_CORE_CPP = addrdb.cpp \
logging.cpp \
main.cpp \
miner.cpp \
neuralnet/beacon.cpp \
neuralnet/claim.cpp \
neuralnet/contract.cpp \
neuralnet/cpid.cpp \
neuralnet/project.cpp \
neuralnet/quorum.cpp \
Expand Down
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ GRIDCOIN_TESTS =\
test/multisig_tests.cpp \
test/netbase_tests.cpp \
test/neuralnet/claim_tests.cpp \
test/neuralnet/contract_tests.cpp \
test/neuralnet/cpid_tests.cpp \
test/neuralnet/magnitude_tests.cpp \
test/neuralnet/project_tests.cpp \
Expand Down
1 change: 0 additions & 1 deletion src/appcache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace
{ "beaconalt", Section::BEACONALT },
{ "global", Section::GLOBAL },
{ "protocol", Section::PROTOCOL },
{ "trxid", Section::TRXID },
{ "poll", Section::POLL },
{ "vote", Section::VOTE },
{ "scraper", Section::SCRAPER }
Expand Down
1 change: 0 additions & 1 deletion src/appcache.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ enum class Section
BEACONALT,
GLOBAL,
PROTOCOL,
TRXID,
POLL,
VOTE,
SCRAPER,
Expand Down
1 change: 0 additions & 1 deletion src/beacon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "main.h"
#include "init.h"
#include "appcache.h"
#include "contract/contract.h"
#include "key.h"
#include "neuralnet/researcher.h"
#include "neuralnet/quorum.h"
Expand Down
222 changes: 0 additions & 222 deletions src/contract/contract.cpp

This file was deleted.

12 changes: 0 additions & 12 deletions src/contract/contract.h

This file was deleted.

78 changes: 78 additions & 0 deletions src/contract/cpid.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include "beacon.h"
#include "contract/cpid.h"
#include "key.h"

bool VerifyCPIDSignature(
const std::string& sCPID,
const std::string& sBlockHash,
const std::string& sSignature,
const std::string& sBeaconPublicKey)
{
CKey key;

bool valid = key.SetPubKey(ParseHex(sBeaconPublicKey))
&& key.Verify(
Hash(sCPID.begin(), sCPID.end(), sBlockHash.begin(), sBlockHash.end()),
DecodeBase64(sSignature.c_str()));

if (!valid) {
LogPrintf(
"VerifyCPIDSignature: invalid signature sSignature=%s, cached key=%s",
sSignature,
sBeaconPublicKey);
}

return valid;
}

bool VerifyCPIDSignature(
const std::string& sCPID,
const std::string& sBlockHash,
const std::string& sSignature)
{
return VerifyCPIDSignature(
sCPID,
sBlockHash,
sSignature,
GetBeaconPublicKey(sCPID, false));
}

bool SignBlockWithCPID(
const std::string& sCPID,
const std::string& sBlockHash,
std::string& sSignature,
std::string& sError,
bool bAdvertising)
{
// Check if there is a beacon for this user
// If not then return false as GetStoresBeaconPrivateKey grabs from the config
if (!HasActiveBeacon(sCPID) && !bAdvertising) {
sError = "No active beacon";
return false;
}

CKey keyBeacon;

if (!GetStoredBeaconPrivateKey(sCPID, keyBeacon)) {
sError = "No beacon key";
return false;
}

std::vector<unsigned char> signature_bytes;

// Returns the Signature of the CPID+BlockHash message.
bool result = keyBeacon.Sign(
Hash(sCPID.begin(), sCPID.end(), sBlockHash.begin(), sBlockHash.end()),
signature_bytes);

if (!result) {
sError = "Unable to sign message, check private key.";
sSignature = "";

return false;
}

sSignature = EncodeBase64(signature_bytes.data(), signature_bytes.size());

return true;
}
Loading

0 comments on commit 982491c

Please sign in to comment.