Skip to content

Commit

Permalink
Merge pull request #704 from onflowser/fix-sdk-adapter-block-payload
Browse files Browse the repository at this point in the history
Fix BlockPayload conversion in SDK adapter
  • Loading branch information
turbolent authored Jul 9, 2024
2 parents d1c6334 + 4c5574a commit 6a67c01
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 3 deletions.
36 changes: 33 additions & 3 deletions adapters/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (b *SDKAdapter) GetLatestBlock(
Height: flowBlock.Header.Height,
Timestamp: flowBlock.Header.Timestamp,
},
BlockPayload: sdk.BlockPayload{},
BlockPayload: convertBlockPayload(flowBlock.Payload),
}
return &block, sdk.BlockStatusSealed, nil
}
Expand All @@ -183,7 +183,7 @@ func (b *SDKAdapter) GetBlockByHeight(
Height: flowBlock.Header.Height,
Timestamp: flowBlock.Header.Timestamp,
},
BlockPayload: sdk.BlockPayload{},
BlockPayload: convertBlockPayload(flowBlock.Payload),
}
return &block, sdk.BlockStatusSealed, nil
}
Expand All @@ -208,11 +208,41 @@ func (b *SDKAdapter) GetBlockByID(
Height: flowBlock.Header.Height,
Timestamp: flowBlock.Header.Timestamp,
},
BlockPayload: sdk.BlockPayload{},
BlockPayload: convertBlockPayload(flowBlock.Payload),
}
return &block, sdk.BlockStatusSealed, nil
}

func convertBlockPayload(payload *flowgo.Payload) sdk.BlockPayload {
var seals []*sdk.BlockSeal
sealCount := len(payload.Seals)
if sealCount > 0 {
seals = make([]*sdk.BlockSeal, 0, sealCount)
for _, seal := range payload.Seals {
seals = append(seals, &sdk.BlockSeal{
BlockID: sdk.Identifier(seal.BlockID),
ExecutionReceiptID: sdk.Identifier(seal.ResultID),
})
}
}

var collectionGuarantees []*sdk.CollectionGuarantee
guaranteesCount := len(payload.Guarantees)
if guaranteesCount > 0 {
collectionGuarantees = make([]*sdk.CollectionGuarantee, 0, guaranteesCount)
for _, guarantee := range payload.Guarantees {
collectionGuarantees = append(collectionGuarantees, &sdk.CollectionGuarantee{
CollectionID: sdk.Identifier(guarantee.CollectionID),
})
}
}

return sdk.BlockPayload{
Seals: seals,
CollectionGuarantees: collectionGuarantees,
}
}

// GetCollectionByID gets a collection by ID.
func (b *SDKAdapter) GetCollectionByID(
_ context.Context,
Expand Down
78 changes: 78 additions & 0 deletions adapters/sdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,39 @@ func TestSDK(t *testing.T) {
Header: &flowgo.Header{
Height: 42,
},
Payload: &flowgo.Payload{
Guarantees: []*flowgo.CollectionGuarantee{
{
CollectionID: flowgo.MustHexStringToIdentifier("db94e7ef4c9e758f27f96777c61b5cca10528e9db5e7dfd3b44ffceb26b284c0"),
},
},
Seals: []*flowgo.Seal{
{
BlockID: flowgo.MustHexStringToIdentifier("890581b4ee0666d2a90b7e9212aaa37535f7bcec76f571c3402bc4bc58ee2918"),
ResultID: flowgo.MustHexStringToIdentifier("a7990b0bab754a68844de3698bb2d2c7966acb9ef65fd5a3a5be53a93a764edf"),
},
},
},
}

block := flowgosdk.Block{
BlockHeader: flowgosdk.BlockHeader{
ID: flowgosdk.Identifier{0x8c, 0x3c, 0xf9, 0x36, 0xbf, 0x2d, 0x3, 0x8d, 0x21, 0x71, 0xb4, 0x80, 0x1f, 0xba, 0x30, 0x36, 0x3c, 0xd5, 0x76, 0xc3, 0x21, 0xb4, 0x3d, 0xbd, 0xa2, 0x69, 0xa1, 0xe2, 0x7c, 0x6f, 0x58, 0x28},
Height: 42,
},
BlockPayload: flowgosdk.BlockPayload{
CollectionGuarantees: []*flowgosdk.CollectionGuarantee{
{
CollectionID: flowgosdk.HexToID("db94e7ef4c9e758f27f96777c61b5cca10528e9db5e7dfd3b44ffceb26b284c0"),
},
},
Seals: []*flowgosdk.BlockSeal{
{
BlockID: flowgosdk.HexToID("890581b4ee0666d2a90b7e9212aaa37535f7bcec76f571c3402bc4bc58ee2918"),
ExecutionReceiptID: flowgosdk.HexToID("a7990b0bab754a68844de3698bb2d2c7966acb9ef65fd5a3a5be53a93a764edf"),
},
},
},
}

//success
Expand Down Expand Up @@ -224,13 +250,39 @@ func TestSDK(t *testing.T) {
Header: &flowgo.Header{
Height: 42,
},
Payload: &flowgo.Payload{
Guarantees: []*flowgo.CollectionGuarantee{
{
CollectionID: flowgo.MustHexStringToIdentifier("db94e7ef4c9e758f27f96777c61b5cca10528e9db5e7dfd3b44ffceb26b284c0"),
},
},
Seals: []*flowgo.Seal{
{
BlockID: flowgo.MustHexStringToIdentifier("890581b4ee0666d2a90b7e9212aaa37535f7bcec76f571c3402bc4bc58ee2918"),
ResultID: flowgo.MustHexStringToIdentifier("a7990b0bab754a68844de3698bb2d2c7966acb9ef65fd5a3a5be53a93a764edf"),
},
},
},
}

block := flowgosdk.Block{
BlockHeader: flowgosdk.BlockHeader{
ID: flowgosdk.Identifier{0x8c, 0x3c, 0xf9, 0x36, 0xbf, 0x2d, 0x3, 0x8d, 0x21, 0x71, 0xb4, 0x80, 0x1f, 0xba, 0x30, 0x36, 0x3c, 0xd5, 0x76, 0xc3, 0x21, 0xb4, 0x3d, 0xbd, 0xa2, 0x69, 0xa1, 0xe2, 0x7c, 0x6f, 0x58, 0x28},
Height: 42,
},
BlockPayload: flowgosdk.BlockPayload{
CollectionGuarantees: []*flowgosdk.CollectionGuarantee{
{
CollectionID: flowgosdk.HexToID("db94e7ef4c9e758f27f96777c61b5cca10528e9db5e7dfd3b44ffceb26b284c0"),
},
},
Seals: []*flowgosdk.BlockSeal{
{
BlockID: flowgosdk.HexToID("890581b4ee0666d2a90b7e9212aaa37535f7bcec76f571c3402bc4bc58ee2918"),
ExecutionReceiptID: flowgosdk.HexToID("a7990b0bab754a68844de3698bb2d2c7966acb9ef65fd5a3a5be53a93a764edf"),
},
},
},
}

//success
Expand Down Expand Up @@ -264,13 +316,39 @@ func TestSDK(t *testing.T) {
Header: &flowgo.Header{
Height: 42,
},
Payload: &flowgo.Payload{
Guarantees: []*flowgo.CollectionGuarantee{
{
CollectionID: flowgo.MustHexStringToIdentifier("db94e7ef4c9e758f27f96777c61b5cca10528e9db5e7dfd3b44ffceb26b284c0"),
},
},
Seals: []*flowgo.Seal{
{
BlockID: flowgo.MustHexStringToIdentifier("890581b4ee0666d2a90b7e9212aaa37535f7bcec76f571c3402bc4bc58ee2918"),
ResultID: flowgo.MustHexStringToIdentifier("a7990b0bab754a68844de3698bb2d2c7966acb9ef65fd5a3a5be53a93a764edf"),
},
},
},
}

block := flowgosdk.Block{
BlockHeader: flowgosdk.BlockHeader{
ID: flowgosdk.Identifier{0x8c, 0x3c, 0xf9, 0x36, 0xbf, 0x2d, 0x3, 0x8d, 0x21, 0x71, 0xb4, 0x80, 0x1f, 0xba, 0x30, 0x36, 0x3c, 0xd5, 0x76, 0xc3, 0x21, 0xb4, 0x3d, 0xbd, 0xa2, 0x69, 0xa1, 0xe2, 0x7c, 0x6f, 0x58, 0x28},
Height: 42,
},
BlockPayload: flowgosdk.BlockPayload{
CollectionGuarantees: []*flowgosdk.CollectionGuarantee{
{
CollectionID: flowgosdk.HexToID("db94e7ef4c9e758f27f96777c61b5cca10528e9db5e7dfd3b44ffceb26b284c0"),
},
},
Seals: []*flowgosdk.BlockSeal{
{
BlockID: flowgosdk.HexToID("890581b4ee0666d2a90b7e9212aaa37535f7bcec76f571c3402bc4bc58ee2918"),
ExecutionReceiptID: flowgosdk.HexToID("a7990b0bab754a68844de3698bb2d2c7966acb9ef65fd5a3a5be53a93a764edf"),
},
},
},
}

//success
Expand Down

0 comments on commit 6a67c01

Please sign in to comment.