Documentation
¶
Index ¶
- Constants
- Variables
- func CheckProofOfWork(prefix []byte, hash []byte) bool
- func ConnectToNode(dst string, timeout time.Duration, retry bool, cb NodeChannel)
- func CreateConnectionsQueue() (ConnectionsQueue, NodeChannel)
- func GetIpAddress() []string
- func HandleIncomingMessage(msg Message)
- func HandleNode(node *Node)
- func LoadKeypair()
- func ReadStdin() chan string
- func Run()
- type Block
- func (block *Block) AddTransaction(t *Transaction)
- func (block *Block) CalcHash() []byte
- func (block *Block) GenerateMerkelRoot() []byte
- func (block *Block) GenerateNonce(prefix []byte) uint32
- func (block *Block) MarshalBinary() ([]byte, error)
- func (block *Block) Sign(keypair *crypto.Keypair) []byte
- func (block *Block) UnmarshalBinary(d []byte) error
- func (block *Block) VerifyBlock(prefix []byte) bool
- type BlockHeader
- type BlockSlice
- type Blockchain
- type BlocksQueue
- type ConnectionsQueue
- type Message
- type Network
- type Node
- type NodeChannel
- type Nodes
- type Transaction
- func (t *Transaction) CalcHash() []byte
- func (t *Transaction) GenerateNonce(prefix []byte) uint32
- func (t *Transaction) MarshalBinary() ([]byte, error)
- func (t *Transaction) Sign(keypair *crypto.Keypair) []byte
- func (t *Transaction) UnmarshalBinary(d []byte) ([]byte, error)
- func (t *Transaction) VerifyTransaction(pow []byte) bool
- type TransactionHeader
- type TransactionSlice
- func (slice TransactionSlice) AddTransaction(t Transaction) TransactionSlice
- func (slice TransactionSlice) Exists(tr Transaction) bool
- func (slice TransactionSlice) Len() int
- func (slice *TransactionSlice) MarshalBinary() ([]byte, error)
- func (slice *TransactionSlice) UnmarshalBinary(d []byte) error
- type TransactionsQueue
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 ¶
View Source
var ( TransactionPow = utils.ArrayOfBytes(TransactionPowComplexity, PowPrefix) BlockPow = utils.ArrayOfBytes(BlockPowComplexity, PowPrefix) TestTransactionPow = utils.ArrayOfBytes(TestTransactionPowComplexity, PowPrefix) TestBlockPow = utils.ArrayOfBytes(TestBlockPowComplexity, PowPrefix) )
Functions ¶
func CheckProofOfWork ¶
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()
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 (*Block) AddTransaction ¶
func (block *Block) AddTransaction(t *Transaction)
AddTransaction to block
func (*Block) GenerateMerkelRoot ¶
func (*Block) GenerateNonce ¶
GenerateNonce to BlockHeader
func (*Block) MarshalBinary ¶
func (*Block) UnmarshalBinary ¶
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
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 ConnectionsQueue ¶
type ConnectionsQueue chan string
type Network ¶
type Network struct { Nodes ConnectionsQueue Address string ConnectionCallback NodeChannel BroadcastQueue chan Message IncomingMessages chan Message }
func SetupNetwork ¶
func (*Network) BroadcastMessage ¶
type NodeChannel ¶
type NodeChannel chan *Node
func StartListening ¶
func StartListening(address string) NodeChannel
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
Click to show internal directories.
Click to hide internal directories.