block

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2018 License: LGPL-3.0 Imports: 13 Imported by: 30

Documentation

Overview

Package block 是区块和区块链的结构体定义和操作方法

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidLengthBlock = fmt.Errorf("proto: negative length found during unmarshaling")
	ErrIntOverflowBlock   = fmt.Errorf("proto: integer overflow")
)

Functions

func ByteToInt64

func ByteToInt64(b []byte) int64

ByteToInt64 is byte to int64

func Int64ToByte

func Int64ToByte(n int64) []byte

Int64ToByte is int64 to byte

Types

type Block

type Block struct {
	Head     *BlockHead
	Sign     *crypto.Signature
	Txs      []*tx.Tx
	Receipts []*tx.TxReceipt
	// contains filtered or unexported fields
}

Block is the implementation of block

func (*Block) CalculateHeadHash

func (b *Block) CalculateHeadHash() error

CalculateHeadHash calculate the hash of the head

func (*Block) CalculateMerkleHash

func (b *Block) CalculateMerkleHash() []byte

CalculateMerkleHash calculate the hash of the MerkleTree

func (*Block) CalculateTxsHash

func (b *Block) CalculateTxsHash() []byte

CalculateTxsHash calculate the hash of the transaction

func (*Block) Decode

func (b *Block) Decode(blockByte []byte) error

Decode is unmarshal

func (*Block) Encode

func (b *Block) Encode() ([]byte, error)

Encode is marshal

func (*Block) HeadHash

func (b *Block) HeadHash() []byte

HeadHash return block hash

func (*Block) LenTx

func (b *Block) LenTx() int

LenTx return len of transaction

type BlockChain

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

BlockChain is the implementation of chain

func (*BlockChain) CheckLength

func (bc *BlockChain) CheckLength()

CheckLength is check length of block in database

func (*BlockChain) Close

func (bc *BlockChain) Close()

Close is close database

func (*BlockChain) Draw

func (bc *BlockChain) Draw(start int64, end int64) string

func (*BlockChain) GetBlockByHash

func (bc *BlockChain) GetBlockByHash(hash []byte) (*Block, error)

GetBlockByHash is get block by hash

func (*BlockChain) GetBlockByNumber

func (bc *BlockChain) GetBlockByNumber(number int64) (*Block, error)

GetBlockByNumber is get block by number

func (*BlockChain) GetBlockByteByHash

func (bc *BlockChain) GetBlockByteByHash(hash []byte) ([]byte, error)

GetBlockByteByHash is get block byte by hash

func (*BlockChain) GetHashByNumber

func (bc *BlockChain) GetHashByNumber(number int64) ([]byte, error)

GetHashByNumber is get hash by number

func (*BlockChain) Length

func (bc *BlockChain) Length() int64

Length return length of block chain

func (*BlockChain) Push

func (bc *BlockChain) Push(block *Block) error

Push save the block to database

func (*BlockChain) Top

func (bc *BlockChain) Top() (*Block, error)

Top return the block

type BlockHead

