blockchain

package
v0.0.0-...-5fb01ef Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2018 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CurrentTime

func CurrentTime() time.Time

TODO implement network time adjustment

func GetTimeStamp

func GetTimeStamp() int64

GetTimeStamp returns current timestamp

func Hash

func Hash(bytes []byte) string

func HashStr

func HashStr(str string) string

func NewKeys

func NewKeys() (crypto.PrivKey, crypto.PubKey, error)

Types

type Block

type Block struct {
	BlockData
	Parent *Block
	Next   *Block
}

func CreateGenesis

func CreateGenesis() *Block

func NewBlock

func NewBlock(privateKey crypto.PrivKey, parent *Block, transactions []Transaction, votes []Vote) *Block
Example
viper.Set("reward", 50)

priv, _, _ := NewKeys()
t := Pay(priv, peer.ID("some"), 42)
v := NewVote(priv, "other")

parent := CreateGenesis()
block := NewBlock(priv, parent, []Transaction{*t}, []Vote{*v})

verified, _ := block.Verify(&parent.BlockData)
fmt.Println(verified)

block.Reward = 100
verified, _ = block.Verify(&parent.BlockData)
fmt.Println(verified)

block.Reward = 50
block.Transactions[0].Amount = 0
verified, _ = block.Verify(&parent.BlockData)
fmt.Println(verified)

block.Transactions[0].Amount = 42
block.Votes[0].Candidate = "third"
verified, _ = block.Verify(&parent.BlockData)
fmt.Println(verified)
Output:

true
false
false
false

type BlockData

type BlockData struct {
	Unit
	ParentHash   string
	Transactions Transactions
	Votes        Votes
	Reward       uint
}

func (*BlockData) GetCorpus

func (block *BlockData) GetCorpus() *bytes.Buffer

func (*BlockData) String

func (block *BlockData) String() string

func (*BlockData) Verify

func (block *BlockData) Verify(parent *BlockData) (valid bool, err error)

Block contents verification. Checks basic cryptography and transactions timing

type Signable

type Signable interface {
	GetSigner() string //getters have not conventional form as would interfere with fields names, which can not be private, or would require a lot of setters
	GetPublicKey() []byte
	GetCorpus() *bytes.Buffer
	GetSign() []byte
	GetTimestamp() int64
}

type Transaction

type Transaction struct {
	Unit
	Recipient string
	Amount    uint64
}
Example
priv, _, _ := NewKeys()
t := Pay(priv, peer.ID("some"), 42)
verified, _ := verify(t)
fmt.Println(verified)
t.Amount++
verified, _ = verify(t)
fmt.Println(verified)
Output:

true
false

func Pay

func Pay(private crypto.PrivKey, recipient peer.ID, amount uint64) *Transaction

func (*Transaction) GetCorpus

func (t *Transaction) GetCorpus() *bytes.Buffer

func (*Transaction) String

func (t *Transaction) String() string

func (*Transaction) Verify

func (t *Transaction) Verify() (result bool, err error)

type Transactions

type Transactions []Transaction

type Unit

type Unit struct {
	Signable
	Hash      string
	Signer    string
	Sign      []byte
	PublicKey []byte
	TimeStamp int64
}

Basic Unit that can be presented in blockchain All fields should be public to be (de)serialize

func (*Unit) GetCorpus

func (u *Unit) GetCorpus() *bytes.Buffer

func (*Unit) GetPublicKey

func (u *Unit) GetPublicKey() []byte

func (*Unit) GetSign

func (u *Unit) GetSign() []byte

func (*Unit) GetSigner

func (u *Unit) GetSigner() string

func (*Unit) GetTimestamp

func (u *Unit) GetTimestamp() int64

type Vote

type Vote struct {
	Unit
	Candidate string
}

func NewVote

func NewVote(private crypto.PrivKey, candidate peer.ID) *Vote

func (*Vote) GetCorpus

func (v *Vote) GetCorpus() *bytes.Buffer

func (*Vote) String

func (v *Vote) String() string

func (*Vote) Verify

func (v *Vote) Verify() (result bool, err error)

type Votes

type Votes []Vote

Jump to

Keyboard shortcuts

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