This repository has been archived by the owner on Sep 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
action_get.go
156 lines (142 loc) · 3.65 KB
/
action_get.go
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package holochain
import (
"errors"
"fmt"
. "github.com/holochain/holochain-proto/hash"
peer "github.com/libp2p/go-libp2p-peer"
"reflect"
)
//------------------------------------------------------------
// Get
type APIFnGet struct {
action ActionGet
}
func (fn *APIFnGet) Name() string {
return fn.action.Name()
}
func (fn *APIFnGet) Args() []Arg {
return []Arg{{Name: "hash", Type: HashArg}, {Name: "options", Type: MapArg, MapType: reflect.TypeOf(GetOptions{}), Optional: true}}
}
func callGet(h *Holochain, req GetReq, options *GetOptions) (response interface{}, err error) {
a := ActionGet{req: req, options: options}
fn := &APIFnGet{action: a}
response, err = fn.Call(h)
return
}
func (fn *APIFnGet) Call(h *Holochain) (response interface{}, err error) {
a := &fn.action
if a.options.Local {
response, err = a.getLocal(h.chain)
return
}
if a.options.Bundle {
bundle := h.Chain().BundleStarted()
if bundle == nil {
err = ErrBundleNotStarted
return
}
response, err = a.getLocal(bundle.chain)
return
}
rsp, err := h.dht.Query(a.req.H, GET_REQUEST, a.req)
if err != nil {
// follow the modified hash
if a.req.StatusMask == StatusDefault && err == ErrHashModified {
var hash Hash
hash, err = NewHash(rsp.(GetResp).FollowHash)
if err != nil {
return
}
if hash.String() == a.req.H.String() {
err = errors.New("FollowHash loop detected")
return
}
req := GetReq{H: hash, StatusMask: StatusDefault, GetMask: a.options.GetMask}
modResp, err := callGet(h, req, a.options)
if err == nil {
response = modResp
}
}
return
}
switch t := rsp.(type) {
case GetResp:
response = t
default:
err = fmt.Errorf("expected GetResp response from GET_REQUEST, got: %T", t)
return
}
return
}
type ActionGet struct {
req GetReq
options *GetOptions
}
func (a *ActionGet) Name() string {
return "get"
}
func (a *ActionGet) getLocal(chain *Chain) (resp GetResp, err error) {
var entry Entry
var entryType string
entry, entryType, err = chain.GetEntry(a.req.H)
if err != nil {
return
}
resp = GetResp{Entry: *entry.(*GobEntry)}
mask := a.options.GetMask
resp.EntryType = entryType
if (mask & GetMaskEntry) != 0 {
resp.Entry = *entry.(*GobEntry)
resp.EntryType = entryType
}
return
}
func (a *ActionGet) SysValidation(h *Holochain, def *EntryDef, pkg *Package, sources []peer.ID) (err error) {
return
}
func (a *ActionGet) Receive(dht *DHT, msg *Message) (response interface{}, err error) {
var entryData []byte
//var status int
req := msg.Body.(GetReq)
mask := req.GetMask
if mask == GetMaskDefault {
mask = GetMaskEntry
}
resp := GetResp{}
// always get the entry type despite what the mas says because we need it for the switch below.
entryData, resp.EntryType, resp.Sources, _, err = dht.Get(req.H, req.StatusMask, req.GetMask|GetMaskEntryType)
if err == nil {
if (mask & GetMaskEntry) != 0 {
switch resp.EntryType {
case DNAEntryType:
// TODO: make this add the requester to the blockedlist rather than panicing, see ticket #421
err = errors.New("nobody should actually get the DNA!")
return
case KeyEntryType:
resp.Entry = GobEntry{C: string(entryData)}
default:
var e GobEntry
err = e.Unmarshal(entryData)
if err != nil {
return
}
resp.Entry = e
}
}
} else {
if err == ErrHashModified {
resp.FollowHash = string(entryData)
} else if err == ErrHashNotFound {
closest := dht.h.node.betterPeersForHash(&req.H, msg.From, true, CloserPeerCount)
if len(closest) > 0 {
err = nil
resp := CloserPeersResp{}
resp.CloserPeers = dht.h.node.peers2PeerInfos(closest)
response = resp
return
}
}
}
response = resp
return
}