core

package
v0.0.0-...-ebb7192 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2017 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Sm2PublicKeySize = 88

	TransactionHeaderSize = Sm2PublicKeySize +
		Sm2PublicKeySize +
		4 +
		32 +
		4 +
		4 /* int32 nonce */

	BlockHeaderSize = Sm2PublicKeySize +
		4 +
		32 +
		32 +
		4 /* int32 nonce */

	TransactionPowComplexity     = 2
	TestTransactionPowComplexity = 1

	BlockPowComplexity     = 4
	TestBlockPowComplexity = 2

	PowPrefix = 0

	// Identifies Hathcoin protocol version
	HTCProtoVersion = 1
)
View Source
const (
	FileName   = "keypair.dat"
	OpenErrMsg = "Failed to open or create keypair data file. "
)
View Source
const (
	MessageTypeSize    = 1
	MessageOptionsSize = 4

	MESSAGE_GET_NODES = iota + 20
	MESSAGE_SEND_NODES

	MessageGetTransaction
	MessageSendTransaction

	MessageGetBlock
	MessageSendBlock
)

Variables

Functions

func CheckProofOfWork

func CheckProofOfWork(prefix []byte, hash []byte) bool

func ConnectToNode

func ConnectToNode(dst string, timeout time.Duration, retry bool, cb NodeChannel)

func CreateConnectionsQueue

func CreateConnectionsQueue() (ConnectionsQueue, NodeChannel)

func GetIpAddress

func GetIpAddress() []string

func HandleIncomingMessage

func HandleIncomingMessage(msg Message)

func HandleNode

func HandleNode(node *Node)

func LoadKeypair

func LoadKeypair()

func ReadStdin

func ReadStdin() chan string

func Run

func Run()

Types

type Block

type Block struct {
	*BlockHeader

	// Hash == Crypto.SM3Hash(current block header)
	// 天朝密码管理局钦定的蛤稀值
	Hash []byte

	*TransactionSlice
}

Block is permanently record file storage transaction data

func NewBlock

func NewBlock(prevHash []byte) Block

New Block

func (*Block) AddTransaction

func (block *Block) AddTransaction(t *Transaction)

AddTransaction to block

func (*Block) CalcHash

func (block *Block) CalcHash() []byte

CalcHash can calculate block Hash

func (*Block) GenerateMerkelRoot

func (block *Block) GenerateMerkelRoot() []byte

func (*Block) GenerateNonce

func (block *Block) GenerateNonce(prefix []byte) uint32

GenerateNonce to BlockHeader

func (*Block) MarshalBinary

func (block *Block) MarshalBinary() ([]byte, error)

func (*Block) Sign

func (block *Block) Sign(keypair *crypto.Keypair) []byte

Sign Block

func (*Block) UnmarshalBinary

func (block *Block) UnmarshalBinary(d []byte) error

func (*Block) VerifyBlock

func (block *Block) VerifyBlock(prefix []byte) bool

Verify Block

type BlockHeader

type BlockHeader struct {
	// Version of the Block.
	// 虵: 协议升级要用遵守基本法啊 识得唔识得啊
	Version int32

	// Origin is Origin public key (use SM2-P-256)
	// 虵: 闷声大发财 识得唔识得啊
	Origin []byte

	// PervHash == Crypto.SM3Hash(previous block header)
	// 虵: 如果将来 PrevHash 有偏差,你们要负责 识得唔识得啊
	PrevHash []byte

	// MerkelRoot == Crypto.SM3Hash(transaction hashes)
	// 虵: Я помню чудное мгновенье 识得唔识得啊
	MerkelRoot []byte

	// Timestamp the block was create.
	// 虵: 垂死病中惊坐起,谈笑风生又一年 识得唔识得啊
	Timestamp uint32

	// Nonce used to generate the block.
	// 虵: 人呐都不知道自己不可以预料 识得唔识得啊
	Nonce uint32
}

BlockHeader defines information about a block

func (*BlockHeader) MarshalBinary

func (h *BlockHeader) MarshalBinary() ([]byte, error)

func (*BlockHeader) UnmarshalBinary

func (h *BlockHeader) UnmarshalBinary(d []byte) error

type BlockSlice

type BlockSlice []Block

BlockSlice

func (BlockSlice) Exists

func (bs BlockSlice) Exists(block Block) bool

Check block is exists in blockchain

func (BlockSlice) PrevBlock

func (bs BlockSlice) PrevBlock() *Block

Return previous block

type Blockchain

type Blockchain struct {
	CurrentBlock Block
	BlockSlice
	TransactionsQueue
	BlocksQueue
}

