Created
May 12, 2024 12:19
-
-
Save dmarzzz/f6e02ce2440a2d88ce76bceb75cb54d1 to your computer and use it in GitHub Desktop.
Super Simple POD Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
) | |
type Transaction struct { | |
ID int | |
Data string | |
} | |
type RecordCertificate struct { | |
TransactionID int | |
Valid bool | |
Signature string // fake cryptographic signature for now | |
} | |
type PastPerfectCertificate struct { | |
Round int | |
Valid bool | |
} | |
type POD struct { | |
Transactions map[int][]Transaction // Keyed by round | |
} | |
type Bulletin struct { | |
mu sync.Mutex | |
pod POD | |
currentRound int | |
} | |
func NewBulletin() *Bulletin { | |
return &Bulletin{ | |
pod: POD{Transactions: make(map[int][]Transaction)}, | |
currentRound: 0, | |
} | |
} | |
func (b *Bulletin) Write(tx Transaction) RecordCertificate { | |
b.mu.Lock() | |
defer b.mu.Unlock() | |
b.currentRound++ | |
if b.pod.Transactions[b.currentRound] == nil { | |
b.pod.Transactions[b.currentRound] = make([]Transaction, 0) | |
} | |
b.pod.Transactions[b.currentRound] = append(b.pod.Transactions[b.currentRound], tx) | |
return RecordCertificate{ | |
TransactionID: tx.ID, | |
Valid: true, | |
Signature: "FakeSignature", | |
} | |
} | |
func (b *Bulletin) Read() (int, []Transaction, POD, PastPerfectCertificate) { | |
b.mu.Lock() | |
defer b.mu.Unlock() | |
var transactions []Transaction | |
for _, txs := range b.pod.Transactions { | |
transactions = append(transactions, txs...) | |
} | |
return b.currentRound, transactions, b.pod, PastPerfectCertificate{ | |
Round: b.currentRound, | |
Valid: true, | |
} | |
} | |
func main() { | |
b := NewBulletin() | |
tx := Transaction{ID: 1, Data: "Sample Transaction"} | |
certificate := b.Write(tx) | |
fmt.Println("Write return:", certificate) | |
r, transactions, pod, pCertificate := b.Read() | |
fmt.Println("Read return:", r, transactions, pod, pCertificate) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment