/
common.go
127 lines (112 loc) · 3.11 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2015 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package types
import (
"math/big"
gconf "github.com/DSiSc/craft/config"
"github.com/DSiSc/craft/rlp"
"github.com/DSiSc/craft/types"
"github.com/DSiSc/crypto-suite/crypto/sha3"
"hash"
)
const (
DefaultGasPrice = 1
)
// 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
if !(block.HeaderHash == types.Hash{}) {
var hash types.Hash
copy(hash[:], block.HeaderHash[:])
return hash
}
return rlpHash(block.Header)
}
func HashBytes(a types.Hash) []byte {
b := make([]byte, len(a))
copy(b, a[:])
return b
}
func CopyBytes(b []byte) (copiedBytes []byte) {
if b == nil {
return nil
}
copiedBytes = make([]byte, len(b))
copy(copiedBytes, b)
return
}
func TypeConvert(a *Address) *types.Address {
var address types.Address
if a != nil {
copy(address[:], a[:])
return &address
}
return nil
}
// New a transaction
func newTransaction(nonce uint64, to *Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, from *Address) *types.Transaction {
if len(data) > 0 {
data = CopyBytes(data)
}
d := types.TxData{
AccountNonce: nonce,
Recipient: TypeConvert(to),
From: TypeConvert(from),
Payload: data,
Amount: new(big.Int),
GasLimit: gasLimit,
Price: new(big.Int),
V: new(big.Int),
R: new(big.Int),
S: new(big.Int),
}
if amount != nil {
d.Amount.Set(amount)
}
if gasPrice != nil {
d.Price.Set(gasPrice)
}
return &types.Transaction{Data: d}
}
func NewTransaction(nonce uint64, to *Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte, from Address) *types.Transaction {
return newTransaction(nonce, to, amount, gasLimit, gasPrice, data, &from)
}