blockchain

package
v0.0.0-...-37f8b3f Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

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 表示区块链中的每个"项目"

func (Block) Checksum

func (b Block) Checksum() string

Checksum 对区块进行SHA256哈希计算

func (Block) IsValid

func (newBlock Block) IsValid(oldBlock Block) bool

IsValid 通过检查索引和比较前一区块的哈希来确保区块有效 参数 oldBlock 为前一区块

func (Block) NewBlock

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

NewBlock 使用前一区块的哈希创建新区块 参数 s 为存储数据

type Blockchain

type Blockchain []Block

Blockchain 是一系列已验证的区块

type Data

type Data string

Data 数据类型,表示存储在区块链中的数据

func (Data) Unmarshal

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

Unmarshal 将结果解析到接口。用于检索用SetValue设置的数据 参数 i 为目标接口指针

type DataString

type DataString string

DataString 数据字符串类型

type DiskStore

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

DiskStore 磁盘存储结构体

func NewDiskStore

func NewDiskStore(d *diskv.Diskv) *DiskStore

NewDiskStore 创建新的磁盘存储 参数 d 为diskv实例

func (*DiskStore) Add

func (m *DiskStore) Add(b Block)

Add 添加区块到磁盘存储 参数 b 为要添加的区块

func (*DiskStore) Last

func (m *DiskStore) Last() Block

Last 返回最后一个区块

func (*DiskStore) Len

func (m *DiskStore) Len() int

Len 返回存储中的区块数量

type Ledger

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

Ledger 账本结构体

func New

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

New 创建新的账本,写入到指定的writer 参数 w 为写入器,s 为存储器

func (*Ledger) Add

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

Add 向区块链添加数据 参数 b 为存储桶名称,s 为键值对映射

func (*Ledger) Announce

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

Announce 持续异步更新数据到区块链。 在指定间隔发送广播, 确保异步获取的值被写入区块链 参数 ctx 为上下文,d 为时间间隔,async 为异步函数

func (*Ledger) AnnounceDeleteBucket

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

AnnounceDeleteBucket 公告删除存储桶。当存储桶被删除时停止 接受间隔时间和最大超时时间。 这是尽力而为的,超时是必要的,否则如果有多个写入者尝试写入同一资源,可能会淹没网络请求 参数 ctx 为上下文,interval 为间隔时间,timeout 为超时时间,bucket 为存储桶名称

func (*Ledger) AnnounceDeleteBucketKey

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

AnnounceDeleteBucketKey 公告删除存储桶中的键。当键被删除时停止 参数 ctx 为上下文,interval 为间隔时间,timeout 为超时时间,bucket 为存储桶名称,key 为键名

func (*Ledger) AnnounceUpdate

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

AnnounceUpdate 如果状态不同,持续向区块链公告内容 参数 ctx 为上下文,interval 为间隔时间,bucket 为存储桶名称,key 为键名,value 为值

func (*Ledger) CurrentData

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

CurrentData 返回当前账本数据(加锁)

func (*Ledger) Delete

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

Delete 从账本删除数据(加锁) 参数 b 为存储桶名称,k 为键名

func (*Ledger) DeleteBucket

func (l *Ledger) DeleteBucket(b string)

DeleteBucket 从账本删除存储桶(加锁) 参数 b 为存储桶名称

func (*Ledger) Exists

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

Exists 如果存在一个匹配值的元素则返回true 参数 b 为存储桶名称,f 为匹配函数

func (*Ledger) GetKey

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

GetKey 从区块链检索当前键 参数 b 为存储桶名称,s 为键名

func (*Ledger) Index

func (l *Ledger) Index() int

Index 返回最后已知的区块链索引

func (*Ledger) LastBlock

func (l *Ledger) LastBlock() Block

LastBlock 返回区块链中的最后一个区块

func (*Ledger) Persist

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

Persist 持续向区块链公告内容,直到协调完成 参数 ctx 为上下文,interval 为间隔时间,timeout 为超时时间,bucket 为存储桶名称,key 为键名,value 为值

func (*Ledger) String

func (l *Ledger) String() string

String 返回区块链的字符串表示

func (*Ledger) Syncronizer

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

Syncronizer 启动一个goroutine,定期将区块链写入通道 参数 ctx 为上下文,t 为时间间隔

func (*Ledger) Update

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

Update 从消息更新区块链 参数 f 为账本,h 为消息,c 为消息通道

type MemoryStore

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

MemoryStore 内存存储结构体

func (*MemoryStore) Add

func (m *MemoryStore) Add(b Block)

Add 添加区块到内存存储 参数 b 为要添加的区块

func (*MemoryStore) Last

func (m *MemoryStore) Last() Block

Last 返回最后一个区块

func (*MemoryStore) Len

func (m *MemoryStore) Len() int

Len 返回存储中的区块数量

type Store

type Store interface {
	Add(Block)   // 添加区块
	Len() int    // 返回长度
	Last() Block // 返回最后区块
}

Store 存储接口

Jump to

Keyboard shortcuts

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