core/types: implement encode/decode for TxData interface #27841 (#1795)

This commit is contained in:
Daniel Liu 2025-12-07 18:22:27 +08:00 committed by GitHub
parent 4881c9445a
commit 8129ac77cd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 49 additions and 13 deletions

View file

@ -1292,12 +1292,17 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
defer bc.wg.Done() defer bc.wg.Done()
// Do a sanity check that the provided chain is actually ordered and linked // Do a sanity check that the provided chain is actually ordered and linked
for i := 1; i < len(blockChain); i++ { for i, block := range blockChain {
if blockChain[i].NumberU64() != blockChain[i-1].NumberU64()+1 || blockChain[i].ParentHash() != blockChain[i-1].Hash() { if i != 0 {
log.Error("Non contiguous receipt insert", "number", blockChain[i].Number(), "hash", blockChain[i].Hash(), "parent", blockChain[i].ParentHash(), prev := blockChain[i-1]
"prevnumber", blockChain[i-1].Number(), "prevhash", blockChain[i-1].Hash()) if block.NumberU64() != prev.NumberU64()+1 || block.ParentHash() != prev.Hash() {
return 0, fmt.Errorf("non contiguous insert: item %d is #%d [%x..], item %d is #%d [%x..] (parent [%x..])", i-1, blockChain[i-1].NumberU64(), log.Error("Non contiguous receipt insert",
blockChain[i-1].Hash().Bytes()[:4], i, blockChain[i].NumberU64(), blockChain[i].Hash().Bytes()[:4], blockChain[i].ParentHash().Bytes()[:4]) "number", block.Number(), "hash", block.Hash(), "parent", block.ParentHash(),
"prevnumber", prev.Number(), "prevhash", prev.Hash())
return 0, fmt.Errorf("non contiguous insert: item %d is #%d [%x..], item %d is #%d [%x..] (parent [%x..])",
i-1, prev.NumberU64(), prev.Hash().Bytes()[:4],
i, block.NumberU64(), block.Hash().Bytes()[:4], blockChain[i].ParentHash().Bytes()[:4])
}
} }
} }

View file

@ -100,6 +100,9 @@ type TxData interface {
// copy of the computed value, i.e. callers are allowed to mutate the result. // copy of the computed value, i.e. callers are allowed to mutate the result.
// Method implementations can use 'dst' to store the result. // Method implementations can use 'dst' to store the result.
effectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int effectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int
encode(*bytes.Buffer) error
decode([]byte) error
} }
// EncodeRLP implements rlp.Encoder // EncodeRLP implements rlp.Encoder
@ -120,7 +123,7 @@ func (tx *Transaction) EncodeRLP(w io.Writer) error {
// encodeTyped writes the canonical encoding of a typed transaction to w. // encodeTyped writes the canonical encoding of a typed transaction to w.
func (tx *Transaction) encodeTyped(w *bytes.Buffer) error { func (tx *Transaction) encodeTyped(w *bytes.Buffer) error {
w.WriteByte(tx.Type()) w.WriteByte(tx.Type())
return rlp.Encode(w, tx.inner) return tx.inner.encode(w)
} }
// MarshalBinary returns the canonical encoding of the transaction. // MarshalBinary returns the canonical encoding of the transaction.
@ -190,18 +193,17 @@ func (tx *Transaction) decodeTyped(b []byte) (TxData, error) {
if len(b) <= 1 { if len(b) <= 1 {
return nil, errShortTypedTx return nil, errShortTypedTx
} }
var inner TxData
switch b[0] { switch b[0] {
case AccessListTxType: case AccessListTxType:
var inner AccessListTx inner = new(AccessListTx)
err := rlp.DecodeBytes(b[1:], &inner)
return &inner, err
case DynamicFeeTxType: case DynamicFeeTxType:
var inner DynamicFeeTx inner = new(DynamicFeeTx)
err := rlp.DecodeBytes(b[1:], &inner)
return &inner, err
default: default:
return nil, ErrTxTypeNotSupported return nil, ErrTxTypeNotSupported
} }
err := inner.decode(b[1:])
return inner, err
} }
// setDecoded sets the inner transaction and size after decoding. // setDecoded sets the inner transaction and size after decoding.

View file

@ -17,9 +17,11 @@
package types package types
import ( import (
"bytes"
"math/big" "math/big"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/rlp"
) )
//go:generate go run github.com/fjl/gencodec -type AccessTuple -out gen_access_tuple.go //go:generate go run github.com/fjl/gencodec -type AccessTuple -out gen_access_tuple.go
@ -117,3 +119,11 @@ func (tx *AccessListTx) rawSignatureValues() (v, r, s *big.Int) {
func (tx *AccessListTx) setSignatureValues(chainID, v, r, s *big.Int) { func (tx *AccessListTx) setSignatureValues(chainID, v, r, s *big.Int) {
tx.ChainID, tx.V, tx.R, tx.S = chainID, v, r, s tx.ChainID, tx.V, tx.R, tx.S = chainID, v, r, s
} }
func (tx *AccessListTx) encode(b *bytes.Buffer) error {
return rlp.Encode(b, tx)
}
func (tx *AccessListTx) decode(input []byte) error {
return rlp.DecodeBytes(input, tx)
}

View file

@ -17,9 +17,11 @@
package types package types
import ( import (
"bytes"
"math/big" "math/big"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/rlp"
) )
type DynamicFeeTx struct { type DynamicFeeTx struct {
@ -112,3 +114,11 @@ func (tx *DynamicFeeTx) rawSignatureValues() (v, r, s *big.Int) {
func (tx *DynamicFeeTx) setSignatureValues(chainID, v, r, s *big.Int) { func (tx *DynamicFeeTx) setSignatureValues(chainID, v, r, s *big.Int) {
tx.ChainID, tx.V, tx.R, tx.S = chainID, v, r, s tx.ChainID, tx.V, tx.R, tx.S = chainID, v, r, s
} }
func (tx *DynamicFeeTx) encode(b *bytes.Buffer) error {
return rlp.Encode(b, tx)
}
func (tx *DynamicFeeTx) decode(input []byte) error {
return rlp.DecodeBytes(input, tx)
}

View file

@ -17,6 +17,7 @@
package types package types
import ( import (
"bytes"
"math/big" "math/big"
"github.com/XinFinOrg/XDPoSChain/common" "github.com/XinFinOrg/XDPoSChain/common"
@ -115,3 +116,11 @@ func (tx *LegacyTx) rawSignatureValues() (v, r, s *big.Int) {
func (tx *LegacyTx) setSignatureValues(chainID, v, r, s *big.Int) { func (tx *LegacyTx) setSignatureValues(chainID, v, r, s *big.Int) {
tx.V, tx.R, tx.S = v, r, s tx.V, tx.R, tx.S = v, r, s
} }
func (tx *LegacyTx) encode(*bytes.Buffer) error {
panic("encode called on LegacyTx")
}
func (tx *LegacyTx) decode([]byte) error {
panic("decode called on LegacyTx)")
}