core

package
v0.0.0-...-7ab076f Latest Latest
Warning

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

Go to latest
Published: May 21, 2021 License: BSL-1.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BLOCKCHAIN_PORT      = "9119"
	MAX_NODE_CONNECTIONS = 400

	NETWORK_KEY_SIZE = 80

	TRANSACTION_HEADER_SIZE = NETWORK_KEY_SIZE + NETWORK_KEY_SIZE + 4 + 32 + 4 + 4 /* int32 nonce */
	BLOCK_HEADER_SIZE       = NETWORK_KEY_SIZE + 4 + 32 + 32 + 4                   /* int32 nonce */

	KEY_POW_COMPLEXITY      = 0
	TEST_KEY_POW_COMPLEXITY = 0

	TRANSACTION_POW_COMPLEXITY      = 1
	TEST_TRANSACTION_POW_COMPLEXITY = 1

	BLOCK_POW_COMPLEXITY      = 2
	TEST_BLOCK_POW_COMPLEXITY = 2

	KEY_SIZE = 28

	POW_PREFIX      = 0
	TEST_POW_PREFIX = 0

	MESSAGE_TYPE_SIZE    = 1
	MESSAGE_OPTIONS_SIZE = 4
)
View Source
const (
	MESSAGE_GET_NODES = iota + 20
	MESSAGE_SEND_NODES

	MESSAGE_GET_TRANSACTION
	MESSAGE_SEND_TRANSACTION

	MESSAGE_GET_BLOCK
	MESSAGE_SEND_BLOCK
)

Variables

View Source
var Core = struct {
	*Keypair
	*Blockchain
	*Network
}{}

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 SEED_NODES

func SEED_NODES() []string

func SignatureVerify

func SignatureVerify(publicKey, sig, hash []byte) bool

func Start

func Start(address string)

Types

type Block

type Block struct {
	*BlockHeader
	Signature []byte
	*TransactionSlice
}

func NewBlock

func NewBlock(previousBlock []byte) Block

func (*Block) AddTransaction

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

func (*Block) GenerateMerkelRoot

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

func (*Block) GenerateNonce

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

func (*Block) Hash

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

func (*Block) MarshalBinary

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

func (*Block) Sign

func (b *Block) Sign(keypair *Keypair) []byte

func (*Block) UnmarshalBinary

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

func (*Block) VerifyBlock

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

type BlockHeader

type BlockHeader struct {
	Origin     []byte
	PrevBlock  []byte
	MerkelRoot []byte
	Timestamp  uint32
	Nonce      uint32
}

func (*BlockHeader) MarshalBinary

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

func (*BlockHeader) UnmarshalBinary

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

type BlockSlice

type BlockSlice []Block

func (BlockSlice) Exists

func (bs BlockSlice) Exists(b Block) bool

func (BlockSlice) PreviousBlock

func (bs BlockSlice) PreviousBlock() *Block

type Blockchain

type Blockchain struct {
	CurrentBlock Block
	BlockSlice

	TransactionsQueue
	BlocksQueue
}

func SetupBlockchan

func SetupBlockchan() *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

type ConnectionsQueue

type ConnectionsQueue chan string

type Keypair

type Keypair struct {
	Public  []byte `json:"public"`  // base58 (x y)
	Private []byte `json:"private"` // d (base58 encoded)
}

Key generation with proof of work

func GenerateNewKeypair

func GenerateNewKeypair() *Keypair

func (*Keypair) Sign

func (k *Keypair) Sign(hash []byte) ([]byte, error)

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
	Signature []byte
	Payload   []byte
}

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) GenerateNonce

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

func (*Transaction) Hash

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

func (*Transaction) MarshalBinary

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

func (*Transaction) Sign

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

func (*Transaction) UnmarshalBinary

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

func (*Transaction) VerifyTransaction

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

type TransactionHeader

type TransactionHeader struct {
	From          []byte
	To            []byte
	Timestamp     uint32
	PayloadHash   []byte
	PayloadLength uint32
	Nonce         uint32
}

func (*TransactionHeader) MarshalBinary

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

func (*TransactionHeader) UnmarshalBinary

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

type TransactionSlice

type TransactionSlice []Transaction

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

Jump to

Keyboard shortcuts

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