Skip to content

Commit 0f6b6cd

Browse files
tilenflareLukaAvbreht
authored andcommitted
Saving server state
1 parent be99005 commit 0f6b6cd

File tree

5 files changed

+99
-38
lines changed

5 files changed

+99
-38
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ coverage.out
33

44
# Logs
55
/logs
6+
7+
# build info
8+
PROJECT_BUILD_DATE
9+
PROJECT_COMMIT_HASH
10+
PROJECT_VERSION

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
github.com/bradleyjkemp/cupaloy v2.3.0+incompatible
77
github.com/ethereum/go-ethereum v1.13.15
88
github.com/flare-foundation/go-flare-common v0.1.4
9-
github.com/flare-foundation/verifier-indexer-framework v0.0.2-0.20241205095200-c7bd102cb88a
9+
github.com/flare-foundation/verifier-indexer-framework v0.0.2-0.20250107115534-4624a5839a95
1010
github.com/pkg/errors v0.9.1
1111
github.com/stretchr/testify v1.9.0
1212
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ github.com/ethereum/go-ethereum v1.13.15 h1:U7sSGYGo4SPjP6iNIifNoyIAiNjrmQkz6EwQ
2323
github.com/ethereum/go-ethereum v1.13.15/go.mod h1:TN8ZiHrdJwSe8Cb6x+p0hs5CxhJZPbqB7hHkaUXcmIU=
2424
github.com/flare-foundation/go-flare-common v0.1.4 h1:h/Xn56QkFwKhBjBkfpWG0qfPoHcez+OJHXbplRE+Q9g=
2525
github.com/flare-foundation/go-flare-common v0.1.4/go.mod h1:Pb1OAtNe8jNsD2fh6syJD6kVdqxAAXWESTGgd7uQlME=
26-
github.com/flare-foundation/verifier-indexer-framework v0.0.2-0.20241205095200-c7bd102cb88a h1:emHRp5TYgAeicX/nCqPa5ngG7ggK7KaGrvk4wiglb4s=
27-
github.com/flare-foundation/verifier-indexer-framework v0.0.2-0.20241205095200-c7bd102cb88a/go.mod h1:Ae/btCPhOtXEWf8Ua9V90mArNV5XPErUusDm/dgelSA=
26+
github.com/flare-foundation/verifier-indexer-framework v0.0.2-0.20250107115534-4624a5839a95 h1:XWrVdT4OWDFWsixVw8Rfv5uUdQArRyaSKM1J6XWg+Cg=
27+
github.com/flare-foundation/verifier-indexer-framework v0.0.2-0.20250107115534-4624a5839a95/go.mod h1:Ae/btCPhOtXEWf8Ua9V90mArNV5XPErUusDm/dgelSA=
2828
github.com/holiman/uint256 v1.2.4 h1:jUc4Nk8fm9jZabQuqr2JzednajVmBpC+oiTiXZJEApU=
2929
github.com/holiman/uint256 v1.2.4/go.mod h1:EOMSn4q6Nyt9P6efbI3bueV4e1b3dGlUCXeiRV4ng7E=
3030
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=

internal/xrp/xrp.go

Lines changed: 75 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,23 @@ type XRPClient struct {
4242
Headers http.Header
4343
}
4444

45-
type LedgerRequest struct {
46-
Method string `json:"method"`
47-
Params []XRPParamas `json:"params"`
45+
type XRPRequest struct {
46+
Method string `json:"method"`
47+
Params []interface{} `json:"params"`
4848
}
4949

50-
type XRPParamas struct {
50+
type LedgerParams struct {
5151
LedgerIndex string `json:"ledger_index"`
5252
Transactions bool `json:"transactions"`
5353
Expand bool `json:"expand"`
5454
OwnerFunds bool `json:"owner_funds"`
5555
}
5656

5757
type LedgerResponse struct {
58-
Result XRPResult `json:"result"`
58+
Result LedgerResult `json:"result"`
5959
}
6060

61-
type XRPResult struct {
61+
type LedgerResult struct {
6262
LedgerIndex uint64 `json:"ledger_index"`
6363
LedgerHash string `json:"ledger_hash"`
6464
Validated bool `json:"validated"`
@@ -101,21 +101,39 @@ type XRPFields struct {
101101
Balance json.RawMessage `json:"Balance"`
102102
}
103103

104-
var getLatestStruct LedgerRequest
104+
type ServerInfoRequest struct {
105+
Method string `json:"method"`
106+
}
107+
108+
type ServerInfoResponse struct {
109+
Result ServerInfoResult `json:"result"`
110+
}
111+
112+
type ServerInfoResult struct {
113+
Info ServerInfo `json:"info"`
114+
}
115+
116+
type ServerInfo struct {
117+
BuildVersion string `json:"build_version"`
118+
ServerState string `json:"server_state"`
119+
}
120+
121+
var getLatestParams LedgerParams
122+
var getServerState XRPRequest
105123

106124
func init() {
107-
getLatestStruct = LedgerRequest{
108-
Method: "ledger",
109-
Params: []XRPParamas{{
110-
LedgerIndex: "validated",
111-
Transactions: false,
112-
Expand: false,
113-
OwnerFunds: false,
114-
}},
125+
getLatestParams = LedgerParams{
126+
LedgerIndex: "validated",
127+
Transactions: false,
128+
Expand: false,
129+
OwnerFunds: false,
130+
}
131+
getServerState = XRPRequest{
132+
Method: "server_info",
115133
}
116134
}
117135

118-
func (c XRPClient) GetLedgerResponse(ctx context.Context, request LedgerRequest) (*LedgerResponse, error) {
136+
func (c XRPClient) GetResponse(ctx context.Context, request XRPRequest) ([]byte, error) {
119137
getReq, err := json.Marshal(request)
120138
if err != nil {
121139
return nil, err
@@ -144,6 +162,20 @@ func (c XRPClient) GetLedgerResponse(ctx context.Context, request LedgerRequest)
144162
return nil, err
145163
}
146164

165+
return resBody, nil
166+
}
167+
168+
func (c XRPClient) GetLedgerResponse(ctx context.Context, params LedgerParams) (*LedgerResponse, error) {
169+
request := XRPRequest{
170+
Method: "ledger",
171+
Params: []interface{}{params},
172+
}
173+
174+
resBody, err := c.GetResponse(ctx, request)
175+
if err != nil {
176+
return nil, err
177+
}
178+
147179
var respStruct LedgerResponse
148180
err = json.Unmarshal(resBody, &respStruct)
149181
if err != nil {
@@ -154,7 +186,7 @@ func (c XRPClient) GetLedgerResponse(ctx context.Context, request LedgerRequest)
154186
}
155187

156188
func (c XRPClient) GetLatestBlockInfo(ctx context.Context) (*indexer.BlockInfo, error) {
157-
respStruct, err := c.GetLedgerResponse(ctx, getLatestStruct)
189+
respStruct, err := c.GetLedgerResponse(ctx, getLatestParams)
158190
if err != nil {
159191
return nil, err
160192
}
@@ -166,16 +198,13 @@ func (c XRPClient) GetLatestBlockInfo(ctx context.Context) (*indexer.BlockInfo,
166198
}
167199

168200
func (c XRPClient) GetBlockTimestamp(ctx context.Context, blockNum uint64) (uint64, error) {
169-
getBlockStruct := LedgerRequest{
170-
Method: "ledger",
171-
Params: []XRPParamas{{
172-
LedgerIndex: strconv.Itoa(int(blockNum)),
173-
Transactions: false,
174-
Expand: false,
175-
OwnerFunds: false,
176-
}},
201+
getBlockParams := LedgerParams{
202+
LedgerIndex: strconv.Itoa(int(blockNum)),
203+
Transactions: false,
204+
Expand: false,
205+
OwnerFunds: false,
177206
}
178-
respStruct, err := c.GetLedgerResponse(ctx, getBlockStruct)
207+
respStruct, err := c.GetLedgerResponse(ctx, getBlockParams)
179208
if err != nil {
180209
return 0, err
181210
}
@@ -185,16 +214,13 @@ func (c XRPClient) GetBlockTimestamp(ctx context.Context, blockNum uint64) (uint
185214

186215
func (c XRPClient) GetBlockResult(ctx context.Context, blockNum uint64,
187216
) (*indexer.BlockResult[Block, Transaction], error) {
188-
getBlockStruct := LedgerRequest{
189-
Method: "ledger",
190-
Params: []XRPParamas{{
191-
LedgerIndex: strconv.Itoa(int(blockNum)),
192-
Transactions: true,
193-
Expand: true,
194-
OwnerFunds: false,
195-
}},
217+
getBlockParams := LedgerParams{
218+
LedgerIndex: strconv.Itoa(int(blockNum)),
219+
Transactions: true,
220+
Expand: true,
221+
OwnerFunds: false,
196222
}
197-
respStruct, err := c.GetLedgerResponse(ctx, getBlockStruct)
223+
respStruct, err := c.GetLedgerResponse(ctx, getBlockParams)
198224
if err != nil {
199225
return nil, err
200226
}
@@ -329,3 +355,17 @@ func sourceAddressesRoot(tx XRPTransaction) (string, error) {
329355

330356
return "", nil
331357
}
358+
359+
func (c XRPClient) GetServerInfo(ctx context.Context) (string, error) {
360+
resBody, err := c.GetResponse(ctx, getServerState)
361+
if err != nil {
362+
return "", err
363+
}
364+
var respStruct ServerInfoResponse
365+
err = json.Unmarshal(resBody, &respStruct)
366+
if err != nil {
367+
return "", err
368+
}
369+
370+
return respStruct.Result.Info.BuildVersion + "_" + respStruct.Result.Info.ServerState, nil
371+
}

internal/xrp/xrp_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package xrp_test
22

33
import (
44
"context"
5+
"strconv"
56
"testing"
67
"time"
78

@@ -61,3 +62,18 @@ func TestGetBlockResult(t *testing.T) {
6162

6263
cupaloy.SnapshotT(t, blockResult)
6364
}
65+
66+
func TestGetServerInfo(t *testing.T) {
67+
cfg := xrp.Config{"https://s.altnet.rippletest.net:51234"}
68+
69+
chainClient, err := xrp.New(&cfg)
70+
require.NoError(t, err)
71+
72+
ctx, cancelFunc := context.WithTimeout(context.Background(), 3*time.Second)
73+
serverInfo, err := chainClient.GetServerInfo(ctx)
74+
cancelFunc()
75+
require.NoError(t, err)
76+
77+
_, err = strconv.Atoi(serverInfo[0:1])
78+
require.NoError(t, err)
79+
}

0 commit comments

Comments
 (0)