Skip to content

Commit

Permalink
Add validation of the bookkeepers.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Xiang Fu authored and dreamfly281 committed Aug 3, 2017
1 parent b4b5a89 commit c2f8841
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
9 changes: 2 additions & 7 deletions account/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions core/ledger/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
2 changes: 1 addition & 1 deletion core/ledger/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
}
Expand Down
5 changes: 5 additions & 0 deletions core/store/ChainStore/ChainStore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 8 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit c2f8841

Please sign in to comment.