1 // Copyright 2021 The Sigstore Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package sharding 16 17 // VirtualLogIndex returns the virtual log index for a given leaf index 18 func VirtualLogIndex(leafIndex int64, tid int64, ranges LogRanges) int64 { 19 // if we have no inactive ranges, we have just one log! return the leafIndex as is 20 // as long as it matches the active tree ID 21 if ranges.NoInactive() { 22 if ranges.GetActive() == tid { 23 return leafIndex 24 } 25 return -1 26 } 27 28 var virtualIndex int64 29 for _, r := range ranges.GetInactive() { 30 if r.TreeID == tid { 31 return virtualIndex + leafIndex 32 } 33 virtualIndex += r.TreeLength 34 } 35 36 // If no TreeID in Inactive matches the tid, the virtual index should be the active tree 37 if ranges.GetActive() == tid { 38 return virtualIndex + leafIndex 39 } 40 41 // Otherwise, the tid is invalid 42 return -1 43 } 44