From c2f88418a0e21736f0e1be135caaa190e8e92c28 Mon Sep 17 00:00:00 2001 From: Xiang Fu Date: Wed, 19 Jul 2017 17:06:06 +0800 Subject: [PATCH] Add validation of the bookkeepers. Check bookkeeper count when start up; Optimize passing argument in GenesisBlockInit() Change some log error to Fatal Check genesis block exist in chain or not TODO: add genesis block check when sync block Signed-off-by: Xiang Fu --- account/client.go | 9 ++------- core/ledger/block.go | 4 ++-- core/ledger/blockchain.go | 2 +- core/store/ChainStore/ChainStore.go | 5 +++++ main.go | 11 ++++++++--- 5 files changed, 18 insertions(+), 13 deletions(-) diff --git a/account/client.go b/account/client.go index 28affc1a..8f856a96 100644 --- a/account/client.go +++ b/account/client.go @@ -525,18 +525,13 @@ func nodeType(typeName string) int { } func GetClient() Client { - if (protocol.SERVICENODENAME != config.Parameters.NodeType) && - (len(config.Parameters.BookKeepers) < DefaultBookKeeperCount) { - log.Error("At least ", DefaultBookKeeperCount, " BookKeepers should be set at config.json") - return nil - } if !FileExisted(WalletFileName) { - log.Error(fmt.Sprintf("No %s detected, please create a wallet by using command line.", WalletFileName)) + log.Fatal(fmt.Sprintf("No %s detected, please create a wallet by using command line.", WalletFileName)) os.Exit(1) } passwd, err := password.GetAccountPassword() if err != nil { - log.Error("Get password error.") + log.Fatal("Get password error.") os.Exit(1) } c := Open(WalletFileName, passwd) diff --git a/core/ledger/block.go b/core/ledger/block.go index f00539c5..7a581439 100644 --- a/core/ledger/block.go +++ b/core/ledger/block.go @@ -147,9 +147,9 @@ func (b *Block) Type() InventoryType { return BLOCK } -func GenesisBlockInit() (*Block, error) { +func GenesisBlockInit(defaultBookKeeper []*crypto.PubKey) (*Block, error) { //getBookKeeper - nextBookKeeper, err := GetBookKeeperAddress(StandbyBookKeepers) + nextBookKeeper, err := GetBookKeeperAddress(defaultBookKeeper) if err != nil { return nil, NewDetailErr(err, ErrNoCode, "[Block],GenesisBlockInit err with GetBookKeeperAddress") } diff --git a/core/ledger/blockchain.go b/core/ledger/blockchain.go index 254ff414..1269d4c1 100644 --- a/core/ledger/blockchain.go +++ b/core/ledger/blockchain.go @@ -24,7 +24,7 @@ func NewBlockchain(height uint32) *Blockchain { } func NewBlockchainWithGenesisBlock(defaultBookKeeper []*crypto.PubKey) (*Blockchain, error) { - genesisBlock, err := GenesisBlockInit() + genesisBlock, err := GenesisBlockInit(defaultBookKeeper) if err != nil { return nil, NewDetailErr(err, ErrNoCode, "[Blockchain], NewBlockchainWithGenesisBlock failed.") } diff --git a/core/store/ChainStore/ChainStore.go b/core/store/ChainStore/ChainStore.go index 0f81790c..f0dd993c 100644 --- a/core/store/ChainStore/ChainStore.go +++ b/core/store/ChainStore/ChainStore.go @@ -162,6 +162,11 @@ func (bd *ChainStore) InitLedgerStoreWithGenesisBlock(genesisBlock *Block, defau } if version[0] == 0x01 { + // GenesisBlock should exist in chain + // Or the bookkeepers are not consistent with the chain + if !bd.containsBlock(hash) { + return 0, errors.New("bookkeepers are not consistent with the chain") + } // Get Current Block currentBlockPrefix := []byte{byte(SYS_CurrentBlock)} data, err := bd.st.Get(currentBlockPrefix) diff --git a/main.go b/main.go index 7cd067f2..14d8bb40 100644 --- a/main.go +++ b/main.go @@ -42,6 +42,11 @@ func main() { var noder protocol.Noder log.Trace("Node version: ", config.Version) + if len(config.Parameters.BookKeepers) < account.DefaultBookKeeperCount { + log.Fatal("At least ", account.DefaultBookKeeperCount, " BookKeepers should be set at config.json") + os.Exit(1) + } + log.Info("0. Loading the Ledger") ledger.DefaultLedger = new(ledger.Ledger) ledger.DefaultLedger.Store, err = ChainStore.NewLedgerStore() @@ -57,12 +62,12 @@ func main() { log.Info("1. Open the account") client := account.GetClient() if client == nil { - log.Error("Can't get local account.") + log.Fatal("Can't get local account.") goto ERROR } acct, err = client.GetDefaultAccount() if err != nil { - log.Error(err) + log.Fatal(err) goto ERROR } log.Debug("The Node's PublicKey ", acct.PublicKey) @@ -71,7 +76,7 @@ func main() { log.Info("3. BlockChain init") blockChain, err = ledger.NewBlockchainWithGenesisBlock(ledger.StandbyBookKeepers) if err != nil { - log.Error(err, " BlockChain generate failed") + log.Fatal(err, " BlockChain generate failed") goto ERROR } ledger.DefaultLedger.Blockchain = blockChain