WIP Removing payloadStore

This commit is contained in:
2026-02-11 12:22:32 +08:00
parent 57e7c94f36
commit 652cfaf766
3 changed files with 15 additions and 154 deletions

View File

@@ -5,24 +5,13 @@ import (
) )
type EntryStore interface { type EntryStore interface {
Append(ctx context.Context, entry Entry) error Put(ctx context.Context, id EntryID, canonical []byte) error
Get(ctx context.Context, entryID EntryID) (Entry, error) Get(ctx context.Context, id EntryID) ([]byte, error)
GetBySeq(ctx context.Context, ledgerID LedgerID, seq uint64) (Entry, error) Has(ctx context.Context, id EntryID) (bool, error)
Delete(ctx context.Context, entryID EntryID) error
Head(ctx context.Context, ledgerID LedgerID) (EntryID, error)
HeadSeq(context.Context, LedgerID) (uint64, error)
Tail(ctx context.Context, ledgerID LedgerID) (EntryID, error)
Iterator(ctx context.Context, ledgerLedgerID, startSeq uint64) (EntryIterator, error)
} }
type EntryIterator interface { type ReferenceStore interface {
Next() bool GetRef(ctx context.Context, name string) (EntryID, bool, error)
Entry() Entry SetRef(ctx context.Context, name string, entryID EntryID) (bool, error)
} RemoveRef(ctx context.Context, name string) error
type PayloadStore interface {
Put(ctx context.Context, payload []byte) (PayloadID, error)
Get(ctx context.Context, payloadID PayloadID) ([]byte, error)
Delete(ctx context.Context, payloadID PayloadID) error
} }

View File

@@ -1,107 +1,14 @@
package core package core
import ( import ()
"context"
"time"
"github.com/google/uuid"
"lukechampine.com/blake3"
)
type Ledger struct { type Ledger struct {
LedgerID LedgerID entryStore EntryStore
referenceStore ReferenceStore
entryStore EntryStore
payloadStore PayloadStore
hashChaining bool
} }
func NewLedger(entryStore EntryStore, payloadStore PayloadStore) (*Ledger, error) { func NewLedger(entryStore EntryStore) (*Ledger, error) {
id, err := uuid.NewV7()
if err != nil {
return nil, err
}
idByes, err := id.MarshalBinary()
if err != nil {
return nil, err
}
return &Ledger{ return &Ledger{
LedgerID: LedgerID(idByes), entryStore: entryStore,
entryStore: entryStore,
payloadStore: payloadStore,
}, nil }, nil
} }
func (l *Ledger) ID() LedgerID {
return l.LedgerID
}
func (l *Ledger) Head() (Entry, error) {
panic("unimplemented")
}
func (l *Ledger) Append(ctx context.Context, payload []byte) (EntryID, error) {
payloadDigest := blake3.Sum256(payload)
payloadID, err := l.payloadStore.Put(ctx, payload)
if err != nil {
return EntryID{}, err
}
entryID, err := uuid.NewV7()
entryIDBytes, err := entryID.MarshalBinary()
if err != nil {
return EntryID{}, err
}
head, err := l.Head()
if err != nil {
return EntryID{}, err
}
entry := Entry{
EntryID: EntryID(entryIDBytes),
LedgerID: l.LedgerID,
Seq: head.Seq + 1,
Timestamp: time.Now(),
PayloadID: payloadID,
PayloadDigest: payloadDigest,
}
entryHash := HashEntry(entry)
entry.EntryHash = entryHash
err = l.entryStore.Append(ctx, entry)
if err != nil {
return EntryID{}, err
}
return EntryID(entryIDBytes), nil
}
func (l *Ledger) Get(ctx context.Context, seq uint64) (Entry, []byte, error) {
entry, err := l.entryStore.GetBySeq(ctx, l.LedgerID, seq)
if err != nil {
return Entry{}, nil, err
}
payload, err := l.payloadStore.Get(ctx, entry.PayloadID)
if err != nil {
return Entry{}, nil, err
}
return entry, payload, nil
}
func (l *Ledger) GetEntry(ctx context.Context, seq uint64) (Entry, error) {
panic("unimplemented")
}
func (l *Ledger) GetPayload(ctx context.Context, seq uint64) ([]byte, error) {
panic("unimplemented")
}
func (l *Ledger) Iter(ctx context.Context, startSeq uint64) (EntryIterator, error) {
panic("unimplemented")
}

View File

@@ -1,51 +1,16 @@
package core package core
import ( import (
"encoding/binary"
"time" "time"
"lukechampine.com/blake3"
) )
type LedgerID [16]byte type EntryID [32]byte
type EntryID [16]byte
type PayloadID [16]byte
type Hash32 [32]byte
type Entry struct { type Entry struct {
EntryID EntryID EntryID EntryID
LedgerID LedgerID Previous []EntryID
Seq uint64
Timestamp time.Time Timestamp time.Time
Payload []byte
PayloadID PayloadID
PayloadDigest Hash32
PreviousHash Hash32
EntryHash Hash32
}
func HashEntry(e Entry) Hash32 {
b := make([]byte, 0, 128)
b = append(b, "chron.entry.v1"...)
b = append(b, e.LedgerID[:]...)
var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], e.Seq)
b = append(b, buf[:]...)
binary.LittleEndian.PutUint64(buf[:], uint64(e.Timestamp.UTC().UnixNano()))
b = append(b, buf[:]...)
b = append(b, e.EntryID[:]...)
b = append(b, e.PayloadID[:]...)
b = append(b, e.PayloadDigest[:]...)
b = append(b, e.PreviousHash[:]...)
sum := blake3.Sum256(b)
return Hash32(sum)
} }