Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion engine/access/testutil/fixture.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func (tb *ProtocolDataFixtureBuilder) buildBlocks() ([]*flow.Block, []*flow.Head
guarantees := make([]*flow.CollectionGuarantee, tb.colPerBlock)
blockCollections := make([]*flow.Collection, tb.colPerBlock)
for j := range tb.colPerBlock {
colTxs := unittest.TransactionBodyListFixture(tb.txPerCol)
colTxs := unittest.TransactionFixtures(tb.txPerCol)
col := unittest.CompleteCollectionFromTransactions(colTxs)
guarantees[j] = col.Guarantee

Expand Down
104 changes: 104 additions & 0 deletions engine/common/rpc/convert/blocks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package convert_test
import (
"bytes"
"testing"
"time"

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

Expand Down Expand Up @@ -76,3 +78,105 @@ func TestConvertRootBlock(t *testing.T) {

assert.Equal(t, block.ID(), converted.ID())
}

// TestConvertBlockStatus tests converting protobuf BlockStatus messages to flow.BlockStatus.
func TestConvertBlockStatus(t *testing.T) {
t.Parallel()

testCases := []struct {
name string
pbStatus entities.BlockStatus
expected flow.BlockStatus
}{
{"Unknown", entities.BlockStatus_BLOCK_UNKNOWN, flow.BlockStatusUnknown},
{"Finalized", entities.BlockStatus_BLOCK_FINALIZED, flow.BlockStatusFinalized},
{"Sealed", entities.BlockStatus_BLOCK_SEALED, flow.BlockStatusSealed},
}

for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

converted := convert.MessageToBlockStatus(tc.pbStatus)
assert.Equal(t, tc.expected, converted)
})
}
}

// TestConvertBlockSeal tests converting a flow.Seal to and from a protobuf BlockSeal message.
func TestConvertBlockSeal(t *testing.T) {
t.Parallel()

seal := unittest.Seal.Fixture()

msg := convert.BlockSealToMessage(seal)
converted, err := convert.MessageToBlockSeal(msg)
require.NoError(t, err)

assert.Equal(t, seal.ID(), converted.ID())
}

// TestConvertBlockSeals tests converting multiple flow.Seal to and from protobuf BlockSeal messages.
func TestConvertBlockSeals(t *testing.T) {
t.Parallel()

seals := unittest.Seal.Fixtures(3)

msgs := convert.BlockSealsToMessages(seals)
require.Len(t, msgs, len(seals))

converted, err := convert.MessagesToBlockSeals(msgs)
require.NoError(t, err)
require.Len(t, converted, len(seals))

for i, seal := range seals {
assert.Equal(t, seal.ID(), converted[i].ID())
}
}

// TestConvertPayloadFromMessage tests converting a protobuf Block message to a flow.Payload.
func TestConvertPayloadFromMessage(t *testing.T) {
t.Parallel()

block := unittest.FullBlockFixture()
signerIDs := unittest.IdentifierListFixture(5)

msg, err := convert.BlockToMessage(block, signerIDs)
require.NoError(t, err)

payload, err := convert.PayloadFromMessage(msg)
require.NoError(t, err)

assert.Equal(t, block.Payload.Hash(), payload.Hash())
}

// TestConvertBlockTimestamp2ProtobufTime tests converting block timestamps to protobuf Timestamp format.
func TestConvertBlockTimestamp2ProtobufTime(t *testing.T) {
t.Parallel()

t.Run("convert current timestamp", func(t *testing.T) {
t.Parallel()

// Use current time in unix milliseconds
now := time.Now()
timestampMillis := uint64(now.UnixMilli())

pbTime := convert.BlockTimestamp2ProtobufTime(timestampMillis)
require.NotNil(t, pbTime)

// Convert back and verify
convertedTime := pbTime.AsTime()
assert.Equal(t, timestampMillis, uint64(convertedTime.UnixMilli()))
})

t.Run("convert zero timestamp", func(t *testing.T) {
t.Parallel()

pbTime := convert.BlockTimestamp2ProtobufTime(0)
require.NotNil(t, pbTime)

convertedTime := pbTime.AsTime()
assert.Equal(t, uint64(0), uint64(convertedTime.UnixMilli()))
})
}
17 changes: 8 additions & 9 deletions engine/common/rpc/convert/compatible_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"math/rand"
"testing"

"github.com/onflow/flow/protobuf/go/flow/entities"
"github.com/stretchr/testify/assert"

"github.com/onflow/flow-go/engine/common/rpc/convert"
Expand All @@ -22,20 +21,20 @@ func TestConvertCompatibleRange(t *testing.T) {
assert.Nil(t, convert.MessageToCompatibleRange(nil))
})

