Skip to content

Commit

Permalink
Revert "Most tests passing (#512)"
Browse files Browse the repository at this point in the history
This reverts commit 8ca3de7.
  • Loading branch information
cmcewen committed Oct 25, 2019
1 parent cdab132 commit 3a65b73
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 81 deletions.
12 changes: 1 addition & 11 deletions consensus/istanbul/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/ethereum/go-ethereum/consensus/istanbul/backend/internal/enodes"
istanbulCore "github.com/ethereum/go-ethereum/consensus/istanbul/core"
"github.com/ethereum/go-ethereum/consensus/istanbul/validator"
"github.com/ethereum/go-ethereum/contract_comm/election"
"github.com/ethereum/go-ethereum/contract_comm/random"
"github.com/ethereum/go-ethereum/contract_comm/validators"
"github.com/ethereum/go-ethereum/core"
Expand Down Expand Up @@ -372,15 +371,6 @@ func (sb *Backend) Verify(proposal istanbul.Proposal) (time.Duration, error) {
return 0, err
}

func (sb *Backend) getNewValidatorSet(header *types.Header, state *state.StateDB) ([]istanbul.ValidatorData, error) {
newValSetAddresses, err := election.GetElectedValidators(header, state)
if err != nil {
return nil, err
}
newValSet, err := validators.GetValidatorData(header, state, newValSetAddresses)
return newValSet, err
}

