diff --git a/common/internal_address.go b/common/internal_address.go index 6e9515a555..d43ea4f8a4 100644 --- a/common/internal_address.go +++ b/common/internal_address.go @@ -17,7 +17,7 @@ type InternalAddress [AddressLength]byte func (a InternalAddress) Bytes() []byte { return a[:] } // Bytes20 gets the bytes20 representation of the underlying address. -func (a InternalAddress) Bytes20() (addr AddressBytes) { copy(addr[:], a[:]); return addr } // this is not very performant +func (a InternalAddress) Bytes20() (addr AddressBytes) { return AddressBytes(a) } // Hash converts an address to a hash by left-padding it with zeros. func (a InternalAddress) Hash() Hash { return BytesToHash(a[:]) } diff --git a/core/state/statedb.go b/core/state/statedb.go index 4902817ae3..45886809fa 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -26,7 +26,6 @@ import ( "time" "github.com/prometheus/client_golang/prometheus" - "google.golang.org/protobuf/proto" "github.com/dominant-strategies/go-quai/common" "github.com/dominant-strategies/go-quai/core/rawdb" @@ -647,11 +646,7 @@ func (s *StateDB) PushETX(etx *types.Transaction) error { if metrics_config.MetricsEnabled() { defer func(start time.Time) { stateMetrics.WithLabelValues("AddETX").Add(float64(time.Since(start))) }(time.Now()) } - protoTx, err := etx.ProtoEncode() - if err != nil { - return err - } - protoTxBytes, err := proto.Marshal(protoTx) + data, err := rlp.EncodeToBytes(etx) if err != nil { return err } @@ -659,7 +654,7 @@ func (s *StateDB) PushETX(etx *types.Transaction) error { if err != nil { return err } - if err := s.etxTrie.TryUpdate(newestIndex.Bytes(), protoTxBytes); err != nil { + if err := s.etxTrie.TryUpdate(newestIndex.Bytes(), data); err != nil { return err } newestIndex.Add(newestIndex, big.NewInt(1)) @@ -678,15 +673,11 @@ func (s *StateDB) PushETXs(etxs []*types.Transaction) error { return err } for _, etx := range etxs { - protoTx, err := etx.ProtoEncode() - if err != nil { - return err - } - protoTxBytes, err := proto.Marshal(protoTx) + data, err := rlp.EncodeToBytes(etx) if err != nil { return err } - if err := s.etxTrie.TryUpdate(newestIndex.Bytes(), protoTxBytes); err != nil { + if err := s.etxTrie.TryUpdate(newestIndex.Bytes(), data); err != nil { return err } newestIndex.Add(newestIndex, big.NewInt(1)) @@ -712,14 +703,11 @@ func (s *StateDB) PopETX() (*types.Transaction, error) { if len(enc) == 0 { return nil, nil } - protoEtx := new(types.ProtoTransaction) - if err := proto.Unmarshal(enc, protoEtx); err != nil { - return nil, err - } etx := new(types.Transaction) - if err := etx.ProtoDecode(protoEtx, s.nodeLocation); err != nil { + if err := rlp.DecodeBytes(enc, etx); err != nil { return nil, err } + etx.SetTo(common.BytesToAddress(etx.To().Bytes(), s.nodeLocation)) if err := s.etxTrie.TryDelete(oldestIndex.Bytes()); err != nil { return nil, err } @@ -738,14 +726,11 @@ func (s *StateDB) ReadETX(index *big.Int) (*types.Transaction, error) { if len(enc) == 0 { return nil, nil } - protoEtx := new(types.ProtoTransaction) - if err := proto.Unmarshal(enc, protoEtx); err != nil { - return nil, err - } etx := new(types.Transaction) - if err := etx.ProtoDecode(protoEtx, s.nodeLocation); err != nil { + if err := rlp.DecodeBytes(enc, etx); err != nil { return nil, err } + etx.SetTo(common.BytesToAddress(etx.To().Bytes(), s.nodeLocation)) return etx, nil } diff --git a/core/types/transaction.go b/core/types/transaction.go index 8d9869a769..5f6a6764fd 100644 --- a/core/types/transaction.go +++ b/core/types/transaction.go @@ -73,12 +73,7 @@ func NewTx(inner TxData) *Transaction { return tx } -// SetInner sets the inner transaction data of a transaction. func (tx *Transaction) SetInner(inner TxData) { - tx.setDecoded(inner.copy(), 0) -} - -func (tx *Transaction) SetInnerNoCopy(inner TxData) { tx.setDecoded(inner, 0) } @@ -291,7 +286,7 @@ func (tx *Transaction) ProtoDecode(protoTx *ProtoTransaction, location common.Lo nonce := BlockNonce(uint64ToByteArr(*protoTx.WorkNonce)) quaiTx.WorkNonce = &nonce } - tx.SetInnerNoCopy(&quaiTx) + tx.SetInner(&quaiTx) case 1: if protoTx.Gas == nil { @@ -329,7 +324,7 @@ func (tx *Transaction) ProtoDecode(protoTx *ProtoTransaction, location common.Lo etx.ETXIndex = uint16(protoTx.GetEtxIndex()) etx.Sender = common.BytesToAddress(protoTx.GetEtxSender(), location) - tx.SetInnerNoCopy(&etx) + tx.SetInner(&etx) case 2: if protoTx.TxIns == nil { @@ -382,7 +377,7 @@ func (tx *Transaction) ProtoDecode(protoTx *ProtoTransaction, location common.Lo nonce := BlockNonce(uint64ToByteArr(*protoTx.WorkNonce)) qiTx.WorkNonce = &nonce } - tx.SetInnerNoCopy(&qiTx) + tx.SetInner(&qiTx) default: return errors.New("invalid transaction type") @@ -597,13 +592,7 @@ func (tx *Transaction) From(nodeLocation common.Location) *common.Address { // To returns the recipient address of the transaction. // For contract-creation transactions, To returns nil. func (tx *Transaction) To() *common.Address { - // Copy the pointed-to address. - ito := tx.inner.to() - if ito == nil { - return nil - } - cpy := *ito - return &cpy + return tx.inner.to() } func (tx *Transaction) SetTo(addr common.Address) {