Skip to content

Commit

Permalink
Fixes bugs with time verifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashy5000 committed Apr 6, 2024
1 parent 0fc2f42 commit 5916947
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
Binary file modified builds/node/node_linux-amd64
Binary file not shown.
Binary file modified builds/node/node_linux-arm64
Binary file not shown.
8 changes: 6 additions & 2 deletions create_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ func CreateBlock() (Block, error) {
} else {
block.PreviousBlockHash = [32]byte{}
}
block.Difficulty = previousBlock.Difficulty * (60 / uint64(previousBlock.MiningTime.Seconds()))
block.Difficulty = GetDifficulty(previousBlock.MiningTime, previousBlock.Difficulty)
if block.Difficulty < minimumBlockDifficulty {
block.Difficulty = minimumBlockDifficulty
}
block.Nonce++
hashBytes = HashBlock(block)
hash = binary.BigEndian.Uint64(hashBytes[:])
} else {
fmt.Println("Pool dry.")
return Block{}, errors.New("pool dry")
}
}
Expand All @@ -98,6 +99,9 @@ func CreateBlock() (Block, error) {
}
// Ask for time verifiers
for _, peer := range GetPeers() {
if int64(len(block.TimeVerifiers)) >= GetMinerCount(len(blockchain))/5 && VerifyTimeVerifiers(block, block.TimeVerifiers, block.TimeVerifierSignatures) {
break
}
// Verify that the peer has mined a block (only miners can be time verifiers)
req, err := http.NewRequest(http.MethodGet, peer+"/identify", nil)
if err != nil {
Expand Down Expand Up @@ -151,7 +155,7 @@ func CreateBlock() (Block, error) {
continue
}
// Split the response body into the signature and the public key
split := strings.Split(string(bodyBytes), ":")
split := strings.Split(string(bodyBytes), "%")
// Unmarshal the signature
var signature Signature
err = json.Unmarshal([]byte(split[0]), &signature)
Expand Down
16 changes: 13 additions & 3 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,11 @@ func HandleVerifyTimeRequest(w http.ResponseWriter, req *http.Request) {
}
// Get the current time
currentTime := time.Now()
// Get the time mining finished
miningFinishedTime := block.Timestamp.Add(block.MiningTime)
// Check if the time the block was mined is within a reasonable range of the current time
// It cannot be in the future, and it cannot be more than 10 seconds in the past
if block.Timestamp.After(currentTime) || block.Timestamp.Before(currentTime.Add(-10*time.Second)) {
if miningFinishedTime.After(currentTime) || miningFinishedTime.Before(currentTime.Add(-10*time.Second)) {
_, err := io.WriteString(w, "invalid")
if err != nil {
panic(err)
Expand All @@ -216,7 +218,7 @@ func HandleVerifyTimeRequest(w http.ResponseWriter, req *http.Request) {
}
// Sign the time with the time verifier's (this node's) private key
key := GetKey()
r, s, err := dsa.Sign(rand.Reader, &key, []byte(block.Timestamp.String()))
r, s, err := dsa.Sign(rand.Reader, &key, []byte(fmt.Sprintf("%d", miningFinishedTime.UnixNano())))
if err != nil {
panic(err)
}
Expand All @@ -229,7 +231,15 @@ func HandleVerifyTimeRequest(w http.ResponseWriter, req *http.Request) {
if err != nil {
panic(err)
}
_, err = io.WriteString(w, string(signatureBytes)+":"+key.Y.String())
// Marshal the public key
publicKeyBytes, err := json.Marshal(key.PublicKey)
if err != nil {
panic(err)
}
_, err = io.WriteString(w, string(signatureBytes)+"%"+string(publicKeyBytes))
if err != nil {
panic(err)
}
}

func HandlePeersRequest(w http.ResponseWriter, _ *http.Request) {
Expand Down
5 changes: 5 additions & 0 deletions verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,29 +129,34 @@ func VerifyBlock(block Block) bool {

func VerifyTimeVerifiers(block Block, verifiers []dsa.PublicKey, signatures []Signature) bool {
if len(verifiers) != len(signatures) {
fmt.Println("Signature count does not match verifier count.")
return false
}
for i, verifier := range verifiers {
if !dsa.Verify(&verifier, []byte(fmt.Sprintf("%d", block.Timestamp.Add(block.MiningTime).UnixNano())), &signatures[i].R, &signatures[i].S) {
fmt.Println("Time verifier signature is not valid.")
return false
}
}
// Ensure all verifiers are unique
verifierMap := make(map[string]bool)
for _, verifier := range verifiers {
if verifierMap[verifier.Y.String()] {
fmt.Println("Time verifier is not unique.")
return false
}
verifierMap[verifier.Y.String()] = true
}
// Ensure all verifiers are miners
for _, verifier := range verifiers {
if !IsNewMiner(verifier, len(blockchain)) {
fmt.Println("Time verifier is not a miner.")
return false
}
}
// Ensure there are enough verifiers
if len(verifiers) < GetMinVerifiers() {
fmt.Println("Not enough time verifiers.")
return false
}
return true
Expand Down

0 comments on commit 5916947

Please sign in to comment.