Skip to content

Commit

Permalink
Add txHash to rlp store marshal/unmarshal (#528)
Browse files Browse the repository at this point in the history
* Add txHash to rlp store marshal/unmarshal

* Fix lint errors

* add unit test

* Fix backwards compactibility for old receipts

* Fix unit tests

* Fix linter errors

* Optimize getBlockByHash call

* Fetch block unconditionally

* Replace reflact package with assert for test
  • Loading branch information
0xAleksaOpacic authored May 16, 2022
1 parent b0f6f0c commit 315cc6f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 14 deletions.
14 changes: 14 additions & 0 deletions jsonrpc/filter_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ type filterManagerStore interface {

// GetReceiptsByHash returns the receipts for a block hash
GetReceiptsByHash(hash types.Hash) ([]*types.Receipt, error)

// GetBlockByHash returns the block using the block hash
GetBlockByHash(hash types.Hash, full bool) (*types.Block, bool)
}

// FilterManager manages all running filters
Expand Down Expand Up @@ -485,7 +488,18 @@ func (f *FilterManager) appendLogsToFilters(header *types.Header, removed bool)
return nil
}

block, ok := f.store.GetBlockByHash(header.Hash, true)
if !ok {
f.logger.Error("could not find block in store", "hash", header.Hash.String())

return nil
}

for indx, receipt := range receipts {
if receipt.TxHash == types.ZeroHash {
// Extract tx Hash
receipt.TxHash = block.Transactions[indx].Hash
}
// check the logs with the filters
for _, log := range receipt.Logs {
nn := &Log{
Expand Down
2 changes: 2 additions & 0 deletions jsonrpc/filter_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func TestFilterLog(t *testing.T) {
},
},
},
TxHash: hash3,
},
},
},
Expand All @@ -56,6 +57,7 @@ func TestFilterLog(t *testing.T) {
},
},
},
TxHash: hash3,
},
},
},
Expand Down
4 changes: 4 additions & 0 deletions jsonrpc/mocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ func (m *mockStore) SubscribeEvents() blockchain.Subscription {
return m.subscription
}

func (m *mockStore) GetBlockByHash(hash types.Hash, full bool) (*types.Block, bool) {
return nil, false
}

func (m *mockStore) GetBlockByNumber(num uint64, full bool) (*types.Block, bool) {
return nil, false
}
Expand Down
56 changes: 55 additions & 1 deletion types/rlp_encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,62 @@ func TestRLPMarshall_And_Unmarshall_Transaction(t *testing.T) {
}
}

func TestRLPStorage_Marshall_And_Unmarshall_Receipt(t *testing.T) {
addr := StringToAddress("11")
hash := StringToHash("10")

testTable := []struct {
name string
receipt *Receipt
setStatus bool
}{
{
"Marshal receipt with status",
&Receipt{
CumulativeGasUsed: 10,
GasUsed: 100,
ContractAddress: addr,
TxHash: hash,
},
true,
},
{
"Marshal receipt without status",
&Receipt{
Root: hash,
CumulativeGasUsed: 10,
GasUsed: 100,
ContractAddress: addr,
TxHash: hash,
},
false,
},
}

for _, testCase := range testTable {
t.Run(testCase.name, func(t *testing.T) {
receipt := testCase.receipt

if testCase.setStatus {
receipt.SetStatus(ReceiptSuccess)
}

unmarshalledReceipt := new(Receipt)
marshaledRlp := receipt.MarshalStoreRLPTo(nil)

if err := unmarshalledReceipt.UnmarshalStoreRLP(marshaledRlp); err != nil {
t.Fatal(err)
}

if !assert.Exactly(t, receipt, unmarshalledReceipt) {
t.Fatal("[ERROR] Unmarshalled receipt not equal to base receipt")
}
})
}
}

func TestRLPUnmarshal_Header_ComputeHash(t *testing.T) {
// header computes hash after unmarshaling
// header computes hash after unmarshalling
h := &Header{}
h.ComputeHash()

Expand Down
7 changes: 6 additions & 1 deletion types/rlp_marshal_storage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package types

import "github.com/umbracle/fastrlp"
import (
"github.com/umbracle/fastrlp"
)

type RLPStoreMarshaler interface {
MarshalStoreRLPTo(dst []byte) []byte
Expand Down Expand Up @@ -80,5 +82,8 @@ func (r *Receipt) MarshalStoreRLPWith(a *fastrlp.Arena) *fastrlp.Value {
// gas used
vv.Set(a.NewUint(r.GasUsed))

// TxHash
vv.Set(a.NewBytes(r.TxHash.Bytes()))

return vv
}
34 changes: 22 additions & 12 deletions types/rlp_unmarshal_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,30 +115,40 @@ func (r *Receipt) UnmarshalStoreRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) err
return err
}

if len(elems) != 3 {
return fmt.Errorf("expected 3 elements")
if len(elems) < 3 {
return fmt.Errorf("expected at least 3 elements")
}

if err := r.UnmarshalRLPFrom(p, elems[0]); err != nil {
return err
}

{
// contract address
vv, err := elems[1].Bytes()
if err != nil {
return err
}
if len(vv) == 20 {
// address
r.ContractAddress = BytesToAddress(vv)
}
// contract address
vv, err := elems[1].Bytes()
if err != nil {
return err
}

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

// gas used
if r.GasUsed, err = elems[2].GetUint64(); err != nil {
return err
}

// tx hash
// backwards compatibility, old receipts did not marshal a TxHash
if len(elems) == 4 {
vv, err := elems[3].Bytes()
if err != nil {
return err
}

r.TxHash = BytesToHash(vv)
}

return nil
}

0 comments on commit 315cc6f

Please sign in to comment.