func (sb *Backend) verifyValSetDiff(proposal istanbul.Proposal, block *types.Block, state *state.StateDB) error {
header := block.Header()

Expand All @@ -390,7 +380,7 @@ func (sb *Backend) verifyValSetDiff(proposal istanbul.Proposal, block *types.Blo
return err
}

newValSet, err := sb.getNewValidatorSet(block.Header(), state)
newValSet, err := validators.GetValidatorSet(block.Header(), state)
if err != nil {
log.Error("Istanbul.verifyValSetDiff - Error in retrieving the validator set. Verifying val set diff empty.", "err", err)
if len(istExtra.AddedValidators) != 0 || istExtra.RemovedValidators.BitLen() != 0 {
Expand Down
43 changes: 39 additions & 4 deletions consensus/istanbul/backend/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"github.com/ethereum/go-ethereum/contract_comm"
contract_errors "github.com/ethereum/go-ethereum/contract_comm/errors"
gpm "github.com/ethereum/go-ethereum/contract_comm/gasprice_minimum"
"github.com/ethereum/go-ethereum/contract_comm/validators"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -51,7 +52,22 @@ const (
inmemoryMessages = 1024
mobileAllowedClockSkew uint64 = 5

// TODO(asa): Move this to contract_comm
// This is taken from celo-monorepo/packages/protocol/build/<env>/contracts/LockedGold.json
setCumulativeRewardWeightABI = `[{
"constant": false,
"inputs": [
{
"name": "blockReward",
"type": "uint256"
}
],
"name": "setCumulativeRewardWeight",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}]`

// This is taken from celo-monorepo/packages/protocol/build/<env>/contracts/GoldToken.json
increaseSupplyABI = `[{
"constant": false,
Expand Down Expand Up @@ -135,8 +151,9 @@ var (
inmemoryAddresses = 20 // Number of recent addresses from ecrecover
recentAddresses, _ = lru.NewARC(inmemoryAddresses)

increaseSupplyFuncABI, _ = abi.JSON(strings.NewReader(increaseSupplyABI))
totalSupplyFuncABI, _ = abi.JSON(strings.NewReader(totalSupplyABI))
increaseSupplyFuncABI, _ = abi.JSON(strings.NewReader(increaseSupplyABI))
totalSupplyFuncABI, _ = abi.JSON(strings.NewReader(totalSupplyABI))
setCumulativeRewardWeightFuncABI, _ = abi.JSON(strings.NewReader(setCumulativeRewardWeightABI))
)

// Author retrieves the Ethereum address of the account that minted the given
Expand Down Expand Up @@ -395,7 +412,7 @@ func (sb *Backend) UpdateValSetDiff(chain consensus.ChainReader, header *types.H
// If this is the last block of the epoch, then get the validator set diff, to save into the header
log.Trace("Called UpdateValSetDiff", "number", header.Number.Uint64(), "epoch", sb.config.Epoch)
if istanbul.IsLastBlockOfEpoch(header.Number.Uint64(), sb.config.Epoch) {
newValSet, err := sb.getNewValidatorSet(header, state)
newValSet, err := validators.GetValidatorSet(header, state)
if err != nil {
log.Error("Istanbul.Finalize - Error in retrieving the validator set. Using the previous epoch's validator set", "err", err)
} else {
Expand Down Expand Up @@ -465,6 +482,24 @@ func (sb *Backend) Finalize(chain consensus.ChainReader, header *types.Header, s
totalBlockRewards.Add(totalBlockRewards, infrastructureBlockReward)
}

stakerBlockReward := big.NewInt(params.Ether)
lockedGoldAddress, err := contract_comm.GetRegisteredAddress(params.LockedGoldRegistryId, header, state)
if err == contract_errors.ErrSmartContractNotDeployed {
log.Debug("Registry address lookup failed", "err", err, "contract id", params.LockedGoldRegistryId)
} else if err != nil {
log.Error(err.Error())
}

if lockedGoldAddress != nil {
state.AddBalance(*lockedGoldAddress, stakerBlockReward)
totalBlockRewards.Add(totalBlockRewards, stakerBlockReward)
_, err := contract_comm.MakeCallWithAddress(*lockedGoldAddress, setCumulativeRewardWeightFuncABI, "setCumulativeRewardWeight", []interface{}{stakerBlockReward}, nil, 1000000, common.Big0, header, state)
if err != nil {
log.Error("Unable to send epoch rewards to LockedGold", "err", err)
return nil, err
}
}

// Update totalSupply of GoldToken.
if totalBlockRewards.Cmp(common.Big0) > 0 {
var totalSupply *big.Int
Expand Down
56 changes: 0 additions & 56 deletions contract_comm/election/election.go

This file was deleted.

39 changes: 32 additions & 7 deletions contract_comm/validators/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,19 @@ const validatorsABIString string = `[
"stateMutability": "view",
"type": "function"
},
{
{"constant": true,
"inputs": [],
"name": "getValidators",
"outputs": [
{
"name": "",
"type": "address[]"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}, {
"name": "getValidator",
"inputs": [
{
Expand All @@ -54,6 +66,10 @@ const validatorsABIString string = `[
}
],
"outputs": [
{
"name": "identifier",
"type": "string"
},
{
"name": "name",
"type": "string"
Expand Down Expand Up @@ -90,16 +106,25 @@ func RetrieveRegisteredValidators(header *types.Header, state vm.StateDB) ([]com
return regVals, nil
}

func GetValidatorData(header *types.Header, state vm.StateDB, validatorAddresses []common.Address) ([]istanbul.ValidatorData, error) {
var validatorData []istanbul.ValidatorData
for _, addr := range validatorAddresses {
func GetValidatorSet(header *types.Header, state vm.StateDB) ([]istanbul.ValidatorData, error) {
var newValSet []istanbul.ValidatorData
var newValSetAddresses []common.Address
// Get the new epoch's validator set
// TODO(asa) - Once the validator election smart contract is completed, then a more accurate gas value should be used.
_, err := contract_comm.MakeStaticCall(params.ValidatorsRegistryId, validatorsABI, "getValidators", []interface{}{}, &newValSetAddresses, params.MaxGasForGetValidators, header, state)
if err != nil {
return nil, err
}

for _, addr := range newValSetAddresses {
validator := struct {
Identifier string
Name string
Url string
PublicKeysData []byte
Affiliation common.Address
}{}
_, err := contract_comm.MakeStaticCall(params.ValidatorsRegistryId, validatorsABI, "getValidator", []interface{}{addr}, &validator, params.MaxGasForGetValidator, header, state)
_, err := contract_comm.MakeStaticCall(params.ValidatorsRegistryId, validatorsABI, "getValidator", []interface{}{addr}, &validator, params.MaxGasForGetValidators, header, state)
if err != nil {
return nil, err
}
Expand All @@ -108,10 +133,10 @@ func GetValidatorData(header *types.Header, state vm.StateDB, validatorAddresses
return nil, fmt.Errorf("length of publicKeysData incorrect. Expected %d, got %d", expectedLength, len(validator.PublicKeysData))
}
blsPublicKey := validator.PublicKeysData[64 : 64+blscrypto.PUBLICKEYBYTES]
validatorData = append(validatorData, istanbul.ValidatorData{
newValSet = append(newValSet, istanbul.ValidatorData{
addr,
blsPublicKey,
})
}
return validatorData, nil
return newValSet, nil
}
4 changes: 1 addition & 3 deletions params/protocol_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ var (
// The names are taken from celo-monorepo/packages/protocol/lib/registry-utils.ts
AttestationsRegistryId = makeRegistryId("Attestations")
LockedGoldRegistryId = makeRegistryId("LockedGold")
ElectionRegistryId = makeRegistryId("Election")
GasCurrencyWhitelistRegistryId = makeRegistryId("GasCurrencyWhitelist")
GasPriceMinimumRegistryId = makeRegistryId("GasPriceMinimum")
GoldTokenRegistryId = makeRegistryId("GoldToken")
Expand Down Expand Up @@ -168,6 +167,5 @@ const (
MaxGasForMedianRate uint64 = 20000
MaxGasForGetWhiteList uint64 = 20000
MaxGasForGetRegisteredValidators uint64 = 1000000
MaxGasForGetValidator uint64 = 100 * 1000
MaxGasForElectValidators uint64 = 50 * 1000000
MaxGasForGetValidators uint64 = 10000000
)

0 comments on commit 3a65b73

Please sign in to comment.