Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change ETX PMT back to RLP with resetting To after every decode #1774

Merged
merged 1 commit into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/internal_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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[:]) }
Expand Down
31 changes: 8 additions & 23 deletions core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -647,19 +646,15 @@ 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
}
newestIndex, err := s.GetNewestIndex()
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))
Expand All @@ -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))
Expand All @@ -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
}
Expand All @@ -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
}

Expand Down
19 changes: 4 additions & 15 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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) {
Expand Down
Loading