diff options
author | Joop Kiefte <ikojba@gmail.com> | 2021-12-12 02:44:11 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-12 02:44:11 +0000 |
commit | 407a75942f8bb7758f40bb04714d2ff0964972e1 (patch) | |
tree | 18f671d672e4eecd00ad650fe6701bc44ef9eeff /message/message.go | |
parent | 7f41a498b057d2ab180d288f212e0adc498e203d (diff) |
Implemented paging and improved POW calculation
Diffstat (limited to 'message/message.go')
-rw-r--r-- | message/message.go | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/message/message.go b/message/message.go index 0c5af56..7a1699d 100644 --- a/message/message.go +++ b/message/message.go @@ -64,11 +64,14 @@ func (m *Messages) MessageList() []*Message { } // Proof of Work: Find the nonce for a message by hashing the message and checking for at least n initial zeroes in the binary representation of the resulting hash -func (msg *Message) ProofOfWork(n int) { +// If it takes too long, return an error +func (msg *Message) ProofOfWork(n int, timeout time.Duration) error { // Create a local copy of the message and start counting m := *msg m.Nonce = 0 + start := time.Now() // Loop until we find a nonce that satisfies the proof of work + // If the nonce is not found within the timeout, return an error for { // Increment the nonce and hash the message m.Nonce++ @@ -77,9 +80,14 @@ func (msg *Message) ProofOfWork(n int) { if CountLeadingZeroes(hash) >= n { break } + // If the nonce is not found within the timeout, return an error + if time.Since(start) > timeout { + return fmt.Errorf("proof of work timed out") + } } // Set the message to the local copy *msg = m + return nil } // Get the SHA256 hash of a message plus the timestamp plus the nonce as a byte slice @@ -115,10 +123,10 @@ func (m *Message) Lead() int { return CountLeadingZeroes(m.Hash()) } -func New(msg string, n int, timestamp int64) *Message { +func New(msg string, n int, timestamp int64, timeout time.Duration) (*Message, error) { m := Message{Message: msg, Timestamp: timestamp} - m.ProofOfWork(n) - return &m + err := m.ProofOfWork(n, timeout) + return &m, err } // Map Messages maps the stamp of the message to the message itself |