Skip to content

Commit bd131a1

Browse files
authored
Merge pull request #2359 from CortexFoundation/dev
hash pool
2 parents c1d12a4 + bf0f29e commit bd131a1

File tree

5 files changed

+19
-16
lines changed

5 files changed

+19
-16
lines changed

core/types/block.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@ import (
2929
"sync/atomic"
3030
"time"
3131

32-
"golang.org/x/crypto/sha3"
33-
34-
//"unsafe"
35-
3632
"github.com/CortexFoundation/CortexTheseus/common"
3733
"github.com/CortexFoundation/CortexTheseus/common/hexutil"
3834
"github.com/CortexFoundation/CortexTheseus/crypto"
@@ -173,9 +169,7 @@ func (h *Header) SanityCheck() error {
173169

174170
// hasherPool holds LegacyKeccak hashers.
175171
var hasherPool = sync.Pool{
176-
New: func() any {
177-
return sha3.NewLegacyKeccak256()
178-
},
172+
New: func() interface{} { return crypto.NewKeccakState() },
179173
}
180174

181175
func rlpHash(x any) (h common.Hash) {

core/vm/cvm.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,9 @@ func (cvm *CVM) Create(caller common.Address, code []byte, gas uint64, value *bi
547547
func (cvm *CVM) Create2(caller common.Address, code []byte, gas uint64, endowment *big.Int, salt *uint256.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, modelGas map[common.Address]uint64, err error) {
548548
//contractAddr = crypto.CreateAddress2(caller.Address(), common.BigToHash(salt), code)
549549
// return cvm.create(caller, code, gas, endowment, contractAddr)
550-
contractAddr = crypto.CreateAddress2(caller, salt.Bytes32(), crypto.Keccak256(code))
550+
//contractAddr = crypto.CreateAddress2(caller, salt.Bytes32(), crypto.Keccak256(code))
551+
inithash := crypto.HashData(cvm.interpreter.hasher, code)
552+
contractAddr = crypto.CreateAddress2(caller, salt.Bytes32(), inithash[:])
551553
return cvm.create(caller, code, gas, endowment, contractAddr, CREATE2)
552554
}
553555

core/vm/instructions.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424

2525
"github.com/CortexFoundation/CortexTheseus/common"
2626
"github.com/CortexFoundation/CortexTheseus/core/types"
27-
"github.com/CortexFoundation/CortexTheseus/crypto"
2827
"github.com/CortexFoundation/CortexTheseus/params"
2928
)
3029

@@ -247,11 +246,7 @@ func opKeccak256(pc *uint64, interpreter *CVMInterpreter, scope *ScopeContext) (
247246
offset, size := scope.Stack.pop(), scope.Stack.peek()
248247
data := scope.Memory.GetPtr(offset.Uint64(), size.Uint64())
249248

250-
if interpreter.hasher == nil {
251-
interpreter.hasher = crypto.NewKeccakState()
252-
} else {
253-
interpreter.hasher.Reset()
254-
}
249+
interpreter.hasher.Reset()
255250
interpreter.hasher.Write(data)
256251
interpreter.hasher.Read(interpreter.hasherBuf[:])
257252

core/vm/interpreter.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ func NewCVMInterpreter(cvm *CVM) *CVMInterpreter {
124124
cvm: cvm,
125125
table: table,
126126
gasTable: cvm.ChainConfig().GasTable(cvm.Context.BlockNumber),
127+
hasher: crypto.NewKeccakState(),
127128
}
128129
}
129130

crypto/crypto.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"io"
2929
"math/big"
3030
"os"
31+
"sync"
3132

3233
"golang.org/x/crypto/sha3"
3334

@@ -36,6 +37,12 @@ import (
3637
"github.com/CortexFoundation/CortexTheseus/rlp"
3738
)
3839

40+
var hasherPool = sync.Pool{
41+
New: func() any {
42+
return sha3.NewLegacyKeccak256().(KeccakState)
43+
},
44+
}
45+
3946
// SignatureLength indicates the byte length required to carry a signature with recovery id.
4047
const SignatureLength = 64 + 1 // 64 bytes ECDSA signature + 1 byte recovery id
4148

@@ -85,22 +92,26 @@ func HashData(kh KeccakState, data []byte) (h common.Hash) {
8592
// Keccak256 calculates and returns the Keccak256 hash of the input data.
8693
func Keccak256(data ...[]byte) []byte {
8794
b := make([]byte, 32)
88-
d := NewKeccakState()
95+
d := hasherPool.Get().(KeccakState)
96+
d.Reset()
8997
for _, b := range data {
9098
d.Write(b)
9199
}
92100
d.Read(b)
101+
hasherPool.Put(d)
93102
return b
94103
}
95104

96105
// Keccak256Hash calculates and returns the Keccak256 hash of the input data,
97106
// converting it to an internal Hash data structure.
98107
func Keccak256Hash(data ...[]byte) (h common.Hash) {
99-
d := NewKeccakState()
108+
d := hasherPool.Get().(KeccakState)
109+
d.Reset()
100110
for _, b := range data {
101111
d.Write(b)
102112
}
103113
d.Read(h[:])
114+
hasherPool.Put(d)
104115
return h
105116
}
106117

0 commit comments

Comments
 (0)