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

Return MixedcaseAddress with proper checksum for RPC requests #2149

Merged
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
5 changes: 5 additions & 0 deletions common/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,11 @@ func MakeErrQuaiAddress(addr string) error {
return fmt.Errorf("expected Qi address, but found Quai address: %s", addr)
}

func (a Address) MixedcaseAddressPtr() *MixedcaseAddress {
m := NewMixedcaseAddress(a)
return &m
}

func (a Address) MixedcaseAddress() MixedcaseAddress {
return NewMixedcaseAddress(a)
}
Expand Down
48 changes: 25 additions & 23 deletions core/types/transaction_marshalling.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,29 @@ type txJSON struct {
Type hexutil.Uint64 `json:"type"`

// Common transaction fields:
Nonce *hexutil.Uint64 `json:"nonce"`
GasPrice *hexutil.Big `json:"gasPrice"`
MaxPriorityFeePerGas *hexutil.Big `json:"maxPriorityFeePerGas"`
MaxFeePerGas *hexutil.Big `json:"maxFeePerGas"`
Gas *hexutil.Uint64 `json:"gas"`
Value *hexutil.Big `json:"value"`
Data *hexutil.Bytes `json:"input"`
To *common.Address `json:"to"`
AccessList *AccessList `json:"accessList"`
TxIn []TxInJSON `json:"inputs,omitempty"`
TxOut []TxOutJSON `json:"outputs,omitempty"`
UTXOSignature *hexutil.Bytes `json:"utxoSignature,omitempty"`
IsCoinbase *hexutil.Uint64 `json:"isCoinbase"`
Nonce *hexutil.Uint64 `json:"nonce"`
GasPrice *hexutil.Big `json:"gasPrice"`
MaxPriorityFeePerGas *hexutil.Big `json:"maxPriorityFeePerGas"`
MaxFeePerGas *hexutil.Big `json:"maxFeePerGas"`
Gas *hexutil.Uint64 `json:"gas"`
Value *hexutil.Big `json:"value"`
Data *hexutil.Bytes `json:"input"`
To *common.MixedcaseAddress `json:"to"`
AccessList *AccessList `json:"accessList"`
TxIn []TxInJSON `json:"inputs,omitempty"`
TxOut []TxOutJSON `json:"outputs,omitempty"`
UTXOSignature *hexutil.Bytes `json:"utxoSignature,omitempty"`
IsCoinbase *hexutil.Uint64 `json:"isCoinbase"`
// Optional fields only present for internal transactions
ChainID *hexutil.Big `json:"chainId,omitempty"`
V *hexutil.Big `json:"v,omitempty"`
R *hexutil.Big `json:"r,omitempty"`
S *hexutil.Big `json:"s,omitempty"`

// Optional fields only present for external transactions
Sender *common.Address `json:"sender,omitempty"`
OriginatingTxHash *common.Hash `json:"originatingTxHash,omitempty"`
ETXIndex *hexutil.Uint64 `json:"etxIndex,omitempty"`
ETXSender *common.MixedcaseAddress `json:"sender,omitempty"`
OriginatingTxHash *common.Hash `json:"originatingTxHash,omitempty"`
ETXIndex *hexutil.Uint64 `json:"etxIndex,omitempty"`

// Only used for encoding:
Hash common.Hash `json:"hash"`
Expand Down Expand Up @@ -92,7 +92,7 @@ func (t *Transaction) MarshalJSON() ([]byte, error) {
enc.MaxPriorityFeePerGas = (*hexutil.Big)(tx.GasTipCap)
enc.Value = (*hexutil.Big)(tx.Value)
enc.Data = (*hexutil.Bytes)(&tx.Data)
enc.To = t.To()
enc.To = t.To().MixedcaseAddressPtr()
enc.V = (*hexutil.Big)(tx.V)
enc.R = (*hexutil.Big)(tx.R)
enc.S = (*hexutil.Big)(tx.S)
Expand All @@ -104,8 +104,8 @@ func (t *Transaction) MarshalJSON() ([]byte, error) {
enc.Gas = (*hexutil.Uint64)(&tx.Gas)
enc.Value = (*hexutil.Big)(tx.Value)
enc.Data = (*hexutil.Bytes)(&tx.Data)
enc.To = t.To()
enc.Sender = &tx.Sender
enc.To = t.To().MixedcaseAddressPtr()
enc.ETXSender = tx.Sender.MixedcaseAddressPtr()
isCoinbase := hexutil.Uint64(0)
if tx.IsCoinbase {
isCoinbase = hexutil.Uint64(1)
Expand Down Expand Up @@ -161,7 +161,8 @@ func (t *Transaction) UnmarshalJSON(input []byte) error {
}
itx.ChainID = (*big.Int)(dec.ChainID)
if dec.To != nil {
itx.To = dec.To
addr := dec.To.Address()
itx.To = &addr
}
if dec.Nonce == nil {
return errors.New("missing required field 'nonce' in internal transaction")
Expand Down Expand Up @@ -214,7 +215,8 @@ func (t *Transaction) UnmarshalJSON(input []byte) error {
}
etx.AccessList = *dec.AccessList
if dec.To != nil {
etx.To = dec.To
addr := dec.To.Address()
etx.To = &addr
}
if dec.OriginatingTxHash == nil {
return errors.New("missing required field 'originatingTxHash' in external transaction")
Expand All @@ -236,10 +238,10 @@ func (t *Transaction) UnmarshalJSON(input []byte) error {
return errors.New("missing required field 'input' in external transaction")
}
etx.Data = *dec.Data
if dec.Sender == nil {
if dec.ETXSender == nil {
return errors.New("missing required field 'sender' in external transaction")
}
etx.Sender = *dec.Sender
etx.Sender = dec.ETXSender.Address()
if dec.IsCoinbase == nil {
return errors.New("missing required field 'isCoinbase' in external transaction")
}
Expand Down
2 changes: 1 addition & 1 deletion core/types/wo.go
Original file line number Diff line number Diff line change
Expand Up @@ -1019,8 +1019,8 @@ func (wh *WorkObjectHeader) RPCMarshalWorkObjectHeader() map[string]interface{}
"txHash": wh.TxHash(),
"timestamp": hexutil.Uint64(wh.Time()),
"mixHash": wh.MixHash(),
"coinbase": wh.Coinbase(),
"lock": hexutil.Uint64(wh.Lock()),
"coinbase": wh.Coinbase().MixedcaseAddress(),
}
return result
}
Expand Down
82 changes: 41 additions & 41 deletions internal/quaiapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,13 @@ func (s *PublicBlockChainAPI) GetOutpointsByAddressAtBlock(ctx context.Context,

// Result structs for GetProof
type AccountResult struct {
Address common.Address `json:"address"`
AccountProof []string `json:"accountProof"`
Balance *hexutil.Big `json:"balance"`
CodeHash common.Hash `json:"codeHash"`
Nonce hexutil.Uint64 `json:"nonce"`
StorageHash common.Hash `json:"storageHash"`
StorageProof []StorageResult `json:"storageProof"`
Address common.MixedcaseAddress `json:"address"`
AccountProof []string `json:"accountProof"`
Balance *hexutil.Big `json:"balance"`
CodeHash common.Hash `json:"codeHash"`
Nonce hexutil.Uint64 `json:"nonce"`
StorageHash common.Hash `json:"storageHash"`
StorageProof []StorageResult `json:"storageProof"`
}

type StorageResult struct {
Expand Down Expand Up @@ -362,7 +362,7 @@ func (s *PublicBlockChainAPI) GetProof(ctx context.Context, address common.Addre
}

return &AccountResult{
Address: address,
Address: address.MixedcaseAddress(),
AccountProof: toHexSlice(accountProof),
Balance: (*hexutil.Big)(state.GetBalance(internal)),
CodeHash: codeHash,
Expand Down Expand Up @@ -1032,33 +1032,33 @@ func (s *PublicBlockChainAPI) rpcMarshalBlock(ctx context.Context, b *types.Work

// RPCTransaction represents a transaction that will serialize to the RPC representation of a transaction
type RPCTransaction struct {
BlockHash *common.Hash `json:"blockHash"`
BlockNumber *hexutil.Big `json:"blockNumber"`
From *common.Address `json:"from,omitempty"`
Gas hexutil.Uint64 `json:"gas"`
GasFeeCap *hexutil.Big `json:"maxFeePerGas,omitempty"`
GasTipCap *hexutil.Big `json:"maxPriorityFeePerGas,omitempty"`
Hash common.Hash `json:"hash"`
Input hexutil.Bytes `json:"input"`
Nonce hexutil.Uint64 `json:"nonce"`
To *common.Address `json:"to,omitempty"`
TransactionIndex *hexutil.Uint64 `json:"transactionIndex"`
Value *hexutil.Big `json:"value,omitempty"`
Type hexutil.Uint64 `json:"type"`
Accesses *types.AccessList `json:"accessList,omitempty"`
ChainID *hexutil.Big `json:"chainId,omitempty"`
V *hexutil.Big `json:"v,omitempty"`
R *hexutil.Big `json:"r,omitempty"`
S *hexutil.Big `json:"s,omitempty"`
TxIn []RPCTxIn `json:"inputs,omitempty"`
TxOut []RPCTxOut `json:"outputs,omitempty"`
UTXOSignature hexutil.Bytes `json:"utxoSignature,omitempty"`
OriginatingTxHash *common.Hash `json:"originatingTxHash,omitempty"`
ETXIndex *hexutil.Uint64 `json:"etxIndex,omitempty"`
IsCoinbase *hexutil.Uint64 `json:"isCoinbase,omitempty"`
ETxType string `json:"etxType"`
BlockHash *common.Hash `json:"blockHash"`
BlockNumber *hexutil.Big `json:"blockNumber"`
From *common.MixedcaseAddress `json:"from,omitempty"`
Gas hexutil.Uint64 `json:"gas"`
GasFeeCap *hexutil.Big `json:"maxFeePerGas,omitempty"`
GasTipCap *hexutil.Big `json:"maxPriorityFeePerGas,omitempty"`
Hash common.Hash `json:"hash"`
Input hexutil.Bytes `json:"input"`
Nonce hexutil.Uint64 `json:"nonce"`
To *common.MixedcaseAddress `json:"to,omitempty"`
TransactionIndex *hexutil.Uint64 `json:"transactionIndex"`
Value *hexutil.Big `json:"value,omitempty"`
Type hexutil.Uint64 `json:"type"`
Accesses *types.AccessList `json:"accessList,omitempty"`
ChainID *hexutil.Big `json:"chainId,omitempty"`
V *hexutil.Big `json:"v,omitempty"`
R *hexutil.Big `json:"r,omitempty"`
S *hexutil.Big `json:"s,omitempty"`
TxIn []RPCTxIn `json:"inputs,omitempty"`
TxOut []RPCTxOut `json:"outputs,omitempty"`
UTXOSignature hexutil.Bytes `json:"utxoSignature,omitempty"`
OriginatingTxHash *common.Hash `json:"originatingTxHash,omitempty"`
ETXIndex *hexutil.Uint64 `json:"etxIndex,omitempty"`
IsCoinbase *hexutil.Uint64 `json:"isCoinbase,omitempty"`
ETxType string `json:"etxType"`
// Optional fields only present for external transactions
Sender *common.Address `json:"sender,omitempty"`
ETXSender *common.MixedcaseAddress `json:"sender,omitempty"`
}

type RPCTxIn struct {
Expand Down Expand Up @@ -1108,12 +1108,12 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber
from, _ := types.Sender(signer, tx)
result = &RPCTransaction{
Type: hexutil.Uint64(tx.Type()),
From: &from,
From: from.MixedcaseAddressPtr(),
Gas: hexutil.Uint64(tx.Gas()),
Hash: tx.Hash(),
Input: hexutil.Bytes(tx.Data()),
Nonce: hexutil.Uint64(tx.Nonce()),
To: tx.To(),
To: tx.To().MixedcaseAddressPtr(),
Value: (*hexutil.Big)(tx.Value()),
ChainID: (*hexutil.Big)(tx.ChainId()),
GasFeeCap: (*hexutil.Big)(tx.GasFeeCap()),
Expand All @@ -1125,14 +1125,14 @@ func newRPCTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber
Gas: hexutil.Uint64(tx.Gas()),
Hash: tx.Hash(),
Input: hexutil.Bytes(tx.Data()),
To: tx.To(),
To: tx.To().MixedcaseAddressPtr(),
Value: (*hexutil.Big)(tx.Value()),
}
originatingTxHash := tx.OriginatingTxHash()
etxIndex := uint64(tx.ETXIndex())
sender := tx.ETXSender()
result.OriginatingTxHash = &originatingTxHash
result.Sender = &sender
result.ETXSender = sender.MixedcaseAddressPtr()
result.ETXIndex = (*hexutil.Uint64)(&etxIndex)
if tx.IsCoinbase() {
isCoinbase := uint64(1)
Expand Down Expand Up @@ -1507,8 +1507,8 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceipt(ctx context.Context, ha
"blockNumber": hexutil.Uint64(blockNumber),
"transactionHash": hash,
"transactionIndex": hexutil.Uint64(index),
"from": from,
"to": tx.To(),
"from": from.MixedcaseAddress(),
"to": tx.To().MixedcaseAddress(),
"gasUsed": hexutil.Uint64(receipt.GasUsed),
"cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed),
"contractAddress": nil,
Expand Down Expand Up @@ -1536,7 +1536,7 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceipt(ctx context.Context, ha
}
// If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation
if !receipt.ContractAddress.Equal(common.Zero) && !receipt.ContractAddress.Equal(common.Address{}) {
fields["contractAddress"] = receipt.ContractAddress
fields["contractAddress"] = receipt.ContractAddress.MixedcaseAddress()
}
return fields, nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/quaiapi/quai_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (s *PublicBlockChainQuaiAPI) GetProof(ctx context.Context, address common.A
}

return &AccountResult{
Address: address,
Address: address.MixedcaseAddress(),
AccountProof: toHexSlice(accountProof),
Balance: (*hexutil.Big)(state.GetBalance(internal)),
CodeHash: codeHash,
Expand Down
Loading