Skip to content

Commit

Permalink
Merge pull request #237 from onflow/gregor/receipt-decoder
Browse files Browse the repository at this point in the history
Fix decoding issues with previous formats
  • Loading branch information
sideninja authored May 9, 2024
2 parents 6d66d37 + be22923 commit 553fca6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
39 changes: 29 additions & 10 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ import (
"errors"
"fmt"

errs "github.com/onflow/flow-evm-gateway/api/errors"
"github.com/onflow/flow-evm-gateway/config"
"github.com/onflow/flow-evm-gateway/models"
"github.com/onflow/flow-evm-gateway/services/logs"
"github.com/onflow/flow-evm-gateway/services/requester"
"github.com/onflow/flow-evm-gateway/storage"
storageErrs "github.com/onflow/flow-evm-gateway/storage/errors"
evmTypes "github.com/onflow/flow-go/fvm/evm/types"
"github.com/onflow/go-ethereum/common"
"github.com/onflow/go-ethereum/common/hexutil"
Expand All @@ -21,6 +14,14 @@ import (
"github.com/onflow/go-ethereum/eth/filters"
"github.com/onflow/go-ethereum/rpc"
"github.com/rs/zerolog"

errs "github.com/onflow/flow-evm-gateway/api/errors"
"github.com/onflow/flow-evm-gateway/config"
"github.com/onflow/flow-evm-gateway/models"
"github.com/onflow/flow-evm-gateway/services/logs"
"github.com/onflow/flow-evm-gateway/services/requester"
"github.com/onflow/flow-evm-gateway/storage"
storageErrs "github.com/onflow/flow-evm-gateway/storage/errors"
)

func SupportedAPIs(blockChainAPI *BlockChainAPI, streamAPI *StreamAPI, pullAPI *PullAPI) []rpc.API {
Expand Down Expand Up @@ -281,7 +282,12 @@ func (b *BlockChainAPI) GetBlockByHash(
return handleError[*Block](b.logger, err)
}

return b.prepareBlockResponse(ctx, block, fullTx)
apiBlock, err := b.prepareBlockResponse(ctx, block, fullTx)
if err != nil {
return handleError[*Block](b.logger, err)
}

return apiBlock, nil
}

// GetBlockByNumber returns the requested canonical block.
Expand Down Expand Up @@ -311,7 +317,12 @@ func (b *BlockChainAPI) GetBlockByNumber(
return handleError[*Block](b.logger, err)
}

return b.prepareBlockResponse(ctx, block, fullTx)
apiBlock, err := b.prepareBlockResponse(ctx, block, fullTx)
if err != nil {
return handleError[*Block](b.logger, err)
}

return apiBlock, nil
}

// GetBlockReceipts returns the block receipts for the given block hash or number or tag.
Expand Down Expand Up @@ -551,7 +562,7 @@ func handleError[T any](log zerolog.Logger, err error) (T, error) {
return zero, nil
}

log.Error().Err(err).Msg("failed to get latest block height")
log.Error().Err(err).Msg("api error")
return zero, errs.ErrInternal
}

Expand All @@ -565,6 +576,14 @@ func (b *BlockChainAPI) fetchBlockTransactions(
if err != nil {
return nil, err
}
if transaction == nil {
b.logger.Warn().
Str("tx-hash", txHash.String()).
Uint64("evm-height", block.Height).
Msg("not found a transaction the block references")

continue
}
transactions = append(transactions, transaction)
}

Expand Down
6 changes: 6 additions & 0 deletions models/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ func UnmarshalTransaction(value []byte) (Transaction, error) {

tx := &gethTypes.Transaction{}
if err := tx.UnmarshalBinary(value[1:]); err != nil {
// todo remove this after previewnet is reset
// breaking change on transaction data, try without type
if err := tx.UnmarshalBinary(value); err == nil {
return TransactionCall{Transaction: tx}, nil
}

return nil, fmt.Errorf("failed to rlp decode transaction: %w", err)
}

Expand Down
19 changes: 13 additions & 6 deletions storage/pebble/receipts.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"sync"

"github.com/cockroachdb/pebble"
"github.com/onflow/flow-evm-gateway/models"
"github.com/onflow/flow-evm-gateway/storage"
errs "github.com/onflow/flow-evm-gateway/storage/errors"
"github.com/onflow/go-ethereum/common"
gethTypes "github.com/onflow/go-ethereum/core/types"
"github.com/onflow/go-ethereum/rlp"

"github.com/onflow/flow-evm-gateway/models"
"github.com/onflow/flow-evm-gateway/storage"
errs "github.com/onflow/flow-evm-gateway/storage/errors"
)

var _ storage.ReceiptIndexer = &Receipts{}
Expand Down Expand Up @@ -138,9 +139,15 @@ func (r *Receipts) getByBlockHeight(height []byte) ([]*gethTypes.Receipt, error)
}

var storeReceipts []*models.StorageReceipt
err = rlp.DecodeBytes(val, &storeReceipts)
if err != nil {
return nil, err
if err = rlp.DecodeBytes(val, &storeReceipts); err != nil {
// todo remove this after previewnet is reset
// try to decode single receipt (breaking change migration)
var storeReceipt models.StorageReceipt
if err = rlp.DecodeBytes(val, &storeReceipt); err != nil {
return nil, err
}

storeReceipts = []*models.StorageReceipt{&storeReceipt}
}

receipts := make([]*gethTypes.Receipt, len(storeReceipts))
Expand Down

0 comments on commit 553fca6

Please sign in to comment.