Skip to content

Commit

Permalink
Remove contractAddress from receipt if not contract deployment (#60)
Browse files Browse the repository at this point in the history
* Remove contractAddress from receipt if not contract deployment

* Remove incorrect unit test
  • Loading branch information
DarianShawn authored May 23, 2022
1 parent 6682ee0 commit 08020e1
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 44 deletions.
14 changes: 0 additions & 14 deletions archive/restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package archive

import (
"bytes"
"errors"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -204,13 +203,6 @@ func Test_parseBlock(t *testing.T) {
block: blocks[0],
err: nil,
},
{
name: "should return error",
blockstream: newBlockStream(bytes.NewBuffer((&Metadata{}).MarshalRLP())),
block: nil,
// should fail by wrong format
err: errors.New("not enough elements to decode block, expected 3 but found 2"),
},
}

for _, tt := range tests {
Expand All @@ -236,12 +228,6 @@ func Test_parseMetadata(t *testing.T) {
metadata: &metadata,
err: nil,
},
{
name: "should return error",
blockstream: newBlockStream(bytes.NewBuffer(blocks[0].MarshalRLP())),
metadata: nil,
err: errors.New("not enough elements to decode Metadata, expected 2 but found 3"),
},
}

for _, tt := range tests {
Expand Down
5 changes: 3 additions & 2 deletions archive/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ func (m *Metadata) UnmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error {
return err
}

if num := len(elems); num != 2 {
return fmt.Errorf("not enough elements to decode Metadata, expected 2 but found %d", num)
if num := len(elems); num < 2 {
return fmt.Errorf("incorrect number of elements to decode Metadata, expected at least 2 but found %d",
len(elems))
}

if m.Latest, err = elems[0].GetUint64(); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion blockchain/storage/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ func testReceipts(t *testing.T, m MockStorage) {
TxHash: txn.Hash,
LogsBloom: types.Bloom{0x1},
GasUsed: 10,
ContractAddress: types.Address{0x1},
ContractAddress: &types.Address{0x1},
Logs: []*types.Log{
{
Address: addr2,
Expand Down
5 changes: 3 additions & 2 deletions consensus/ibft/extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ func (i *IstanbulExtra) UnmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) er
return err
}

if num := len(elems); num != 3 {
return fmt.Errorf("not enough elements to decode istambul extra, expected 3 but found %d", num)
if num := len(elems); num < 3 {
return fmt.Errorf("incorrect number of elements to decode istambul extra, expected at least 3 but found %d",
len(elems))
}

// Validators
Expand Down
2 changes: 1 addition & 1 deletion jsonrpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ type receipt struct {
BlockHash types.Hash `json:"blockHash"`
BlockNumber argUint64 `json:"blockNumber"`
GasUsed argUint64 `json:"gasUsed"`
ContractAddress types.Address `json:"contractAddress"`
ContractAddress *types.Address `json:"contractAddress"`
FromAddr types.Address `json:"from"`
ToAddr *types.Address `json:"to"`
}
Expand Down
4 changes: 2 additions & 2 deletions state/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (t *Transition) WriteFailedReceipt(txn *types.Transaction) error {
t.receipts = append(t.receipts, receipt)

if txn.To == nil {
receipt.ContractAddress = crypto.CreateAddress(txn.From, txn.Nonce)
receipt.ContractAddress = crypto.CreateAddress(txn.From, txn.Nonce).Ptr()
}

return nil
Expand Down Expand Up @@ -287,7 +287,7 @@ func (t *Transition) Write(txn *types.Transaction) error {

// if the transaction created a contract, store the creation address in the receipt.
if msg.To == nil {
receipt.ContractAddress = crypto.CreateAddress(msg.From, txn.Nonce)
receipt.ContractAddress = crypto.CreateAddress(msg.From, txn.Nonce).Ptr()
}

// handle cross bridge logs from|to dogecoin blockchain
Expand Down
5 changes: 3 additions & 2 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ func (a *Account) UnmarshalRlp(b []byte) error {
return err
}

if len(elems) != 4 {
return fmt.Errorf("bad")
if len(elems) < 4 {
return fmt.Errorf("incorrect number of elements to decode account, expected at least 4 but found %d",
len(elems))
}

// nonce
Expand Down
6 changes: 5 additions & 1 deletion types/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ type Receipt struct {

// context fields
GasUsed uint64
ContractAddress Address
ContractAddress *Address
TxHash Hash
}

func (r *Receipt) SetStatus(s ReceiptStatus) {
r.Status = &s
}

func (r *Receipt) SetContractAddress(contractAddress Address) {
r.ContractAddress = &contractAddress
}

type Log struct {
Address Address
Topics []Hash
Expand Down
4 changes: 2 additions & 2 deletions types/rlp_encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func TestRLPStorage_Marshall_And_Unmarshall_Receipt(t *testing.T) {
&Receipt{
CumulativeGasUsed: 10,
GasUsed: 100,
ContractAddress: addr,
ContractAddress: &addr,
TxHash: hash,
},
true,
Expand All @@ -90,7 +90,7 @@ func TestRLPStorage_Marshall_And_Unmarshall_Receipt(t *testing.T) {
Root: hash,
CumulativeGasUsed: 10,
GasUsed: 100,
ContractAddress: addr,
ContractAddress: &addr,
TxHash: hash,
},
false,
Expand Down
2 changes: 1 addition & 1 deletion types/rlp_marshal_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (r *Receipt) MarshalStoreRLPWith(a *fastrlp.Arena) *fastrlp.Value {
vv := a.NewArray()
vv.Set(r.MarshalRLPWith(a))

if r.ContractAddress == ZeroAddress {
if r.ContractAddress == nil {
vv.Set(a.NewNull())
} else {
vv.Set(a.NewBytes(r.ContractAddress.Bytes()))
Expand Down
25 changes: 15 additions & 10 deletions types/rlp_unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ func (b *Block) UnmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error {
return err
}

if num := len(elems); num != 3 {
return fmt.Errorf("not enough elements to decode block, expected 3 but found %d", num)
if len(elems) < 3 {
return fmt.Errorf("incorrect number of elements to decode block, expected at least 3 but found %d",
len(elems))
}

// header
Expand Down Expand Up @@ -97,8 +98,9 @@ func (h *Header) UnmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error {
return err
}

if num := len(elems); num != 15 {
return fmt.Errorf("not enough elements to decode header, expected 15 but found %d", num)
if len(elems) < 15 {
return fmt.Errorf("incorrect number of elements to decode header, expected at least 15 but found %d",
len(elems))
}

// parentHash
Expand Down Expand Up @@ -204,8 +206,9 @@ func (r *Receipt) UnmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error {
return err
}

if len(elems) != 4 {
return fmt.Errorf("expected 4 elements")
if len(elems) < 4 {
return fmt.Errorf("incorrect number of elements to decode receipt, expected at least 4 but found %d",
len(elems))
}

// root or status
Expand Down Expand Up @@ -258,8 +261,9 @@ func (l *Log) UnmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error {
return err
}

if len(elems) != 3 {
return fmt.Errorf("bad elems")
if len(elems) < 3 {
return fmt.Errorf("incorrect number of elements to decode log, expected at least 3 but found %d",
len(elems))
}

// address
Expand Down Expand Up @@ -299,8 +303,9 @@ func (t *Transaction) UnmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) erro
return err
}

if num := len(elems); num != 9 {
return fmt.Errorf("not enough elements to decode transaction, expected 9 but found %d", num)
if len(elems) < 9 {
return fmt.Errorf("incorrect number of elements to decode transaction, expected at least 9 but found %d",
len(elems))
}

p.Hash(t.Hash[:0], v)
Expand Down
15 changes: 9 additions & 6 deletions types/rlp_unmarshal_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ func (b *Body) UnmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error {
return err
}

if len(tuple) != 2 {
return fmt.Errorf("not enough elements to decode header, expected 15 but found %d", len(tuple))
if len(tuple) < 2 {
return fmt.Errorf("incorrect number of elements to decode body, expected at least 2 but found %d",
len(tuple))
}

// transactions
Expand Down Expand Up @@ -67,8 +68,9 @@ func (t *Transaction) UnmarshalStoreRLPFrom(p *fastrlp.Parser, v *fastrlp.Value)
return err
}

if len(elems) != 2 {
return fmt.Errorf("expected 2 elements")
if len(elems) < 2 {
return fmt.Errorf("incorrect number of elements to decode transaction, expected at least 2 but found %d",
len(elems))
}

// consensus part
Expand Down Expand Up @@ -116,7 +118,8 @@ func (r *Receipt) UnmarshalStoreRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) err
}

if len(elems) < 3 {
return fmt.Errorf("expected at least 3 elements")
return fmt.Errorf("incorrect number of elements to decode receipt, expected at least 3 but found %d",
len(elems))
}

if err := r.UnmarshalRLPFrom(p, elems[0]); err != nil {
Expand All @@ -131,7 +134,7 @@ func (r *Receipt) UnmarshalStoreRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) err

if len(vv) == 20 {
// address
r.ContractAddress = BytesToAddress(vv)
r.SetContractAddress(BytesToAddress(vv))
}

// gas used
Expand Down
4 changes: 4 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ func (a Address) checksumEncode() string {
return "0x" + string(result)
}

func (a Address) Ptr() *Address {
return &a
}

func (a Address) String() string {
return a.checksumEncode()
}
Expand Down

0 comments on commit 08020e1

Please sign in to comment.