Skip to content

Commit

Permalink
Update logic for decoding legacy EVM Block events
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Sep 30, 2024
1 parent f325ce3 commit 697ef57
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 51 deletions.
47 changes: 7 additions & 40 deletions models/block.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package models

import (
"fmt"
"math/big"

"github.com/onflow/cadence"
Expand Down Expand Up @@ -73,11 +72,13 @@ func (b *Block) Hash() (gethCommon.Hash, error) {
func decodeBlockEvent(event cadence.Event) (*Block, error) {
payload, err := events.DecodeBlockEventPayload(event)
if err != nil {
if block, err := decodeLegacyBlockEvent(event); err == nil {
return block, nil
}
return nil, err
}

return nil, fmt.Errorf("failed to cadence decode block [%s]: %w", event.String(), err)
var fixedHash *string
if payload.PrevRandao == gethCommon.HexToHash("0x0") {
hash := payload.Hash.String()
fixedHash = &hash
}

return &Block{
Expand All @@ -91,41 +92,7 @@ func decodeBlockEvent(event cadence.Event) (*Block, error) {
TotalGasUsed: payload.TotalGasUsed,
PrevRandao: payload.PrevRandao,
},
}, nil
}

// todo remove this after updated in flow-go
type blockEventPayloadV0 struct {
Height uint64 `cadence:"height"`
Hash gethCommon.Hash `cadence:"hash"`
Timestamp uint64 `cadence:"timestamp"`
TotalSupply cadence.Int `cadence:"totalSupply"`
TotalGasUsed uint64 `cadence:"totalGasUsed"`
ParentBlockHash gethCommon.Hash `cadence:"parentHash"`
ReceiptRoot gethCommon.Hash `cadence:"receiptRoot"`
TransactionHashRoot gethCommon.Hash `cadence:"transactionHashRoot"`
}

// DecodeBlockEventPayload decodes Cadence event into block event payload.
func decodeLegacyBlockEvent(event cadence.Event) (*Block, error) {
var block blockEventPayloadV0
err := cadence.DecodeFields(event, &block)
if err != nil {
return nil, err
}

h := block.Hash.String()
return &Block{
Block: &types.Block{
ParentBlockHash: block.ParentBlockHash,
Height: block.Height,
Timestamp: block.Timestamp,
TotalSupply: block.TotalSupply.Value,
ReceiptRoot: block.ReceiptRoot,
TransactionHashRoot: block.TransactionHashRoot,
TotalGasUsed: block.TotalGasUsed,
},
FixedHash: &h,
FixedHash: fixedHash,
}, nil
}

Expand Down
7 changes: 2 additions & 5 deletions models/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func Test_DecodingLegacyBlockExecutedEvent(t *testing.T) {
hashToCadenceArrayValue(block.TransactionHashRoot),
}).WithType(eventType)

b, err := decodeLegacyBlockEvent(legacyEvent)
b, err := decodeBlockEvent(legacyEvent)
require.NoError(t, err)

require.Equal(t, block.ParentBlockHash, b.ParentBlockHash)
Expand All @@ -160,10 +160,7 @@ func Test_DecodingLegacyBlockExecutedEvent(t *testing.T) {
dech, err := b.Hash()
require.NoError(t, err)
require.Equal(t, bh, dech)

b2, err := decodeBlockEvent(legacyEvent)
require.NoError(t, err)
require.Equal(t, b, b2)
require.NotNil(t, b.FixedHash)
}

func Test_Hash(t *testing.T) {
Expand Down
13 changes: 7 additions & 6 deletions services/ingestion/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,12 +550,13 @@ func TestBlockAndTransactionIngestion(t *testing.T) {
}

func newBlock(height uint64, txHashes []gethCommon.Hash) (cadence.Event, *models.Block, *events.Event, error) {
gethBlock := &types.Block{
ParentBlockHash: gethCommon.HexToHash("0x1"),
Height: height,
TotalSupply: big.NewInt(100),
ReceiptRoot: gethCommon.HexToHash("0x2"),
}
gethBlock := types.NewBlock(
gethCommon.HexToHash("0x1"),
height,
uint64(1337),
big.NewInt(100),
gethCommon.HexToHash("0x15"),
)
gethBlock.TransactionHashRoot = types.TransactionHashes(txHashes).RootHash()
block := &models.Block{
Block: gethBlock,
Expand Down

0 comments on commit 697ef57

Please sign in to comment.