Skip to content

Commit

Permalink
refactor converters to support configurable event encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
peterargue committed May 31, 2024
1 parent 70bd377 commit 5a89b68
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 112 deletions.
32 changes: 18 additions & 14 deletions access/grpc/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,16 @@ func MessageToBlockHeader(m *entities.BlockHeader) (flow.BlockHeader, error) {
}, nil
}

func CadenceValueToMessage(value cadence.Value) ([]byte, error) {
func CadenceValueToMessage(value cadence.Value, encodingVersion flow.EventEncodingVersion) ([]byte, error) {
if encodingVersion == flow.EventEncodingVersionCCF {
b, err := ccf.Encode(value)
if err != nil {
return nil, fmt.Errorf("ccf convert: %w", err)
}

return b, nil
}

b, err := jsoncdc.Encode(value)
if err != nil {
return nil, fmt.Errorf("jsoncdc convert: %w", err)
Expand All @@ -201,10 +210,10 @@ func CadenceValueToMessage(value cadence.Value) ([]byte, error) {
return b, nil
}

func CadenceValuesToMessages(values []cadence.Value) ([][]byte, error) {
func CadenceValuesToMessages(values []cadence.Value, encodingVersion flow.EventEncodingVersion) ([][]byte, error) {
msgs := make([][]byte, len(values))
for i, val := range values {
msg, err := CadenceValueToMessage(val)
msg, err := CadenceValueToMessage(val, encodingVersion)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -333,8 +342,8 @@ func MessagesToBlockSeals(l []*entities.BlockSeal) ([]*flow.BlockSeal, error) {
return results, nil
}

func EventToMessage(e flow.Event) (*entities.Event, error) {
payload, err := CadenceValueToMessage(e.Value)
func EventToMessage(e flow.Event, encodingVersion flow.EventEncodingVersion) (*entities.Event, error) {
payload, err := CadenceValueToMessage(e.Value, encodingVersion)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -497,11 +506,11 @@ func MessageToTransaction(m *entities.Transaction) (flow.Transaction, error) {
return *t, nil
}

func TransactionResultToMessage(result flow.TransactionResult) (*access.TransactionResultResponse, error) {
func TransactionResultToMessage(result flow.TransactionResult, encodingVersion flow.EventEncodingVersion) (*access.TransactionResultResponse, error) {
eventMessages := make([]*entities.Event, len(result.Events))

for i, event := range result.Events {
eventMsg, err := EventToMessage(event)
eventMsg, err := EventToMessage(event, encodingVersion)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -624,15 +633,10 @@ func ChunkExecutionDataToMessage(

events := make([]*entities.Event, len(chunk.Events))
for i, ev := range chunk.Events {
res, err := EventToMessage(*ev)
if err != nil {
return nil, err
}

// execution data uses CCF encoding
res.Payload, err = ccf.Encode(ev.Value)
res, err := EventToMessage(*ev, flow.EventEncodingVersionCCF)
if err != nil {
return nil, fmt.Errorf("ccf convert: %w", err)
return nil, err
}

events[i] = res
Expand Down
81 changes: 45 additions & 36 deletions access/grpc/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
"time"

"github.com/onflow/cadence"
"github.com/onflow/cadence/encoding/ccf"
"github.com/onflow/flow/protobuf/go/flow/entities"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -107,10 +105,10 @@ func TestConvert_BlockHeader(t *testing.T) {
}

func TestConvert_CadenceValue(t *testing.T) {
t.Run("Valid value", func(t *testing.T) {
t.Run("Valid jsoncdc encoded value", func(t *testing.T) {
valueA := cadence.NewInt(42)

msg, err := CadenceValueToMessage(valueA)
msg, err := CadenceValueToMessage(valueA, flow.EventEncodingVersionJSONCDC)
require.NoError(t, err)

valueB, err := MessageToCadenceValue(msg, nil)
Expand All @@ -119,25 +117,25 @@ func TestConvert_CadenceValue(t *testing.T) {
assert.Equal(t, valueA, valueB)
})

t.Run("Invalid message", func(t *testing.T) {
msg := []byte("invalid JSON-CDC bytes")

value, err := MessageToCadenceValue(msg, nil)
assert.Error(t, err)
assert.Nil(t, value)
})

t.Run("CCF encoded value", func(t *testing.T) {
t.Run("Valid CCF encoded value", func(t *testing.T) {
valueA := cadence.NewInt(42)

msg, err := ccf.Encode(valueA)
msg, err := CadenceValueToMessage(valueA, flow.EventEncodingVersionCCF)
require.NoError(t, err)

valueB, err := MessageToCadenceValue(msg, nil)
require.NoError(t, err)

assert.Equal(t, valueA, valueB)
})

t.Run("Invalid message", func(t *testing.T) {
msg := []byte("invalid JSON-CDC bytes")

value, err := MessageToCadenceValue(msg, nil)
assert.Error(t, err)
assert.Nil(t, value)
})
}

func TestConvert_Collection(t *testing.T) {
Expand Down Expand Up @@ -210,10 +208,8 @@ func TestConvert_BlockSeals(t *testing.T) {
func TestConvert_Event(t *testing.T) {

t.Run("JSON-CDC encoded payload", func(t *testing.T) {
eventA := test.EventGenerator().
WithEncoding(entities.EventEncodingVersion_JSON_CDC_V0).
New()
msg, err := EventToMessage(eventA)
eventA := test.EventGenerator(flow.EventEncodingVersionJSONCDC).New()
msg, err := EventToMessage(eventA, flow.EventEncodingVersionJSONCDC)
require.NoError(t, err)

eventB, err := MessageToEvent(msg, nil)
Expand All @@ -227,15 +223,9 @@ func TestConvert_Event(t *testing.T) {
})

t.Run("CCF encoded payload", func(t *testing.T) {
eventA := test.EventGenerator().
WithEncoding(entities.EventEncodingVersion_CCF_V0).
New()

msg, err := EventToMessage(eventA)
require.NoError(t, err)
eventA := test.EventGenerator(flow.EventEncodingVersionCCF).New()

// explicitly re-encode the payload using CCF
msg.Payload, err = ccf.Encode(eventA.Value)
msg, err := EventToMessage(eventA, flow.EventEncodingVersionCCF)
require.NoError(t, err)

eventB, err := MessageToEvent(msg, nil)
Expand Down Expand Up @@ -301,20 +291,39 @@ func TestConvert_Transaction(t *testing.T) {
}

func TestConvert_TransactionResult(t *testing.T) {
resultA := test.TransactionResultGenerator().New()
t.Run("with JSON-CDC encoded events", func(t *testing.T) {
resultA := test.TransactionResultGenerator(flow.EventEncodingVersionJSONCDC).New()

msg, err := TransactionResultToMessage(resultA)
msg, err := TransactionResultToMessage(resultA, flow.EventEncodingVersionJSONCDC)

resultB, err := MessageToTransactionResult(msg, nil)
require.NoError(t, err)
resultB, err := MessageToTransactionResult(msg, nil)
require.NoError(t, err)

// Force evaluation of type ID, which is cached in type.
// Necessary for equality check below
for _, event := range resultB.Events {
_ = event.Value.Type().ID()
}
// Force evaluation of type ID, which is cached in type.
// Necessary for equality check below
for _, event := range resultB.Events {
_ = event.Value.Type().ID()
}

assert.Equal(t, resultA, resultB)
})

t.Run("with CCF encoded events", func(t *testing.T) {
resultA := test.TransactionResultGenerator(flow.EventEncodingVersionCCF).New()

assert.Equal(t, resultA, resultB)
msg, err := TransactionResultToMessage(resultA, flow.EventEncodingVersionCCF)

resultB, err := MessageToTransactionResult(msg, nil)
require.NoError(t, err)

// Force evaluation of type ID, which is cached in type.
// Necessary for equality check below
for _, event := range resultB.Events {
_ = event.Value.Type().ID()
}

assert.Equal(t, resultA, resultB)
})
}

func TestConvert_ExecutionData(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions access/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ func (c *BaseClient) ExecuteScriptAtLatestBlock(
opts ...grpc.CallOption,
) (cadence.Value, error) {

args, err := convert.CadenceValuesToMessages(arguments)
args, err := convert.CadenceValuesToMessages(arguments, flow.EventEncodingVersionJSONCDC)
if err != nil {
return nil, newEntityToMessageError(entityCadenceValue, err)
}
Expand All @@ -545,7 +545,7 @@ func (c *BaseClient) ExecuteScriptAtBlockID(
opts ...grpc.CallOption,
) (cadence.Value, error) {

args, err := convert.CadenceValuesToMessages(arguments)
args, err := convert.CadenceValuesToMessages(arguments, flow.EventEncodingVersionJSONCDC)
if err != nil {
return nil, newEntityToMessageError(entityCadenceValue, err)
}
Expand All @@ -572,7 +572,7 @@ func (c *BaseClient) ExecuteScriptAtBlockHeight(
opts ...grpc.CallOption,
) (cadence.Value, error) {

args, err := convert.CadenceValuesToMessages(arguments)
args, err := convert.CadenceValuesToMessages(arguments, flow.EventEncodingVersionJSONCDC)
if err != nil {
return nil, newEntityToMessageError(entityCadenceValue, err)
}
Expand Down
Loading

0 comments on commit 5a89b68

Please sign in to comment.