Skip to content

Commit e9846d8

Browse files
committed
improve get options
1 parent 886ff97 commit e9846d8

5 files changed

+64
-49
lines changed

client_block.go

+20-15
Original file line numberDiff line numberDiff line change
@@ -9,32 +9,37 @@ import (
99
)
1010

1111
type GetBlockOption struct {
12-
OnlyIdAndHeight bool
12+
HeaderOnlyIdHeight bool
1313
WithTransactions bool
14-
OnlyTransactionID bool
14+
WithConsensus bool
15+
TransactionOnlyID bool
1516
WithContractBytecode bool
17+
WithContractSalt bool
1618
}
1719

1820
func (o GetBlockOption) BuildIgnoreChecker() query.IgnoreChecker {
1921
var checkers []query.IgnoreChecker
20-
if o.OnlyIdAndHeight {
21-
checkers = []query.IgnoreChecker{
22-
query.IgnoreOtherFields(types.Block{}, "Id", "Header"),
23-
query.IgnoreOtherFields(types.Header{}, "Id", "Height"),
24-
}
25-
} else if o.WithTransactions {
26-
if o.OnlyTransactionID {
27-
checkers = []query.IgnoreChecker{query.IgnoreOtherFields(types.Transaction{}, "Id")}
28-
} else {
29-
// Otherwise it will create circular dependencies
30-
checkers = []query.IgnoreChecker{query.IgnoreObjects(types.SuccessStatus{}, types.FailureStatus{})}
31-
}
22+
if !o.WithTransactions {
23+
checkers = append(checkers, query.IgnoreObjects(types.Transaction{}))
24+
}
25+
if !o.WithConsensus {
26+
checkers = append(checkers, query.IgnoreField(types.Block{}, "Consensus"))
27+
}
28+
if o.HeaderOnlyIdHeight {
29+
checkers = append(checkers, query.IgnoreOtherFields(types.Header{}, "Id", "Height"))
30+
}
31+
if o.TransactionOnlyID {
32+
checkers = append(checkers, query.IgnoreOtherFields(types.Transaction{}, "Id"))
3233
} else {
33-
checkers = []query.IgnoreChecker{query.IgnoreObjects(types.Transaction{})}
34+
// Otherwise it will create circular dependencies
35+
checkers = append(checkers, query.IgnoreObjects(types.SuccessStatus{}, types.FailureStatus{}))
3436
}
3537
if !o.WithContractBytecode {
3638
checkers = append(checkers, query.IgnoreField(types.Contract{}, "Bytecode"))
3739
}
40+
if !o.WithContractSalt {
41+
checkers = append(checkers, query.IgnoreField(types.Contract{}, "Salt"))
42+
}
3843
return query.MergeIgnores(checkers...)
3944
}
4045

client_block_test.go

+31-17
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func Test_GetBlock0(t *testing.T) {
1515
cli := NewClient(testnetEndpoint)
1616
block, err := cli.GetBlock(context.Background(), types.QueryBlockParams{
1717
Height: util.GetPointer(types.U32(9758550)),
18-
}, GetBlockOption{WithTransactions: false, OnlyTransactionID: false})
18+
}, GetBlockOption{WithTransactions: false, TransactionOnlyID: false, WithConsensus: true})
1919
assert.NoError(t, err)
2020
assert.Equal(t, &types.Block{
2121
Id: types.BlockId{Hash: common.HexToHash("0x5d7f48fc777144b21ea760525936db069329dee2ccce509550c1478c1c0b5b2c")},
@@ -40,13 +40,33 @@ func Test_GetBlock0(t *testing.T) {
4040
},
4141
},
4242
}, block)
43+
44+
block, err = cli.GetBlock(context.Background(), types.QueryBlockParams{
45+
Height: util.GetPointer(types.U32(9758550)),
46+
}, GetBlockOption{WithTransactions: false, TransactionOnlyID: false})
47+
assert.NoError(t, err)
48+
assert.Equal(t, &types.Block{
49+
Id: types.BlockId{Hash: common.HexToHash("0x5d7f48fc777144b21ea760525936db069329dee2ccce509550c1478c1c0b5b2c")},
50+
Header: types.Header{
51+
Id: types.BlockId{Hash: common.HexToHash("0x5d7f48fc777144b21ea760525936db069329dee2ccce509550c1478c1c0b5b2c")},
52+
DaHeight: 5700482,
53+
TransactionsCount: 3,
54+
MessageReceiptCount: 0,
55+
TransactionsRoot: types.Bytes32{Hash: common.HexToHash("0x6acba90c0da2a5946cde70bc5d211ca06f1903b0fe7318bf7653ad4de3caf004")},
56+
MessageReceiptRoot: types.Bytes32{Hash: common.HexToHash("0xe3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855")},
57+
Height: 9758550,
58+
PrevRoot: types.Bytes32{Hash: common.HexToHash("0xe14198c9e1fbc499df5a9dacdb1219135a2d4915011962b5ac379c54b9499b83")},
59+
Time: types.Tai64Timestamp{Time: time.Date(2024, time.April, 15, 2, 44, 2, 0, time.UTC)},
60+
ApplicationHash: types.Bytes32{Hash: common.HexToHash("0xe0c1360865782cc46da4f65787896aa264e4e8812b6fdb7864cdf7ef6bf42437")},
61+
},
62+
}, block)
4363
}
4464

4565
func Test_GetBlock1(t *testing.T) {
4666
cli := NewClient(testnetEndpoint)
4767
block, err := cli.GetBlock(context.Background(), types.QueryBlockParams{
4868
Height: util.GetPointer(types.U32(9758550)),
49-
}, GetBlockOption{WithTransactions: true, OnlyTransactionID: true})
69+
}, GetBlockOption{WithTransactions: true, TransactionOnlyID: true, WithConsensus: true})
5070
assert.NoError(t, err)
5171
assert.Equal(t, &types.Block{
5272
Id: types.BlockId{Hash: common.HexToHash("0x5d7f48fc777144b21ea760525936db069329dee2ccce509550c1478c1c0b5b2c")},
@@ -84,7 +104,7 @@ func Test_GetBlock2(t *testing.T) {
84104
cli := NewClient(testnetEndpoint)
85105
block, err := cli.GetBlock(context.Background(), types.QueryBlockParams{
86106
Id: &types.BlockId{Hash: common.HexToHash("0x5d7f48fc777144b21ea760525936db069329dee2ccce509550c1478c1c0b5b2c")},
87-
}, GetBlockOption{WithTransactions: true})
107+
}, GetBlockOption{WithTransactions: true, WithConsensus: true})
88108
assert.NoError(t, err)
89109
assert.Equal(t, &types.Block{
90110
Id: types.BlockId{Hash: common.HexToHash("0x5d7f48fc777144b21ea760525936db069329dee2ccce509550c1478c1c0b5b2c")},
@@ -112,8 +132,7 @@ func Test_GetBlock2(t *testing.T) {
112132
Id: types.TransactionId{Hash: common.HexToHash("0x9b7a9353faacd4ce91c47707d66c81ec7e4d547905168a592312a94a5c67b69f")},
113133
InputAssetIds: []types.AssetId{{Hash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")}},
114134
InputContracts: []types.Contract{{
115-
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
116-
Salt: "0x572e0502c9ca4347b88a0faf5b4a36bbbbf3c4c62d4f77ea893f4be7541b42e6",
135+
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
117136
}},
118137
Policies: &types.Policies{
119138
GasPrice: util.GetPointer[types.U64](1),
@@ -132,8 +151,7 @@ func Test_GetBlock2(t *testing.T) {
132151
StateRoot: types.Bytes32{Hash: common.HexToHash("0x8f36f4ef87d3260fcbbb8b7d047bae772b12265d9c45bb11814855d57fdacee3")},
133152
TxPointer: "0094e7550005",
134153
Contract: types.Contract{
135-
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
136-
Salt: "0x572e0502c9ca4347b88a0faf5b4a36bbbbf3c4c62d4f77ea893f4be7541b42e6",
154+
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
137155
},
138156
},
139157
}, {
@@ -178,8 +196,7 @@ func Test_GetBlock2(t *testing.T) {
178196
Pc: util.GetPointer[types.U64](11640),
179197
Is: util.GetPointer[types.U64](11640),
180198
To: &types.Contract{
181-
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
182-
Salt: "0x572e0502c9ca4347b88a0faf5b4a36bbbbf3c4c62d4f77ea893f4be7541b42e6",
199+
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
183200
},
184201
Amount: util.GetPointer[types.U64](0),
185202
AssetId: &types.AssetId{Hash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")},
@@ -189,8 +206,7 @@ func Test_GetBlock2(t *testing.T) {
189206
ReceiptType: "CALL",
190207
}, {
191208
Contract: &types.Contract{
192-
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
193-
Salt: "0x572e0502c9ca4347b88a0faf5b4a36bbbbf3c4c62d4f77ea893f4be7541b42e6",
209+
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
194210
},
195211
Pc: util.GetPointer[types.U64](44000),
196212
Is: util.GetPointer[types.U64](11640),
@@ -276,17 +292,15 @@ func Test_GetBlock2(t *testing.T) {
276292
}, {
277293
Id: types.TransactionId{Hash: common.HexToHash("0x1a978dcf45d87d2793d7da58d45244d68241aa6363d6a435a38c5fdfeafff178")},
278294
InputContracts: []types.Contract{{
279-
Id: types.ContractId{Hash: common.HexToHash("0x7777777777777777777777777777777777777777777777777777777777777777")},
280-
Salt: "0x1bfd51cb31b8d0bc7d93d38f97ab771267d8786ab87073e0c2b8f9ddc44b274e",
295+
Id: types.ContractId{Hash: common.HexToHash("0x7777777777777777777777777777777777777777777777777777777777777777")},
281296
}},
282297
InputContract: &types.InputContract{
283298
UtxoId: types.UtxoId{Bytes: common.FromHex("0xae426ee0c79cac25ffe515ca4148e27086669bee7043b23cd380dce443213eff00")},
284299
BalanceRoot: types.Bytes32{Hash: common.HexToHash("0x2d19f8c34395032b25ae83bf88cd618a8598c6f2459f137c931879b323a41e04")},
285300
StateRoot: types.Bytes32{Hash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")},
286301
TxPointer: "0094e7550006",
287302
Contract: types.Contract{
288-
Id: types.ContractId{Hash: common.HexToHash("0x7777777777777777777777777777777777777777777777777777777777777777")},
289-
Salt: "0x1bfd51cb31b8d0bc7d93d38f97ab771267d8786ab87073e0c2b8f9ddc44b274e",
303+
Id: types.ContractId{Hash: common.HexToHash("0x7777777777777777777777777777777777777777777777777777777777777777")},
290304
},
291305
},
292306
MintAmount: util.GetPointer[types.U64](6379),
@@ -314,7 +328,7 @@ func Test_GetBlock3(t *testing.T) {
314328
cli := NewClient(testnetEndpoint)
315329
block, err := cli.GetBlock(context.Background(), types.QueryBlockParams{
316330
Id: &types.BlockId{Hash: common.HexToHash("0x5d7f48fc777144b21ea760525936db069329dee2ccce509550c1478c1c0b5b2c")},
317-
}, GetBlockOption{OnlyIdAndHeight: true})
331+
}, GetBlockOption{HeaderOnlyIdHeight: true})
318332
assert.NoError(t, err)
319333
assert.Equal(t, &types.Block{
320334
Id: types.BlockId{Hash: common.HexToHash("0x5d7f48fc777144b21ea760525936db069329dee2ccce509550c1478c1c0b5b2c")},
@@ -374,7 +388,7 @@ func Test_GetBlocks(t *testing.T) {
374388
Id: &types.BlockId{Hash: common.HexToHash("0x5d7f48fc777144b21ea760525936db069329dee2ccce509550c1478c1c0b5b2c")},
375389
}, {
376390
Id: &types.BlockId{Hash: common.HexToHash("0xd00bd892604b2bacff3f0dc485586105caa3826b7818a729ff049eb40d3fb26d")},
377-
}}, GetBlockOption{OnlyIdAndHeight: true})
391+
}}, GetBlockOption{HeaderOnlyIdHeight: true})
378392
assert.NoError(t, err)
379393
assert.Equal(t, []*types.Block{{
380394
Id: types.BlockId{Hash: common.HexToHash("0x5d7f48fc777144b21ea760525936db069329dee2ccce509550c1478c1c0b5b2c")},

client_chain.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (c *Client) GetLatestBlockHeight(ctx context.Context) (types.U32, error) {
4040
info, err := c.GetChain(ctx, GetChainOption{
4141
Simple: true,
4242
GetBlockOption: GetBlockOption{
43-
OnlyIdAndHeight: true,
43+
HeaderOnlyIdHeight: true,
4444
},
4545
})
4646
return info.LatestBlock.Header.Height, err

client_transaction.go

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type GetTransactionOption struct {
1111
WithReceipts bool
1212
WithStatus bool
1313
WithContractBytecode bool
14+
WithContractSalt bool
1415
}
1516

1617
func (o GetTransactionOption) BuildIgnoreChecker() query.IgnoreChecker {
@@ -22,6 +23,9 @@ func (o GetTransactionOption) BuildIgnoreChecker() query.IgnoreChecker {
2223
if !o.WithContractBytecode {
2324
ignoreCheckers = append(ignoreCheckers, query.IgnoreField(types.Contract{}, "Bytecode"))
2425
}
26+
if !o.WithContractSalt {
27+
ignoreCheckers = append(ignoreCheckers, query.IgnoreField(types.Contract{}, "Salt"))
28+
}
2529
if !o.WithReceipts {
2630
ignoreCheckers = append(ignoreCheckers, query.IgnoreField(types.Transaction{}, "Receipts"))
2731
}

client_transaction_test.go

+8-16
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ func Test_GetTransaction1(t *testing.T) {
3535
Id: types.TransactionId{Hash: common.HexToHash("0x9b7a9353faacd4ce91c47707d66c81ec7e4d547905168a592312a94a5c67b69f")},
3636
InputAssetIds: []types.AssetId{{Hash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")}},
3737
InputContracts: []types.Contract{{
38-
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
39-
Salt: "0x572e0502c9ca4347b88a0faf5b4a36bbbbf3c4c62d4f77ea893f4be7541b42e6",
38+
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
4039
}},
4140
Policies: &types.Policies{
4241
GasPrice: util.GetPointer[types.U64](1),
@@ -55,8 +54,7 @@ func Test_GetTransaction1(t *testing.T) {
5554
StateRoot: types.Bytes32{Hash: common.HexToHash("0x8f36f4ef87d3260fcbbb8b7d047bae772b12265d9c45bb11814855d57fdacee3")},
5655
TxPointer: "0094e7550005",
5756
Contract: types.Contract{
58-
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
59-
Salt: "0x572e0502c9ca4347b88a0faf5b4a36bbbbf3c4c62d4f77ea893f4be7541b42e6",
57+
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
6058
},
6159
},
6260
}, {
@@ -112,8 +110,7 @@ func Test_GetTransaction2(t *testing.T) {
112110
Id: types.TransactionId{Hash: common.HexToHash("0x9b7a9353faacd4ce91c47707d66c81ec7e4d547905168a592312a94a5c67b69f")},
113111
InputAssetIds: []types.AssetId{{Hash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")}},
114112
InputContracts: []types.Contract{{
115-
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
116-
Salt: "0x572e0502c9ca4347b88a0faf5b4a36bbbbf3c4c62d4f77ea893f4be7541b42e6",
113+
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
117114
}},
118115
Policies: &types.Policies{
119116
GasPrice: util.GetPointer[types.U64](1),
@@ -132,8 +129,7 @@ func Test_GetTransaction2(t *testing.T) {
132129
StateRoot: types.Bytes32{Hash: common.HexToHash("0x8f36f4ef87d3260fcbbb8b7d047bae772b12265d9c45bb11814855d57fdacee3")},
133130
TxPointer: "0094e7550005",
134131
Contract: types.Contract{
135-
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
136-
Salt: "0x572e0502c9ca4347b88a0faf5b4a36bbbbf3c4c62d4f77ea893f4be7541b42e6",
132+
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
137133
},
138134
},
139135
}, {
@@ -223,8 +219,7 @@ func Test_GetTransaction3(t *testing.T) {
223219
Id: types.TransactionId{Hash: common.HexToHash("0x9b7a9353faacd4ce91c47707d66c81ec7e4d547905168a592312a94a5c67b69f")},
224220
InputAssetIds: []types.AssetId{{Hash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")}},
225221
InputContracts: []types.Contract{{
226-
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
227-
Salt: "0x572e0502c9ca4347b88a0faf5b4a36bbbbf3c4c62d4f77ea893f4be7541b42e6",
222+
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
228223
}},
229224
Policies: &types.Policies{
230225
GasPrice: util.GetPointer[types.U64](1),
@@ -243,8 +238,7 @@ func Test_GetTransaction3(t *testing.T) {
243238
StateRoot: types.Bytes32{Hash: common.HexToHash("0x8f36f4ef87d3260fcbbb8b7d047bae772b12265d9c45bb11814855d57fdacee3")},
244239
TxPointer: "0094e7550005",
245240
Contract: types.Contract{
246-
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
247-
Salt: "0x572e0502c9ca4347b88a0faf5b4a36bbbbf3c4c62d4f77ea893f4be7541b42e6",
241+
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
248242
},
249243
},
250244
}, {
@@ -319,8 +313,7 @@ func Test_GetTransaction3(t *testing.T) {
319313
Pc: util.GetPointer[types.U64](11640),
320314
Is: util.GetPointer[types.U64](11640),
321315
To: &types.Contract{
322-
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
323-
Salt: "0x572e0502c9ca4347b88a0faf5b4a36bbbbf3c4c62d4f77ea893f4be7541b42e6",
316+
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
324317
},
325318
Amount: util.GetPointer[types.U64](0),
326319
AssetId: &types.AssetId{Hash: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000000")},
@@ -330,8 +323,7 @@ func Test_GetTransaction3(t *testing.T) {
330323
ReceiptType: "CALL",
331324
}, {
332325
Contract: &types.Contract{
333-
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
334-
Salt: "0x572e0502c9ca4347b88a0faf5b4a36bbbbf3c4c62d4f77ea893f4be7541b42e6",
326+
Id: types.ContractId{Hash: common.HexToHash("0xd2a93abef5c3f45f48bb9f0736ccfda4c3f32c9c57fc307ab9363ef7712f305f")},
335327
},
336328
Pc: util.GetPointer[types.U64](44000),
337329
Is: util.GetPointer[types.U64](11640),

0 commit comments

Comments
 (0)