diff --git a/builds/node/node_linux-amd64 b/builds/node/node_linux-amd64 index 9053cad..f87d5fd 100755 Binary files a/builds/node/node_linux-amd64 and b/builds/node/node_linux-amd64 differ diff --git a/builds/node/node_linux-arm64 b/builds/node/node_linux-arm64 index 1739aee..2d05cdc 100755 Binary files a/builds/node/node_linux-arm64 and b/builds/node/node_linux-arm64 differ diff --git a/create_block.go b/create_block.go index f53c94c..c6dfc56 100644 --- a/create_block.go +++ b/create_block.go @@ -79,7 +79,7 @@ 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 } @@ -87,6 +87,7 @@ func CreateBlock() (Block, error) { hashBytes = HashBlock(block) hash = binary.BigEndian.Uint64(hashBytes[:]) } else { + fmt.Println("Pool dry.") return Block{}, errors.New("pool dry") } } @@ -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 { @@ -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) diff --git a/server.go b/server.go index 954a8f4..f63d63f 100644 --- a/server.go +++ b/server.go @@ -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) @@ -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) } @@ -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) { diff --git a/verify.go b/verify.go index 99252ec..a99d611 100644 --- a/verify.go +++ b/verify.go @@ -129,10 +129,12 @@ 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 } } @@ -140,6 +142,7 @@ func VerifyTimeVerifiers(block Block, verifiers []dsa.PublicKey, signatures []Si 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 @@ -147,11 +150,13 @@ func VerifyTimeVerifiers(block Block, verifiers []dsa.PublicKey, signatures []Si // 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