t.Run("convert range to message", func(t *testing.T) {
t.Run("roundtrip conversion", func(t *testing.T) {
t.Parallel()

startHeight := uint64(rand.Uint32())
endHeight := uint64(rand.Uint32())

comparableRange := &accessmodel.CompatibleRange{
StartHeight: startHeight,
EndHeight: endHeight,
}
expected := &entities.CompatibleRange{
original := &accessmodel.CompatibleRange{
StartHeight: startHeight,
EndHeight: endHeight,
}

msg := convert.CompatibleRangeToMessage(comparableRange)
assert.Equal(t, msg, expected)
msg := convert.CompatibleRangeToMessage(original)
converted := convert.MessageToCompatibleRange(msg)

assert.Equal(t, original, converted)
})
}
160 changes: 160 additions & 0 deletions engine/common/rpc/convert/convert_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package convert_test

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/engine/common/rpc/convert"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/utils/unittest"
)

// TestConvertIdentifier tests converting a flow.Identifier to and from a protobuf message.
func TestConvertIdentifier(t *testing.T) {
t.Parallel()

id := unittest.IdentifierFixture()

msg := convert.IdentifierToMessage(id)
converted := convert.MessageToIdentifier(msg)

assert.Equal(t, id, converted)
}

// TestConvertIdentifiers tests converting a slice of flow.Identifiers to and from protobuf messages.
func TestConvertIdentifiers(t *testing.T) {
t.Parallel()

ids := unittest.IdentifierListFixture(10)

msgs := convert.IdentifiersToMessages(ids)
converted := convert.MessagesToIdentifiers(msgs)

assert.Equal(t, ids, flow.IdentifierList(converted))
}

// TestConvertSignature tests converting a crypto.Signature to and from a protobuf message.
func TestConvertSignature(t *testing.T) {
t.Parallel()

sig := unittest.SignatureFixture()

msg := convert.SignatureToMessage(sig)
converted := convert.MessageToSignature(msg)

assert.Equal(t, sig, converted)
}

// TestConvertSignatures tests converting a slice of crypto.Signatures to and from protobuf messages.
func TestConvertSignatures(t *testing.T) {
t.Parallel()

sigs := unittest.SignaturesFixture(5)

msgs := convert.SignaturesToMessages(sigs)
converted := convert.MessagesToSignatures(msgs)

assert.Equal(t, sigs, converted)
}

// TestConvertStateCommitment tests converting a flow.StateCommitment to and from a protobuf message.
func TestConvertStateCommitment(t *testing.T) {
t.Parallel()

sc := unittest.StateCommitmentFixture()

msg := convert.StateCommitmentToMessage(sc)
converted, err := convert.MessageToStateCommitment(msg)
require.NoError(t, err)

assert.Equal(t, sc, converted)
}

// TestConvertStateCommitmentInvalidLength tests that MessageToStateCommitment returns an error
// for invalid length byte slices.
func TestConvertStateCommitmentInvalidLength(t *testing.T) {
t.Parallel()

invalidMsg := []byte{0x01, 0x02, 0x03} // Too short

_, err := convert.MessageToStateCommitment(invalidMsg)
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid state commitment length")
}

// TestConvertAggregatedSignatures tests converting a slice of flow.AggregatedSignatures to and from
// protobuf messages.
func TestConvertAggregatedSignatures(t *testing.T) {
t.Parallel()

aggSigs := unittest.Seal.AggregatedSignatureFixtures(2)

msgs := convert.AggregatedSignaturesToMessages(aggSigs)
converted := convert.MessagesToAggregatedSignatures(msgs)

assert.Equal(t, aggSigs, converted)
}

// TestConvertAggregatedSignaturesEmpty tests converting an empty slice of flow.AggregatedSignatures.
func TestConvertAggregatedSignaturesEmpty(t *testing.T) {
t.Parallel()

aggSigs := []flow.AggregatedSignature{}

msgs := convert.AggregatedSignaturesToMessages(aggSigs)
converted := convert.MessagesToAggregatedSignatures(msgs)

assert.Empty(t, converted)
}

// TestConvertChainId tests converting a valid chainId string to flow.ChainID.
func TestConvertChainId(t *testing.T) {
t.Parallel()

t.Run("valid chain IDs", func(t *testing.T) {
t.Parallel()

validChainIDs := []flow.ChainID{
flow.Mainnet,
flow.Testnet,
flow.Emulator,
flow.Localnet,
flow.Sandboxnet,
flow.Previewnet,
flow.Benchnet,
flow.BftTestnet,
flow.MonotonicEmulator,
}

for _, chainID := range validChainIDs {
t.Run(chainID.String(), func(t *testing.T) {
t.Parallel()

result, err := convert.MessageToChainId(chainID.String())
require.NoError(t, err)
require.NotNil(t, result)
assert.Equal(t, chainID, *result)
})
}
})

t.Run("invalid chain IDs", func(t *testing.T) {
t.Parallel()

invalid := []string{"invalid-chain", ""}

for _, chainID := range invalid {
t.Run(fmt.Sprintf("invalid_%q", chainID), func(t *testing.T) {
t.Parallel()

result, err := convert.MessageToChainId(chainID)
assert.Error(t, err)
assert.Nil(t, result)
assert.Contains(t, err.Error(), "invalid chainId")
})
}
})
}
16 changes: 2 additions & 14 deletions engine/common/rpc/convert/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,21 +280,9 @@ func CcfEventToJsonEvent(e flow.Event) (*flow.Event, error) {
func CcfEventsToJsonEvents(events []flow.Event) ([]flow.Event, error) {
convertedEvents := make([]flow.Event, len(events))
for i, e := range events {
payload, err := CcfPayloadToJsonPayload(e.Payload)
convertedEvent, err := CcfEventToJsonEvent(e)
if err != nil {
return nil, fmt.Errorf("failed to convert event payload for event %d: %w", i, err)
}
convertedEvent, err := flow.NewEvent(
flow.UntrustedEvent{
Type: e.Type,
TransactionID: e.TransactionID,
TransactionIndex: e.TransactionIndex,
EventIndex: e.EventIndex,
Payload: payload,
},
)
if err != nil {
return nil, fmt.Errorf("could not construct event: %w", err)
return nil, err
}
convertedEvents[i] = *convertedEvent
}
Expand Down
Loading
Loading