Updated interfaces and added EntryID Hash
This commit is contained in:
@@ -5,13 +5,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type EntryStore interface {
|
type EntryStore interface {
|
||||||
Put(ctx context.Context, id EntryID, canonical []byte) error
|
Store(ctx context.Context, entry Entry) error
|
||||||
Get(ctx context.Context, id EntryID) ([]byte, error)
|
Load(ctx context.Context, id EntryID) (Entry, error)
|
||||||
Has(ctx context.Context, id EntryID) (bool, error)
|
Exists(ctx context.Context, id EntryID) (bool, error)
|
||||||
|
Delete(ctx context.Context, id EntryID) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type ReferenceStore interface {
|
type ReferenceStore interface {
|
||||||
GetRef(ctx context.Context, name string) (EntryID, bool, error)
|
Set(ctx context.Context, name string, entryID EntryID) error
|
||||||
SetRef(ctx context.Context, name string, entryID EntryID) (bool, error)
|
Get(ctx context.Context, name string) (EntryID, error)
|
||||||
RemoveRef(ctx context.Context, name string) error
|
Delete(ctx context.Context, name string) error
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,17 @@ type Ledger struct {
|
|||||||
referenceStore ReferenceStore
|
referenceStore ReferenceStore
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLedger(entryStore EntryStore) (*Ledger, error) {
|
func NewLedger(entryStore EntryStore, referenceStore ReferenceStore) (*Ledger, error) {
|
||||||
return &Ledger{
|
return &Ledger{
|
||||||
entryStore: entryStore,
|
entryStore: entryStore,
|
||||||
|
referenceStore: referenceStore,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *Ledger) Append() {}
|
||||||
|
func (l *Ledger) AppendToReference() {}
|
||||||
|
func (l *Ledger) Get() {}
|
||||||
|
func (l *Ledger) GetFromReference() {}
|
||||||
|
func (l *Ledger) SetHead() {}
|
||||||
|
func (l *Ledger) SetReference() {}
|
||||||
|
func (l *Ledger) RemoveReference() {}
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/binary"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"lukechampine.com/blake3"
|
||||||
)
|
)
|
||||||
|
|
||||||
type EntryID [32]byte
|
type EntryID [32]byte
|
||||||
@@ -9,8 +12,28 @@ type EntryID [32]byte
|
|||||||
type Entry struct {
|
type Entry struct {
|
||||||
EntryID EntryID
|
EntryID EntryID
|
||||||
|
|
||||||
Previous []EntryID
|
Previous EntryID
|
||||||
|
|
||||||
Timestamp time.Time
|
Timestamp time.Time
|
||||||
Payload []byte
|
Payload []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ComputeEntryID(prev EntryID, ts time.Time, payload []byte) EntryID {
|
||||||
|
h := blake3.New(32, nil)
|
||||||
|
|
||||||
|
h.Write(prev[:])
|
||||||
|
|
||||||
|
var i64 [8]byte
|
||||||
|
binary.LittleEndian.PutUint64(i64[:], uint64(ts.UTC().UnixNano()))
|
||||||
|
h.Write(i64[:])
|
||||||
|
|
||||||
|
var u32 [4]byte
|
||||||
|
binary.LittleEndian.PutUint32(u32[:], uint32(len(payload)))
|
||||||
|
h.Write(u32[:])
|
||||||
|
|
||||||
|
h.Write(payload)
|
||||||
|
|
||||||
|
var id EntryID
|
||||||
|
copy(id[:], h.Sum(nil))
|
||||||
|
return id
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user