blockchain

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2025 License: MPL-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BlockCache = func() *sync.Map {
	var cache sync.Map

	the_genesis_block := Block{
		Timestamp:    time.Duration(time.Now().UnixNano()).Seconds(),
		MinerAddress: chaindb_config.MinerAddress,
		UUID:         0,
		Height:       0,
	}

	cache.Store(the_genesis_block.UUID, the_genesis_block)

	return &cache

}() // UUID:uint64 -> Block

Functions

func BuildBlockTryAppend

func BuildBlockTryAppend(chain *Chain, tx_pool *TxPool, proposer func(Block))

func StartMining

func StartMining(chain *Chain, tx_pool *TxPool, proposer func(Block))

Types

type Block

type Block struct {
	Timestamp    float64 `json:"Timestamp"`
	MinerAddress string  `json:"MinerAddress"`

	UUID       uint64 `json:"UUID"`
	Height     uint32 `json:"Height"`
	ParentUUID uint64 `json:"ParentUUID"`

	Transactions []Transaction `json:"Transactions,omitempty"`
}

func ForkFrom

func ForkFrom(parent_uuid uint64) Block

func (*Block) FromGob

func (blk *Block) FromGob(obj []byte) error

func (Block) Previous

func (blk Block) Previous() (previous Block, err error)

func (Block) ToGob

func (blk Block) ToGob() []byte

type Chain

type Chain atomic.Uint64

区块链最后一个区块的 UUID.

func New

func New() *Chain

func (*Chain) AvgBlockTime

func (chain *Chain) AvgBlockTime() time.Duration

对最近两个 由同一个矿工挖出的 block 进行采样, 算出平均区块时间.

func (*Chain) Head

func (chain *Chain) Head() Block

func (*Chain) ListOwners

func (chain *Chain) ListOwners() (
	owners []struct {
		OwnerID           uint32
		ConfirmationScore uint32
	},
)

按插入顺序返回.

func (*Chain) ListTxsOwnedBy

func (chain *Chain) ListTxsOwnedBy(owner uint32) (
	txs []struct {
		Tx                Transaction
		ConfirmationScore uint32
	},
)

func (*Chain) Snapshot

func (chain *Chain) Snapshot() []Block

func (*Chain) TrySwitchHead

func (chain *Chain) TrySwitchHead(new_tail_uuid uint64) (switched bool)

type Transaction

type Transaction struct {
	OwnerID uint32 `json:"OwnerID"`
	Nonce   uint32 `json:"Nonce"`
	Data    string `json:"Data"`
}

type TxPool

type TxPool struct {
	// contains filtered or unexported fields

} // 按照 (OwnerID, Nonce) 升序.

func (*TxPool) Add

func (pool *TxPool) Add(txs ...Transaction)

如果 OwnerID 和 Nonce 相同, 则覆盖掉旧的.

func (*TxPool) PopFunc

func (pool *TxPool) PopFunc(txout chan<- Transaction, do_what func(tx Transaction) string)

Jump to

Keyboard shortcuts

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