Skip to content
Snippets Groups Projects
Commit 8e295830 authored by Hans Moog's avatar Hans Moog
Browse files

Feat: added colored coins + introduced additional classes

parent bb3a4deb
No related branches found
No related tags found
No related merge requests found
package ledgerstate
type Address struct {
hash AddressHash
unspentTransferOutputs map[TransferHash]*ColoredBalance
}
func NewAddress(hash AddressHash) *Address {
return &Address{
hash: hash,
}
}
func (address *Address) GetUnspentTransferOutputs() map[TransferHash]*ColoredBalance {
return address.unspentTransferOutputs
}
func (address *Address) Exists() bool {
return address != nil
}
package ledgerstate
type ColoredBalance struct {
colorHash ColorHash
balance uint64
}
......@@ -4,3 +4,36 @@ type LedgerState struct {
mainReality *Reality
subRealities map[string]*Reality
}
func NewLedgerState() *LedgerState {
return &LedgerState{
mainReality: NewReality(),
subRealities: make(map[string]*Reality),
}
}
func (ledgerState *LedgerState) GetUnspentTransferOutputs(address AddressHash, includedSubRealities ...TransferHash) (result map[TransferHash]*ColoredBalance) {
result = make(map[TransferHash]*ColoredBalance)
for _, reality := range ledgerState.getRealities(includedSubRealities...) {
if address := reality.GetAddress(address); address.Exists() {
for transferHash, coloredBalance := range address.GetUnspentTransferOutputs() {
result[transferHash] = coloredBalance
}
}
}
return
}
func (ledgerState *LedgerState) getRealities(includedSubRealities ...TransferHash) (realities []*Reality) {
realities = append(realities, ledgerState.mainReality)
for _, subRealityToInclude := range includedSubRealities {
if subReality := ledgerState.subRealities[subRealityToInclude]; subReality.Exists() {
realities = append(realities, subReality)
}
}
return
}
package ledgerstate
import (
"fmt"
"testing"
)
func TestLedgerState_GetUnspentTransferOutputs(t *testing.T) {
ledgerState := NewLedgerState()
NewAddress("ABC")
fmt.Println(ledgerState.GetUnspentTransferOutputs("ABC"))
}
package ledgerstate
type Reality struct {
addresses map[string]*Address
addresses map[AddressHash]*Address
}
func NewReality() *Reality {
return &Reality{
addresses: make(map[AddressHash]*Address),
}
}
func (reality *Reality) GetAddress(address AddressHash) *Address {
return reality.addresses[address]
}
func (reality *Reality) Exists() bool {
return reality != nil
}
package ledgerstate
type AddressHash = string
type TransferHash = string
type ColorHash = string
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment