kowala-tech / kcoin

A stable cryptocurrency that algorithmically targets $1 USD using the Kowala Protocol
https://www.kowala.tech/
Other
18 stars 16 forks source link

Validator can't resume validation after process restart #727

Closed JekaMas closed 6 years ago

JekaMas commented 6 years ago

Type:

Issue

What happened? / What do you need?:

A validator don't resume validation after process restart.

If it's an issue, how can we reproduce it?:

// Start the validator.
func (api *PrivateValidatorAPI) Start(deposit *big.Int) error {
    // Start the validator and return
    if !api.kcoin.IsValidating() {
        // Propagate the initial price point to the transaction pool
        api.kcoin.lock.RLock()
        price := api.kcoin.gasPrice
        api.kcoin.lock.RUnlock()

        if deposit != nil {
            err := api.kcoin.SetDeposit(deposit)
            if err != nil && err != validator.ErrIsNotRunning {
                return err
            }
        }

        api.kcoin.txPool.SetGasPrice(price)
        return api.kcoin.StartValidating()
    }
    return nil
}

but

func (val *validator) Validating() bool {
    return atomic.LoadInt32(&val.validating) > 0
}

and

func (val *validator) notLoggedInState() stateFn {
        ...
    atomic.StoreInt32(&val.validating, 1)
        ...

So it looks like we should check the actual state of a validator from the smart contract. The same logic already works in https://github.com/kowala-tech/kcoin/blob/dev/client/knode/validator/states.go#L44

In total there're 3 places to do changes: https://github.com/kowala-tech/kcoin/blob/dev/client/cmd/kcoin/main.go#L310 (doesn't check isValidation at all), https://github.com/kowala-tech/kcoin/blob/dev/client/knode/validator/validator.go#L191 (use isValidator from the smart contract) and PrivateValidatorAPI.Start()+Kowala.StartValidating (them should initialize the proper state but don't run start validating contract again).

Affected servers or services:

Priority:

High

rgeraldes commented 6 years ago

https://github.com/kowala-tech/kcoin/blob/dev/client/knode/validator/states.go#L44 client/knode/validator/states.go:44 txHash, err := val.consensus.Join(val.walletAccount, val.deposit)

JekaMas commented 6 years ago

The logic in the consensus state machine is still present. Hovewer, as far as I can see the backend is always trust to atomic flag from the state machine. It's a kind chicken and egg problem.

So we just need to use the value from the smart contract.