Blockchain

func SetupBlockchain

func SetupBlockchain() *Blockchain

func (*Blockchain) AddBlock

func (bl *Blockchain) AddBlock(b Block)

func (*Blockchain) CreateNewBlock

func (bl *Blockchain) CreateNewBlock() Block

func (*Blockchain) GenerateBlocks

func (bl *Blockchain) GenerateBlocks() chan Block

func (*Blockchain) Run

func (bl *Blockchain) Run()

type BlocksQueue

type BlocksQueue chan Block

BlocksQueue

type ConnectionsQueue

type ConnectionsQueue chan string

type Message

type Message struct {
	Identifier byte
	Options    []byte
	Data       []byte

	Reply chan Message
}

func NewMessage

func NewMessage(id byte) *Message

func (*Message) MarshalBinary

func (m *Message) MarshalBinary() ([]byte, error)

func (*Message) UnmarshalBinary

func (m *Message) UnmarshalBinary(d []byte) error

type Network

type Network struct {
	Nodes
	ConnectionsQueue
	Address            string
	ConnectionCallback NodeChannel
	BroadcastQueue     chan Message
	IncomingMessages   chan Message
}

func SetupNetwork

func SetupNetwork(address, port string) *Network

func (*Network) BroadcastMessage

func (n *Network) BroadcastMessage(message Message)

func (*Network) Run

func (n *Network) Run()

type Node

type Node struct {
	*net.TCPConn
	// contains filtered or unexported fields
}

type NodeChannel

type NodeChannel chan *Node

func StartListening

func StartListening(address string) NodeChannel

type Nodes

type Nodes map[string]*Node

func (Nodes) AddNode

func (n Nodes) AddNode(node *Node) bool

type Transaction

type Transaction struct {
	Header TransactionHeader

	// Hash == Crypto.SM3Hash(current transaction header)
	Hash []byte

	// Payload is raw transaction data.
	Payload []byte
}

Transaction is a transfer of HathCoin value that is broadcast to the network and collected into blocks.

func CreateTransaction

func CreateTransaction(txt string) *Transaction

func NewTransaction

func NewTransaction(from, to, payload []byte) *Transaction

Returns bytes to be sent to the network

func (*Transaction) CalcHash

func (t *Transaction) CalcHash() []byte

CalcHash can calculate Transaction Hash

func (*Transaction) GenerateNonce

func (t *Transaction) GenerateNonce(prefix []byte) uint32

GenerateNonce to TransactionHeader

func (*Transaction) MarshalBinary

func (t *Transaction) MarshalBinary() ([]byte, error)

func (*Transaction) Sign

func (t *Transaction) Sign(keypair *crypto.Keypair) []byte

Sign Transaction

func (*Transaction) UnmarshalBinary

func (t *Transaction) UnmarshalBinary(d []byte) ([]byte, error)

func (*Transaction) VerifyTransaction

func (t *Transaction) VerifyTransaction(pow []byte) bool

VerifyTransaction by Hash

type TransactionHeader

type TransactionHeader struct {
	// From is origin public key
	From []byte

	// To is destination public key
	To []byte

	// Timestamp the transaction was create.
	Timestamp uint32

	// PayloadHash == Crypto.SM3Hash(current transaction payload)
	PayloadHash []byte

	// PayloadLength == len(current transaction payload)
	PayloadLength uint32

	// Nonce Proof of work.
	Nonce uint32
}

TransactionHeader defines information about a transaction

func (*TransactionHeader) MarshalBinary

func (th *TransactionHeader) MarshalBinary() ([]byte, error)

func (*TransactionHeader) UnmarshalBinary

func (th *TransactionHeader) UnmarshalBinary(d []byte) error

type TransactionSlice

type TransactionSlice []Transaction

TransactionSlice

func DiffTransactionSlices

func DiffTransactionSlices(a, b TransactionSlice) (diff TransactionSlice)

func (TransactionSlice) AddTransaction

func (slice TransactionSlice) AddTransaction(t Transaction) TransactionSlice

func (TransactionSlice) Exists

func (slice TransactionSlice) Exists(tr Transaction) bool

func (TransactionSlice) Len

func (slice TransactionSlice) Len() int

func (*TransactionSlice) MarshalBinary

func (slice *TransactionSlice) MarshalBinary() ([]byte, error)

func (*TransactionSlice) UnmarshalBinary

func (slice *TransactionSlice) UnmarshalBinary(d []byte) error

type TransactionsQueue

type TransactionsQueue chan *Transaction

TransactionsQueue

Jump to

Keyboard shortcuts

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