Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b134702
Implemented basic structure of firewall
tomaszslabon Jul 8, 2022
4954e86
Implemented function returning roles of a staking provider from Token…
tomaszslabon Jul 11, 2022
367ccff
Merge branch 'main' into firewall-checks
tomaszslabon Jul 11, 2022
4a657f0
Adjusted imports to new package structure
tomaszslabon Jul 11, 2022
87fab03
Adjusted operator to staking provider resolution function so that it …
tomaszslabon Jul 11, 2022
8c37dbe
Added missing comments to functions
tomaszslabon Jul 11, 2022
49aa2c6
Merge branch 'main' into firewall-checks
tomaszslabon Jul 12, 2022
0031a3b
Added OperatorToStakingProvider to the Chain interface
tomaszslabon Jul 12, 2022
ae0a759
Brought back caching in firewall
tomaszslabon Jul 12, 2022
ef19e7e
Brought back firewall unit tests
tomaszslabon Jul 12, 2022
63fd1a3
Renamed variables
tomaszslabon Jul 12, 2022
66f1efb
Replaced repeating bytes with using zero initialized Address during c…
tomaszslabon Jul 12, 2022
1c58d94
Replaced stake monitor with application checking policy as firewall
tomaszslabon Jul 12, 2022
7300195
Merge branch 'main' into firewall-checks
tomaszslabon Jul 13, 2022
6bfd8b4
Removed stake monitor
tomaszslabon Jul 13, 2022
472811b
Added tests for firewall
tomaszslabon Jul 13, 2022
844715d
Improved function descriptions
tomaszslabon Jul 13, 2022
17be711
Added a TODO
tomaszslabon Jul 13, 2022
b42276e
Minor changes: renames, comment changes, log formatting
tomaszslabon Jul 14, 2022
b72738a
Used testutils package when testing firewall
tomaszslabon Jul 14, 2022
d76f335
Minor comment changes
tomaszslabon Jul 14, 2022
f3da1d0
Used reflect.DeepEqual in testutils rather than simple equality compa…
tomaszslabon Jul 14, 2022
db75026
Made firewall validation fail if there is an error returned
tomaszslabon Jul 14, 2022
6f58c8b
Enabled firewall
tomaszslabon Jul 14, 2022
cb1f6f7
Added assertion for chained error matching in test utils
tomaszslabon Jul 14, 2022
3380072
Added wallet registry handle to TbtcChain
tomaszslabon Jul 14, 2022
3e96c77
Renamed receiver variable iname in firewall
tomaszslabon Jul 14, 2022
75be1d4
Moved error checking right after error obtained
tomaszslabon Jul 14, 2022
cd2144f
Updated sample toml config file
tomaszslabon Jul 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions cmd/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,7 @@ func (pm *PongMessage) Unmarshal(bytes []byte) error {
}

