Skip to content

Commit

Permalink
Add celo transaction type
Browse files Browse the repository at this point in the history
  • Loading branch information
palango authored and karlb committed Nov 17, 2023
1 parent 8c8355a commit bfba411
Show file tree
Hide file tree
Showing 13 changed files with 388 additions and 3 deletions.
2 changes: 1 addition & 1 deletion accounts/external/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ func (api *ExternalSigner) SignTx(account accounts.Account, tx *types.Transactio
switch tx.Type() {
case types.LegacyTxType, types.AccessListTxType:
args.GasPrice = (*hexutil.Big)(tx.GasPrice())
case types.DynamicFeeTxType:
case types.DynamicFeeTxType, types.CeloDynamicFeeTxType:
args.MaxFeePerGas = (*hexutil.Big)(tx.GasFeeCap())
args.MaxPriorityFeePerGas = (*hexutil.Big)(tx.GasTipCap())
default:
Expand Down
117 changes: 117 additions & 0 deletions core/types/celo_dynamic_fee_tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// TODO: needs copyright header?

package types

import (
"bytes"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
)

// CeloDynamicFeeTx represents a CIP-64 transaction.
type CeloDynamicFeeTx struct {
ChainID *big.Int
Nonce uint64
GasTipCap *big.Int
GasFeeCap *big.Int
Gas uint64
To *common.Address `rlp:"nil"` // nil means contract creation
Value *big.Int
Data []byte
AccessList AccessList

FeeCurrency *common.Address `rlp:"nil"` // nil means native currency

// Signature values
V *big.Int `json:"v" gencodec:"required"`
R *big.Int `json:"r" gencodec:"required"`
S *big.Int `json:"s" gencodec:"required"`
}

// copy creates a deep copy of the transaction data and initializes all fields.
func (tx *CeloDynamicFeeTx) copy() TxData {
cpy := &CeloDynamicFeeTx{
Nonce: tx.Nonce,
To: copyAddressPtr(tx.To),
Data: common.CopyBytes(tx.Data),
Gas: tx.Gas,
FeeCurrency: copyAddressPtr(tx.FeeCurrency),
// These are copied below.
AccessList: make(AccessList, len(tx.AccessList)),
Value: new(big.Int),
ChainID: new(big.Int),
GasTipCap: new(big.Int),
GasFeeCap: new(big.Int),
V: new(big.Int),
R: new(big.Int),
S: new(big.Int),
}
copy(cpy.AccessList, tx.AccessList)
if tx.Value != nil {
cpy.Value.Set(tx.Value)
}
if tx.ChainID != nil {
cpy.ChainID.Set(tx.ChainID)
}
if tx.GasTipCap != nil {
cpy.GasTipCap.Set(tx.GasTipCap)
}
if tx.GasFeeCap != nil {
cpy.GasFeeCap.Set(tx.GasFeeCap)
}
if tx.V != nil {
cpy.V.Set(tx.V)
}
if tx.R != nil {
cpy.R.Set(tx.R)
}
if tx.S != nil {
cpy.S.Set(tx.S)
}
return cpy
}

// accessors for innerTx.
func (tx *CeloDynamicFeeTx) txType() byte { return CeloDynamicFeeTxType }
func (tx *CeloDynamicFeeTx) chainID() *big.Int { return tx.ChainID }
func (tx *CeloDynamicFeeTx) accessList() AccessList { return tx.AccessList }
func (tx *CeloDynamicFeeTx) data() []byte { return tx.Data }
func (tx *CeloDynamicFeeTx) gas() uint64 { return tx.Gas }
func (tx *CeloDynamicFeeTx) gasFeeCap() *big.Int { return tx.GasFeeCap }
func (tx *CeloDynamicFeeTx) gasTipCap() *big.Int { return tx.GasTipCap }
func (tx *CeloDynamicFeeTx) gasPrice() *big.Int { return tx.GasFeeCap }
func (tx *CeloDynamicFeeTx) value() *big.Int { return tx.Value }
func (tx *CeloDynamicFeeTx) nonce() uint64 { return tx.Nonce }
func (tx *CeloDynamicFeeTx) to() *common.Address { return tx.To }
func (tx *CeloDynamicFeeTx) isSystemTx() bool { return false }

func (tx *CeloDynamicFeeTx) effectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int {
if baseFee == nil {
return dst.Set(tx.GasFeeCap)
}
tip := dst.Sub(tx.GasFeeCap, baseFee)
if tip.Cmp(tx.GasTipCap) > 0 {
tip.Set(tx.GasTipCap)
}
return tip.Add(tip, baseFee)
}

func (tx *CeloDynamicFeeTx) rawSignatureValues() (v, r, s *big.Int) {
return tx.V, tx.R, tx.S
}

func (tx *CeloDynamicFeeTx) setSignatureValues(chainID, v, r, s *big.Int) {
tx.ChainID, tx.V, tx.R, tx.S = chainID, v, r, s
}

func (tx *CeloDynamicFeeTx) encode(b *bytes.Buffer) error {
return rlp.Encode(b, tx)
}

func (tx *CeloDynamicFeeTx) decode(input []byte) error {
return rlp.DecodeBytes(input, tx)
}

func (tx *CeloDynamicFeeTx) feeCurrency() *common.Address { return tx.FeeCurrency }
94 changes: 94 additions & 0 deletions core/types/celo_transaction_signing.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

// TODO(pl)

package types

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
)

type cel2Signer struct{ cancunSigner }

// NewCel2Signer returns a signer that accepts
// - CIP-64 celo dynamic fee transaction (v2)
// - EIP-4844 blob transactions
// - EIP-1559 dynamic fee transactions
// - EIP-2930 access list transactions,
// - EIP-155 replay protected transactions, and
// - legacy Homestead transactions.
func NewCel2Signer(chainId *big.Int) Signer {
return cel2Signer{cancunSigner{londonSigner{eip2930Signer{NewEIP155Signer(chainId)}}}}
}

func (s cel2Signer) Sender(tx *Transaction) (common.Address, error) {
if tx.Type() != CeloDynamicFeeTxType {
return s.cancunSigner.Sender(tx)
}
V, R, S := tx.RawSignatureValues()
// DynamicFee txs are defined to use 0 and 1 as their recovery
// id, add 27 to become equivalent to unprotected Homestead signatures.
V = new(big.Int).Add(V, big.NewInt(27))
if tx.ChainId().Cmp(s.chainId) != 0 {
return common.Address{}, ErrInvalidChainId
}
return recoverPlain(s.Hash(tx), R, S, V, true)
}

func (s cel2Signer) Equal(s2 Signer) bool {
x, ok := s2.(cel2Signer)
return ok && x.chainId.Cmp(s.chainId) == 0
}

func (s cel2Signer) SignatureValues(tx *Transaction, sig []byte) (R, S, V *big.Int, err error) {
txdata, ok := tx.inner.(*CeloDynamicFeeTx)
if !ok {
return s.cancunSigner.SignatureValues(tx, sig)
}
// Check that chain ID of tx matches the signer. We also accept ID zero here,
// because it indicates that the chain ID was not specified in the tx.
if txdata.ChainID.Sign() != 0 && txdata.ChainID.Cmp(s.chainId) != 0 {
return nil, nil, nil, ErrInvalidChainId
}
R, S, _ = decodeSignature(sig)
V = big.NewInt(int64(sig[64]))
return R, S, V, nil
}

// Hash returns the hash to be signed by the sender.
// It does not uniquely identify the transaction.
func (s cel2Signer) Hash(tx *Transaction) common.Hash {
if tx.Type() == CeloDynamicFeeTxType {
return prefixedRlpHash(
tx.Type(),
[]interface{}{
s.chainId,
tx.Nonce(),
tx.GasTipCap(),
tx.GasFeeCap(),
tx.Gas(),
tx.To(),
tx.Value(),
tx.Data(),
tx.AccessList(),
tx.FeeCurrency(),
})
}
return s.cancunSigner.Hash(tx)
}
2 changes: 2 additions & 0 deletions core/types/deposit_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,5 @@ func (tx *DepositTx) encode(b *bytes.Buffer) error {
func (tx *DepositTx) decode(input []byte) error {
return rlp.DecodeBytes(input, tx)
}

func (tx *DepositTx) feeCurrency() *common.Address { return nil }
12 changes: 12 additions & 0 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const (
AccessListTxType = 0x01
DynamicFeeTxType = 0x02
BlobTxType = 0x03
// CeloDynamicFeeTxType = 0x7c old Celo tx type with gateway fee
CeloDynamicFeeTxType = 0x7b
)

// Transaction is an Ethereum transaction.
Expand Down Expand Up @@ -101,6 +103,9 @@ type TxData interface {

encode(*bytes.Buffer) error
decode([]byte) error

// Celo specific fields
feeCurrency() *common.Address
}

// EncodeRLP implements rlp.Encoder
Expand Down Expand Up @@ -205,6 +210,8 @@ func (tx *Transaction) decodeTyped(b []byte) (TxData, error) {
inner = new(AccessListTx)
case DynamicFeeTxType:
inner = new(DynamicFeeTx)
case CeloDynamicFeeTxType:
inner = new(CeloDynamicFeeTx)
case BlobTxType:
inner = new(BlobTx)
case DepositTxType:
Expand Down Expand Up @@ -588,6 +595,11 @@ func (tx *Transaction) WithSignature(signer Signer, sig []byte) (*Transaction, e
return &Transaction{inner: cpy, time: tx.time}, nil
}

// FeeCurrency returns the fee currency of the transaction. Nil implies paying in CELO.
func (tx *Transaction) FeeCurrency() *common.Address {
return copyAddressPtr(tx.inner.feeCurrency())
}

// Transactions implements DerivableList for transactions.
type Transactions []*Transaction

Expand Down
75 changes: 75 additions & 0 deletions core/types/transaction_marshalling.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ type txJSON struct {

// Only used for encoding:
Hash common.Hash `json:"hash"`

// Celo specific fields
FeeCurrency *common.Address `json:"feeCurrency"` // nil means native currency
}

// yParityValue returns the YParity value from JSON. For backwards-compatibility reasons,
Expand Down Expand Up @@ -133,6 +136,21 @@ func (tx *Transaction) MarshalJSON() ([]byte, error) {
yparity := itx.V.Uint64()
enc.YParity = (*hexutil.Uint64)(&yparity)

case *CeloDynamicFeeTx:
enc.ChainID = (*hexutil.Big)(itx.ChainID)
enc.Nonce = (*hexutil.Uint64)(&itx.Nonce)
enc.To = tx.To()
enc.Gas = (*hexutil.Uint64)(&itx.Gas)
enc.MaxFeePerGas = (*hexutil.Big)(itx.GasFeeCap)
enc.MaxPriorityFeePerGas = (*hexutil.Big)(itx.GasTipCap)
enc.FeeCurrency = tx.FeeCurrency()
enc.Value = (*hexutil.Big)(itx.Value)
enc.Input = (*hexutil.Bytes)(&itx.Data)
enc.AccessList = &itx.AccessList
enc.V = (*hexutil.Big)(itx.V)
enc.R = (*hexutil.Big)(itx.R)
enc.S = (*hexutil.Big)(itx.S)

case *BlobTx:
enc.ChainID = (*hexutil.Big)(itx.ChainID.ToBig())
enc.Nonce = (*hexutil.Uint64)(&itx.Nonce)
Expand Down Expand Up @@ -342,6 +360,63 @@ func (tx *Transaction) UnmarshalJSON(input []byte) error {
}
}

case CeloDynamicFeeTxType:
var itx CeloDynamicFeeTx
inner = &itx
if dec.ChainID == nil {
return errors.New("missing required field 'chainId' in transaction")
}
itx.ChainID = (*big.Int)(dec.ChainID)
if dec.Nonce == nil {
return errors.New("missing required field 'nonce' in transaction")
}
itx.Nonce = uint64(*dec.Nonce)
if dec.To != nil {
itx.To = dec.To
}
if dec.Gas == nil {
return errors.New("missing required field 'gas' for txdata")
}
itx.Gas = uint64(*dec.Gas)
if dec.MaxPriorityFeePerGas == nil {
return errors.New("missing required field 'maxPriorityFeePerGas' for txdata")
}
itx.GasTipCap = (*big.Int)(dec.MaxPriorityFeePerGas)
if dec.MaxFeePerGas == nil {
return errors.New("missing required field 'maxFeePerGas' for txdata")
}
itx.GasFeeCap = (*big.Int)(dec.MaxFeePerGas)
if dec.Value == nil {
return errors.New("missing required field 'value' in transaction")
}
itx.FeeCurrency = dec.FeeCurrency
itx.Value = (*big.Int)(dec.Value)
if dec.Input == nil {
return errors.New("missing required field 'input' in transaction")
}
itx.Data = *dec.Input
if dec.V == nil {
return errors.New("missing required field 'v' in transaction")
}
if dec.AccessList != nil {
itx.AccessList = *dec.AccessList
}
itx.V = (*big.Int)(dec.V)
if dec.R == nil {
return errors.New("missing required field 'r' in transaction")
}
itx.R = (*big.Int)(dec.R)
if dec.S == nil {
return errors.New("missing required field 's' in transaction")
}
itx.S = (*big.Int)(dec.S)
withSignature := itx.V.Sign() != 0 || itx.R.Sign() != 0 || itx.S.Sign() != 0
if withSignature {
if err := sanityCheckSignature(itx.V, itx.R, itx.S, false); err != nil {
return err
}
}

case BlobTxType:
var itx BlobTx
inner = &itx
Expand Down
5 changes: 4 additions & 1 deletion core/types/transaction_signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ func MakeSigner(config *params.ChainConfig, blockNumber *big.Int, blockTime uint
// have the current block number available, use MakeSigner instead.
func LatestSigner(config *params.ChainConfig) Signer {
if config.ChainID != nil {
if config.Cel2Time != nil {
return NewCel2Signer(config.ChainID)
}
if config.CancunTime != nil {
return NewCancunSigner(config.ChainID)
}
Expand Down Expand Up @@ -92,7 +95,7 @@ func LatestSignerForChainID(chainID *big.Int) Signer {
if chainID == nil {
return HomesteadSigner{}
}
return NewCancunSigner(chainID)
return NewCel2Signer(chainID)
}

// SignTx signs the transaction using the given signer and private key.
Expand Down
2 changes: 2 additions & 0 deletions core/types/tx_access_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,5 @@ func (tx *AccessListTx) encode(b *bytes.Buffer) error {
func (tx *AccessListTx) decode(input []byte) error {
return rlp.DecodeBytes(input, tx)
}

func (tx *AccessListTx) feeCurrency() *common.Address { return nil }
2 changes: 2 additions & 0 deletions core/types/tx_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,5 @@ func blobHash(commit *kzg4844.Commitment) common.Hash {
vhash[0] = params.BlobTxHashVersion
return vhash
}

func (tx *BlobTx) feeCurrency() *common.Address { return nil }
Loading

0 comments on commit bfba411

Please sign in to comment.