From a86fca141e86c475a47e60b3490e5ba6d1368a7d Mon Sep 17 00:00:00 2001 From: OpacicAleksa Date: Mon, 16 May 2022 17:45:38 +0200 Subject: [PATCH] Remove contractAddress from receipt if not contract deployment (#546) * 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 --- archive/restore_test.go | 8 +------- archive/types.go | 4 ++-- blockchain/storage/testing.go | 2 +- consensus/ibft/extra.go | 4 ++-- jsonrpc/types.go | 2 +- state/executor.go | 4 ++-- state/state.go | 4 ++-- types/receipt.go | 6 +++++- types/rlp_encoding_test.go | 4 ++-- types/rlp_marshal_storage.go | 2 +- types/rlp_unmarshal.go | 20 ++++++++++---------- types/rlp_unmarshal_storage.go | 12 ++++++------ types/types.go | 4 ++++ 13 files changed, 39 insertions(+), 37 deletions(-) diff --git a/archive/restore_test.go b/archive/restore_test.go index eb3a8da61c..18d6085fb8 100644 --- a/archive/restore_test.go +++ b/archive/restore_test.go @@ -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"), }, } @@ -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 { diff --git a/archive/types.go b/archive/types.go index 2fa6e38c56..c33db10b0a 100644 --- a/archive/types.go +++ b/archive/types.go @@ -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 { diff --git a/blockchain/storage/testing.go b/blockchain/storage/testing.go index 37bbf2fcbd..2f90f72c53 100644 --- a/blockchain/storage/testing.go +++ b/blockchain/storage/testing.go @@ -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, diff --git a/consensus/ibft/extra.go b/consensus/ibft/extra.go index f15ba186b3..6b82535af0 100644 --- a/consensus/ibft/extra.go +++ b/consensus/ibft/extra.go @@ -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 diff --git a/jsonrpc/types.go b/jsonrpc/types.go index 57cb4cf48f..ad8c999a47 100644 --- a/jsonrpc/types.go +++ b/jsonrpc/types.go @@ -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"` } diff --git a/state/executor.go b/state/executor.go index b15e25b21a..00caa51837 100644 --- a/state/executor.go +++ b/state/executor.go @@ -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 @@ -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 diff --git a/state/state.go b/state/state.go index 4bbedae466..ded9038209 100644 --- a/state/state.go +++ b/state/state.go @@ -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 diff --git a/types/receipt.go b/types/receipt.go index 06ade6105b..4da5960989 100644 --- a/types/receipt.go +++ b/types/receipt.go @@ -29,7 +29,7 @@ type Receipt struct { // context fields GasUsed uint64 - ContractAddress Address + ContractAddress *Address TxHash Hash } @@ -37,6 +37,10 @@ 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 diff --git a/types/rlp_encoding_test.go b/types/rlp_encoding_test.go index 3bedd8b0fb..5af0c28251 100644 --- a/types/rlp_encoding_test.go +++ b/types/rlp_encoding_test.go @@ -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, @@ -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, diff --git a/types/rlp_marshal_storage.go b/types/rlp_marshal_storage.go index b6f8373d17..4c3105980c 100644 --- a/types/rlp_marshal_storage.go +++ b/types/rlp_marshal_storage.go @@ -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())) diff --git a/types/rlp_unmarshal.go b/types/rlp_unmarshal.go index 9334c7ebee..e7a782f8ef 100644 --- a/types/rlp_unmarshal.go +++ b/types/rlp_unmarshal.go @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) diff --git a/types/rlp_unmarshal_storage.go b/types/rlp_unmarshal_storage.go index 4845a74ec1..3ab0368679 100644 --- a/types/rlp_unmarshal_storage.go +++ b/types/rlp_unmarshal_storage.go @@ -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 @@ -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 @@ -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 { @@ -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 diff --git a/types/types.go b/types/types.go index e7777ea44a..6ccbfe73c3 100644 --- a/types/types.go +++ b/types/types.go @@ -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() }