Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CCF encoding when requesting events from AccessAPI #501

Merged
merged 4 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions access/grpc/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/onflow/cadence"
"github.com/onflow/cadence/encoding/ccf"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/flow/protobuf/go/flow/access"
"github.com/onflow/flow/protobuf/go/flow/entities"
Expand Down Expand Up @@ -213,6 +214,15 @@ func cadenceValuesToMessages(values []cadence.Value) ([][]byte, error) {
}

func messageToCadenceValue(m []byte, options []jsoncdc.Option) (cadence.Value, error) {
if ccf.HasMsgPrefix(m) {
// modern Access nodes support encoding events in CCF format
v, err := ccf.Decode(nil, m)
if err == nil {
return v, nil
}
return v, nil
}

v, err := jsoncdc.Decode(nil, m, options...)
if err != nil {
return nil, fmt.Errorf("convert: %w", err)
Expand Down
58 changes: 49 additions & 9 deletions access/grpc/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ 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 @@ -124,6 +126,18 @@ func TestConvert_CadenceValue(t *testing.T) {
assert.Error(t, err)
assert.Nil(t, value)
})

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

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

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

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

func TestConvert_Collection(t *testing.T) {
Expand Down Expand Up @@ -194,19 +208,45 @@ func TestConvert_BlockSeals(t *testing.T) {
}

func TestConvert_Event(t *testing.T) {
eventA := test.EventGenerator().New()

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

eventB, err := messageToEvent(msg, nil)
require.NoError(t, err)
eventB, err := messageToEvent(msg, nil)
require.NoError(t, err)

// Force evaluation of type ID, which is cached in type.
// Necessary for equality check below
_ = eventB.Value.Type().ID()
// Force evaluation of type ID, which is cached in type.
// Necessary for equality check below
_ = eventB.Value.Type().ID()

assert.Equal(t, eventA, eventB)
})

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)

assert.Equal(t, eventA, eventB)
// explicitly re-encode the payload using CCF
msg.Payload, err = ccf.Encode(eventA.Value)
require.NoError(t, err)

eventB, err := messageToEvent(msg, nil)
require.NoError(t, err)

// Force evaluation of type ID, which is cached in type.
// Necessary for equality check below
_ = eventB.Value.Type().ID()

assert.Equal(t, eventA, eventB)
})
}