// getBootstrapPeerOperatorKey returns hardcoded public and private operator key
// of the bootstrap peer. We hardcode those values because we need to initialize
// stakes on both sides of the connection using the local, stubbed `StakeMonitor`.
// of the bootstrap peer.
func getBootstrapPeerOperatorKey() (
*operator.PrivateKey,
*operator.PublicKey,
Expand All @@ -313,8 +312,7 @@ func getBootstrapPeerOperatorKey() (
}

// getStandardPeerOperatorKey returns hardcoded public and private operator key
// of the standard peer. We hardcode those values because we need to initialize
// stake on both sides of the connection using local, stubbed `StakeMonitor`.
// of the standard peer.
func getStandardPeerOperatorKey() (
*operator.PrivateKey,
*operator.PublicKey,
Expand Down
9 changes: 6 additions & 3 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func Start(c *cli.Context) error {
config.LibP2P.Port = c.Int(portFlag)
}

beaconChain, _, err := ethereum.Connect(ctx, config.Ethereum)
beaconChain, tbtcChain, err := ethereum.Connect(ctx, config.Ethereum)
if err != nil {
return fmt.Errorf("error connecting to Ethereum node: [%v]", err)
}
Expand All @@ -77,13 +77,16 @@ func Start(c *cli.Context) error {
return fmt.Errorf("failed to get block counter: [%v]", err)
}

firewall := firewall.AnyApplicationPolicy(
[]firewall.Application{beaconChain, tbtcChain},
)

netProvider, err := libp2p.Connect(
ctx,
config.LibP2P,
operatorPrivateKey,
libp2p.ProtocolBeacon,
// TODO: Use the firewall policy developed in https://github.com/keep-network/keep-core/pull/3060
firewall.Disabled,
firewall,
retransmission.NewTicker(blockCounter.WatchBlocks(ctx)),
)
if err != nil {
Expand Down
7 changes: 2 additions & 5 deletions configs/config.toml.SAMPLE
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,9 @@
KeyFile = "/Users/someuser/ethereum/data/keystore/UTC--2018-03-11T01-37-33.202765887Z--AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA8AAAAAAAAA"

[ethereum.ContractAddresses]
TokenStaking = "0xBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
RandomBeacon = "0xBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"

# Legacy v1 contracts, do not have to be set. Soon to be removed.
KeepRandomBeaconOperator = "0xCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
TokenStaking = "0xCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
KeepRandomBeaconService = "0xCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"
WalletRegistry = "0xBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"

[LibP2P]
Peers = ["/ip4/127.0.0.1/tcp/3919/ipfs/16Uiu2HAmFRJtCWfdXhZEZHWb4tUpH1QMMgzH1oiamCfUuK6NgqWX"]
Expand Down
2 changes: 0 additions & 2 deletions pkg/beacon/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ type Interface interface {
BlockCounter() (chain.BlockCounter, error)
// Signing returns the chain's signer.
Signing() chain.Signing
// StakeMonitor returns the chain's stake monitor.
StakeMonitor() (chain.StakeMonitor, error)
// OperatorKeyPair returns the key pair of the operator assigned to this
// chain handle.
OperatorKeyPair() (*operator.PrivateKey, *operator.PublicKey, error)
Expand Down
2 changes: 1 addition & 1 deletion pkg/beacon/dkg/dkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func setup() {
gjkrResult = &gjkr.Result{GroupPublicKey: groupPublicKey}
dkgResultChannel = make(chan *event.DKGResultSubmission, 1)
startPublicationBlockHeight = uint64(0)
localChain := local_v1.Connect(5, 3, big.NewInt(10))
localChain := local_v1.Connect(5, 3)
beaconChain = localChain
blockCounter, _ = beaconChain.BlockCounter()
}
Expand Down
3 changes: 0 additions & 3 deletions pkg/beacon/dkg/result/signing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package result

import (
"github.com/keep-network/keep-core/pkg/chain/local_v1"
"math/big"
"reflect"
"testing"

Expand Down Expand Up @@ -310,7 +309,6 @@ func initializeSigningMembers(groupSize int) (
) {
honestThreshold := groupSize/2 + 1
dishonestThreshold := groupSize - honestThreshold
minimumStake := big.NewInt(200)

dkgGroup := group.NewDkgGroup(dishonestThreshold, groupSize)

Expand All @@ -334,7 +332,6 @@ func initializeSigningMembers(groupSize int) (
localChain := local_v1.ConnectWithKey(
groupSize,
honestThreshold,
minimumStake,
operatorPrivateKey,
)

Expand Down
3 changes: 1 addition & 2 deletions pkg/beacon/dkg/result/submission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package result

import (
"github.com/keep-network/keep-core/pkg/chain/local_v1"
"math/big"
"testing"

beaconchain "github.com/keep-network/keep-core/pkg/beacon/chain"
Expand Down Expand Up @@ -221,7 +220,7 @@ func initChainHandle(honestThreshold int, groupSize int) (
uint64,
error,
) {
chainHandle := local_v1.Connect(groupSize, honestThreshold, big.NewInt(200))
chainHandle := local_v1.Connect(groupSize, honestThreshold)

blockCounter, err := chainHandle.BlockCounter()
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions pkg/beacon/group/membership_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"github.com/keep-network/keep-core/pkg/chain"
"github.com/keep-network/keep-core/pkg/chain/local_v1"
"github.com/keep-network/keep-core/pkg/operator"
"math/big"
"testing"
)

func TestIsInGroup(t *testing.T) {
localChain := local_v1.Connect(3, 3, big.NewInt(100))
localChain := local_v1.Connect(3, 3)
signing := localChain.Signing()

publicKey1 := generatePublicKey(t)
Expand Down Expand Up @@ -43,7 +42,7 @@ func TestIsInGroup(t *testing.T) {
}

func TestIsValidMembership(t *testing.T) {
localChain := local_v1.Connect(3, 3, big.NewInt(100))
localChain := local_v1.Connect(3, 3)
signing := localChain.Signing()

publicKey1 := generatePublicKeyBytes(t)
Expand Down
4 changes: 2 additions & 2 deletions pkg/beacon/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var address = "0x65ea55c1f10491038425725dc00dffeab2a1e28a"
var relayEntryTimeout = uint64(15)

func TestMonitorRelayEntryOnChain_EntrySubmitted(t *testing.T) {
localChain := local_v1.Connect(5, 3, big.NewInt(200))
localChain := local_v1.Connect(5, 3)

node := &node{
beaconChain: localChain,
Expand Down Expand Up @@ -62,7 +62,7 @@ func TestMonitorRelayEntryOnChain_EntrySubmitted(t *testing.T) {
}

func TestMonitorRelayEntryOnChain_EntryNotSubmitted(t *testing.T) {
localChain := local_v1.Connect(5, 3, big.NewInt(200))
localChain := local_v1.Connect(5, 3)

node := &node{
beaconChain: localChain,
Expand Down
4 changes: 2 additions & 2 deletions pkg/beacon/registry/groups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var (
)

func TestRegisterGroup(t *testing.T) {
localChain := local_v1.Connect(5, 3, big.NewInt(200))
localChain := local_v1.Connect(5, 3)

gr := NewGroupRegistry(localChain, persistenceMock)

Expand All @@ -76,7 +76,7 @@ func TestRegisterGroup(t *testing.T) {
}

func TestLoadGroup(t *testing.T) {
localChain := local_v1.Connect(5, 3, big.NewInt(200))
localChain := local_v1.Connect(5, 3)
gr := NewGroupRegistry(localChain, persistenceMock)

if len(gr.myGroups) != 0 {
Expand Down
3 changes: 1 addition & 2 deletions pkg/beacon/state/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"github.com/keep-network/keep-core/pkg/chain/local_v1"
"math/big"
"reflect"
"testing"

Expand All @@ -20,7 +19,7 @@ var blockCounter chain.BlockCounter
func TestExecute(t *testing.T) {
testLog = make(map[uint64][]string)

localChain := local_v1.Connect(10, 5, big.NewInt(200))
localChain := local_v1.Connect(10, 5)
blockCounter, _ = localChain.BlockCounter()
provider := netLocal.Connect()
channel, err := provider.BroadcastChannelFor("transitions_test")
Expand Down
56 changes: 50 additions & 6 deletions pkg/chain/ethereum/beacon.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ethereum

import (
"bytes"
"context"
"encoding/binary"
"fmt"
Expand All @@ -19,6 +18,7 @@ import (
"github.com/keep-network/keep-common/pkg/chain/ethereum"
"github.com/keep-network/keep-core/pkg/chain"
"github.com/keep-network/keep-core/pkg/chain/ethereum/beacon/gen/contract"
"github.com/keep-network/keep-core/pkg/operator"
)

// Definitions of contract names.
Expand Down Expand Up @@ -131,16 +131,13 @@ func (bc *BeaconChain) OperatorToStakingProvider() (chain.Address, bool, error)
stakingProvider, err := bc.randomBeacon.OperatorToStakingProvider(bc.key.Address)
if err != nil {
return "", false, fmt.Errorf(
"failed to map operator %v to a staking provider: [%v]",
"failed to map operator [%v] to a staking provider: [%v]",
bc.key.Address,
err,
)
}

if bytes.Equal(
stakingProvider.Bytes(),
bytes.Repeat([]byte{0}, common.AddressLength),
) {
if (stakingProvider == common.Address{}) {
return "", false, nil
}

Expand Down Expand Up @@ -365,6 +362,53 @@ func (bc *BeaconChain) CalculateDKGResultHash(
return beaconchain.DKGResultHashFromBytes(hash)
}

// IsRecognized checks whether the given operator is recognized by the BeaconChain
// as eligible to join the network. If the operator has a stake delegation or
// had a stake delegation in the past, it will be recognized.
func (bc *BeaconChain) IsRecognized(operatorPublicKey *operator.PublicKey) (bool, error) {
operatorAddress, err := operatorPublicKeyToChainAddress(operatorPublicKey)
if err != nil {
return false, fmt.Errorf(
"cannot convert from operator key to chain address: [%v]",
err,
)
}

stakingProvider, err := bc.randomBeacon.OperatorToStakingProvider(
operatorAddress,
)
if err != nil {
return false, fmt.Errorf(
"failed to map operator [%v] to a staking provider: [%v]",
operatorAddress,
err,
)
}

if (stakingProvider == common.Address{}) {
return false, nil
}

// Check if the staking provider has an owner. This check ensures that there
// is/was a stake delegation for the given staking provider.
_, _, _, hasStakeDelegation, err := bc.Chain.RolesOf(
chain.Address(stakingProvider.Hex()),
)
if err != nil {
return false, fmt.Errorf(
"failed to check stake delegation for staking provider [%v]: [%v]",
stakingProvider,
err,
)
}

if !hasStakeDelegation {
return false, nil
}

return true, nil
}

// TODO: Implement a real SubmitRelayEntry function.
func (bc *BeaconChain) SubmitRelayEntry(
entry []byte,
Expand Down
42 changes: 40 additions & 2 deletions pkg/chain/ethereum/ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import (
"github.com/keep-network/keep-common/pkg/chain/ethereum"
"github.com/keep-network/keep-common/pkg/chain/ethereum/ethutil"
"github.com/keep-network/keep-common/pkg/rate"
"github.com/keep-network/keep-core/pkg/chain/ethereum/threshold/gen/contract"
)

// Definitions of contract names.
const (
TokenStakingContractName = "TokenStaking"
)

var logger = log.Logger("keep-chain-ethereum")
Expand Down Expand Up @@ -44,6 +50,8 @@ type Chain struct {
// nonce. Serializing submission ensures that each nonce is requested after
// a previous transaction has been submitted.
transactionMutex *sync.Mutex

tokenStaking *contract.TokenStaking
}

// Connect creates Random Beacon and TBTC Ethereum chain handles.
Expand All @@ -70,7 +78,7 @@ func Connect(
return nil, nil, fmt.Errorf("could not create beacon chain handle: [%v]", err)
}

tbtcChain, err := newTbtcChain(baseChain)
tbtcChain, err := newTbtcChain(config, baseChain)
if err != nil {
return nil, nil, fmt.Errorf("could not create TBTC chain handle: [%v]", err)
}
Expand Down Expand Up @@ -117,16 +125,46 @@ func newChain(

miningWaiter := ethutil.NewMiningWaiter(clientWithAddons, config)

transactionMutex := &sync.Mutex{}

// TODO: Consider adding the balance monitoring.

tokenStakingAddress, err := config.ContractAddress(TokenStakingContractName)
if err != nil {
return nil, fmt.Errorf(
"failed to resolve %s contract address: [%v]",
TokenStakingContractName,
err,
)
}

tokenStaking, err :=
contract.NewTokenStaking(
tokenStakingAddress,
chainID,
key,
client,
nonceManager,
miningWaiter,
blockCounter,
transactionMutex,
)
if err != nil {
return nil, fmt.Errorf(
"failed to attach to TokenStaking contract: [%v]",
err,
)
}

return &Chain{
key: key,
client: clientWithAddons,
chainID: chainID,
blockCounter: blockCounter,
nonceManager: nonceManager,
miningWaiter: miningWaiter,
transactionMutex: &sync.Mutex{},
transactionMutex: transactionMutex,
tokenStaking: tokenStaking,
}, nil
}

Expand Down
25 changes: 0 additions & 25 deletions pkg/chain/ethereum/stake_monitor.go

This file was deleted.

Loading