Skip to content

Commit

Permalink
feat(explorer/graphql): xmsg query (#830)
Browse files Browse the repository at this point in the history
xmsg(sourceChainID, destChainID, streamOffset)

task: https://app.asana.com/0/1206208509925075/1206970544933854
  • Loading branch information
DanFlannel authored Apr 11, 2024
1 parent e8362e9 commit f7698fd
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 3 deletions.
7 changes: 7 additions & 0 deletions explorer/graphql/app/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ type XMsg {

"Hash of the source chain block"
BlockHash: Bytes32!

"XBlock message was emitted in"
Block: XBlock!

"Receipts of the message"
Receipts: [XReceipt!]!
}

"""
Expand Down Expand Up @@ -127,4 +133,5 @@ type Query {
xmsgrange(from: BigInt!, to: BigInt!): [XMsg]!
xmsgcount: BigInt
xreceiptcount: BigInt
xmsg(sourceChainID: BigInt!, destChainID: BigInt! streamOffset: BigInt!): XMsg
}
41 changes: 38 additions & 3 deletions explorer/graphql/data/message_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
func (p Provider) XMsgCount(ctx context.Context) (*hexutil.Big, bool, error) {
query, err := p.EntClient.Msg.Query().Count(ctx)
if err != nil {
log.Error(ctx, "Graphql provider err", err)
log.Error(ctx, "Msg count query", err)
return nil, false, err
}

Expand All @@ -35,18 +35,53 @@ func (p Provider) XMsgRange(ctx context.Context, from uint64, to uint64) ([]*res
Limit(int(amount)).
All(ctx)
if err != nil {
log.Error(ctx, "Ent query", err)
log.Error(ctx, "Msg range query", err)
return nil, false, err
}

var res []*resolvers.XMsg
for _, m := range query {
graphQL, err := EntMsgToGraphQLXMsg(ctx, m, nil)
if err != nil {
return nil, false, errors.Wrap(err, " decode message")
return nil, false, errors.Wrap(err, "decode message")
}
res = append(res, graphQL)
}

return res, true, nil
}

func (p Provider) XMsg(ctx context.Context, sourceChainID, destChainID, streamOffset uint64) (*resolvers.XMsg, bool, error) {
query, err := p.EntClient.Msg.Query().
Where(
msg.SourceChainID(sourceChainID),
msg.DestChainID(destChainID),
msg.StreamOffset(streamOffset),
).
First(ctx)
if err != nil {
log.Error(ctx, "Msg query", err)
return nil, false, err
}

block := query.QueryBlock().OnlyX(ctx)
receipts := query.QueryReceipts().AllX(ctx)

res, err := EntMsgToGraphQLXMsg(ctx, query, block)
if err != nil {
return nil, false, errors.Wrap(err, "decoding message")
}

var receiptsRes []resolvers.XReceipt
for _, r := range receipts {
receipt, err := EntReceiptToGraphQLXReceipt(r)
if err != nil {
return nil, false, errors.Wrap(err, "decoding receipt")
}
receiptsRes = append(receiptsRes, *receipt)
}

res.Receipts = receiptsRes

return res, true, nil
}
1 change: 1 addition & 0 deletions explorer/graphql/resolvers/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type BlocksProvider interface {
XMsgCount(ctx context.Context) (*hexutil.Big, bool, error)
XReceiptCount(ctx context.Context) (*hexutil.Big, bool, error)
XMsgRange(ctx context.Context, Amount uint64, Offset uint64) ([]*XMsg, bool, error)
XMsg(ctx context.Context, SourceChainID uint64, DestChainID uint64, StreamOffset uint64) (*XMsg, bool, error)
}

type BlocksResolver struct {
Expand Down
18 changes: 18 additions & 0 deletions explorer/graphql/resolvers/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ type XMsgRangeArgs struct {
To hexutil.Big
}

type XMsgArgs struct {
SourceChainID hexutil.Big
DestChainID hexutil.Big
StreamOffset hexutil.Big
}

func (b *BlocksResolver) XMsgCount(ctx context.Context) (*hexutil.Big, error) {
res, found, err := b.BlocksProvider.XMsgCount(ctx)
if err != nil {
Expand Down Expand Up @@ -40,3 +46,15 @@ func (b *BlocksResolver) XMsgRange(ctx context.Context, args XMsgRangeArgs) ([]*

return res, nil
}

func (b *BlocksResolver) XMsg(ctx context.Context, args XMsgArgs) (*XMsg, error) {
res, found, err := b.BlocksProvider.XMsg(ctx, args.SourceChainID.ToInt().Uint64(), args.DestChainID.ToInt().Uint64(), args.StreamOffset.ToInt().Uint64())
if err != nil {
return nil, errors.New("failed to fetch message")
}
if !found {
return nil, errors.New("message not found")
}

return res, nil
}
51 changes: 51 additions & 0 deletions explorer/graphql/resolvers/messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,54 @@ func TestXMsgRange(t *testing.T) {
},
})
}

func TestXMsg(t *testing.T) {
t.Parallel()
ctx := context.Background()
test := createGqlTest(t)
t.Cleanup(func() {
if err := test.Client.Close(); err != nil {
t.Error(err)
}
})
db.CreateTestBlocks(t, ctx, test.Client, 2)

gqltesting.RunTests(t, []*gqltesting.Test{
{
Context: ctx,
Schema: graphql.MustParseSchema(app.Schema, &resolvers.Query{BlocksResolver: test.Resolver}, test.Opts...),
Query: `
{
xmsg(sourceChainID: 1, destChainID: 2, streamOffset: 0){
SourceMessageSender
TxHash
BlockHash
Block {
BlockHeight
}
Receipts {
SourceChainID
}
}
}
`,
ExpectedResult: `
{
"xmsg":{
"BlockHash":"0x0000000000000000000000000103176f1b2d62675e370103176f1b2d62675e37",
"Block":{
"BlockHeight":"0x0"
},
"SourceMessageSender":"0x0102030405060708090a0b0c0d0e0f1011121314",
"TxHash":"0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20",
"Receipts":[
{
"SourceChainID":"0x1"
}
]
}
}
`,
},
})
}
2 changes: 2 additions & 0 deletions explorer/graphql/resolvers/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type XMsg struct {
TxHash common.Hash
BlockHeight hexutil.Big
BlockHash common.Hash
Block XBlock
Receipts []XReceipt
}

type XBlock struct {
Expand Down
12 changes: 12 additions & 0 deletions explorer/ui/app/graphql/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type Query = {
xblock?: Maybe<XBlock>;
xblockcount?: Maybe<Scalars['BigInt']['output']>;
xblockrange: Array<Maybe<XBlock>>;
xmsg?: Maybe<XMsg>;
xmsgcount?: Maybe<Scalars['BigInt']['output']>;
xmsgrange: Array<Maybe<XMsg>>;
xreceiptcount?: Maybe<Scalars['BigInt']['output']>;
Expand All @@ -61,6 +62,13 @@ export type QueryXblockrangeArgs = {
};


export type QueryXmsgArgs = {
destChainID: Scalars['BigInt']['input'];
sourceChainID: Scalars['BigInt']['input'];
streamOffset: Scalars['BigInt']['input'];
};


export type QueryXmsgrangeArgs = {
from: Scalars['BigInt']['input'];
to: Scalars['BigInt']['input'];
Expand Down Expand Up @@ -88,6 +96,8 @@ export type XBlock = {
/** XMsg is a cross-chain message. */
export type XMsg = {
__typename?: 'XMsg';
/** XBlock message was emitted in */
Block: XBlock;
/** Hash of the source chain block */
BlockHash: Scalars['Bytes32']['output'];
/** Height of the source chain block */
Expand All @@ -98,6 +108,8 @@ export type XMsg = {
DestChainID: Scalars['BigInt']['output'];
/** Gas limit to use for 'call' on destination chain */
DestGasLimit: Scalars['BigInt']['output'];
/** Receipts of the message */
Receipts: Array<XReceipt>;
/** Source chain ID as per https://chainlist.org/ */
SourceChainID: Scalars['BigInt']['output'];
/** Sender on source chain, set to msg.Sender */
Expand Down

0 comments on commit f7698fd

Please sign in to comment.