Skip to content

Commit 6afa54b

Browse files
committed
Make synchronizer use the received MutableObject in the validation of ops, instead of lifting one from the database.
1 parent aa96cc8 commit 6afa54b

File tree

4 files changed

+31
-28
lines changed

4 files changed

+31
-28
lines changed

src/data/model/MutableObject.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ abstract class MutableObject extends HashedObject {
502502
}
503503

504504
createSyncAgent(peerGroupAgent: PeerGroupAgent) : StateSyncAgent {
505-
return new HeaderBasedSyncAgent(peerGroupAgent, this.getLastHash(), this.getResources() as Resources, this._acceptedMutationOpClasses, this.getSyncAgentStateFilter());
505+
return new HeaderBasedSyncAgent(peerGroupAgent, this, this.getResources() as Resources, this._acceptedMutationOpClasses, this.getSyncAgentStateFilter());
506506
//return new TerminalOpsSyncAgent(peerGroupAgent, this.getLastHash(), this.getStore(), this._acceptedMutationOpClasses);
507507
}
508508

src/mesh/agents/state/HeaderBasedSyncAgent.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Hash, HashedObject, HashedSet, Literal, LiteralUtils, MutationOp } from 'data/model';
1+
import { Hash, HashedObject, HashedSet, Literal, LiteralUtils, MutableObject, MutationOp } from 'data/model';
22
import { AgentPod } from 'mesh/service/AgentPod';
33
import { Store } from 'storage/store';
44
import { Logger, LogLevel } from 'util/logging';
@@ -27,7 +27,8 @@ class HeaderBasedSyncAgent extends PeeringAgentBase implements StateSyncAgent {
2727

2828
static MaxRequestsPerRemote = 2;
2929

30-
mutableObj: Hash;
30+
mutableObj: MutableObject;
31+
mutableObjHash: Hash;
3132
acceptedMutationOpClasses: string[];
3233
stateOpFilter?: StateFilter;
3334

@@ -50,10 +51,11 @@ class HeaderBasedSyncAgent extends PeeringAgentBase implements StateSyncAgent {
5051
controlLog: Logger;
5152
messageLog: Logger;
5253

53-
constructor(peerGroupAgent: PeerGroupAgent, mutableObj: Hash, resources: Resources, acceptedMutationOpClasses : string[], stateOpFilter?: StateFilter) {
54+
constructor(peerGroupAgent: PeerGroupAgent, mutableObj: MutableObject, resources: Resources, acceptedMutationOpClasses : string[], stateOpFilter?: StateFilter) {
5455
super(peerGroupAgent);
5556

56-
this.mutableObj = mutableObj;
57+
this.mutableObj = mutableObj;
58+
this.mutableObjHash = mutableObj.hash();
5759
this.acceptedMutationOpClasses = acceptedMutationOpClasses;
5860
this.stateOpFilter = stateOpFilter;
5961

@@ -75,7 +77,7 @@ class HeaderBasedSyncAgent extends PeeringAgentBase implements StateSyncAgent {
7577

7678

7779
getAgentId(): string {
78-
return HeaderBasedSyncAgent.syncAgentIdFor(this.mutableObj, this.peerGroupAgent.peerGroupId);
80+
return HeaderBasedSyncAgent.syncAgentIdFor(this.mutableObjHash, this.peerGroupAgent.peerGroupId);
7981
}
8082

8183
ready(pod: AgentPod): void {
@@ -111,7 +113,7 @@ class HeaderBasedSyncAgent extends PeeringAgentBase implements StateSyncAgent {
111113

112114
let isNew = false;
113115

114-
if (state instanceof HeaderBasedState && state.mutableObj === this.mutableObj) {
116+
if (state instanceof HeaderBasedState && state.mutableObj === this.mutableObjHash) {
115117

116118

117119

@@ -174,18 +176,18 @@ class HeaderBasedSyncAgent extends PeeringAgentBase implements StateSyncAgent {
174176
// Monitoring local state for changes:
175177

176178
watchStoreForOps() {
177-
this.store.watchReferences('targetObject', this.mutableObj, this.opCallback);
179+
this.store.watchReferences('targetObject', this.mutableObjHash, this.opCallback);
178180
}
179181

180182
unwatchStoreForOps() {
181-
this.store.removeReferencesWatch('targetObject', this.mutableObj, this.opCallback);
183+
this.store.removeReferencesWatch('targetObject', this.mutableObjHash, this.opCallback);
182184
}
183185

184186
async opCallback(opHash: Hash): Promise<void> {
185187

186188
if (this.terminated) return;
187189

188-
this.controlLog.trace('Op ' + opHash + ' found for object ' + this.mutableObj + ' in peer ' + this.peerGroupAgent.getLocalPeer().endpoint);
190+
this.controlLog.trace('Op ' + opHash + ' found for object ' + this.mutableObjHash + ' in peer ' + this.peerGroupAgent.getLocalPeer().endpoint);
189191

190192
let op = await this.store.load(opHash) as MutationOp;
191193
if (this.shouldAcceptMutationOp(op)) {
@@ -202,7 +204,7 @@ class HeaderBasedSyncAgent extends PeeringAgentBase implements StateSyncAgent {
202204
const fields = LiteralUtils.getFields(literal);
203205
const className = LiteralUtils.getClassName(literal);
204206

205-
if (fields['targetObject'] !== undefined && fields['targetObject']._hash === this.mutableObj &&
207+
if (fields['targetObject'] !== undefined && fields['targetObject']._hash === this.mutableObjHash &&
206208
this.acceptedMutationOpClasses.indexOf(className) >= 0) {
207209

208210
valid = true;
@@ -229,7 +231,7 @@ class HeaderBasedSyncAgent extends PeeringAgentBase implements StateSyncAgent {
229231
}*/
230232

231233
private async loadStateFromStore(): Promise<HeaderBasedState> {
232-
let terminalOpsInfo = await this.store.loadTerminalOpsForMutable(this.mutableObj);
234+
let terminalOpsInfo = await this.store.loadTerminalOpsForMutable(this.mutableObjHash);
233235

234236
if (terminalOpsInfo === undefined) {
235237
terminalOpsInfo = {terminalOps: []};
@@ -244,7 +246,7 @@ class HeaderBasedSyncAgent extends PeeringAgentBase implements StateSyncAgent {
244246

245247
terminalOpHeaders.push(terminalOpHeader as OpHeader)
246248
}
247-
const state = HeaderBasedState.create(this.mutableObj, terminalOpHeaders);
249+
const state = HeaderBasedState.create(this.mutableObjHash, terminalOpHeaders);
248250

249251
if (this.stateOpFilter === undefined) {
250252
return state;
@@ -257,7 +259,7 @@ class HeaderBasedSyncAgent extends PeeringAgentBase implements StateSyncAgent {
257259
const stateHash = state.hash();
258260

259261
if (this.stateHash === undefined || this.stateHash !== stateHash) {
260-
HeaderBasedSyncAgent.controlLog.trace('Found new state ' + stateHash + ' for ' + this.mutableObj + ' in ' + this.peerGroupAgent.getLocalPeer().endpoint);
262+
HeaderBasedSyncAgent.controlLog.trace('Found new state ' + stateHash + ' for ' + this.mutableObjHash + ' in ' + this.peerGroupAgent.getLocalPeer().endpoint);
261263
this.state = state;
262264
this.stateHash = stateHash;
263265
this.stateOpHeadersByOpHash = new Map();
@@ -278,17 +280,17 @@ class HeaderBasedSyncAgent extends PeeringAgentBase implements StateSyncAgent {
278280

279281
private shouldAcceptMutationOp(op: MutationOp): boolean {
280282

281-
return this.mutableObj === op.targetObject?.hash() &&
283+
return this.mutableObjHash === op.targetObject?.hash() &&
282284
this.acceptedMutationOpClasses.indexOf(op.getClassName()) >= 0;
283285
}
284286

285287
async lastStoredOpsDescription(limit=25) {
286288

287-
const load = await this.store.loadByReference('targetObject', this.mutableObj, { order: 'desc', limit: limit});
289+
const load = await this.store.loadByReference('targetObject', this.mutableObjHash, { order: 'desc', limit: limit});
288290

289291
const last = load.objects.length === limit? 'last ' : '';
290292

291-
let contents = 'Showing ' + last + load.objects.length + ' ops in store for ' + this.mutableObj + '\n';
293+
let contents = 'Showing ' + last + load.objects.length + ' ops in store for ' + this.mutableObjHash + '\n';
292294

293295
let idx=0;
294296
for (const op of load.objects) {

src/mesh/agents/state/history/HistoryProvider.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ class HistoryProvider {
175175
requestArrivalTimestamp: Date.now()
176176
};
177177

178-
if (msg.mutableObj !== this.syncAgent.mutableObj) {
179-
const detail = 'Rejecting request ' + respInfo.request.requestId + ', mutableObj is ' + respInfo.request.mutableObj + ' but it should be ' + this.syncAgent.mutableObj;
178+
if (msg.mutableObj !== this.syncAgent.mutableObjHash) {
179+
const detail = 'Rejecting request ' + respInfo.request.requestId + ', mutableObj is ' + respInfo.request.mutableObj + ' but it should be ' + this.syncAgent.mutableObjHash;
180180
this.rejectRequest(respInfo, 'invalid-request', detail);
181181
return;
182182
} else {
@@ -285,7 +285,7 @@ class HistoryProvider {
285285

286286
// Generate history fragment to include in the response
287287

288-
const respDelta = new HistoryDelta(this.syncAgent.mutableObj, this.syncAgent.store);
288+
const respDelta = new HistoryDelta(this.syncAgent.mutableObjHash, this.syncAgent.store);
289289

290290
let maxHistory = req.maxHistory;
291291
let maxOps = req.maxLiterals;

src/mesh/agents/state/history/HistorySynchronizer.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ class HistorySynchronizer {
106106

107107
this.syncAgent = syncAgent;
108108

109-
this.localStateFragment = new HistoryFragment(this.syncAgent.mutableObj);
109+
this.localStateFragment = new HistoryFragment(this.syncAgent.mutableObjHash);
110110
this.remoteStateFragments = new Map();
111111

112-
this.discoveredHistory = new HistoryFragment(this.syncAgent.mutableObj);
112+
this.discoveredHistory = new HistoryFragment(this.syncAgent.mutableObjHash);
113113

114-
this.requestedOps = new HistoryFragment(this.syncAgent.mutableObj);
114+
this.requestedOps = new HistoryFragment(this.syncAgent.mutableObjHash);
115115

116116
this.requests = new Map();
117117

@@ -427,7 +427,7 @@ class HistorySynchronizer {
427427
const msg: RequestMsg = {
428428
type: MessageType.Request,
429429
requestId: new RNGImpl().randomHexString(128),
430-
mutableObj: this.syncAgent.mutableObj,
430+
mutableObj: this.syncAgent.mutableObjHash,
431431
mode: mode,
432432
maxLiterals: MaxLiteralsPerRequest,
433433
maxHistory: MaxHistoryPerRequest
@@ -609,7 +609,7 @@ class HistorySynchronizer {
609609
let remoteState = this.remoteStateFragments.get(remote);
610610

611611
if (remoteState === undefined) {
612-
remoteState = new HistoryFragment(this.syncAgent.mutableObj);
612+
remoteState = new HistoryFragment(this.syncAgent.mutableObjHash);
613613
this.remoteStateFragments.set(remote, remoteState);
614614
}
615615

@@ -859,6 +859,7 @@ class HistorySynchronizer {
859859
if (resp.sendingOps !== undefined && resp.sendingOps.length > 0) {
860860
reqInfo.receivedObjects = new Context();
861861
reqInfo.receivedObjects.resources = this.syncAgent.resources;
862+
reqInfo.receivedObjects.objects.set(this.syncAgent.mutableObjHash, this.syncAgent.mutableObj);
862863
}
863864

864865
if (resp.omittedObjsOwnershipProofs !== undefined &&
@@ -983,7 +984,7 @@ class HistorySynchronizer {
983984
}
984985

985986
} else {
986-
const detail = '\n'+this.logPrefix+'\nReceived op '+ literal.hash +' is not valid for mutableObj ' + this.syncAgent.mutableObj + ', in response to request ' + reqInfo.request.requestId + '(op sequence: ' + reqInfo.nextOpSequence + ')';
987+
const detail = '\n'+this.logPrefix+'\nReceived op '+ literal.hash +' is not valid for mutableObj ' + this.syncAgent.mutableObjHash + ', in response to request ' + reqInfo.request.requestId + '(op sequence: ' + reqInfo.nextOpSequence + ')';
987988
this.cancelRequest(reqInfo, 'invalid-literal', detail);
988989
return false;
989990
}
@@ -1135,7 +1136,7 @@ class HistorySynchronizer {
11351136
// Validate received history
11361137

11371138
if (resp.history !== undefined) {
1138-
receivedHistory = new HistoryFragment(this.syncAgent.mutableObj);
1139+
receivedHistory = new HistoryFragment(this.syncAgent.mutableObjHash);
11391140

11401141
// Verify all received op history literals and create a fragment from 'em:
11411142
for (const opHistoryLiteral of resp.history) {
@@ -1194,7 +1195,7 @@ class HistorySynchronizer {
11941195

11951196
// The reply MAY contain ops we didn't request, if they directly follow our stated current state.
11961197
// Make a history fragment using this additional ops to check that is indeed the case.
1197-
const additionalOpsHistory = new HistoryFragment(this.syncAgent.mutableObj);
1198+
const additionalOpsHistory = new HistoryFragment(this.syncAgent.mutableObjHash);
11981199

11991200
if (resp.sendingOps !== undefined) {
12001201
for (const hash of resp.sendingOps) {

0 commit comments

Comments
 (0)