Documentation ¶
Index ¶
- type Block
- func (blk Block) Encode() (j []byte)
- func (blk *Block) GenerateHash(save bool) (hs []byte)
- func (blk Block) IsMined() bool
- func (blk Block) IsValid() bool
- func (blk Block) IsValidNonce() bool
- func (blk Block) MarshalJSON() ([]byte, error)
- func (blk *Block) Mine() (n int)
- func (blk Block) ValidateNonce(n int) bool
- type Chain
- func (c *Chain) AddBlock(blk *Block) *Chain
- func (c *Chain) BuildBlock(dif int, dat string) (blk *Block)
- func (c Chain) Encode() (j []byte)
- func (c Chain) FirstBlock() (*Block, error)
- func (c Chain) IsSameBlock(b1 *Block, b2 *Block) bool
- func (c Chain) IsValid() bool
- func (c Chain) LastBlock() (*Block, error)
- func (c Chain) Length() int
- func (c Chain) PreviousBlock(i int) (*Block, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Block ¶
type Block struct { Previous *Block `json:"-"` Hash []byte `json:"hash"` Index int `json:"index"` Nonce int `json:"nonce"` Difficulty int `json:"difficulty"` Data string `json:"data"` Timestamp time.Time `json:"timestamp"` }
Reprecents a block.
func (*Block) GenerateHash ¶
Generate a hash for the block based on the block's struct data in JSON format. Option to save or simply generate.
func (Block) IsValidNonce ¶
Checks if this block's nonce is valid.
func (Block) MarshalJSON ¶
Marshal for JSON encode. Used to add "previous_hash" to the JSON output.
func (*Block) Mine ¶
Mines the block. Will keep running until the nonce is valid and solved for the difficulty.
func (Block) ValidateNonce ¶
Validates the nonce by combining previous block's nonce with input n. Adding both together and hashing, should equal the padding of the difficulty.
type Chain ¶
type Chain struct {
Blocks []*Block `json:"blocks"`
}
Reprecents a block chain.
func (*Chain) BuildBlock ¶
Builds a block easily. Difficult and data are the only two items required. The newly created block is returned and added to the chain.
func (Chain) FirstBlock ¶
Get the first block in the chain.
func (Chain) IsSameBlock ¶
Checks if the blocks are the same (simple hash check).