type BlockHead struct {
	Version              int64    `protobuf:"varint,1,opt,name=version,proto3" json:"version,omitempty"`
	ParentHash           []byte   `protobuf:"bytes,2,opt,name=parentHash,proto3" json:"parentHash,omitempty"`
	TxsHash              []byte   `protobuf:"bytes,3,opt,name=txsHash,proto3" json:"txsHash,omitempty"`
	MerkleHash           []byte   `protobuf:"bytes,4,opt,name=merkleHash,proto3" json:"merkleHash,omitempty"`
	Info                 []byte   `protobuf:"bytes,5,opt,name=info,proto3" json:"info,omitempty"`
	Number               int64    `protobuf:"varint,6,opt,name=number,proto3" json:"number,omitempty"`
	Witness              string   `protobuf:"bytes,7,opt,name=witness,proto3" json:"witness,omitempty"`
	Time                 int64    `protobuf:"varint,8,opt,name=time,proto3" json:"time,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func (*BlockHead) Decode

func (b *BlockHead) Decode(bhByte []byte) error

Decode is unmarshal

func (*BlockHead) Descriptor

func (*BlockHead) Descriptor() ([]byte, []int)

func (*BlockHead) Encode

func (b *BlockHead) Encode() ([]byte, error)

Encode is marshal

func (*BlockHead) GetInfo

func (m *BlockHead) GetInfo() []byte

func (*BlockHead) GetMerkleHash

func (m *BlockHead) GetMerkleHash() []byte

func (*BlockHead) GetNumber

func (m *BlockHead) GetNumber() int64

func (*BlockHead) GetParentHash

func (m *BlockHead) GetParentHash() []byte

func (*BlockHead) GetTime

func (m *BlockHead) GetTime() int64

func (*BlockHead) GetTxsHash

func (m *BlockHead) GetTxsHash() []byte

func (*BlockHead) GetVersion

func (m *BlockHead) GetVersion() int64

func (*BlockHead) GetWitness

func (m *BlockHead) GetWitness() string

func (*BlockHead) Hash

func (b *BlockHead) Hash() ([]byte, error)

Hash return hash

func (*BlockHead) Marshal

func (m *BlockHead) Marshal() (dAtA []byte, err error)

func (*BlockHead) MarshalTo

func (m *BlockHead) MarshalTo(dAtA []byte) (int, error)

func (*BlockHead) ProtoMessage

func (*BlockHead) ProtoMessage()

func (*BlockHead) Reset

func (m *BlockHead) Reset()

func (*BlockHead) Size

func (m *BlockHead) Size() (n int)

func (*BlockHead) String

func (m *BlockHead) String() string

func (*BlockHead) Unmarshal

func (m *BlockHead) Unmarshal(dAtA []byte) error

func (*BlockHead) XXX_DiscardUnknown

func (m *BlockHead) XXX_DiscardUnknown()

func (*BlockHead) XXX_Marshal

func (m *BlockHead) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*BlockHead) XXX_Merge

func (dst *BlockHead) XXX_Merge(src proto.Message)

func (*BlockHead) XXX_Size

func (m *BlockHead) XXX_Size() int

func (*BlockHead) XXX_Unmarshal

func (m *BlockHead) XXX_Unmarshal(b []byte) error

type BlockRaw

type BlockRaw struct {
	Head                 *BlockHead `protobuf:"bytes,1,opt,name=head" json:"head,omitempty"`
	Sign                 []byte     `protobuf:"bytes,2,opt,name=sign,proto3" json:"sign,omitempty"`
	Txs                  [][]byte   `protobuf:"bytes,3,rep,name=txs" json:"txs,omitempty"`
	Receipts             [][]byte   `protobuf:"bytes,4,rep,name=receipts" json:"receipts,omitempty"`
	XXX_NoUnkeyedLiteral struct{}   `json:"-"`
	XXX_unrecognized     []byte     `json:"-"`
	XXX_sizecache        int32      `json:"-"`
}

func (*BlockRaw) Descriptor

func (*BlockRaw) Descriptor() ([]byte, []int)

func (*BlockRaw) GetHead

func (m *BlockRaw) GetHead() *BlockHead

func (*BlockRaw) GetReceipts

func (m *BlockRaw) GetReceipts() [][]byte

func (*BlockRaw) GetSign

func (m *BlockRaw) GetSign() []byte

func (*BlockRaw) GetTxs

func (m *BlockRaw) GetTxs() [][]byte

func (*BlockRaw) Marshal

func (m *BlockRaw) Marshal() (dAtA []byte, err error)

func (*BlockRaw) MarshalTo

func (m *BlockRaw) MarshalTo(dAtA []byte) (int, error)

func (*BlockRaw) ProtoMessage

func (*BlockRaw) ProtoMessage()

func (*BlockRaw) Reset

func (m *BlockRaw) Reset()

func (*BlockRaw) Size

func (m *BlockRaw) Size() (n int)

func (*BlockRaw) String

func (m *BlockRaw) String() string

func (*BlockRaw) Unmarshal

func (m *BlockRaw) Unmarshal(dAtA []byte) error

func (*BlockRaw) XXX_DiscardUnknown

func (m *BlockRaw) XXX_DiscardUnknown()

func (*BlockRaw) XXX_Marshal

func (m *BlockRaw) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*BlockRaw) XXX_Merge

func (dst *BlockRaw) XXX_Merge(src proto.Message)

func (*BlockRaw) XXX_Size

func (m *BlockRaw) XXX_Size() int

func (*BlockRaw) XXX_Unmarshal

func (m *BlockRaw) XXX_Unmarshal(b []byte) error

type Chain

type Chain interface {
	Push(block *Block) error
	Length() int64
	CheckLength()
	Top() (*Block, error)
	GetHashByNumber(number int64) ([]byte, error)
	GetBlockByNumber(number int64) (*Block, error)
	GetBlockByHash(blockHash []byte) (*Block, error)
	GetBlockByteByHash(blockHash []byte) ([]byte, error)
	Close()
	Draw(int64, int64) string
}

Chain defines Chain's API.

func NewBlockChain

func NewBlockChain(path string) (Chain, error)

NewBlockChain returns a Chain instance

type ChainIterator

type ChainIterator interface {
	Next() *Block
}

ChainIterator is iterator of block chain

Jump to

Keyboard shortcuts

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