blockchain

package
v0.19.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2022 License: Apache-2.0 Imports: 17 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Block

type Block struct {
	Index     int
	Timestamp string
	Storage   map[string]map[string]Data
	Hash      string
	PrevHash  string
}

Block represents each 'item' in the blockchain

func (Block) Checksum

func (b Block) Checksum() string

Checksum does SHA256 hashing of the block

func (Block) IsValid

func (newBlock Block) IsValid(oldBlock Block) bool

make sure block is valid by checking index, and comparing the hash of the previous block

func (Block) NewBlock

func (oldBlock Block) NewBlock(s map[string]map[string]Data) Block

create a new block using previous block's hash

type Blockchain

type Blockchain []Block

Blockchain is a series of validated Blocks

type Data added in v0.1.0

type Data string

func (Data) Unmarshal added in v0.2.0

func (d Data) Unmarshal(i interface{}) error

Unmarshal the result into the interface. Use it to retrieve data set with SetValue

type DataString added in v0.2.0

type DataString string

type DiskStore added in v0.3.0

type DiskStore struct {
	// contains filtered or unexported fields
}

func NewDiskStore added in v0.3.0

func NewDiskStore(d *diskv.Diskv) *DiskStore

func (*DiskStore) Add added in v0.3.0

func (m *DiskStore) Add(b Block)

func (*DiskStore) Last added in v0.3.0

func (m *DiskStore) Last() Block

func (*DiskStore) Len added in v0.3.0

func (m *DiskStore) Len() int

type Ledger

type Ledger struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func New

func New(w io.Writer, s Store) *Ledger

New returns a new ledger which writes to the writer

func (*Ledger) Add

func (l *Ledger) Add(b string, s map[string]interface{})

Add data to the blockchain

func (*Ledger) Announce added in v0.1.0

func (l *Ledger) Announce(ctx context.Context, d time.Duration, async func())

Announce keeps updating async data to the blockchain. Sends a broadcast at the specified interval by making sure the async retrieved value is written to the blockchain

func (*Ledger) AnnounceDeleteBucket added in v0.2.5

func (l *Ledger) AnnounceDeleteBucket(ctx context.Context, interval, timeout time.Duration, bucket string)

AnnounceDeleteBucket Announce a deletion of a bucket. It stops when the bucket is deleted It takes an interval time and a max timeout. It is best effort, and the timeout is necessary, or we might flood network with requests if more writers are attempting to write to the same resource

func (*Ledger) AnnounceDeleteBucketKey added in v0.2.5

func (l *Ledger) AnnounceDeleteBucketKey(ctx context.Context, interval, timeout time.Duration, bucket, key string)

AnnounceDeleteBucketKey Announce a deletion of a key from a bucket. It stops when the key is deleted

func (*Ledger) AnnounceUpdate added in v0.8.4

func (l *Ledger) AnnounceUpdate(ctx context.Context, interval time.Duration, bucket, key string, value interface{})

AnnounceUpdate Keeps announcing something into the blockchain if state is differing

func (*Ledger) CurrentData added in v0.1.0

func (l *Ledger) CurrentData() map[string]map[string]Data

CurrentData returns the current ledger data (locking)

func (*Ledger) Delete added in v0.2.4

func (l *Ledger) Delete(b string, k string)

Delete data from the ledger (locking)

func (*Ledger) DeleteBucket added in v0.2.4

func (l *Ledger) DeleteBucket(b string)

DeleteBucket deletes a bucket from the ledger (locking)

func (*Ledger) Exists added in v0.1.0

func (l *Ledger) Exists(b string, f func(Data) bool) (exists bool)

Exists returns true if there is one element with a matching value

func (*Ledger) GetKey

func (l *Ledger) GetKey(b, s string) (value Data, exists bool)

GetKey retrieve the current key from the blockchain

func (*Ledger) Index added in v0.3.0

func (l *Ledger) Index() int

Index returns last known blockchain index

func (*Ledger) LastBlock added in v0.3.0

func (l *Ledger) LastBlock() Block

LastBlock returns the last block in the blockchain

func (*Ledger) Persist

func (l *Ledger) Persist(ctx context.Context, interval, timeout time.Duration, bucket, key string, value interface{})

Persist Keeps announcing something into the blockchain until it is reconciled

func (*Ledger) String

func (l *Ledger) String() string

String returns the blockchain as string

func (*Ledger) Syncronizer

func (l *Ledger) Syncronizer(ctx context.Context, t time.Duration)

Syncronizer starts a goroutine which writes the blockchain to the periodically

func (*Ledger) Update

func (l *Ledger) Update(f *Ledger, h *hub.Message, c chan *hub.Message) (err error)

Update the blockchain from a message

type MemoryStore added in v0.3.0

type MemoryStore struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func (*MemoryStore) Add added in v0.3.0

func (m *MemoryStore) Add(b Block)

func (*MemoryStore) Last added in v0.3.0

func (m *MemoryStore) Last() Block

func (*MemoryStore) Len added in v0.3.0

func (m *MemoryStore) Len() int

type Store added in v0.3.0

type Store interface {
	Add(Block)
	Len() int
	Last() Block
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL