blockchain

package module
v0.0.0-...-862a6af Latest Latest
Warning

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

Go to latest
Published: Apr 18, 2018 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

gencode go -schema block.schema -package blockchain -out block_schema.go

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound = errors.New("not found")
)

Functions

func Int2Bytes

func Int2Bytes(height uint64) []byte

func Sign

func Sign(privateKey string, data []byte) (sign []byte, err error)

Sign 对数据data进行签名.

func Verify

func Verify(publicKey string, signed []byte, data []byte) bool

Verify 校验签名的真实性.

Types

type Block

type Block struct {
	// 本区块在区块链中的高度
	Height uint64 `json:"height,omitempty"`
	// 本区块产生的时间戳
	Timestamp int64 `json:"timestamp,omitempty"`
	// 数据Data的哈希值
	Hash string `json:"hash,omitempty"`
	// 上一个区块中的Data的哈希值
	PrevHash string `json:"prev_hash,omitempty"`
	// 难度系数
	Difficulty uint32 `json:"difficulty"`
	// 随机数
	Nonce uint32 `json:"nonce"`
	// 本区块中的数据
	Data []byte `json:"data,omitempty"`
}

Block 代表区块链中的一块.

func (*Block) Marshal

func (d *Block) Marshal(buf []byte) ([]byte, error)

func (*Block) Size

func (d *Block) Size() (s uint64)

func (*Block) Unmarshal

func (d *Block) Unmarshal(buf []byte) (uint64, error)

type Blockchain

type Blockchain struct {
	Blocks []*Block
	sync.RWMutex
	Store      Store
	Difficulty uint32
	PrefixZero string
}

Blockchain 是一条完整的区块链

func (*Blockchain) AddBlock

func (bc *Blockchain) AddBlock(block *Block)

AddBlock 在区块链上增加一个区块.

func (*Blockchain) GenerateGenesisBlock

func (bc *Blockchain) GenerateGenesisBlock()

GenerateGenesisBlock 初始化创世块.

func (*Blockchain) LoadFromStore

func (bc *Blockchain) LoadFromStore() error

LoadFromStore 从文件中加载blockchain.

type Server

type Server struct {
	Addr string

	Blockchain *Blockchain
	// contains filtered or unexported fields
}

Server 提供区块链rpc服务,比如增加区块,查看区块等.

func NewServer

func NewServer(privateKey string, addr string, bc *Blockchain) *Server

NewServer 创建一个新的blockchain服务器.

func (*Server) Serve

func (s *Server) Serve() error

Serve 开启http rpc server.

type Store

type Store interface {
	Get(height uint64) (*Block, error)
	Add(height uint64, block *Block) error
	GetBatch(height uint64, count int) ([]*Block, error)
	Exist(height uint64) (bool, error)
	Close() error
}

Store 定义了存储的通用接口。

Directories

Path Synopsis
cmd
bc command
key command
base58check/base58
Package base58 implements a human-friendly base58 encoding.
Package base58 implements a human-friendly base58 encoding.

Jump to

Keyboard shortcuts

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