Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
* dev:
  Latest changes

# Conflicts:
#	README-docker.md
  • Loading branch information
JoshuaGEdwards committed Jan 20, 2023
2 parents b351262 + 226b0da commit 9351b98
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 102 deletions.
2 changes: 1 addition & 1 deletion avalanchego/genesis/genesis_localFlare.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion avalanchego/scripts/test_deploy_validators.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ printf "\nCreating new validator: NodeID-MFrZFVCXPv5iCn6M9K6XduxGTYp891xXZ "

CURR_TIME=$(date +%s)
START_TIME=$(($CURR_TIME+300))
END_TIME=$(($START_TIME+1512000))
END_TIME=$(($START_TIME+86400))

curl -s --location --request POST 'http://localhost:9650/ext/bc/P' \
--header 'Content-Type: application/json' \
Expand Down
8 changes: 4 additions & 4 deletions avalanchego/utils/constants/validator_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ func VerifyLocalFlare(currentTimestamp time.Time, validatorConfigHash string) bo
return CompareValidatorConfigs(
validatorConfigHash,
[]string{
"28db06ceca182e433d236d09eeec11141f2dcbb14679d0fb0bc4869b79accbb4",
"4c5a2b0f87d45be2cfd44ac5af4e454716a0bd9cd74f0046d2dcd22203bbdc25",
"3d06d854ebc7e101b7f0673a388153c8dceb26f84106eea17043bbd8c16a4dc7",
"297ab94e9c6d94e9df2628d6a2b5e6b6dca4b41550675147bbe17c7d75fda61f",
"3b66ad21620fe6d0dd1665b89b7c5f7a3b18e34d7d18ca56b732f833b8259108",
"0f750b09f2702ecea445657120e7dcb0cdb46a8c87d1eae9a508fa4e3bfa5a32",
"39e596bdd2e00f5cb7fd86069c94159b98bdd79a4ed8684a88c0faf49f63bfab",
"aa43e6ef2d60823406e7e3cb6fcdb148fdd3ffcd137a261457f9ee6d541f9ca9",
},
)
default:
Expand Down
40 changes: 40 additions & 0 deletions avalanchego/utils/hashing/hashing_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package hashing

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestToValidatorConfigHash(t *testing.T) {
require := require.New(t)
networkId := "162"
duration := "86400"
weight := "10000000000000"

// pChainPkReadable := "P-localflare18jma8ppw3nhx5r4ap8clazz0dps7rv5uj3gy4v"
// pChainPk, err := address.ParseToID(pChainPkReadable)
// require.NoError(err)
pChainPk := "6Y3kysjF9jnHnYkdS9yGAuoHyae2eNmeV"

nodeId1 := "NodeID-MFrZFVCXPv5iCn6M9K6XduxGTYp891xXZ"
nodeId2 := "NodeID-NFBbbJ4qCmNaCzeW7sxErhvWqvEQMnYcN"
nodeId3 := "NodeID-GWPcbFJZFfZreETSoWjPimr846mXEKCtu"
nodeId4 := "NodeID-P7oB2McjBGgW2NXXWVYjV8JEDFoW9xDE5"

result1 := ToValidatorConfigHash(networkId, pChainPk, nodeId1, weight, duration)
t.Logf("Result: %v", result1)
require.True(result1 == "3b66ad21620fe6d0dd1665b89b7c5f7a3b18e34d7d18ca56b732f833b8259108")

result2 := ToValidatorConfigHash(networkId, pChainPk, nodeId2, weight, duration)
t.Logf("Result: %v", result2)
require.True(result2 == "0f750b09f2702ecea445657120e7dcb0cdb46a8c87d1eae9a508fa4e3bfa5a32")

result3 := ToValidatorConfigHash(networkId, pChainPk, nodeId3, weight, duration)
t.Logf("Result: %v", result3)
require.True(result3 == "39e596bdd2e00f5cb7fd86069c94159b98bdd79a4ed8684a88c0faf49f63bfab")

result4 := ToValidatorConfigHash(networkId, pChainPk, nodeId4, weight, duration)
t.Logf("Result: %v", result4)
require.True(result4 == "aa43e6ef2d60823406e7e3cb6fcdb148fdd3ffcd137a261457f9ee6d541f9ca9")
}
2 changes: 1 addition & 1 deletion avalanchego/version/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var (
Current = &Semantic{
Major: 1,
Minor: 7,
Patch: 18,
Patch: 1802,
}
CurrentApp = &Application{
Major: Current.Major,
Expand Down
86 changes: 0 additions & 86 deletions avalanchego/vms/platformvm/txs/executor/proposal_tx_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ import (

"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/vms/components/avax"
"github.com/ava-labs/avalanchego/vms/components/verify"
"github.com/ava-labs/avalanchego/vms/platformvm/reward"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/utxo"
Expand Down Expand Up @@ -796,30 +794,6 @@ func (e *ProposalTxExecutor) RewardValidatorTx(tx *txs.RewardValidatorTx) error
e.OnAbort.AddUTXO(utxo)
}

// Provide the reward here
if stakerToRemove.PotentialReward > 0 {
outIntf, err := e.Fx.CreateOutput(stakerToRemove.PotentialReward, uStakerTx.RewardsOwner)
if err != nil {
return fmt.Errorf("failed to create output: %w", err)
}
out, ok := outIntf.(verify.State)
if !ok {
return errInvalidState
}

utxo := &avax.UTXO{
UTXOID: avax.UTXOID{
TxID: tx.TxID,
OutputIndex: uint32(len(uStakerTx.Outs) + len(uStakerTx.Stake)),
},
Asset: avax.Asset{ID: e.Ctx.AVAXAssetID},
Out: out,
}

e.OnCommit.AddUTXO(utxo)
e.OnCommit.AddRewardUTXO(tx.TxID, utxo)
}

// Handle reward preferences
nodeID = uStakerTx.Validator.ID()
startTime = uStakerTx.StartTime()
Expand Down Expand Up @@ -866,66 +840,6 @@ func (e *ProposalTxExecutor) RewardValidatorTx(tx *txs.RewardValidatorTx) error
return errWrongTxType
}

// Calculate split of reward between delegator/delegatee
// The delegator gives stake to the validatee
delegatorShares := reward.PercentDenominator - uint64(vdrTx.Shares) // parentTx.Shares <= reward.PercentDenominator so no underflow
delegatorReward := delegatorShares * (stakerToRemove.PotentialReward / reward.PercentDenominator) // delegatorShares <= reward.PercentDenominator so no overflow
// Delay rounding as long as possible for small numbers
if optimisticReward, err := math.Mul64(delegatorShares, stakerToRemove.PotentialReward); err == nil {
delegatorReward = optimisticReward / reward.PercentDenominator
}
delegateeReward := stakerToRemove.PotentialReward - delegatorReward // delegatorReward <= reward so no underflow

offset := 0

// Reward the delegator here
if delegatorReward > 0 {
outIntf, err := e.Fx.CreateOutput(delegatorReward, uStakerTx.RewardsOwner)
if err != nil {
return fmt.Errorf("failed to create output: %w", err)
}
out, ok := outIntf.(verify.State)
if !ok {
return errInvalidState
}
utxo := &avax.UTXO{
UTXOID: avax.UTXOID{
TxID: tx.TxID,
OutputIndex: uint32(len(uStakerTx.Outs) + len(uStakerTx.Stake)),
},
Asset: avax.Asset{ID: e.Ctx.AVAXAssetID},
Out: out,
}

e.OnCommit.AddUTXO(utxo)
e.OnCommit.AddRewardUTXO(tx.TxID, utxo)

offset++
}

// Reward the delegatee here
if delegateeReward > 0 {
outIntf, err := e.Fx.CreateOutput(delegateeReward, vdrTx.RewardsOwner)
if err != nil {
return fmt.Errorf("failed to create output: %w", err)
}
out, ok := outIntf.(verify.State)
if !ok {
return errInvalidState
}
utxo := &avax.UTXO{
UTXOID: avax.UTXOID{
TxID: tx.TxID,
OutputIndex: uint32(len(uStakerTx.Outs) + len(uStakerTx.Stake) + offset),
},
Asset: avax.Asset{ID: e.Ctx.AVAXAssetID},
Out: out,
}

e.OnCommit.AddUTXO(utxo)
e.OnCommit.AddRewardUTXO(tx.TxID, utxo)
}

nodeID = uStakerTx.Validator.ID()
startTime = vdrTx.StartTime()
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func TestRewardDelegatorTxExecuteOnCommit(t *testing.T) {
delDestSet := ids.ShortSet{}
delDestSet.Add(delRewardAddress)

expectedReward := uint64(1000000)
expectedReward := uint64(0)

oldVdrBalance, err := avax.GetBalance(env.state, vdrDestSet)
require.NoError(err)
Expand All @@ -293,15 +293,15 @@ func TestRewardDelegatorTxExecuteOnCommit(t *testing.T) {
require.NoError(err)
vdrReward, err := math.Sub64(commitVdrBalance, oldVdrBalance)
require.NoError(err)
require.NotZero(vdrReward, "expected delegatee balance to increase because of reward")
require.Zero(vdrReward, "expected delegatee balance to be zero")

commitDelBalance, err := avax.GetBalance(env.state, delDestSet)
require.NoError(err)
delReward, err := math.Sub64(commitDelBalance, oldDelBalance)
require.NoError(err)
require.NotZero(delReward, "expected delegator balance to increase because of reward")
require.Zero(delReward, "expected delegator balance to be zero")

require.Less(vdrReward, delReward, "the delegator's reward should be greater than the delegatee's because the delegatee's share is 25%")
require.Equal(vdrReward, delReward, "the delegator's reward should be greater than the delegatee's because the delegatee's share is 25%")
require.Equal(expectedReward, delReward+vdrReward, "expected total reward to be %d but is %d", expectedReward, delReward+vdrReward)

stake, ok = set.GetWeight(vdrNodeID)
Expand Down
94 changes: 90 additions & 4 deletions coreth/core/governance_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ var (
flareInitialAirdropChangeActivationTime = big.NewInt(time.Date(2022, time.November, 10, 15, 0, 0, 0, time.UTC).Unix())
costwoInitialAirdropChangeActivationTime = big.NewInt(time.Date(2022, time.October, 27, 20, 0, 0, 0, time.UTC).Unix())
localFlareInitialAirdropChangeActivationTime = big.NewInt(time.Date(2022, time.January, 1, 0, 0, 0, 0, time.UTC).Unix())

flareDistributionChangeActivationTime = big.NewInt(time.Date(2023, time.February, 16, 15, 0, 0, 0, time.UTC).Unix())
costwoDistributionChangeActivationTime = big.NewInt(time.Date(2023, time.January, 26, 15, 0, 0, 0, time.UTC).Unix())
localFlareDistributionChangeActivationTime = big.NewInt(time.Date(2023, time.January, 1, 0, 0, 0, 0, time.UTC).Unix())
)

func GetGovernanceSettingIsActivatedAndCalled(chainID *big.Int, blockTime *big.Int, to common.Address) bool {
Expand Down Expand Up @@ -73,6 +77,28 @@ func GetInitialAirdropChangeIsActivatedAndCalled(chainID *big.Int, blockTime *bi
}
}

func GetDistributionChangeIsActivatedAndCalled(chainID *big.Int, blockTime *big.Int, to common.Address) bool {
switch {
case chainID.Cmp(params.FlareChainID) == 0 && blockTime.Cmp(flareDistributionChangeActivationTime) >= 0:
switch blockTime {
default:
return to == common.HexToAddress("0x4d1c42F41555Ae35DfC1819bd718f7D9Fb28abdD")
}
case chainID.Cmp(params.CostwoChainID) == 0 && blockTime.Cmp(costwoDistributionChangeActivationTime) >= 0:
switch blockTime {
default:
return to == common.HexToAddress("0xdF1deD5f1905C5012cbeE8367e3F4849afEAE545")
}
case chainID.Cmp(params.LocalFlareChainID) == 0 && blockTime.Cmp(localFlareDistributionChangeActivationTime) >= 0:
switch blockTime {
default:
return to == common.HexToAddress("0x1000000000000000000000000000000000000009")
}
default:
return false
}
}

// Signalling block.coinbase value
// address public constant SIGNAL_COINBASE = address(0x00000000000000000000000000000000000DEaD0);
//https://gitlab.com/flarenetwork/flare-smart-contracts/-/blob/4bb79bfe7266b43ea46e681f8a86ab8b9ef36446/contracts/genesis/implementation/GovernanceSettings.sol#L17
Expand All @@ -91,6 +117,13 @@ func GetInitialAirdropChangeCoinbaseSignalAddr(chainID *big.Int, blockTime *big.
}
}

func GetDistributionChangeCoinbaseSignalAddr(chainID *big.Int, blockTime *big.Int) common.Address {
switch {
default:
return common.HexToAddress("0x00000000000000000000000000000000000deAD3")
}
}

// function setGovernanceAddress(address _newGovernance) external
// https://gitlab.com/flarenetwork/flare-smart-contracts/-/blob/4bb79bfe7266b43ea46e681f8a86ab8b9ef36446/contracts/genesis/implementation/GovernanceSettings.sol#L73

Expand Down Expand Up @@ -121,6 +154,16 @@ func UpdateInitialAirdropAddressSelector(chainID *big.Int, blockTime *big.Int) [
}
}

// function updateDistributionAddress() external
// https://flare-explorer.flare.network/address/0x4d1c42F41555Ae35DfC1819bd718f7D9Fb28abdD/contracts#address-tabs

func UpdateDistributionAddressSelector(chainID *big.Int, blockTime *big.Int) []byte {
switch {
default:
return []byte{0x5a, 0xce, 0x4f, 0x0d}
}
}

func NewGovernanceAddressIsPermitted(chainID *big.Int, blockTime *big.Int, newGovernanceAddress common.Address) bool {
switch {
case chainID.Cmp(params.FlareChainID) == 0:
Expand All @@ -141,7 +184,7 @@ func NewGovernanceAddressIsPermitted(chainID *big.Int, blockTime *big.Int, newGo
case chainID.Cmp(params.LocalFlareChainID) == 0:
switch {
case blockTime.Cmp(big.NewInt(time.Date(2022, time.January, 1, 0, 0, 0, 0, time.UTC).Unix())) >= 0:
return newGovernanceAddress == common.HexToAddress("0x1000000000000000000000000000000000000009")
return newGovernanceAddress == common.HexToAddress("0x100000000000000000000000000000000000000f")
default:
return false
}
Expand Down Expand Up @@ -192,6 +235,13 @@ func GetInitialAirdropContractAddress(chainID *big.Int, blockTime *big.Int) comm
}
}

func GetDistributionContractAddress(chainID *big.Int, blockTime *big.Int) common.Address {
switch {
default:
return common.HexToAddress("0x1000000000000000000000000000000000000004")
}
}

func GetTargetAirdropContractAddress(chainID *big.Int, blockTime *big.Int) common.Address {
switch {
case chainID.Cmp(params.FlareChainID) == 0:
Expand All @@ -209,6 +259,23 @@ func GetTargetAirdropContractAddress(chainID *big.Int, blockTime *big.Int) commo
}
}

func GetTargetDistributionContractAddress(chainID *big.Int, blockTime *big.Int) common.Address {
switch {
case chainID.Cmp(params.FlareChainID) == 0:
switch {
default:
return common.HexToAddress("0xbe653C54DF337F13Fcb726101388F4a4803049F3")
}
case chainID.Cmp(params.CostwoChainID) == 0:
switch {
default:
return common.HexToAddress("0xC83Ec6a4aFf2099942836860A28C7e248Fabc32C")
}
default:
return common.HexToAddress("0x000000000000000000000000000000000000dEaD")
}
}

func (st *StateTransition) SetGovernanceAddress(chainID *big.Int, timestamp *big.Int, newGovernanceAddress []byte) error {
if NewGovernanceAddressIsPermitted(chainID, timestamp, common.BytesToAddress(newGovernanceAddress)) {
coinbaseSignal := GetGovernanceSettingsCoinbaseSignalAddr(chainID, timestamp)
Expand All @@ -217,7 +284,7 @@ func (st *StateTransition) SetGovernanceAddress(chainID *big.Int, timestamp *big
st.evm.Context.Coinbase = originalCoinbase
}()
st.evm.Context.Coinbase = coinbaseSignal
_, _, err := st.evm.Call(vm.AccountRef(coinbaseSignal), st.to(), st.data, st.evm.Context.GasLimit, big.NewInt(0))
_, _, _, err := st.evm.DaemonCall(vm.AccountRef(coinbaseSignal), st.to(), st.data, st.evm.Context.GasLimit)
if err != nil {
return err
}
Expand All @@ -233,7 +300,7 @@ func (st *StateTransition) SetTimelock(chainID *big.Int, timestamp *big.Int, new
st.evm.Context.Coinbase = originalCoinbase
}()
st.evm.Context.Coinbase = coinbaseSignal
_, _, err := st.evm.Call(vm.AccountRef(coinbaseSignal), st.to(), st.data, st.evm.Context.GasLimit, big.NewInt(0))
_, _, _, err := st.evm.DaemonCall(vm.AccountRef(coinbaseSignal), st.to(), st.data, st.evm.Context.GasLimit)
if err != nil {
return err
}
Expand All @@ -248,7 +315,7 @@ func (st *StateTransition) UpdateInitialAirdropAddress(chainID *big.Int, timesta
st.evm.Context.Coinbase = originalCoinbase
}()
st.evm.Context.Coinbase = coinbaseSignal
_, _, err := st.evm.Call(vm.AccountRef(coinbaseSignal), st.to(), st.data, st.evm.Context.GasLimit, big.NewInt(0))
_, _, _, err := st.evm.DaemonCall(vm.AccountRef(coinbaseSignal), st.to(), st.data, st.evm.Context.GasLimit)
if err != nil {
return err
}
Expand All @@ -259,3 +326,22 @@ func (st *StateTransition) UpdateInitialAirdropAddress(chainID *big.Int, timesta
st.state.AddBalance(targetAidropAddress, airdropBalance)
return nil
}

func (st *StateTransition) UpdateDistributionAddress(chainID *big.Int, timestamp *big.Int) error {
coinbaseSignal := GetDistributionChangeCoinbaseSignalAddr(chainID, timestamp)
originalCoinbase := st.evm.Context.Coinbase
defer func() {
st.evm.Context.Coinbase = originalCoinbase
}()
st.evm.Context.Coinbase = coinbaseSignal
_, _, _, err := st.evm.DaemonCall(vm.AccountRef(coinbaseSignal), st.to(), st.data, st.evm.Context.GasLimit)
if err != nil {
return err
}
distributionAddress := GetDistributionContractAddress(chainID, timestamp)
targetDistributionAddress := GetTargetDistributionContractAddress(chainID, timestamp)
distributionBalance := st.state.GetBalance(distributionAddress)
st.state.SubBalance(distributionAddress, distributionBalance)
st.state.AddBalance(targetDistributionAddress, distributionBalance)
return nil
}
2 changes: 1 addition & 1 deletion coreth/core/state_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (st *StateTransition) FinalisePreviousRound(chainID *big.Int, timestamp *bi
// by this check: burnAddress == common.HexToAddress("0x0100000000000000000000000000000000000000") on line 373, which occurs
// right before st.FinalisePreviousRound(chainID, timestamp, st.data[4:36]) is called.
// 2) Know the private key to the address 0x00000000000000000000000000000000000DEaD1 in order to become msg.sender.
_, _, err = st.evm.Call(vm.AccountRef(coinbaseSignal), st.to(), finalisedData, st.evm.Context.GasLimit, big.NewInt(0))
_, _, _, err = st.evm.DaemonCall(vm.AccountRef(coinbaseSignal), st.to(), finalisedData, st.evm.Context.GasLimit)
if err != nil {
return err
}
Expand Down
7 changes: 7 additions & 0 deletions coreth/core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,13 @@ func (st *StateTransition) TransitionDb() (*ExecutionResult, error) {
log.Warn("Error updating initialAirdrop contract", "error", err)
}
}
} else if GetDistributionChangeIsActivatedAndCalled(chainID, timestamp, *msg.To()) && len(st.data) == 4 {
if bytes.Equal(st.data[0:4], UpdateDistributionAddressSelector(chainID, timestamp)) {
err = st.UpdateDistributionAddress(chainID, timestamp)
if err != nil {
log.Warn("Error updating distribution contract", "error", err)
}
}
}
}
}
Expand Down

0 comments on commit 9351b98

Please sign in to comment.