From 3a65b737129e4edd58b1166c5e2e61530fa25e05 Mon Sep 17 00:00:00 2001 From: Connor McEwen Date: Thu, 24 Oct 2019 17:53:05 -0700 Subject: [PATCH] Revert "Most tests passing (#512)" This reverts commit 8ca3de7d8784ce4d96e5bf4b588686b2292732db. --- consensus/istanbul/backend/backend.go | 12 +----- consensus/istanbul/backend/engine.go | 43 ++++++++++++++++++-- contract_comm/election/election.go | 56 -------------------------- contract_comm/validators/validators.go | 39 ++++++++++++++---- params/protocol_params.go | 4 +- 5 files changed, 73 insertions(+), 81 deletions(-) delete mode 100644 contract_comm/election/election.go diff --git a/consensus/istanbul/backend/backend.go b/consensus/istanbul/backend/backend.go index cbd66b1d3c..a0002e13eb 100644 --- a/consensus/istanbul/backend/backend.go +++ b/consensus/istanbul/backend/backend.go @@ -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" @@ -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() @@ -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 { diff --git a/consensus/istanbul/backend/engine.go b/consensus/istanbul/backend/engine.go index a3ce3d0680..77aafb4c26 100644 --- a/consensus/istanbul/backend/engine.go +++ b/consensus/istanbul/backend/engine.go @@ -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" @@ -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//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//contracts/GoldToken.json increaseSupplyABI = `[{ "constant": false, @@ -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 @@ -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 { @@ -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 diff --git a/contract_comm/election/election.go b/contract_comm/election/election.go deleted file mode 100644 index 32f1cf7cdf..0000000000 --- a/contract_comm/election/election.go +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright 2017 The Celo Authors -// This file is part of the celo library. -// -// The celo library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The celo library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the celo library. If not, see . -package election - -import ( - "strings" - - "github.com/ethereum/go-ethereum/accounts/abi" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/contract_comm" - "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/core/vm" - "github.com/ethereum/go-ethereum/params" -) - -// This is taken from celo-monorepo/packages/protocol/build//contracts/Election.json -const electionABIString string = `[ - {"constant": true, - "inputs": [], - "name": "electValidators", - "outputs": [ - { - "name": "", - "type": "address[]" - } - ], - "payable": false, - "stateMutability": "view", - "type": "function" - } -]` - -var electionABI, _ = abi.JSON(strings.NewReader(electionABIString)) - -func GetElectedValidators(header *types.Header, state vm.StateDB) ([]common.Address, error) { - var newValSet []common.Address - // Get the new epoch's validator set - _, err := contract_comm.MakeStaticCall(params.ElectionRegistryId, electionABI, "electValidators", []interface{}{}, &newValSet, params.MaxGasForElectValidators, header, state) - if err != nil { - return nil, err - } - return newValSet, nil -} diff --git a/contract_comm/validators/validators.go b/contract_comm/validators/validators.go index dd9e6883c9..3376c804a2 100644 --- a/contract_comm/validators/validators.go +++ b/contract_comm/validators/validators.go @@ -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": [ { @@ -54,6 +66,10 @@ const validatorsABIString string = `[ } ], "outputs": [ + { + "name": "identifier", + "type": "string" + }, { "name": "name", "type": "string" @@ -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 } @@ -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 } diff --git a/params/protocol_params.go b/params/protocol_params.go index 0e09bd72f9..1147864050 100644 --- a/params/protocol_params.go +++ b/params/protocol_params.go @@ -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") @@ -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 )