mirror of
https://github.com/tendermint/tendermint.git
synced 2026-09-20 06:54:41 +00:00
Tendermint <-> Application refactor
This commit is contained in:
+16
-9
@@ -57,7 +57,7 @@ func (b *Block) ValidateBasic(chainID string, lastBlockHeight int, lastBlockHash
|
||||
if !bytes.Equal(b.DataHash, b.Data.Hash()) {
|
||||
return errors.New(Fmt("Wrong Block.Header.DataHash. Expected %X, got %X", b.DataHash, b.Data.Hash()))
|
||||
}
|
||||
// NOTE: the StateHash is validated later.
|
||||
// NOTE: the AppHash and ValidatorsHash are validated later.
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -67,8 +67,7 @@ func (b *Block) FillHeader() {
|
||||
}
|
||||
|
||||
// Computes and returns the block hash.
|
||||
// If the block is incomplete (e.g. missing Header.StateHash)
|
||||
// then the hash is nil, to prevent the usage of that hash.
|
||||
// If the block is incomplete, block hash is nil for safety.
|
||||
func (b *Block) Hash() []byte {
|
||||
if b.Header == nil || b.Data == nil || b.LastValidation == nil {
|
||||
return nil
|
||||
@@ -133,12 +132,13 @@ type Header struct {
|
||||
LastBlockParts PartSetHeader `json:"last_block_parts"`
|
||||
LastValidationHash []byte `json:"last_validation_hash"`
|
||||
DataHash []byte `json:"data_hash"`
|
||||
StateHash []byte `json:"state_hash"`
|
||||
ValidatorsHash []byte `json:"validators_hash"`
|
||||
AppHash []byte `json:"app_hash"`
|
||||
}
|
||||
|
||||
// NOTE: hash is nil if required fields are missing.
|
||||
func (h *Header) Hash() []byte {
|
||||
if len(h.StateHash) == 0 {
|
||||
if len(h.ValidatorsHash) == 0 {
|
||||
return nil
|
||||
}
|
||||
return merkle.SimpleHashFromMap(map[string]interface{}{
|
||||
@@ -151,7 +151,8 @@ func (h *Header) Hash() []byte {
|
||||
"LastBlockParts": h.LastBlockParts,
|
||||
"LastValidation": h.LastValidationHash,
|
||||
"Data": h.DataHash,
|
||||
"State": h.StateHash,
|
||||
"Validators": h.ValidatorsHash,
|
||||
"App": h.AppHash,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -165,9 +166,12 @@ func (h *Header) StringIndented(indent string) string {
|
||||
%s Time: %v
|
||||
%s Fees: %v
|
||||
%s NumTxs: %v
|
||||
%s LastBlockHash: %X
|
||||
%s LastBlock: %X
|
||||
%s LastBlockParts: %v
|
||||
%s StateHash: %X
|
||||
%s LastValidation: %X
|
||||
%s Data: %X
|
||||
%s Validators: %X
|
||||
%s App: %X
|
||||
%s}#%X`,
|
||||
indent, h.ChainID,
|
||||
indent, h.Height,
|
||||
@@ -176,7 +180,10 @@ func (h *Header) StringIndented(indent string) string {
|
||||
indent, h.NumTxs,
|
||||
indent, h.LastBlockHash,
|
||||
indent, h.LastBlockParts,
|
||||
indent, h.StateHash,
|
||||
indent, h.LastValidationHash,
|
||||
indent, h.DataHash,
|
||||
indent, h.ValidatorsHash,
|
||||
indent, h.AppHash,
|
||||
indent, h.Hash())
|
||||
}
|
||||
|
||||
|
||||
+24
-53
@@ -1,37 +1,31 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
. "github.com/tendermint/go-common"
|
||||
"github.com/tendermint/go-wire"
|
||||
)
|
||||
|
||||
// Functions to generate eventId strings
|
||||
|
||||
func EventStringAccInput(addr []byte) string { return fmt.Sprintf("Acc/%X/Input", addr) }
|
||||
func EventStringAccOutput(addr []byte) string { return fmt.Sprintf("Acc/%X/Output", addr) }
|
||||
func EventStringAccCall(addr []byte) string { return fmt.Sprintf("Acc/%X/Call", addr) }
|
||||
func EventStringLogEvent(addr []byte) string { return fmt.Sprintf("Log/%X", addr) }
|
||||
func EventStringPermissions(name string) string { return fmt.Sprintf("Permissions/%s", name) }
|
||||
func EventStringNameReg(name string) string { return fmt.Sprintf("NameReg/%s", name) }
|
||||
func EventStringBond() string { return "Bond" }
|
||||
func EventStringUnbond() string { return "Unbond" }
|
||||
func EventStringRebond() string { return "Rebond" }
|
||||
func EventStringDupeout() string { return "Dupeout" }
|
||||
func EventStringNewBlock() string { return "NewBlock" }
|
||||
func EventStringFork() string { return "Fork" }
|
||||
// Reserved
|
||||
func EventStringBond() string { return "Bond" }
|
||||
func EventStringUnbond() string { return "Unbond" }
|
||||
func EventStringRebond() string { return "Rebond" }
|
||||
func EventStringDupeout() string { return "Dupeout" }
|
||||
func EventStringFork() string { return "Fork" }
|
||||
|
||||
func EventStringNewRound() string { return fmt.Sprintf("NewRound") }
|
||||
func EventStringTimeoutPropose() string { return fmt.Sprintf("TimeoutPropose") }
|
||||
func EventStringCompleteProposal() string { return fmt.Sprintf("CompleteProposal") }
|
||||
func EventStringPolka() string { return fmt.Sprintf("Polka") }
|
||||
func EventStringUnlock() string { return fmt.Sprintf("Unlock") }
|
||||
func EventStringLock() string { return fmt.Sprintf("Lock") }
|
||||
func EventStringRelock() string { return fmt.Sprintf("Relock") }
|
||||
func EventStringTimeoutWait() string { return fmt.Sprintf("TimeoutWait") }
|
||||
func EventStringVote() string { return fmt.Sprintf("Vote") }
|
||||
func EventStringNewBlock() string { return "NewBlock" }
|
||||
func EventStringNewRound() string { return "NewRound" }
|
||||
func EventStringTimeoutPropose() string { return "TimeoutPropose" }
|
||||
func EventStringCompleteProposal() string { return "CompleteProposal" }
|
||||
func EventStringPolka() string { return "Polka" }
|
||||
func EventStringUnlock() string { return "Unlock" }
|
||||
func EventStringLock() string { return "Lock" }
|
||||
func EventStringRelock() string { return "Relock" }
|
||||
func EventStringTimeoutWait() string { return "TimeoutWait" }
|
||||
func EventStringVote() string { return "Vote" }
|
||||
func EventStringApp() string { return "App" }
|
||||
|
||||
//----------------------------------------
|
||||
|
||||
@@ -39,8 +33,7 @@ const (
|
||||
EventDataTypeNewBlock = byte(0x01)
|
||||
EventDataTypeFork = byte(0x02)
|
||||
EventDataTypeTx = byte(0x03)
|
||||
EventDataTypeCall = byte(0x04)
|
||||
EventDataTypeLog = byte(0x05)
|
||||
EventDataTypeApp = byte(0x04) // Custom app event
|
||||
|
||||
EventDataTypeRoundState = byte(0x11)
|
||||
EventDataTypeVote = byte(0x12)
|
||||
@@ -55,8 +48,7 @@ var _ = wire.RegisterInterface(
|
||||
wire.ConcreteType{EventDataNewBlock{}, EventDataTypeNewBlock},
|
||||
// wire.ConcreteType{EventDataFork{}, EventDataTypeFork },
|
||||
wire.ConcreteType{EventDataTx{}, EventDataTypeTx},
|
||||
wire.ConcreteType{EventDataCall{}, EventDataTypeCall},
|
||||
wire.ConcreteType{EventDataLog{}, EventDataTypeLog},
|
||||
wire.ConcreteType{EventDataApp{}, EventDataTypeApp},
|
||||
wire.ConcreteType{EventDataRoundState{}, EventDataTypeRoundState},
|
||||
wire.ConcreteType{EventDataVote{}, EventDataTypeVote},
|
||||
)
|
||||
@@ -68,36 +60,16 @@ type EventDataNewBlock struct {
|
||||
Block *Block `json:"block"`
|
||||
}
|
||||
|
||||
// All txs fire EventDataTx, but only CallTx might have Return or Exception
|
||||
// All txs fire EventDataTx
|
||||
type EventDataTx struct {
|
||||
Tx Tx `json:"tx"`
|
||||
Return []byte `json:"return"`
|
||||
Exception string `json:"exception"`
|
||||
}
|
||||
|
||||
// EventDataCall fires when we call a contract, and when a contract calls another contract
|
||||
type EventDataCall struct {
|
||||
CallData *CallData `json:"call_data"`
|
||||
Origin []byte `json:"origin"`
|
||||
TxID []byte `json:"tx_id"`
|
||||
Return []byte `json:"return"`
|
||||
Exception string `json:"exception"`
|
||||
}
|
||||
|
||||
type CallData struct {
|
||||
Caller []byte `json:"caller"`
|
||||
Callee []byte `json:"callee"`
|
||||
Data []byte `json:"data"`
|
||||
Value int64 `json:"value"`
|
||||
Gas int64 `json:"gas"`
|
||||
}
|
||||
|
||||
// EventDataLog fires when a contract executes the LOG opcode
|
||||
type EventDataLog struct {
|
||||
Address Word256 `json:"address"`
|
||||
Topics []Word256 `json:"topics"`
|
||||
Data []byte `json:"data"`
|
||||
Height int64 `json:"height"`
|
||||
type EventDataApp struct {
|
||||
Key string `json:"key"`
|
||||
Data []byte `json:"bytes"`
|
||||
}
|
||||
|
||||
// We fire the most recent round state that led to the event
|
||||
@@ -125,7 +97,6 @@ type EventDataVote struct {
|
||||
|
||||
func (_ EventDataNewBlock) AssertIsEventData() {}
|
||||
func (_ EventDataTx) AssertIsEventData() {}
|
||||
func (_ EventDataCall) AssertIsEventData() {}
|
||||
func (_ EventDataLog) AssertIsEventData() {}
|
||||
func (_ EventDataApp) AssertIsEventData() {}
|
||||
func (_ EventDataRoundState) AssertIsEventData() {}
|
||||
func (_ EventDataVote) AssertIsEventData() {}
|
||||
|
||||
+1
-24
@@ -1,7 +1,6 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
. "github.com/tendermint/go-common"
|
||||
@@ -27,6 +26,7 @@ type GenesisDoc struct {
|
||||
GenesisTime time.Time `json:"genesis_time"`
|
||||
ChainID string `json:"chain_id"`
|
||||
Validators []GenesisValidator `json:"validators"`
|
||||
AppHash []byte `json:"app_hash"`
|
||||
}
|
||||
|
||||
//------------------------------------------------------------
|
||||
@@ -40,26 +40,3 @@ func GenesisDocFromJSON(jsonBlob []byte) (genState *GenesisDoc) {
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
//------------------------------------------------------------
|
||||
// Make random genesis state
|
||||
|
||||
func RandGenesisDoc(numValidators int, randPower bool, minPower int64) (*GenesisDoc, []*PrivValidator) {
|
||||
validators := make([]GenesisValidator, numValidators)
|
||||
privValidators := make([]*PrivValidator, numValidators)
|
||||
for i := 0; i < numValidators; i++ {
|
||||
val, privVal := RandValidator(randPower, minPower)
|
||||
validators[i] = GenesisValidator{
|
||||
PubKey: val.PubKey,
|
||||
Amount: val.VotingPower,
|
||||
}
|
||||
privValidators[i] = privVal
|
||||
}
|
||||
sort.Sort(PrivValidatorsByAddress(privValidators))
|
||||
return &GenesisDoc{
|
||||
GenesisTime: time.Now(),
|
||||
ChainID: "tendermint_test",
|
||||
Validators: validators,
|
||||
}, privValidators
|
||||
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ type Proposal struct {
|
||||
Signature crypto.SignatureEd25519 `json:"signature"`
|
||||
}
|
||||
|
||||
// polRound: -1 if no polRound.
|
||||
func NewProposal(height int, round int, blockPartsHeader PartSetHeader, polRound int) *Proposal {
|
||||
return &Proposal{
|
||||
Height: height,
|
||||
|
||||
Reference in New Issue
Block a user