|
@ -1,12 +1,15 @@ |
|
|
package blocks |
|
|
package blocks |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
|
|
|
"bytes" |
|
|
"crypto/sha256" |
|
|
"crypto/sha256" |
|
|
|
|
|
"errors" |
|
|
"io" |
|
|
"io" |
|
|
"time" |
|
|
"time" |
|
|
|
|
|
|
|
|
. "github.com/tendermint/tendermint/binary" |
|
|
. "github.com/tendermint/tendermint/binary" |
|
|
. "github.com/tendermint/tendermint/common" |
|
|
. "github.com/tendermint/tendermint/common" |
|
|
|
|
|
. "github.com/tendermint/tendermint/config" |
|
|
"github.com/tendermint/tendermint/merkle" |
|
|
"github.com/tendermint/tendermint/merkle" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
@ -14,6 +17,12 @@ const ( |
|
|
defaultBlockPartSizeBytes = 4096 |
|
|
defaultBlockPartSizeBytes = 4096 |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
|
|
ErrBlockInvalidNetwork = errors.New("Error block invalid network") |
|
|
|
|
|
ErrBlockInvalidBlockHeight = errors.New("Error block invalid height") |
|
|
|
|
|
ErrBlockInvalidLastBlockHash = errors.New("Error block invalid last blockhash") |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
type Block struct { |
|
|
type Block struct { |
|
|
Header |
|
|
Header |
|
|
Validation |
|
|
Validation |
|
@ -38,8 +47,18 @@ func (b *Block) WriteTo(w io.Writer) (n int64, err error) { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (b *Block) ValidateBasic() error { |
|
|
|
|
|
// TODO Basic validation that doesn't involve context.
|
|
|
|
|
|
|
|
|
// Basic validation that doesn't involve state data.
|
|
|
|
|
|
func (b *Block) ValidateBasic(lastBlockHeight uint32, lastBlockHash []byte) error { |
|
|
|
|
|
if b.Header.Network != Config.Network { |
|
|
|
|
|
return ErrBlockInvalidNetwork |
|
|
|
|
|
} |
|
|
|
|
|
if b.Header.Height != lastBlockHeight { |
|
|
|
|
|
return ErrBlockInvalidBlockHeight |
|
|
|
|
|
} |
|
|
|
|
|
if !bytes.Equal(b.Header.LastBlockHash, lastBlockHash) { |
|
|
|
|
|
return ErrBlockInvalidLastBlockHash |
|
|
|
|
|
} |
|
|
|
|
|
// XXX more validation
|
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -83,11 +102,11 @@ func (b *Block) ToBlockPartSet() *BlockPartSet { |
|
|
func (b *Block) MakeNextBlock() *Block { |
|
|
func (b *Block) MakeNextBlock() *Block { |
|
|
return &Block{ |
|
|
return &Block{ |
|
|
Header: Header{ |
|
|
Header: Header{ |
|
|
Name: b.Header.Name, |
|
|
|
|
|
Height: b.Header.Height + 1, |
|
|
|
|
|
|
|
|
Network: b.Header.Network, |
|
|
|
|
|
Height: b.Header.Height + 1, |
|
|
//Fees: uint64(0),
|
|
|
//Fees: uint64(0),
|
|
|
Time: time.Now(), |
|
|
|
|
|
PrevHash: b.Hash(), |
|
|
|
|
|
|
|
|
Time: time.Now(), |
|
|
|
|
|
LastBlockHash: b.Hash(), |
|
|
//ValidationStateHash: nil,
|
|
|
//ValidationStateHash: nil,
|
|
|
//AccountStateHash: nil,
|
|
|
//AccountStateHash: nil,
|
|
|
}, |
|
|
}, |
|
@ -149,11 +168,11 @@ func (bp *BlockPart) Hash() []byte { |
|
|
//-----------------------------------------------------------------------------
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
type Header struct { |
|
|
type Header struct { |
|
|
Name string |
|
|
|
|
|
|
|
|
Network string |
|
|
Height uint32 |
|
|
Height uint32 |
|
|
Fees uint64 |
|
|
Fees uint64 |
|
|
Time time.Time |
|
|
Time time.Time |
|
|
PrevHash []byte |
|
|
|
|
|
|
|
|
LastBlockHash []byte |
|
|
ValidationStateHash []byte |
|
|
ValidationStateHash []byte |
|
|
AccountStateHash []byte |
|
|
AccountStateHash []byte |
|
|
|
|
|
|
|
@ -166,22 +185,22 @@ func ReadHeader(r io.Reader, n *int64, err *error) (h Header) { |
|
|
return Header{} |
|
|
return Header{} |
|
|
} |
|
|
} |
|
|
return Header{ |
|
|
return Header{ |
|
|
Name: ReadString(r, n, err), |
|
|
|
|
|
|
|
|
Network: ReadString(r, n, err), |
|
|
Height: ReadUInt32(r, n, err), |
|
|
Height: ReadUInt32(r, n, err), |
|
|
Fees: ReadUInt64(r, n, err), |
|
|
Fees: ReadUInt64(r, n, err), |
|
|
Time: ReadTime(r, n, err), |
|
|
Time: ReadTime(r, n, err), |
|
|
PrevHash: ReadByteSlice(r, n, err), |
|
|
|
|
|
|
|
|
LastBlockHash: ReadByteSlice(r, n, err), |
|
|
ValidationStateHash: ReadByteSlice(r, n, err), |
|
|
ValidationStateHash: ReadByteSlice(r, n, err), |
|
|
AccountStateHash: ReadByteSlice(r, n, err), |
|
|
AccountStateHash: ReadByteSlice(r, n, err), |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (h *Header) WriteTo(w io.Writer) (n int64, err error) { |
|
|
func (h *Header) WriteTo(w io.Writer) (n int64, err error) { |
|
|
WriteString(w, h.Name, &n, &err) |
|
|
|
|
|
|
|
|
WriteString(w, h.Network, &n, &err) |
|
|
WriteUInt32(w, h.Height, &n, &err) |
|
|
WriteUInt32(w, h.Height, &n, &err) |
|
|
WriteUInt64(w, h.Fees, &n, &err) |
|
|
WriteUInt64(w, h.Fees, &n, &err) |
|
|
WriteTime(w, h.Time, &n, &err) |
|
|
WriteTime(w, h.Time, &n, &err) |
|
|
WriteByteSlice(w, h.PrevHash, &n, &err) |
|
|
|
|
|
|
|
|
WriteByteSlice(w, h.LastBlockHash, &n, &err) |
|
|
WriteByteSlice(w, h.ValidationStateHash, &n, &err) |
|
|
WriteByteSlice(w, h.ValidationStateHash, &n, &err) |
|
|
WriteByteSlice(w, h.AccountStateHash, &n, &err) |
|
|
WriteByteSlice(w, h.AccountStateHash, &n, &err) |
|
|
return |
|
|
return |
|
|