aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2015-06-25 20:45:54 +0800
committerJeffrey Wilcke <geffobscura@gmail.com>2015-06-30 00:51:48 +0800
commit11b8d1df59b5a038be2066f97427da3d4d096fe6 (patch)
treefe69aad816aa40cf902e6f7f1ddee84aebac200b /core
parent8743cc1c1ce6b78a02cd29cc06eed3a85ea74209 (diff)
downloaddexon-11b8d1df59b5a038be2066f97427da3d4d096fe6.tar.gz
dexon-11b8d1df59b5a038be2066f97427da3d4d096fe6.tar.zst
dexon-11b8d1df59b5a038be2066f97427da3d4d096fe6.zip
core/types: cache computed transaction values
Diffstat (limited to 'core')
-rw-r--r--core/types/transaction.go33
1 files changed, 28 insertions, 5 deletions
diff --git a/core/types/transaction.go b/core/types/transaction.go
index 14b0e4174..95deac36e 100644
--- a/core/types/transaction.go
+++ b/core/types/transaction.go
@@ -6,6 +6,7 @@ import (
"fmt"
"io"
"math/big"
+ "sync/atomic"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
@@ -20,6 +21,10 @@ func IsContractAddr(addr []byte) bool {
type Transaction struct {
data txdata
+ // caches
+ hash atomic.Value
+ size atomic.Value
+ from atomic.Value
}
type txdata struct {
@@ -88,7 +93,12 @@ func (tx *Transaction) EncodeRLP(w io.Writer) error {
}
func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
- return s.Decode(&tx.data)
+ _, size, _ := s.Kind()
+ err := s.Decode(&tx.data)
+ if err == nil {
+ tx.size.Store(common.StorageSize(rlp.ListSize(size)))
+ }
+ return err
}
func (tx *Transaction) Data() []byte { return common.CopyBytes(tx.data.Payload) }
@@ -107,6 +117,9 @@ func (tx *Transaction) To() *common.Address {
}
func (tx *Transaction) Hash() common.Hash {
+ if hash := tx.hash.Load(); hash != nil {
+ return hash.(common.Hash)
+ }
v := rlpHash([]interface{}{
tx.data.AccountNonce,
tx.data.Price,
@@ -115,21 +128,31 @@ func (tx *Transaction) Hash() common.Hash {
tx.data.Amount,
tx.data.Payload,
})
+ tx.hash.Store(v)
return v
}
func (tx *Transaction) Size() common.StorageSize {
- v, _, _ := rlp.EncodeToReader(&tx.data)
- return common.StorageSize(v)
+ if size := tx.size.Load(); size != nil {
+ return size.(common.StorageSize)
+ }
+ c := writeCounter(0)
+ rlp.Encode(&c, &tx.data)
+ tx.size.Store(common.StorageSize(c))
+ return common.StorageSize(c)
}
func (tx *Transaction) From() (common.Address, error) {
- pubkey, err := tx.PublicKey()
+ if from := tx.from.Load(); from != nil {
+ return from.(common.Address), nil
+ }
+ pubkey, err := tx.publicKey()
if err != nil {
return common.Address{}, err
}
var addr common.Address
copy(addr[:], crypto.Sha3(pubkey[1:])[12:])
+ tx.from.Store(addr)
return addr, nil
}
@@ -144,7 +167,7 @@ func (tx *Transaction) SignatureValues() (v byte, r *big.Int, s *big.Int) {
return tx.data.V, new(big.Int).Set(tx.data.R), new(big.Int).Set(tx.data.S)
}
-func (tx *Transaction) PublicKey() ([]byte, error) {
+func (tx *Transaction) publicKey() ([]byte, error) {
if !crypto.ValidateSignatureValues(tx.data.V, tx.data.R, tx.data.S) {
return nil, errors.New("invalid v, r, s values")
}
01206428f18'>c581043e5106
841c3fc184a5

8f6a20189350

31fce817407c
681855d28113
db813da108b8
9de8e4b94be0

9faec07913fa

00fc5a329f2b




4af04b906116
d132a62a6567
1c09095ac7c4


e30c71bc7af2
2febb36f10d8
e30c71bc7af2
a13ab0085400





6f6cfc660bf3
7910ecc4a449

e5edb8c5449f

120dfa0c9e7e
84b9ce92e55a
1b92fd077441




0d6e575b0941

6b029e3813df
fa2d9b764fda



fa2d9b764fda





d289e580d15a

e4410acc9491

d9cb20e1c02a
191a306591ce




4cd752682063
508163b5a7ef

edfd8c650992



4597d49081f5



9941deaf0b74

1e45c8cae4ce












3f67fbb7e4c0
08f5574a4c02
d105b85d048f
a089111ce495
a171c30a421c
f84512f0e14b
88f2833aa3ad
564c67cc84f2
b98af57ac1d3
fd803f91e436
6b029e3813df
dd6729f395f1

ad1db06124a3


6d4f4727fb9c
13d4b095dabe

8103cc9c74d5
c7a54a5c6005
92ea118f23bd

3f67fbb7e4c0

92ea118f23bd


6d161310a973
dccaaa91de3e
0dad0208bd83
057784071017
8dd757f12a29
e57649950d4e
abe09504bd34


c365ceae2caf
c30c4963352a
0acb6d0a5de3
0dd0d0dc84c1

4ae1258e9f64

0dd0d0dc84c1
233bfab6dcf7



4ff051b42c72

ed4952c69742
5de272569028
6b029e3813df
0838d1027408
77bbedac2153
0838d1027408

dec984940c3b
8994151d166c
00bb49c61133
25f52fc227c7



31547f2f429b

1de89a5c2b22


0acb6d0a5de3
a089111ce495
5099e77b6b20
f12d535f5534
1cf4ad0f138c

bd1f855e0623
ba490fd4b8c7
175f46881b39
27a123537517
b2b32546c425

0acb6d0a5de3
115c4eb0900c

976922026302

6b029e3813df
0e94e899b1f5
4ff051b42c72

1ab298aeab08
3652a0cb9250
135c022c7c85
11a40e6df90b
a4356cf23249
25b79ea46f98
25f52fc227c7