func TestConvert_Identifier(t *testing.T) {
Expand Down
24 changes: 15 additions & 9 deletions access/grpc/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/onflow/cadence"
"github.com/onflow/cadence/encoding/json"
"github.com/onflow/flow/protobuf/go/flow/access"
"github.com/onflow/flow/protobuf/go/flow/entities"

"github.com/onflow/flow-go-sdk"
)
Expand Down Expand Up @@ -315,7 +316,8 @@ func (c *BaseClient) GetTransactionResult(
opts ...grpc.CallOption,
) (*flow.TransactionResult, error) {
req := &access.GetTransactionRequest{
Id: txID.Bytes(),
Id: txID.Bytes(),
EventEncodingVersion: entities.EventEncodingVersion_CCF_V0,
}

res, err := c.rpcClient.GetTransactionResult(ctx, req, opts...)
Expand All @@ -339,8 +341,9 @@ func (c *BaseClient) GetTransactionResultByIndex(
) (*flow.TransactionResult, error) {

req := &access.GetTransactionByIndexRequest{
BlockId: blockID.Bytes(),
Index: index,
BlockId: blockID.Bytes(),
Index: index,
EventEncodingVersion: entities.EventEncodingVersion_CCF_V0,
}

res, err := c.rpcClient.GetTransactionResultByIndex(ctx, req, opts...)
Expand All @@ -362,7 +365,8 @@ func (c *BaseClient) GetTransactionResultsByBlockID(
) ([]*flow.TransactionResult, error) {

req := &access.GetTransactionsByBlockIDRequest{
BlockId: blockID.Bytes(),
BlockId: blockID.Bytes(),
EventEncodingVersion: entities.EventEncodingVersion_CCF_V0,
}

res, err := c.rpcClient.GetTransactionResultsByBlockID(ctx, req, opts...)
Expand Down Expand Up @@ -537,9 +541,10 @@ func (c *BaseClient) GetEventsForHeightRange(
opts ...grpc.CallOption,
) ([]flow.BlockEvents, error) {
req := &access.GetEventsForHeightRangeRequest{
Type: query.Type,
StartHeight: query.StartHeight,
EndHeight: query.EndHeight,
Type: query.Type,
StartHeight: query.StartHeight,
EndHeight: query.EndHeight,
EventEncodingVersion: entities.EventEncodingVersion_CCF_V0,
}

res, err := c.rpcClient.GetEventsForHeightRange(ctx, req, opts...)
Expand All @@ -557,8 +562,9 @@ func (c *BaseClient) GetEventsForBlockIDs(
opts ...grpc.CallOption,
) ([]flow.BlockEvents, error) {
req := &access.GetEventsForBlockIDsRequest{
Type: eventType,
BlockIds: identifiersToMessages(blockIDs),
Type: eventType,
BlockIds: identifiersToMessages(blockIDs),
EventEncodingVersion: entities.EventEncodingVersion_CCF_V0,
}

res, err := c.rpcClient.GetEventsForBlockIDs(ctx, req, opts...)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/onflow/cadence v0.42.1
github.com/onflow/crypto v0.24.9
github.com/onflow/flow-cli/flowkit v1.4.3
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20230628215638-83439d22e0ce
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20231018182244-e72527c55c63
github.com/onflow/sdks v0.5.0
github.com/pkg/errors v0.9.1
github.com/spf13/afero v1.10.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,8 @@ github.com/onflow/crypto v0.24.9 h1:jYP1qdwid0qCineFzBFlxBchg710A7RuSWpTqxaOdog=
github.com/onflow/crypto v0.24.9/go.mod h1:J/V7IEVaqjDajvF8K0B/SJPJDgAOP2G+LVLeb0hgmbg=
github.com/onflow/flow-cli/flowkit v1.4.3 h1:fwG20ZfVKAP2geSzImZ+LL1NhVSMLQfko5ZvicTkZv8=
github.com/onflow/flow-cli/flowkit v1.4.3/go.mod h1:AQWWUCq3UDXIEI1dG2MADyHi5ZeGE5pIvS5Fh0k/ITU=
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20230628215638-83439d22e0ce h1:YQKijiQaq8SF1ayNqp3VVcwbBGXSnuHNHq4GQmVGybE=
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20230628215638-83439d22e0ce/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20231018182244-e72527c55c63 h1:SX8OhYbyKBExhy4qEDR/Hw6MVTBTzlDb8LfCHfFyte4=
github.com/onflow/flow/protobuf/go/flow v0.3.2-0.20231018182244-e72527c55c63/go.mod h1:NA2pX2nw8zuaxfKphhKsk00kWLwfd+tv8mS23YXO4Sk=
github.com/onflow/sdks v0.5.0 h1:2HCRibwqDaQ1c9oUApnkZtEAhWiNY2GTpRD5+ftdkN8=
github.com/onflow/sdks v0.5.0/go.mod h1:F0dj0EyHC55kknLkeD10js4mo14yTdMotnWMslPirrU=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
Expand Down
28 changes: 25 additions & 3 deletions test/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ import (
"time"

"github.com/onflow/cadence"
"github.com/onflow/cadence/encoding/ccf"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow/protobuf/go/flow/entities"

"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/crypto"
Expand Down Expand Up @@ -239,8 +241,9 @@ func (g *BlockSeals) New() *flow.BlockSeal {
}

type Events struct {
count int
ids *Identifiers
count int
ids *Identifiers
encoding entities.EventEncodingVersion
}

func EventGenerator() *Events {
Expand All @@ -250,6 +253,18 @@ func EventGenerator() *Events {
}
}

func (g *Events) WithEncoding(encoding entities.EventEncodingVersion) *Events {
switch encoding {
case entities.EventEncodingVersion_CCF_V0:
g.encoding = encoding
case entities.EventEncodingVersion_JSON_CDC_V0:
g.encoding = encoding
default:
panic(fmt.Errorf("unsupported event encoding: %v", encoding))
}
return g
}

func (g *Events) New() flow.Event {
defer func() { g.count++ }()

Expand Down Expand Up @@ -280,7 +295,14 @@ func (g *Events) New() flow.Event {

typeID := location.TypeID(nil, identifier)

payload, err := jsoncdc.Encode(testEvent)
var payload []byte
var err error
if g.encoding == entities.EventEncodingVersion_CCF_V0 {
payload, err = ccf.Encode(testEvent)
} else {
payload, err = jsoncdc.Encode(testEvent)
}

if err != nil {
panic(fmt.Errorf("cannot encode test event: %w", err))
}
Expand Down