fix: LT hash computation failing

Was trying to remove a patch that was already removed, adding a check to ensure to only remove patches that are of the SET type
This commit is contained in:
Adhiraj Singh
2021-11-23 12:23:25 +05:30
parent e222ec4151
commit b4332488b8

View File

@@ -51,21 +51,23 @@ const to64BitNetworkOrder = function(e) {
type Mac = { indexMac: Uint8Array, valueMac: Uint8Array, operation: proto.SyncdMutation.SyncdMutationSyncdOperation } type Mac = { indexMac: Uint8Array, valueMac: Uint8Array, operation: proto.SyncdMutation.SyncdMutationSyncdOperation }
const computeLtHash = (initial: Uint8Array, macs: Mac[], getPrevSetValueMac: (index: Uint8Array, internalIndex: number) => Uint8Array) => { const computeLtHash = (initial: Uint8Array, macs: Mac[], getPrevSetValueMac: (index: Uint8Array, internalIndex: number) => { valueMac: Uint8Array, operation: number }) => {
const addBuffs: ArrayBuffer[] = [] const addBuffs: ArrayBuffer[] = []
const subBuffs: ArrayBuffer[] = [] const subBuffs: ArrayBuffer[] = []
for(let i = 0; i < macs.length;i++) { for(let i = 0; i < macs.length;i++) {
const { indexMac, valueMac, operation } = macs[i] const { indexMac, valueMac, operation } = macs[i]
const subBuff = getPrevSetValueMac(indexMac, i) const subOp = getPrevSetValueMac(indexMac, i)
if(operation === proto.SyncdMutation.SyncdMutationSyncdOperation.REMOVE) { if(operation === proto.SyncdMutation.SyncdMutationSyncdOperation.REMOVE) {
if(!subBuff) { if(!subOp) {
throw new Boom('tried remove, but no buffer', { statusCode: 500 }) throw new Boom('tried remove, but no buffer', { statusCode: 500 })
} }
} else { } else {
addBuffs.push(new Uint8Array(valueMac).buffer) addBuffs.push(new Uint8Array(valueMac).buffer)
} }
if(subBuff) { if(subOp) {
subBuffs.push(new Uint8Array(subBuff).buffer) if(subOp.operation === proto.SyncdMutation.SyncdMutationSyncdOperation.SET) {
subBuffs.push(new Uint8Array(subOp.valueMac).buffer)
}
} }
} }
@@ -123,7 +125,7 @@ export const encodeSyncdPatch = async(
state.hash = computeLtHash( state.hash = computeLtHash(
state.hash, state.hash,
[ { indexMac, valueMac, operation } ], [ { indexMac, valueMac, operation } ],
(index) => [...state.mutations].reverse().find(m => Buffer.compare(m.indexMac, index) === 0)?.valueMac (index) => [...state.mutations].reverse().find(m => Buffer.compare(m.indexMac, index) === 0)
) )
state.version += 1 state.version += 1
@@ -300,10 +302,10 @@ export const decodePatches = async(
currentVersion = toNumber(version.version!) currentVersion = toNumber(version.version!)
current = computeLtHash(current, macs, (index, maxIndex) => { current = computeLtHash(current, macs, (index, maxIndex) => {
let value: Uint8Array let result: { valueMac: Uint8Array, operation: number }
for(const item of initial.mutations) { for(const item of initial.mutations) {
if(Buffer.compare(item.indexMac, index) === 0) { if(Buffer.compare(item.indexMac, index) === 0) {
value = item.valueMac result = item
} }
} }
for(const { version, mutations } of syncds) { for(const { version, mutations } of syncds) {
@@ -313,14 +315,18 @@ export const decodePatches = async(
}) })
if(mutationIdx >= 0 && (versionNum < currentVersion || mutationIdx < maxIndex)) { if(mutationIdx >= 0 && (versionNum < currentVersion || mutationIdx < maxIndex)) {
value = mutations[mutationIdx].record!.value!.blob!.slice(-32) const mut = mutations[mutationIdx]
result = {
valueMac: mut.record!.value!.blob!.slice(-32),
operation: mut.operation
}
} }
if(versionNum >= currentVersion) { if(versionNum >= currentVersion) {
break break
} }
} }
return value return result
}) })
if(validateMacs) { if(validateMacs) {