aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2015-03-18 21:15:48 +0800
committerobscuren <geffobscura@gmail.com>2015-03-18 21:15:58 +0800
commit48dd601de0ffea4e1bf57a8923f2a6126553b575 (patch)
tree95194c294a6f4eabe9c178da2c8b4c199e1662e4
parent0a1eeca41e6ba5920ba65d9b41654768299bc7e3 (diff)
downloaddexon-48dd601de0ffea4e1bf57a8923f2a6126553b575.tar.gz
dexon-48dd601de0ffea4e1bf57a8923f2a6126553b575.tar.zst
dexon-48dd601de0ffea4e1bf57a8923f2a6126553b575.zip
prep template for fixed size hashes
-rw-r--r--common/types_template.go48
-rw-r--r--core/chain_manager.go4
2 files changed, 50 insertions, 2 deletions
diff --git a/common/types_template.go b/common/types_template.go
new file mode 100644
index 000000000..1c82a36dc
--- /dev/null
+++ b/common/types_template.go
@@ -0,0 +1,48 @@
+// +build none
+//sed -e 's/_N_/Hash/g' -e 's/_S_/32/g' -e '1d' types_template.go | gofmt -w hash.go
+
+package common
+
+import "math/big"
+
+type _N_ [_S_]byte
+
+func BytesTo_N_(b []byte) _N_ {
+ var h _N_
+ h.SetBytes(b)
+ return h
+}
+func StringTo_N_(s string) _N_ { return BytesTo_N_([]byte(s)) }
+func BigTo_N_(b *big.Int) _N_ { return BytesTo_N_(b.Bytes()) }
+func HexTo_N_(s string) _N_ { return BytesTo_N_(FromHex(s)) }
+
+// Don't use the default 'String' method in case we want to overwrite
+
+// Get the string representation of the underlying hash
+func (h _N_) Str() string { return string(h[:]) }
+func (h _N_) Bytes() []byte { return h[:] }
+func (h _N_) Big() *big.Int { return Bytes2Big(h[:]) }
+func (h _N_) Hex() string { return "0x" + Bytes2Hex(h[:]) }
+
+// Sets the hash to the value of b. If b is larger than len(h) it will panic
+func (h *_N_) SetBytes(b []byte) {
+ // Use the right most bytes
+ if len(b) > len(h) {
+ b = b[len(b)-_S_:]
+ }
+
+ // Reverse the loop
+ for i := len(b) - 1; i >= 0; i-- {
+ h[_S_-len(b)+i] = b[i]
+ }
+}
+
+// Set string `s` to h. If s is larger than len(h) it will panic
+func (h *_N_) SetString(s string) { h.SetBytes([]byte(s)) }
+
+// Sets h to other
+func (h *_N_) Set(other _N_) {
+ for i, v := range other {
+ h[i] = v
+ }
+}
diff --git a/core/chain_manager.go b/core/chain_manager.go
index 5316a3423..0251f7c72 100644
--- a/core/chain_manager.go
+++ b/core/chain_manager.go
@@ -37,8 +37,8 @@ func CalcDifficulty(block, parent *types.Header) *big.Int {
diff.Sub(parent.Difficulty, adjust)
}
- if diff.Cmp(GenesisDiff) < 0 {
- return GenesisDiff
+ if diff.Cmp(min) < 0 {
+ return min
}
return diff