Skip to content

Commit

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

* Fix unmarshall guard for len

* Fix Log message

* Change error log message

* Remove unnecessary test

* Change contractAddress to pointer in TestRLPStorage_Marshall_And_Unmarshall_Receipt

* Fix linter errors
  • Loading branch information
0xAleksaOpacic committed May 16, 2022
1 parent 315cc6f commit a86fca1
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 37 deletions.
8 changes: 1 addition & 7 deletions archive/restore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func Test_parseBlock(t *testing.T) {
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"),
err: errors.New("incorrect number of elements to decode block, expected 3 but found 2"),
},
}

Expand All @@ -236,12 +236,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
4 changes: 2 additions & 2 deletions archive/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ 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 len(elems) < 2 {
return fmt.Errorf("incorrect number of elements to decode Metadata, expected 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 @@ -370,7 +370,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
4 changes: 2 additions & 2 deletions consensus/ibft/extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ 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 len(elems) < 3 {
return fmt.Errorf("incorrect number of elements to decode istambul extra, expected 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 @@ -227,7 +227,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 @@ -286,7 +286,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()
}

// Set the receipt logs and create a bloom for filtering
Expand Down
4 changes: 2 additions & 2 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ 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 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 @@ -29,14 +29,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 @@ -73,7 +73,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
20 changes: 10 additions & 10 deletions types/rlp_unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ 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 3 but found %d", len(elems))
}

// header
Expand Down Expand Up @@ -97,8 +97,8 @@ 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 15 but found %d", len(elems))
}

// parentHash
Expand Down Expand Up @@ -204,8 +204,8 @@ 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 4 but found %d", len(elems))
}

// root or status
Expand Down Expand Up @@ -258,8 +258,8 @@ 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 3 but found %d", len(elems))
}

// address
Expand Down Expand Up @@ -299,8 +299,8 @@ 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 9 but found %d", len(elems))
}

p.Hash(t.Hash[:0], v)
Expand Down
12 changes: 6 additions & 6 deletions types/rlp_unmarshal_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ 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 header, expected 2 but found %d", len(tuple))
}

// transactions
Expand Down Expand Up @@ -67,8 +67,8 @@ 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 2 but found %d", len(elems))
}

// consensus part
Expand Down Expand Up @@ -116,7 +116,7 @@ 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 +131,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 @@ -91,6 +91,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 a86fca1

Please sign in to comment.