Skip to content

Conversation

@pdyraga
Copy link
Member

@pdyraga pdyraga commented Jul 7, 2022

Depends on #3062

Refs #3006

This PR implements Ethereum chain functions for Random Beacon Sortition Pool. The following functions are implemented for Ethereum:

OperatorToStakingProvider() (chain.Address, bool, error)
EligibleStake(stakingProvider chain.Address) (*big.Int, error)
IsPoolLocked() (bool, error)
IsOperatorInPool() (bool, error)
JoinSortitionPool() error

Some additional changes were necessary to instantiate RandomBeacon and SortitionPool contract references:

  • The base Ethereum chain needs to provide the chain ID, transaction mutex, nonce manager, and mining waiter.
  • We need to ensure there is just one instance of transaction mutex. This mutex is held by the base Ethereum chain so there has to be only one instance of Ethereum base chain. The idea is to call global Connect only one time and create all the chain instances only one time. Creating chain instances outside of Connect is not possible from the outside of ethereum package.
  • We do not need to instantiate ethclient.Client outside of Connect, this is the action that should be executed upon establishing the connection, so Connect function is the right place.

RandomBeacon contract binding is now initialized for Ethereum bBacon chain
implementation.

This required some changes to the base Ethereum chain implementation. The base
chain needs to provide the chain ID, transaction mutex, nonce manager, and
mining waiter.

Note that the current implementation is not entirely correct because the base
Ethereum chain is instantiated inside Beacon chain. We need to have the
transaction mutex created only one time, hence the base chain should be
created outside of application-specific chains.
pdyraga added 5 commits July 7, 2022 09:49
…ains

We need to ensure there is just one instance of transactionMutex. This mutex is
held by the base Ethereum chain so there has to be only one instance of Ethereum
base chain. The idea is to keep the base chain as a singleton and create
application chains using public functions of the base chain (to be implemented
separately).
We need to ensure there is just one instance of transaction mutex. This mutex is
held by the base Ethereum chain so there has to be only one instance of Ethereum
base chain. The idea is to call global `Connect` only one time and create all
the chain instances only one time. Creating chain instances outside of `Connect`
is not possible from the outside of `ethereum` package.
We do not need to instantiate ethclient.Client outside of Connect,
this is the action that should be executed upon establishing the
connection so Connect function is the right place.
All functions of sortition.Chain interface are now implemented in the
RandomBeacon Ethereum chain implementation.
@pdyraga
Copy link
Member Author

pdyraga commented Jul 7, 2022

There are no unit tests for Ethereum implementation, as they would require having an Ethereum client instance running. This will be covered by system tests. To test functions during the review, the following simple implementation can be used along with scripts/install.sh and scripts/initialize.sh:

package ethereum

import (
	"context"
	"fmt"
	"testing"

	"github.com/keep-network/keep-common/pkg/chain/ethereum"
)

func TestBeaconChain_KnownOperator(t *testing.T) {
	ctx := context.Background()

	config := ethereum.Config{}

	config.URL = "http://127.0.0.1:8545"
	config.Account.KeyFile = "/Users/piotr/dev/ethereum/data/keystore/UTC--2018-11-01T06-24-04.418691008Z--65ea55c1f10491038425725dc00dffeab2a1e28a"
	config.Account.KeyFilePassword = "password"
	config.ContractAddresses = make(map[string]string)
	config.ContractAddresses["RandomBeacon"] = "0x33a4bB615E56bB7538cFF511C4dc0A66fb1B27F8"

	beacon, _, err := Connect(ctx, config)
	if err != nil {
		t.Fatal(err)
	}

	stakingProvider, ok, err := beacon.OperatorToStakingProvider()
	if err != nil {
		t.Fatal(err)
	}
	fmt.Printf("stakingProvider = %v, ok = %v\n", stakingProvider, ok)

	eligibleStake, err := beacon.EligibleStake("0x65ea55c1f10491038425725dc00dffeab2a1e28a")
	if err != nil {
		t.Fatal(err)
	}
	fmt.Printf("eligibleStake = %v\n", eligibleStake)

	eligibleStake, err = beacon.EligibleStake("0x33a4bB615E56bB7538cFF511C4dc0A66fb1B27F8")
	if err != nil {
		t.Fatal(err)
	}
	fmt.Printf("eligibleStake = %v\n", eligibleStake)

	isPoolLocked, err := beacon.IsPoolLocked()
	if err != nil {
		t.Fatal(err)
	}
	fmt.Printf("isPoolLocked = %v\n", isPoolLocked)

	isOperatorInPool, err := beacon.IsOperatorInPool()
	if err != nil {
		t.Fatal(err)
	}
	fmt.Printf("isOperatorInPool = %v\n", isOperatorInPool)

	err = beacon.JoinSortitionPool()
	if err != nil {
		t.Fatal(err)
	}

	isOperatorInPool, err = beacon.IsOperatorInPool()
	if err != nil {
		t.Fatal(err)
	}
	fmt.Printf("isOperatorInPool = %v\n", isOperatorInPool)
}

@pdyraga pdyraga requested a review from lukasz-zimnoch July 7, 2022 09:42
Base automatically changed from pkg-chain-refactoring to main July 7, 2022 11:53
@pdyraga pdyraga changed the title Instantiate RandomBeacon contract for the Beacon Ethereum chain RandomBeacon contract for the Beacon Ethereum chain - sortition functions Jul 7, 2022
@pdyraga pdyraga marked this pull request as ready for review July 7, 2022 11:56
@pdyraga pdyraga self-assigned this Jul 7, 2022
@pdyraga pdyraga added this to the v2.0.0-m1 milestone Jul 7, 2022
There is no reason to create a new context if we have one passed as a
parameter.
@lukasz-zimnoch lukasz-zimnoch enabled auto-merge July 7, 2022 12:37
@lukasz-zimnoch lukasz-zimnoch merged commit 3775a90 into main Jul 7, 2022
@lukasz-zimnoch lukasz-zimnoch deleted the sortition-chain branch July 7, 2022 12:54
@pdyraga pdyraga modified the milestones: v2.0.0-m0, v2.0.0-m4 Sep 30, 2022
@pdyraga pdyraga removed their assignment Sep 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants