-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchain.go
More file actions
78 lines (61 loc) · 1.96 KB
/
chain.go
File metadata and controls
78 lines (61 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"encoding/json"
"fmt"
)
// handleChainGetHeight gets the current height of the chain
func handleChainGetHeight(registry *Registry, req Request) (Response, error) {
var params struct {
Chain RefObject `json:"chain"`
}
if err := json.Unmarshal(req.Params, ¶ms); err != nil {
return Response{}, fmt.Errorf("failed to parse params: %w", err)
}
chain, err := registry.GetChain(params.Chain.Ref)
if err != nil {
return Response{}, err
}
return NewSuccessResponse(req.ID, chain.GetHeight()), nil
}
// handleChainGetByHeight gets a block tree entry at the specified height
func handleChainGetByHeight(registry *Registry, req Request) (Response, error) {
var params struct {
Chain RefObject `json:"chain"`
BlockHeight int32 `json:"block_height"`
}
if err := json.Unmarshal(req.Params, ¶ms); err != nil {
return Response{}, fmt.Errorf("failed to parse params: %w", err)
}
if req.Ref == "" {
return Response{}, fmt.Errorf("ref field is required")
}
chain, err := registry.GetChain(params.Chain.Ref)
if err != nil {
return Response{}, err
}
entry := chain.GetByHeight(params.BlockHeight)
if entry == nil {
return NewEmptyErrorResponse(req.ID), nil
}
registry.Store(req.Ref, entry)
return NewSuccessResponseWithRef(req.ID, req.Ref), nil
}
// handleChainContains checks if a block tree entry is in the active chain
func handleChainContains(registry *Registry, req Request) (Response, error) {
var params struct {
Chain RefObject `json:"chain"`
BlockTreeEntry RefObject `json:"block_tree_entry"`
}
if err := json.Unmarshal(req.Params, ¶ms); err != nil {
return Response{}, fmt.Errorf("failed to parse params: %w", err)
}
chain, err := registry.GetChain(params.Chain.Ref)
if err != nil {
return Response{}, err
}
entry, err := registry.GetBlockTreeEntry(params.BlockTreeEntry.Ref)
if err != nil {
return Response{}, err
}
return NewSuccessResponse(req.ID, chain.Contains(entry)), nil
}