Skip to content

Commit

Permalink
util: Fix RevHash func.
Browse files Browse the repository at this point in the history
Previous version was not working properly. New one is validated with a
test.
  • Loading branch information
jholdstock authored and davecgh committed Sep 7, 2023
1 parent 107c435 commit 293bbb5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
15 changes: 7 additions & 8 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,14 @@ func ReverseToInt(s string) (int32, error) {

// RevHash reverses a hash in string format.
func RevHash(hash string) string {
revHash := ""
for i := 0; i < 7; i++ {
j := i * 8
part := fmt.Sprintf("%c%c%c%c%c%c%c%c",
hash[6+j], hash[7+j], hash[4+j], hash[5+j],
hash[2+j], hash[3+j], hash[0+j], hash[1+j])
revHash += part
rev := []rune(hash)
for i := 0; i <= len(rev)/2-2; i += 2 {
opp := len(rev) - 2 - i
rev[i], rev[opp] = rev[opp], rev[i]
rev[i+1], rev[opp+1] = rev[opp+1], rev[i+1]
}
return revHash

return string(rev)
}

// DiffToTarget converts a whole number difficulty into a target.
Expand Down
24 changes: 24 additions & 0 deletions util/util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package util

import "testing"

func TestRevHash(t *testing.T) {
tests := []struct {
name string
hash string
want string
}{
{
"ok",
"f82aadcc5f683978c0cf7b616b41f956bb6c4bbd073e93ccffb868972e000000",
"0000002e9768b8ffcc933e07bd4b6cbb56f9416b617bcfc07839685fccad2af8",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RevHash(tt.hash); got != tt.want {
t.Errorf("RevHash() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 293bbb5

Please sign in to comment.