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_update.go
151 lines (130 loc) · 3.46 KB
/
action_update.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
package holochain
import (
. "github.com/holochain/holochain-proto/hash"
peer "github.com/libp2p/go-libp2p-peer"
)
//------------------------------------------------------------
// Mod
type APIFnMod struct {
action ActionMod
}
func (fn *APIFnMod) Name() string {
return fn.action.Name()
}
func (fn *APIFnMod) Args() []Arg {
return []Arg{{Name: "entryType", Type: StringArg}, {Name: "entry", Type: EntryArg}, {Name: "replaces", Type: HashArg}}
}
func (fn *APIFnMod) Call(h *Holochain) (response interface{}, err error) {
a := &fn.action
response, err = h.commitAndShare(a, a.replaces)
return
}
type ActionMod struct {
entryType string
entry Entry
header *Header
replaces Hash
}
func NewModAction(entryType string, entry Entry, replaces Hash) *ActionMod {
a := ActionMod{entryType: entryType, entry: entry, replaces: replaces}
return &a
}
func (a *ActionMod) Entry() Entry {
return a.entry
}
func (a *ActionMod) EntryType() string {
return a.entryType
}
func (a *ActionMod) Name() string {
return "mod"
}
func (a *ActionMod) SetHeader(header *Header) {
a.header = header
}
func (a *ActionMod) GetHeader() (header *Header) {
return a.header
}
func (a *ActionMod) Share(h *Holochain, def *EntryDef) (err error) {
if def.isSharingPublic() {
// if it's a public entry send the DHT MOD & PUT messages
// TODO handle errors better!!
h.dht.Change(a.header.EntryLink, PUT_REQUEST, HoldReq{EntryHash: a.header.EntryLink})
h.dht.Change(a.replaces, MOD_REQUEST, HoldReq{RelatedHash: a.replaces, EntryHash: a.header.EntryLink})
}
return
}
func (a *ActionMod) SysValidation(h *Holochain, def *EntryDef, pkg *Package, sources []peer.ID) (err error) {
switch def.Name {
case DNAEntryType:
err = ErrNotValidForDNAType
return
case HeadersEntryType:
err = ErrNotValidForHeadersType
return
case DelEntryType:
err = ErrNotValidForDelType
return
case KeyEntryType:
case AgentEntryType:
}
if def.DataFormat == DataFormatLinks {
err = ErrModInvalidForLinks
return
}
if a.entry == nil {
err = ErrNilEntryInvalid
return
}
if a.header == nil {
err = ErrModMissingHeader
return
}
if a.replaces.String() == a.header.EntryLink.String() {
err = ErrModReplacesHashNotDifferent
return
}
// no need to check for virtual entries on the chain because they aren't there
// currently the only virtual entry is the node id
/*
if !def.IsVirtualEntry() {
var header *Header
header, err = h.chain.GetEntryHeader(a.replaces)
if err != nil {
return
}
if header.Type != a.entryType {
err = ErrEntryTypeMismatch
return
}
}*/
err = sysValidateEntry(h, def, a.entry, pkg)
return
}
func (a *ActionMod) Receive(dht *DHT, msg *Message) (response interface{}, err error) {
//var hashStatus int
t := msg.Body.(HoldReq)
var holdResp *HoldResp
err = RunValidationPhase(dht.h, msg.From, VALIDATE_MOD_REQUEST, t.EntryHash, func(resp ValidateResponse) error {
a := NewModAction(resp.Type, &resp.Entry, t.RelatedHash)
a.header = &resp.Header
//@TODO what comes back from Validate Mod
_, err = dht.h.ValidateAction(a, resp.Type, &resp.Package, []peer.ID{msg.From})
if err != nil {
// how do we record an invalid Mod?
//@TODO store as REJECTED?
} else {
err = dht.Mod(msg, t.RelatedHash, t.EntryHash)
if err == nil {
holdResp, err = dht.MakeHoldResp(msg, StatusLive)
}
}
return err
})
if holdResp != nil {
response = *holdResp
}
return
}
func (a *ActionMod) CheckValidationRequest(def *EntryDef) (err error) {
return
}