Skip to content

Commit

Permalink
Merge pull request #344 from onflow/previewnet-patching
Browse files Browse the repository at this point in the history
Patch `DirectCall` hash calculation change
  • Loading branch information
sideninja authored Jul 12, 2024
2 parents 90de2f5 + 5a373c6 commit 8009cc2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 19 deletions.
5 changes: 3 additions & 2 deletions models/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ func (c *CadenceEvents) Transactions() ([]Transaction, []*StorageReceipt, error)
rcps := make([]*StorageReceipt, 0)
for _, e := range c.events.Events {
if isTransactionExecutedEvent(e.Value) {
tx, err := decodeTransaction(e.Value)
rcp, err := decodeReceipt(e.Value)
if err != nil {
return nil, nil, err
}
rcp, err := decodeReceipt(e.Value)

tx, err := decodeTransaction(e.Value, rcp.BlockNumber.Uint64())
if err != nil {
return nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion models/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func decodeReceipt(event cadence.Event) (*StorageReceipt, error) {
}
}

t, err := decodeTransaction(event)
t, err := decodeTransaction(event, tx.BlockHeight)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions models/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (tc TransactionCall) MarshalBinary() ([]byte, error) {
// decodeTransaction takes a cadence event for transaction executed
// and decodes it into a Transaction interface. The concrete type
// will be either a TransactionCall or a DirectCall.
func decodeTransaction(event cadence.Event) (Transaction, error) {
func decodeTransaction(event cadence.Event, evmHeight uint64) (Transaction, error) {
tx, err := types.DecodeTransactionEventPayload(event)
if err != nil {
return nil, fmt.Errorf("failed to cadence decode transaction: %w", err)
Expand All @@ -203,7 +203,7 @@ func decodeTransaction(event cadence.Event) (Transaction, error) {
return nil, fmt.Errorf("failed to rlp decode direct call: %w", err)
}

return DirectCall{DirectCall: directCall}, nil
return DirectCall{DirectCall: directCall, blockHeight: evmHeight}, nil
}

gethTx := &gethTypes.Transaction{}
Expand Down
10 changes: 5 additions & 5 deletions models/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func createTestEvent(t *testing.T, txBinary string) (cadence.Event, *types.Resul
func Test_DecodeEVMTransaction(t *testing.T) {
cdcEv, _ := createTestEvent(t, evmTxBinary)

decTx, err := decodeTransaction(cdcEv)
decTx, err := decodeTransaction(cdcEv, 10)
require.NoError(t, err)
require.IsType(t, TransactionCall{}, decTx)

Expand Down Expand Up @@ -133,7 +133,7 @@ func Test_DecodeEVMTransaction(t *testing.T) {
func Test_DecodeDirectCall(t *testing.T) {
cdcEv, _ := createTestEvent(t, directCallBinary)

decTx, err := decodeTransaction(cdcEv)
decTx, err := decodeTransaction(cdcEv, 10)
require.NoError(t, err)
require.IsType(t, DirectCall{}, decTx)

Expand Down Expand Up @@ -181,7 +181,7 @@ func Test_UnmarshalTransaction(t *testing.T) {

cdcEv, _ := createTestEvent(t, evmTxBinary)

tx, err := decodeTransaction(cdcEv)
tx, err := decodeTransaction(cdcEv, 10)
require.NoError(t, err)

encodedTx, err := tx.MarshalBinary()
Expand Down Expand Up @@ -235,7 +235,7 @@ func Test_UnmarshalTransaction(t *testing.T) {

cdcEv, _ := createTestEvent(t, directCallBinary)

tx, err := decodeTransaction(cdcEv)
tx, err := decodeTransaction(cdcEv, 10)
require.NoError(t, err)

encodedTx, err := tx.MarshalBinary()
Expand Down Expand Up @@ -287,7 +287,7 @@ func Test_UnmarshalTransaction(t *testing.T) {

cdcEv, _ := createTestEvent(t, directCallBinary)

tx, err := decodeTransaction(cdcEv)
tx, err := decodeTransaction(cdcEv, 10)
require.NoError(t, err)

encodedTx, err := tx.MarshalBinary()
Expand Down
16 changes: 7 additions & 9 deletions storage/pebble/transactions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pebble

import (
"encoding/binary"
"math/big"
"sync"

"github.com/cockroachdb/pebble"
Expand Down Expand Up @@ -48,15 +48,13 @@ func (t *Transactions) Get(ID common.Hash) (models.Transaction, error) {
return nil, err
}

// TEMP: Remove this after PreviewNet is reset.
// Needed only for backwards compatibility with the
// direct call hash calculation breaking change.
heightVal, err := t.store.get(latestCadenceHeightKey)
var evmHeight uint64
height, err := t.store.get(receiptTxIDToHeightKey, ID.Bytes())
if err != nil {
heightVal = []byte{0, 0, 0, 0, 0, 0, 0, 0}
evmHeight = 0
} else {
evmHeight = big.NewInt(0).SetBytes(height).Uint64()
}

cadenceHeight := binary.BigEndian.Uint64(heightVal)

return models.UnmarshalTransaction(val, cadenceHeight)
return models.UnmarshalTransaction(val, evmHeight)
}

0 comments on commit 8009cc2

Please sign in to comment.