aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/dexon-foundation/dexon-consensus/common
diff options
context:
space:
mode:
authorWei-Ning Huang <w@dexon.org>2018-11-02 12:04:20 +0800
committerWei-Ning Huang <w@dexon.org>2019-04-09 21:32:52 +0800
commit8e367a13fe30a6a63880ef4d031b7f6c153cf5f0 (patch)
treea88f84c84b735c95fb22f03e3207a085d6edf36a /vendor/github.com/dexon-foundation/dexon-consensus/common
parentb1172dab3b91335729325f3effe48688418129d1 (diff)
downloaddexon-8e367a13fe30a6a63880ef4d031b7f6c153cf5f0.tar.gz
dexon-8e367a13fe30a6a63880ef4d031b7f6c153cf5f0.tar.zst
dexon-8e367a13fe30a6a63880ef4d031b7f6c153cf5f0.zip
Rename import due to dexon-consensus rename
Diffstat (limited to 'vendor/github.com/dexon-foundation/dexon-consensus/common')
-rw-r--r--vendor/github.com/dexon-foundation/dexon-consensus/common/event.go102
-rw-r--r--vendor/github.com/dexon-foundation/dexon-consensus/common/logger.go87
-rw-r--r--vendor/github.com/dexon-foundation/dexon-consensus/common/types.go107
-rw-r--r--vendor/github.com/dexon-foundation/dexon-consensus/common/utils.go14
4 files changed, 310 insertions, 0 deletions
diff --git a/vendor/github.com/dexon-foundation/dexon-consensus/common/event.go b/vendor/github.com/dexon-foundation/dexon-consensus/common/event.go
new file mode 100644
index 000000000..6c6bf49d4
--- /dev/null
+++ b/vendor/github.com/dexon-foundation/dexon-consensus/common/event.go
@@ -0,0 +1,102 @@
+// Copyright 2018 The dexon-consensus Authors
+// This file is part of the dexon-consensus library.
+//
+// The dexon-consensus library is free software: you can redistribute it
+// and/or modify it under the terms of the GNU Lesser General Public License as
+// published by the Free Software Foundation, either version 3 of the License,
+// or (at your option) any later version.
+//
+// The dexon-consensus library is distributed in the hope that it will be
+// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
+// General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the dexon-consensus library. If not, see
+// <http://www.gnu.org/licenses/>.
+
+package common
+
+import (
+ "container/heap"
+ "sync"
+ "time"
+)
+
+type timeEventFn func(time.Time)
+
+type timeEvent struct {
+ t time.Time
+ fn timeEventFn
+}
+
+// timeEvents implements a Min-Heap structure.
+type timeEvents []timeEvent
+
+func (h timeEvents) Len() int { return len(h) }
+func (h timeEvents) Less(i, j int) bool { return h[i].t.Before(h[j].t) }
+func (h timeEvents) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
+func (h *timeEvents) Push(x interface{}) {
+ *h = append(*h, x.(timeEvent))
+}
+func (h *timeEvents) Pop() interface{} {
+ old := *h
+ n := len(old)
+ x := old[n-1]
+ *h = old[0 : n-1]
+ return x
+}
+
+// Event implements the Observer pattern.
+type Event struct {
+ timeEvents timeEvents
+ timeEventsLock sync.Mutex
+}
+
+// NewEvent creates a new event instance.
+func NewEvent() *Event {
+ te := timeEvents{}
+ heap.Init(&te)
+ return &Event{
+ timeEvents: te,
+ }
+}
+
+// RegisterTime to get notified on and after specific time.
+func (e *Event) RegisterTime(t time.Time, fn timeEventFn) {
+ e.timeEventsLock.Lock()
+ defer e.timeEventsLock.Unlock()
+ heap.Push(&e.timeEvents, timeEvent{
+ t: t,
+ fn: fn,
+ })
+}
+
+// NotifyTime and trigger function callback.
+func (e *Event) NotifyTime(t time.Time) {
+ fns := func() (fns []timeEventFn) {
+ e.timeEventsLock.Lock()
+ defer e.timeEventsLock.Unlock()
+ if len(e.timeEvents) == 0 {
+ return
+ }
+ for !t.Before(e.timeEvents[0].t) {
+ te := heap.Pop(&e.timeEvents).(timeEvent)
+ fns = append(fns, te.fn)
+ if len(e.timeEvents) == 0 {
+ return
+ }
+ }
+ return
+ }()
+ for _, fn := range fns {
+ fn(t)
+ }
+}
+
+// Reset clears all pending event
+func (e *Event) Reset() {
+ e.timeEventsLock.Lock()
+ defer e.timeEventsLock.Unlock()
+ e.timeEvents = timeEvents{}
+}
diff --git a/vendor/github.com/dexon-foundation/dexon-consensus/common/logger.go b/vendor/github.com/dexon-foundation/dexon-consensus/common/logger.go
new file mode 100644
index 000000000..2eb1e2bd0
--- /dev/null
+++ b/vendor/github.com/dexon-foundation/dexon-consensus/common/logger.go
@@ -0,0 +1,87 @@
+// Copyright 2018 The dexon-consensus Authors
+// This file is part of the dexon-consensus library.
+//
+// The dexon-consensus library is free software: you can redistribute it
+// and/or modify it under the terms of the GNU Lesser General Public License as
+// published by the Free Software Foundation, either version 3 of the License,
+// or (at your option) any later version.
+//
+// The dexon-consensus library is distributed in the hope that it will be
+// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
+// General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the dexon-consensus library. If not, see
+// <http://www.gnu.org/licenses/>.
+
+package common
+
+import "log"
+
+// Logger define the way to receive logs from Consensus instance.
+// NOTE: parameter in 'ctx' should be paired as key-value mapping. For example,
+// to log an error with message:
+// logger.Error("some message", "error", err)
+// which is similar to loggers with context:
+// logger.Error("some message", map[string]interface{}{
+// "error": err,
+// })
+type Logger interface {
+ // Info logs info level logs.
+ Debug(msg string, ctx ...interface{})
+ Info(msg string, ctx ...interface{})
+ Warn(msg string, ctx ...interface{})
+ Error(msg string, ctx ...interface{})
+}
+
+// NullLogger logs nothing.
+type NullLogger struct{}
+
+// Debug implements Logger interface.
+func (logger *NullLogger) Debug(msg string, ctx ...interface{}) {
+}
+
+// Info implements Logger interface.
+func (logger *NullLogger) Info(msg string, ctx ...interface{}) {
+}
+
+// Warn implements Logger interface.
+func (logger *NullLogger) Warn(msg string, ctx ...interface{}) {
+}
+
+// Error implements Logger interface.
+func (logger *NullLogger) Error(msg string, ctx ...interface{}) {
+}
+
+// SimpleLogger logs everything.
+type SimpleLogger struct{}
+
+// composeVargs makes (msg, ctx...) could be pass to log.Println
+func composeVargs(msg string, ctxs []interface{}) []interface{} {
+ args := []interface{}{msg}
+ for _, c := range ctxs {
+ args = append(args, c)
+ }
+ return args
+}
+
+// Debug implements Logger interface.
+func (logger *SimpleLogger) Debug(msg string, ctx ...interface{}) {
+ log.Println(composeVargs(msg, ctx)...)
+}
+
+// Info implements Logger interface.
+func (logger *SimpleLogger) Info(msg string, ctx ...interface{}) {
+ log.Println(composeVargs(msg, ctx)...)
+}
+
+// Warn implements Logger interface.
+func (logger *SimpleLogger) Warn(msg string, ctx ...interface{}) {
+ log.Println(composeVargs(msg, ctx)...)
+}
+
+// Error implements Logger interface.
+func (logger *SimpleLogger) Error(msg string, ctx ...interface{}) {
+ log.Println(composeVargs(msg, ctx)...)
+}
diff --git a/vendor/github.com/dexon-foundation/dexon-consensus/common/types.go b/vendor/github.com/dexon-foundation/dexon-consensus/common/types.go
new file mode 100644
index 000000000..a5dfab10e
--- /dev/null
+++ b/vendor/github.com/dexon-foundation/dexon-consensus/common/types.go
@@ -0,0 +1,107 @@
+// Copyright 2018 The dexon-consensus Authors
+// This file is part of the dexon-consensus library.
+//
+// The dexon-consensus library is free software: you can redistribute it
+// and/or modify it under the terms of the GNU Lesser General Public License as
+// published by the Free Software Foundation, either version 3 of the License,
+// or (at your option) any later version.
+//
+// The dexon-consensus library is distributed in the hope that it will be
+// useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
+// General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the dexon-consensus library. If not, see
+// <http://www.gnu.org/licenses/>.
+
+// Copyright 2018 The dexon-consensus Authors
+// This file is part of the dexon-consensus library.
+//
+// The dexon-consensus library is free software: you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public License as
+// published by the Free Software Foundation, either version 3 of the License,
+// or (at your option) any later version.
+//
+// The dexon-consensus library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public License
+// along with the dexon-consensus library. If not, see
+// <http://www.gnu.org/licenses/>.
+
+package common
+
+import (
+ "bytes"
+ "encoding/hex"
+ "sort"
+ "time"
+)
+
+const (
+ // HashLength is the length of a hash in DEXON.
+ HashLength = 32
+)
+
+// Hash is the basic hash type in DEXON.
+type Hash [HashLength]byte
+
+func (h Hash) String() string {
+ return hex.EncodeToString([]byte(h[:]))
+}
+
+// Bytes return the hash as slice of bytes.
+func (h Hash) Bytes() []byte {
+ return h[:]
+}
+
+// Equal compares if two hashes are the same.
+func (h Hash) Equal(hp Hash) bool {
+ return h == hp
+}
+
+// Less compares if current hash is lesser.
+func (h Hash) Less(hp Hash) bool {
+ return bytes.Compare(h[:], hp[:]) < 0
+}
+
+// MarshalText implements the encoding.TextMarhsaler interface.
+func (h Hash) MarshalText() ([]byte, error) {
+ result := make([]byte, hex.EncodedLen(HashLength))
+ hex.Encode(result, h[:])
+ return result, nil
+}
+
+// UnmarshalText implements the encoding.TextUnmarshaler interface.
+func (h *Hash) UnmarshalText(text []byte) error {
+ _, err := hex.Decode(h[:], text)
+ return err
+}
+
+// Hashes is for sorting hashes.
+type Hashes []Hash
+
+func (hs Hashes) Len() int { return len(hs) }
+func (hs Hashes) Less(i, j int) bool { return hs[i].Less(hs[j]) }
+func (hs Hashes) Swap(i, j int) { hs[i], hs[j] = hs[j], hs[i] }
+
+// SortedHashes is a slice of hashes sorted in ascending order.
+type SortedHashes Hashes
+
+// NewSortedHashes converts a slice of hashes to a sorted one. It's a
+// firewall to prevent us from assigning unsorted hashes to a variable
+// declared as SortedHashes directly.
+func NewSortedHashes(hs Hashes) SortedHashes {
+ sort.Sort(hs)
+ return SortedHashes(hs)
+}
+
+// ByTime implements sort.Interface for time.Time.
+type ByTime []time.Time
+
+func (t ByTime) Len() int { return len(t) }
+func (t ByTime) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
+func (t ByTime) Less(i, j int) bool { return t[i].Before(t[j]) }
diff --git a/vendor/github.com/dexon-foundation/dexon-consensus/common/utils.go b/vendor/github.com/dexon-foundation/dexon-consensus/common/utils.go
new file mode 100644
index 000000000..7e89c059d
--- /dev/null
+++ b/vendor/github.com/dexon-foundation/dexon-consensus/common/utils.go
@@ -0,0 +1,14 @@
+package common
+
+import (
+ "math/rand"
+)
+
+// NewRandomHash returns a random Hash-like value.
+func NewRandomHash() Hash {
+ x := Hash{}
+ for i := 0; i < HashLength; i++ {
+ x[i] = byte(rand.Int() % 256)
+ }
+ return x
+}