/
common.go
56 lines (48 loc) · 1.13 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package filter
import (
gconf "github.com/DSiSc/craft/config"
"github.com/DSiSc/craft/rlp"
"github.com/DSiSc/craft/types"
"github.com/DSiSc/crypto-suite/crypto/sha3"
"hash"
)
// get hash algorithm by global config
func HashAlg() hash.Hash {
var alg string
if value, ok := gconf.GlobalConfig.Load(gconf.HashAlgName); ok {
alg = value.(string)
} else {
alg = "SHA256"
}
return sha3.NewHashByAlgName(alg)
}
// calculate the hash value of the rlp encoded byte of x
func rlpHash(x interface{}) (h types.Hash) {
hw := HashAlg()
rlp.Encode(hw, x)
hw.Sum(h[:0])
return h
}
// TxHash calculate tx's hash
func TxHash(tx *types.Transaction) (hash types.Hash) {
if hash := tx.Hash.Load(); hash != nil {
return hash.(types.Hash)
}
v := rlpHash(tx)
tx.Hash.Store(v)
return v
}
// HeaderHash calculate block's hash
func HeaderHash(block *types.Block) (hash types.Hash) {
//var defaultHash types.Hash
return rlpHash(block.Header)
}
type RefAddress struct {
Addr types.Address
}
func NewRefAddress(addr types.Address) *RefAddress {
return &RefAddress{Addr: addr}
}
func (self *RefAddress) Address() types.Address {
return self.Addr
}