Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated ETHDKG with new flow and contract bindings #66

Merged
merged 52 commits into from
Feb 22, 2022

Conversation

ricardopinto
Copy link
Contributor

@ricardopinto ricardopinto commented Feb 16, 2022

Scope

What is changing with this PR?

This PR introduces improvements to the ETHDKG flow enabling it to execute faster on good conditions, as well as leverage the new smart contracts' logic. It also introduces improvements in state management, retry logics, scheduling of tasks, and a great deal of testing. This new ETHDKG flow is event driven and allows for the process to complete quicker than before, with less phases to go through, and achieves greater code coverage.

Why?

Why are we doing this?

We're doing this to simplify the existing flow, speed up the process and integrate with the latest and greatest ETHDKG smart contracts.

Follow up stories

If any, what are the follow-up tasks required other than merging this PR? Have they been arranged?

vtleonardo and others added 30 commits January 12, 2022 11:51
- Fixed ShouldRetry logic on tasks
- ETHDKG can now be restarted
- Improved RetrieveParticipants to be faster and more efficient
…pulling data from the chain

- Improved scheduling management in regards to ShouldRetry logic that now relies on start and end blocks set when the task is created
- Fixed bug where evicting a validator would cause access out of array boundaries due to poor state management
- Cleanups
- Fixed base genesis file to support london fork
- Improved code reusability on registration (NeedsRegistration)
…ncluding a task, to avoid race conditions on ethereum forks
- Improved WaitConfirmations() method to use eth.GetFinalityDelay()
… to test it properly

- Improved testing structure for better state management and keeping DRY tests
- Tested bad distributed shares
done = true
}

time.Sleep(5 * time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code smell

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will be addressed and fixed in the following ticket: https://madhive.atlassian.net/browse/MP-299

@@ -13,43 +13,72 @@ import (
func SetupEventMap(em *objects.EventMap, cdb *db.Database, adminHandler interfaces.AdminHandler, depositHandler interfaces.DepositHandler) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not hardcode the event hashes as this will result in silent failure when an event is updated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

parse from bindings

if err != nil {
return err
}

epoch := uint32(event.Epoch.Int64())

index := uint8(event.Index.Uint64()) - 1
index := uint32(event.Index.Uint64()) // - 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clean up how this index is used below

if err != nil {
return err
}
// err = checkValidatorSet(state, epoch, logger, adminHandler)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove, not used here

logger.WithField("EventID", eventID).
WithField("info", info.Name).
Info("event received")
} /* else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clean up

}

// GetSortedParticipants returns the participant list sorted by Index field
func (state *DkgState) GetSortedParticipants() ParticipantList {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to make sure this has state.lock/unlock where used, see previous state locking comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for functions below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -66,7 +67,7 @@ func (registry *TypeRegistry) WrapInstance(t interface{}) (*InstanceWrapper, err

name, present := registry.lookupName(tipe)
if !present {
return nil, ErrUnknownType
return nil, fmt.Errorf("unable to wrapInstance: %v", tipe)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

evaluate if panic would be better here

t.Log("error:", err)
assert.NotNil(t, err)

// _, err = s.Schedule(6, 14, task)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clean up


// These are the valid phases of ETHDKG
const (
Registration EthDKGPhase = iota
RegistrationOpen EthDKGPhase = iota
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iota + 1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

force default value is invalid

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

! investigate, if necessary preserve.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we get through the bindings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bindings don't give us this information yet ethereum/EIPs#47

@@ -146,6 +146,12 @@ func (b *Behind) collectReceipts() {
b.logger.Debugf("receipt not found: %v", txn.Hex())
return
}

//This only could happen using a SimulatedBackend during the tests
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build tag?

}
eu := &blockchain.Updater{Updater: ethdkgUpdater, TxnOpts: txnOpts, Logger: logger}
eth.Queue().QueueGroupTransaction(ctx, MIGRATION_GRP, eu.Add("migrate(uint256,uint32,uint32,uint256[4],address[],uint256[4][])", migrateEthDKGAddr))
// // Wire EthDKG migration contract
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clean up

@@ -252,8 +264,13 @@ func register(logger *logrus.Entry, eth interfaces.Ethereum, cmd *cobra.Command,

// More ethereum setup
acct := eth.GetDefaultAccount()

if acct.Address.String() == "0x546F99F244b7B58B855330AE0E2BC1b30b41302F" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no magic values / hardcoded addresses

logger.Errorf("StakingToken.Approve() failed")
var retry bool = true
for retry {
txn, err := c.StakingToken().Approve(txnOpts, c.ValidatorsAddress(), big.NewInt(1_000_000))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no magic values, resolve from contracts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is going to be deleted soon because we'll be using the StakeNFT contract for this logic

logger.Errorf("Staking.LockStake() failed")
retry = true
for retry {
txn, err := c.Staking().LockStake(txnOpts, big.NewInt(1_000_000))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is going to be deleted soon because we'll be using the StakeNFT contract for this logic

}
if rcpt != nil && rcpt.Status != 1 {
logger.Errorf("StakingToken.Approve() failed")
var retry bool = true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bound these for-loops

@@ -485,6 +516,82 @@ func deposittokens(logger *logrus.Entry, eth interfaces.Ethereum, cmd *cobra.Com
return 0
}

func testtx(logger *logrus.Entry, eth interfaces.Ethereum, cmd *cobra.Command, args []string) int {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is scary

@@ -581,17 +688,10 @@ func ethdkg(logger *logrus.Entry, eth interfaces.Ethereum, cmd *cobra.Command, a

for _, log := range logs {
if log.Topics[0].Hex() == "0x9c6f8368fe7e77e8cb9438744581403bcb3f53298e517f04c1b8475487402e97" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove magic values

// If we aren't registered correctly retry won't work -- Not true for registration
if status == NoRegistration || status == BadRegistration {
logger.Warnf("registration status: %v", status)
if currentBlock >= expectedLastBlock || currentBlock < expectedFirstBlock {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oddity around expectedFirstBlock

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

investigate

Copy link
Contributor

@Cr0wn-Gh0ul Cr0wn-Gh0ul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See and evaluate comments, make tickets as necessary. Fix only required comments for merge.

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

@Cr0wn-Gh0ul Cr0wn-Gh0ul merged commit 32e7ce6 into MadBase:candidate Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants