github.com/cranelv/ethereum_mpc@v0.0.0-20191031014521-23aeb1415092/consensus_pbft/pbft/deduplicator.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package pbft 18 19 import ( 20 "github.com/ethereum/go-ethereum/consensus_pbft/message" 21 "github.com/ethereum/go-ethereum/consensus_pbft/pbftTypes" 22 ) 23 24 // deduplicator maintains the most recent Request timestamp for each 25 // replica. Two timestamps are maintained per replica. One timestamp 26 // tracks the most recent Request received from a replica, the other 27 // timeout tracks the most recent executed Request. 28 type deduplicator struct { 29 reqTimestamps map[pbftTypes.ReplicaID]uint64 30 execTimestamps map[pbftTypes.ReplicaID]uint64 31 } 32 33 // newDeduplicator creates a new deduplicator. 34 func newDeduplicator() *deduplicator { 35 d := &deduplicator{} 36 d.reqTimestamps = make(map[pbftTypes.ReplicaID]uint64) 37 d.execTimestamps = make(map[pbftTypes.ReplicaID]uint64) 38 return d 39 } 40 41 // Request updates the received request timestamp for the submitting 42 // replica. If the request is older than any previously received or 43 // executed request, Request() will return false, indicating a stale 44 // request. 45 func (d *deduplicator) Request(req *message.Request) bool { 46 if req.Timestamp <= d.reqTimestamps[req.ReplicaId] || 47 req.Timestamp <= d.execTimestamps[req.ReplicaId] { 48 return false 49 } 50 d.reqTimestamps[req.ReplicaId] = req.Timestamp 51 return true 52 } 53 54 // Execute updates the executed request timestamp for the submitting 55 // replica. If the request is older than any previously executed 56 // request from the same replica, Execute() will return false, 57 // indicating a stale request. 58 func (d *deduplicator) Execute(req *message.Request) bool { 59 if req.Timestamp <= d.execTimestamps[req.ReplicaId] { 60 return false 61 } 62 d.execTimestamps[req.ReplicaId] = req.Timestamp 63 return true 64 } 65 66 // IsNew returns true if this Request is newer than any previously 67 // executed request of the submitting replica. 68 func (d *deduplicator) IsNew(req *message.Request) bool { 69 return req.Timestamp > d.execTimestamps[req.ReplicaId] 70 }