diff --git a/go.mod b/go.mod index 635c71bd7..a95461cff 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( github.com/gogo/protobuf v1.3.1 github.com/google/gofuzz v1.1.0 github.com/ipfs/go-log v1.0.4 - github.com/keep-network/keep-common v1.3.0 + github.com/keep-network/keep-common v1.3.1-0.20210127121021-c6adefdc082b github.com/keep-network/keep-core v1.3.2-0.20201229154408-59ac640ed0cb github.com/keep-network/tbtc v1.1.1-0.20201117095624-38508bdb562e github.com/pkg/errors v0.9.1 diff --git a/go.sum b/go.sum index 48f36c046..facfb6fbe 100644 --- a/go.sum +++ b/go.sum @@ -332,8 +332,9 @@ github.com/keep-network/cli v1.20.0/go.mod h1:nzsst4JjU+rGE8Q5J839fYxectxWHpLhxK github.com/keep-network/go-libp2p-bootstrap v0.0.0-20200423153828-ed815bc50aec h1:2pXAsi4OUUjZKr5ds5UOF2IxXN+jVW0WetVO+czkf+A= github.com/keep-network/go-libp2p-bootstrap v0.0.0-20200423153828-ed815bc50aec/go.mod h1:xR8jf3/VJAjh3nWu5tFe8Yxnt2HvWsqZHfGef1P5oDk= github.com/keep-network/keep-common v1.2.1-0.20201116151638-8af057907255/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo= -github.com/keep-network/keep-common v1.3.0 h1:4aWCznDfzk0kZS+bzlRxYqq5ffvuFlA/DvD+viptHfQ= github.com/keep-network/keep-common v1.3.0/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo= +github.com/keep-network/keep-common v1.3.1-0.20210127121021-c6adefdc082b h1:xWeLAJYE1jbzkkDFkNTKVjhAhslfH5bHaxWjM9Ba5R8= +github.com/keep-network/keep-common v1.3.1-0.20210127121021-c6adefdc082b/go.mod h1:emxogTbBdey7M3jOzfxZOdfn139kN2mI2b2wA6AHKKo= github.com/keep-network/keep-core v1.3.2-0.20201229154408-59ac640ed0cb h1:pDhLagUiOYWOn72UiG3jSDVDXZ4dYlsfRa0ToF/ru18= github.com/keep-network/keep-core v1.3.2-0.20201229154408-59ac640ed0cb/go.mod h1:4KezOJWc//c5lbtAE1ob7qYE/Gs5+a1QTjGBzzv2tu8= github.com/keep-network/tbtc v1.1.1-0.20201117095624-38508bdb562e h1:go3irX4olJUZOi59gaBX7iYIFFbJEV5/zbfCG4tOPaQ= diff --git a/pkg/chain/ethereum/ethereum.go b/pkg/chain/ethereum/ethereum.go index dcbb3c79b..0b1ffff2b 100644 --- a/pkg/chain/ethereum/ethereum.go +++ b/pkg/chain/ethereum/ethereum.go @@ -74,7 +74,7 @@ func (ec *EthereumChain) RegisterAsMemberCandidate(application common.Address) e func (ec *EthereumChain) OnBondedECDSAKeepCreated( handler func(event *eth.BondedECDSAKeepCreatedEvent), ) subscription.EventSubscription { - subscription, err := ec.bondedECDSAKeepFactoryContract.WatchBondedECDSAKeepCreated( + subscription := ec.bondedECDSAKeepFactoryContract.WatchBondedECDSAKeepCreated( func( KeepAddress common.Address, Members []common.Address, @@ -89,16 +89,10 @@ func (ec *EthereumChain) OnBondedECDSAKeepCreated( HonestThreshold: HonestThreshold.Uint64(), }) }, - func(err error) error { - return fmt.Errorf("watch keep created failed: [%v]", err) - }, nil, nil, nil, ) - if err != nil { - logger.Errorf("could not watch BondedECDSAKeepCreated event: [%v]", err) - } return subscription } @@ -116,10 +110,7 @@ func (ec *EthereumChain) OnKeepClosed( func(blockNumber uint64) { handler(ð.KeepClosedEvent{BlockNumber: blockNumber}) }, - func(err error) error { - return fmt.Errorf("keep closed callback failed: [%v]", err) - }, - ) + ), nil } // OnKeepTerminated installs a callback that is invoked on-chain when keep @@ -136,10 +127,7 @@ func (ec *EthereumChain) OnKeepTerminated( func(blockNumber uint64) { handler(ð.KeepTerminatedEvent{BlockNumber: blockNumber}) }, - func(err error) error { - return fmt.Errorf("keep terminated callback failed: [%v]", err) - }, - ) + ), nil } // OnPublicKeyPublished installs a callback that is invoked when an on-chain @@ -162,10 +150,7 @@ func (ec *EthereumChain) OnPublicKeyPublished( PublicKey: PublicKey, }) }, - func(err error) error { - return fmt.Errorf("keep created callback failed: [%v]", err) - }, - ) + ), nil } // OnConflictingPublicKeySubmitted installs a callback that is invoked when an @@ -190,11 +175,8 @@ func (ec *EthereumChain) OnConflictingPublicKeySubmitted( ConflictingPublicKey: ConflictingPublicKey, }) }, - func(err error) error { - return fmt.Errorf("keep created callback failed: [%v]", err) - }, nil, - ) + ), nil } // OnSignatureRequested installs a callback that is invoked on-chain @@ -218,11 +200,8 @@ func (ec *EthereumChain) OnSignatureRequested( BlockNumber: blockNumber, }) }, - func(err error) error { - return fmt.Errorf("keep signature requested callback failed: [%v]", err) - }, nil, - ) + ), nil } // SubmitKeepPublicKey submits a public key to a keep contract deployed under diff --git a/pkg/chain/gen/contract/BondedECDSAKeep.go b/pkg/chain/gen/contract/BondedECDSAKeep.go index fa64ec020..134282bd6 100644 --- a/pkg/chain/gen/contract/BondedECDSAKeep.go +++ b/pkg/chain/gen/contract/BondedECDSAKeep.go @@ -4,6 +4,7 @@ package contract import ( + "context" "fmt" "math/big" "strings" @@ -15,6 +16,7 @@ import ( "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/event" "github.com/ipfs/go-log" @@ -28,6 +30,21 @@ import ( // included or excluded from logging at startup by name. var becdsakLogger = log.Logger("keep-contract-BondedECDSAKeep") +const ( + // Maximum backoff time between event resubscription attempts. + becdsakSubscriptionBackoffMax = 2 * time.Minute + + // Threshold below which event resubscription emits an error to the logs. + // WS connection can be dropped at any moment and event resubscription will + // follow. However, if WS connection for event subscription is getting + // dropped too often, it may indicate something is wrong with Ethereum + // client. This constant defines the minimum lifetime of an event + // subscription required before the subscription failure happens and + // resubscription follows so that the resubscription does not emit an error + // to the logs alerting about potential problems with Ethereum client. + becdsakSubscriptionAlertThreshold = 15 * time.Minute +) + type BondedECDSAKeep struct { contract *abi.BondedECDSAKeep contractAddress common.Address @@ -94,137 +111,16 @@ func NewBondedECDSAKeep( // ----- Non-const Methods ------ // Transaction submission. -func (becdsak *BondedECDSAKeep) DistributeETHReward( - value *big.Int, - - transactionOptions ...ethutil.TransactionOptions, -) (*types.Transaction, error) { - becdsakLogger.Debug( - "submitting transaction distributeETHReward", - "value: ", value, - ) - - becdsak.transactionMutex.Lock() - defer becdsak.transactionMutex.Unlock() - - // create a copy - transactorOptions := new(bind.TransactOpts) - *transactorOptions = *becdsak.transactorOptions - - transactorOptions.Value = value - - if len(transactionOptions) > 1 { - return nil, fmt.Errorf( - "could not process multiple transaction options sets", - ) - } else if len(transactionOptions) > 0 { - transactionOptions[0].Apply(transactorOptions) - } - - nonce, err := becdsak.nonceManager.CurrentNonce() - if err != nil { - return nil, fmt.Errorf("failed to retrieve account nonce: %v", err) - } - - transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - - transaction, err := becdsak.contract.DistributeETHReward( - transactorOptions, - ) - if err != nil { - return transaction, becdsak.errorResolver.ResolveError( - err, - becdsak.transactorOptions.From, - value, - "distributeETHReward", - ) - } - - becdsakLogger.Infof( - "submitted transaction distributeETHReward with id: [%v] and nonce [%v]", - transaction.Hash().Hex(), - transaction.Nonce(), - ) - - go becdsak.miningWaiter.ForceMining( - transaction, - func(newGasPrice *big.Int) (*types.Transaction, error) { - transactorOptions.GasLimit = transaction.Gas() - transactorOptions.GasPrice = newGasPrice - - transaction, err := becdsak.contract.DistributeETHReward( - transactorOptions, - ) - if err != nil { - return transaction, becdsak.errorResolver.ResolveError( - err, - becdsak.transactorOptions.From, - value, - "distributeETHReward", - ) - } - - becdsakLogger.Infof( - "submitted transaction distributeETHReward with id: [%v] and nonce [%v]", - transaction.Hash().Hex(), - transaction.Nonce(), - ) - - return transaction, nil - }, - ) - - becdsak.nonceManager.IncrementNonce() - - return transaction, err -} - -// Non-mutating call, not a transaction submission. -func (becdsak *BondedECDSAKeep) CallDistributeETHReward( - value *big.Int, - blockNumber *big.Int, -) error { - var result interface{} = nil - - err := ethutil.CallAtBlock( - becdsak.transactorOptions.From, - blockNumber, value, - becdsak.contractABI, - becdsak.caller, - becdsak.errorResolver, - becdsak.contractAddress, - "distributeETHReward", - &result, - ) - - return err -} - -func (becdsak *BondedECDSAKeep) DistributeETHRewardGasEstimate() (uint64, error) { - var result uint64 - - result, err := ethutil.EstimateGas( - becdsak.callerOptions.From, - becdsak.contractAddress, - "distributeETHReward", - becdsak.contractABI, - becdsak.transactor, - ) - - return result, err -} - -// Transaction submission. -func (becdsak *BondedECDSAKeep) Withdraw( - _member common.Address, +func (becdsak *BondedECDSAKeep) SubmitPublicKey( + _publicKey []uint8, transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakLogger.Debug( - "submitting transaction withdraw", + "submitting transaction submitPublicKey", "params: ", fmt.Sprint( - _member, + _publicKey, ), ) @@ -250,22 +146,22 @@ func (becdsak *BondedECDSAKeep) Withdraw( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsak.contract.Withdraw( + transaction, err := becdsak.contract.SubmitPublicKey( transactorOptions, - _member, + _publicKey, ) if err != nil { return transaction, becdsak.errorResolver.ResolveError( err, becdsak.transactorOptions.From, nil, - "withdraw", - _member, + "submitPublicKey", + _publicKey, ) } becdsakLogger.Infof( - "submitted transaction withdraw with id: [%v] and nonce [%v]", + "submitted transaction submitPublicKey with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -276,22 +172,22 @@ func (becdsak *BondedECDSAKeep) Withdraw( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsak.contract.Withdraw( + transaction, err := becdsak.contract.SubmitPublicKey( transactorOptions, - _member, + _publicKey, ) if err != nil { return transaction, becdsak.errorResolver.ResolveError( err, becdsak.transactorOptions.From, nil, - "withdraw", - _member, + "submitPublicKey", + _publicKey, ) } becdsakLogger.Infof( - "submitted transaction withdraw with id: [%v] and nonce [%v]", + "submitted transaction submitPublicKey with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -306,8 +202,8 @@ func (becdsak *BondedECDSAKeep) Withdraw( } // Non-mutating call, not a transaction submission. -func (becdsak *BondedECDSAKeep) CallWithdraw( - _member common.Address, +func (becdsak *BondedECDSAKeep) CallSubmitPublicKey( + _publicKey []uint8, blockNumber *big.Int, ) error { var result interface{} = nil @@ -319,26 +215,26 @@ func (becdsak *BondedECDSAKeep) CallWithdraw( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "withdraw", + "submitPublicKey", &result, - _member, + _publicKey, ) return err } -func (becdsak *BondedECDSAKeep) WithdrawGasEstimate( - _member common.Address, +func (becdsak *BondedECDSAKeep) SubmitPublicKeyGasEstimate( + _publicKey []uint8, ) (uint64, error) { var result uint64 result, err := ethutil.EstimateGas( becdsak.callerOptions.From, becdsak.contractAddress, - "withdraw", + "submitPublicKey", becdsak.contractABI, becdsak.transactor, - _member, + _publicKey, ) return result, err @@ -515,30 +411,16 @@ func (becdsak *BondedECDSAKeep) SubmitSignatureFraudGasEstimate( } // Transaction submission. -func (becdsak *BondedECDSAKeep) Initialize( - _owner common.Address, - _members []common.Address, - _honestThreshold *big.Int, - _memberStake *big.Int, - _stakeLockDuration *big.Int, - _tokenStaking common.Address, - _keepBonding common.Address, - _keepFactory common.Address, +func (becdsak *BondedECDSAKeep) Withdraw( + _member common.Address, transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakLogger.Debug( - "submitting transaction initialize", + "submitting transaction withdraw", "params: ", fmt.Sprint( - _owner, - _members, - _honestThreshold, - _memberStake, - _stakeLockDuration, - _tokenStaking, - _keepBonding, - _keepFactory, + _member, ), ) @@ -564,36 +446,22 @@ func (becdsak *BondedECDSAKeep) Initialize( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsak.contract.Initialize( + transaction, err := becdsak.contract.Withdraw( transactorOptions, - _owner, - _members, - _honestThreshold, - _memberStake, - _stakeLockDuration, - _tokenStaking, - _keepBonding, - _keepFactory, + _member, ) if err != nil { return transaction, becdsak.errorResolver.ResolveError( err, becdsak.transactorOptions.From, nil, - "initialize", - _owner, - _members, - _honestThreshold, - _memberStake, - _stakeLockDuration, - _tokenStaking, - _keepBonding, - _keepFactory, + "withdraw", + _member, ) } becdsakLogger.Infof( - "submitted transaction initialize with id: [%v] and nonce [%v]", + "submitted transaction withdraw with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -604,36 +472,22 @@ func (becdsak *BondedECDSAKeep) Initialize( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsak.contract.Initialize( + transaction, err := becdsak.contract.Withdraw( transactorOptions, - _owner, - _members, - _honestThreshold, - _memberStake, - _stakeLockDuration, - _tokenStaking, - _keepBonding, - _keepFactory, + _member, ) if err != nil { return transaction, becdsak.errorResolver.ResolveError( err, becdsak.transactorOptions.From, nil, - "initialize", - _owner, - _members, - _honestThreshold, - _memberStake, - _stakeLockDuration, - _tokenStaking, - _keepBonding, - _keepFactory, + "withdraw", + _member, ) } becdsakLogger.Infof( - "submitted transaction initialize with id: [%v] and nonce [%v]", + "submitted transaction withdraw with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -648,15 +502,8 @@ func (becdsak *BondedECDSAKeep) Initialize( } // Non-mutating call, not a transaction submission. -func (becdsak *BondedECDSAKeep) CallInitialize( - _owner common.Address, - _members []common.Address, - _honestThreshold *big.Int, - _memberStake *big.Int, - _stakeLockDuration *big.Int, - _tokenStaking common.Address, - _keepBonding common.Address, - _keepFactory common.Address, +func (becdsak *BondedECDSAKeep) CallWithdraw( + _member common.Address, blockNumber *big.Int, ) error { var result interface{} = nil @@ -668,60 +515,39 @@ func (becdsak *BondedECDSAKeep) CallInitialize( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "initialize", + "withdraw", &result, - _owner, - _members, - _honestThreshold, - _memberStake, - _stakeLockDuration, - _tokenStaking, - _keepBonding, - _keepFactory, + _member, ) return err } -func (becdsak *BondedECDSAKeep) InitializeGasEstimate( - _owner common.Address, - _members []common.Address, - _honestThreshold *big.Int, - _memberStake *big.Int, - _stakeLockDuration *big.Int, - _tokenStaking common.Address, - _keepBonding common.Address, - _keepFactory common.Address, +func (becdsak *BondedECDSAKeep) WithdrawGasEstimate( + _member common.Address, ) (uint64, error) { var result uint64 result, err := ethutil.EstimateGas( becdsak.callerOptions.From, becdsak.contractAddress, - "initialize", + "withdraw", becdsak.contractABI, becdsak.transactor, - _owner, - _members, - _honestThreshold, - _memberStake, - _stakeLockDuration, - _tokenStaking, - _keepBonding, - _keepFactory, + _member, ) return result, err } // Transaction submission. -func (becdsak *BondedECDSAKeep) ReturnPartialSignerBonds( +func (becdsak *BondedECDSAKeep) DistributeETHReward( value *big.Int, transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakLogger.Debug( - "submitting transaction returnPartialSignerBonds", + "submitting transaction distributeETHReward", "value: ", value, ) @@ -749,7 +575,7 @@ func (becdsak *BondedECDSAKeep) ReturnPartialSignerBonds( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsak.contract.ReturnPartialSignerBonds( + transaction, err := becdsak.contract.DistributeETHReward( transactorOptions, ) if err != nil { @@ -757,12 +583,12 @@ func (becdsak *BondedECDSAKeep) ReturnPartialSignerBonds( err, becdsak.transactorOptions.From, value, - "returnPartialSignerBonds", + "distributeETHReward", ) } becdsakLogger.Infof( - "submitted transaction returnPartialSignerBonds with id: [%v] and nonce [%v]", + "submitted transaction distributeETHReward with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -773,7 +599,7 @@ func (becdsak *BondedECDSAKeep) ReturnPartialSignerBonds( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsak.contract.ReturnPartialSignerBonds( + transaction, err := becdsak.contract.DistributeETHReward( transactorOptions, ) if err != nil { @@ -781,12 +607,12 @@ func (becdsak *BondedECDSAKeep) ReturnPartialSignerBonds( err, becdsak.transactorOptions.From, value, - "returnPartialSignerBonds", + "distributeETHReward", ) } becdsakLogger.Infof( - "submitted transaction returnPartialSignerBonds with id: [%v] and nonce [%v]", + "submitted transaction distributeETHReward with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -801,7 +627,7 @@ func (becdsak *BondedECDSAKeep) ReturnPartialSignerBonds( } // Non-mutating call, not a transaction submission. -func (becdsak *BondedECDSAKeep) CallReturnPartialSignerBonds( +func (becdsak *BondedECDSAKeep) CallDistributeETHReward( value *big.Int, blockNumber *big.Int, ) error { @@ -814,20 +640,20 @@ func (becdsak *BondedECDSAKeep) CallReturnPartialSignerBonds( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "returnPartialSignerBonds", + "distributeETHReward", &result, ) return err } -func (becdsak *BondedECDSAKeep) ReturnPartialSignerBondsGasEstimate() (uint64, error) { +func (becdsak *BondedECDSAKeep) DistributeETHRewardGasEstimate() (uint64, error) { var result uint64 result, err := ethutil.EstimateGas( becdsak.callerOptions.From, becdsak.contractAddress, - "returnPartialSignerBonds", + "distributeETHReward", becdsak.contractABI, becdsak.transactor, ) @@ -836,16 +662,20 @@ func (becdsak *BondedECDSAKeep) ReturnPartialSignerBondsGasEstimate() (uint64, e } // Transaction submission. -func (becdsak *BondedECDSAKeep) Sign( - _digest [32]uint8, +func (becdsak *BondedECDSAKeep) SubmitSignature( + _r [32]uint8, + _s [32]uint8, + _recoveryID uint8, transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakLogger.Debug( - "submitting transaction sign", + "submitting transaction submitSignature", "params: ", fmt.Sprint( - _digest, + _r, + _s, + _recoveryID, ), ) @@ -871,22 +701,26 @@ func (becdsak *BondedECDSAKeep) Sign( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsak.contract.Sign( + transaction, err := becdsak.contract.SubmitSignature( transactorOptions, - _digest, + _r, + _s, + _recoveryID, ) if err != nil { return transaction, becdsak.errorResolver.ResolveError( err, becdsak.transactorOptions.From, nil, - "sign", - _digest, + "submitSignature", + _r, + _s, + _recoveryID, ) } becdsakLogger.Infof( - "submitted transaction sign with id: [%v] and nonce [%v]", + "submitted transaction submitSignature with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -897,22 +731,26 @@ func (becdsak *BondedECDSAKeep) Sign( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsak.contract.Sign( + transaction, err := becdsak.contract.SubmitSignature( transactorOptions, - _digest, + _r, + _s, + _recoveryID, ) if err != nil { return transaction, becdsak.errorResolver.ResolveError( err, becdsak.transactorOptions.From, nil, - "sign", - _digest, + "submitSignature", + _r, + _s, + _recoveryID, ) } becdsakLogger.Infof( - "submitted transaction sign with id: [%v] and nonce [%v]", + "submitted transaction submitSignature with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -927,8 +765,10 @@ func (becdsak *BondedECDSAKeep) Sign( } // Non-mutating call, not a transaction submission. -func (becdsak *BondedECDSAKeep) CallSign( - _digest [32]uint8, +func (becdsak *BondedECDSAKeep) CallSubmitSignature( + _r [32]uint8, + _s [32]uint8, + _recoveryID uint8, blockNumber *big.Int, ) error { var result interface{} = nil @@ -940,45 +780,44 @@ func (becdsak *BondedECDSAKeep) CallSign( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "sign", + "submitSignature", &result, - _digest, + _r, + _s, + _recoveryID, ) return err } -func (becdsak *BondedECDSAKeep) SignGasEstimate( - _digest [32]uint8, +func (becdsak *BondedECDSAKeep) SubmitSignatureGasEstimate( + _r [32]uint8, + _s [32]uint8, + _recoveryID uint8, ) (uint64, error) { var result uint64 result, err := ethutil.EstimateGas( becdsak.callerOptions.From, becdsak.contractAddress, - "sign", + "submitSignature", becdsak.contractABI, becdsak.transactor, - _digest, + _r, + _s, + _recoveryID, ) return result, err } // Transaction submission. -func (becdsak *BondedECDSAKeep) DistributeERC20Reward( - _tokenAddress common.Address, - _value *big.Int, +func (becdsak *BondedECDSAKeep) CloseKeep( transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakLogger.Debug( - "submitting transaction distributeERC20Reward", - "params: ", - fmt.Sprint( - _tokenAddress, - _value, - ), + "submitting transaction closeKeep", ) becdsak.transactionMutex.Lock() @@ -1003,24 +842,20 @@ func (becdsak *BondedECDSAKeep) DistributeERC20Reward( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsak.contract.DistributeERC20Reward( + transaction, err := becdsak.contract.CloseKeep( transactorOptions, - _tokenAddress, - _value, ) if err != nil { return transaction, becdsak.errorResolver.ResolveError( err, becdsak.transactorOptions.From, nil, - "distributeERC20Reward", - _tokenAddress, - _value, + "closeKeep", ) } becdsakLogger.Infof( - "submitted transaction distributeERC20Reward with id: [%v] and nonce [%v]", + "submitted transaction closeKeep with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -1031,24 +866,20 @@ func (becdsak *BondedECDSAKeep) DistributeERC20Reward( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsak.contract.DistributeERC20Reward( + transaction, err := becdsak.contract.CloseKeep( transactorOptions, - _tokenAddress, - _value, ) if err != nil { return transaction, becdsak.errorResolver.ResolveError( err, becdsak.transactorOptions.From, nil, - "distributeERC20Reward", - _tokenAddress, - _value, + "closeKeep", ) } becdsakLogger.Infof( - "submitted transaction distributeERC20Reward with id: [%v] and nonce [%v]", + "submitted transaction closeKeep with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -1063,9 +894,7 @@ func (becdsak *BondedECDSAKeep) DistributeERC20Reward( } // Non-mutating call, not a transaction submission. -func (becdsak *BondedECDSAKeep) CallDistributeERC20Reward( - _tokenAddress common.Address, - _value *big.Int, +func (becdsak *BondedECDSAKeep) CallCloseKeep( blockNumber *big.Int, ) error { var result interface{} = nil @@ -1077,49 +906,40 @@ func (becdsak *BondedECDSAKeep) CallDistributeERC20Reward( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "distributeERC20Reward", + "closeKeep", &result, - _tokenAddress, - _value, ) return err } -func (becdsak *BondedECDSAKeep) DistributeERC20RewardGasEstimate( - _tokenAddress common.Address, - _value *big.Int, -) (uint64, error) { +func (becdsak *BondedECDSAKeep) CloseKeepGasEstimate() (uint64, error) { var result uint64 result, err := ethutil.EstimateGas( becdsak.callerOptions.From, becdsak.contractAddress, - "distributeERC20Reward", + "closeKeep", becdsak.contractABI, becdsak.transactor, - _tokenAddress, - _value, ) return result, err } // Transaction submission. -func (becdsak *BondedECDSAKeep) SubmitSignature( - _r [32]uint8, - _s [32]uint8, - _recoveryID uint8, +func (becdsak *BondedECDSAKeep) DistributeERC20Reward( + _tokenAddress common.Address, + _value *big.Int, transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakLogger.Debug( - "submitting transaction submitSignature", + "submitting transaction distributeERC20Reward", "params: ", fmt.Sprint( - _r, - _s, - _recoveryID, + _tokenAddress, + _value, ), ) @@ -1145,26 +965,24 @@ func (becdsak *BondedECDSAKeep) SubmitSignature( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsak.contract.SubmitSignature( + transaction, err := becdsak.contract.DistributeERC20Reward( transactorOptions, - _r, - _s, - _recoveryID, + _tokenAddress, + _value, ) if err != nil { return transaction, becdsak.errorResolver.ResolveError( err, becdsak.transactorOptions.From, nil, - "submitSignature", - _r, - _s, - _recoveryID, + "distributeERC20Reward", + _tokenAddress, + _value, ) } becdsakLogger.Infof( - "submitted transaction submitSignature with id: [%v] and nonce [%v]", + "submitted transaction distributeERC20Reward with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -1175,26 +993,24 @@ func (becdsak *BondedECDSAKeep) SubmitSignature( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsak.contract.SubmitSignature( + transaction, err := becdsak.contract.DistributeERC20Reward( transactorOptions, - _r, - _s, - _recoveryID, + _tokenAddress, + _value, ) if err != nil { return transaction, becdsak.errorResolver.ResolveError( err, becdsak.transactorOptions.From, nil, - "submitSignature", - _r, - _s, - _recoveryID, + "distributeERC20Reward", + _tokenAddress, + _value, ) } becdsakLogger.Infof( - "submitted transaction submitSignature with id: [%v] and nonce [%v]", + "submitted transaction distributeERC20Reward with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -1209,10 +1025,9 @@ func (becdsak *BondedECDSAKeep) SubmitSignature( } // Non-mutating call, not a transaction submission. -func (becdsak *BondedECDSAKeep) CallSubmitSignature( - _r [32]uint8, - _s [32]uint8, - _recoveryID uint8, +func (becdsak *BondedECDSAKeep) CallDistributeERC20Reward( + _tokenAddress common.Address, + _value *big.Int, blockNumber *big.Int, ) error { var result interface{} = nil @@ -1224,49 +1039,41 @@ func (becdsak *BondedECDSAKeep) CallSubmitSignature( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "submitSignature", + "distributeERC20Reward", &result, - _r, - _s, - _recoveryID, + _tokenAddress, + _value, ) return err } -func (becdsak *BondedECDSAKeep) SubmitSignatureGasEstimate( - _r [32]uint8, - _s [32]uint8, - _recoveryID uint8, +func (becdsak *BondedECDSAKeep) DistributeERC20RewardGasEstimate( + _tokenAddress common.Address, + _value *big.Int, ) (uint64, error) { var result uint64 result, err := ethutil.EstimateGas( becdsak.callerOptions.From, becdsak.contractAddress, - "submitSignature", + "distributeERC20Reward", becdsak.contractABI, becdsak.transactor, - _r, - _s, - _recoveryID, + _tokenAddress, + _value, ) return result, err } // Transaction submission. -func (becdsak *BondedECDSAKeep) SubmitPublicKey( - _publicKey []uint8, +func (becdsak *BondedECDSAKeep) SeizeSignerBonds( transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakLogger.Debug( - "submitting transaction submitPublicKey", - "params: ", - fmt.Sprint( - _publicKey, - ), + "submitting transaction seizeSignerBonds", ) becdsak.transactionMutex.Lock() @@ -1291,22 +1098,20 @@ func (becdsak *BondedECDSAKeep) SubmitPublicKey( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsak.contract.SubmitPublicKey( + transaction, err := becdsak.contract.SeizeSignerBonds( transactorOptions, - _publicKey, ) if err != nil { return transaction, becdsak.errorResolver.ResolveError( err, becdsak.transactorOptions.From, nil, - "submitPublicKey", - _publicKey, + "seizeSignerBonds", ) } becdsakLogger.Infof( - "submitted transaction submitPublicKey with id: [%v] and nonce [%v]", + "submitted transaction seizeSignerBonds with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -1317,22 +1122,20 @@ func (becdsak *BondedECDSAKeep) SubmitPublicKey( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsak.contract.SubmitPublicKey( + transaction, err := becdsak.contract.SeizeSignerBonds( transactorOptions, - _publicKey, ) if err != nil { return transaction, becdsak.errorResolver.ResolveError( err, becdsak.transactorOptions.From, nil, - "submitPublicKey", - _publicKey, + "seizeSignerBonds", ) } becdsakLogger.Infof( - "submitted transaction submitPublicKey with id: [%v] and nonce [%v]", + "submitted transaction seizeSignerBonds with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -1347,8 +1150,7 @@ func (becdsak *BondedECDSAKeep) SubmitPublicKey( } // Non-mutating call, not a transaction submission. -func (becdsak *BondedECDSAKeep) CallSubmitPublicKey( - _publicKey []uint8, +func (becdsak *BondedECDSAKeep) CallSeizeSignerBonds( blockNumber *big.Int, ) error { var result interface{} = nil @@ -1360,38 +1162,36 @@ func (becdsak *BondedECDSAKeep) CallSubmitPublicKey( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "submitPublicKey", + "seizeSignerBonds", &result, - _publicKey, ) return err } -func (becdsak *BondedECDSAKeep) SubmitPublicKeyGasEstimate( - _publicKey []uint8, -) (uint64, error) { +func (becdsak *BondedECDSAKeep) SeizeSignerBondsGasEstimate() (uint64, error) { var result uint64 result, err := ethutil.EstimateGas( becdsak.callerOptions.From, becdsak.contractAddress, - "submitPublicKey", + "seizeSignerBonds", becdsak.contractABI, becdsak.transactor, - _publicKey, ) return result, err } // Transaction submission. -func (becdsak *BondedECDSAKeep) CloseKeep( +func (becdsak *BondedECDSAKeep) ReturnPartialSignerBonds( + value *big.Int, transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakLogger.Debug( - "submitting transaction closeKeep", + "submitting transaction returnPartialSignerBonds", + "value: ", value, ) becdsak.transactionMutex.Lock() @@ -1401,6 +1201,8 @@ func (becdsak *BondedECDSAKeep) CloseKeep( transactorOptions := new(bind.TransactOpts) *transactorOptions = *becdsak.transactorOptions + transactorOptions.Value = value + if len(transactionOptions) > 1 { return nil, fmt.Errorf( "could not process multiple transaction options sets", @@ -1416,20 +1218,20 @@ func (becdsak *BondedECDSAKeep) CloseKeep( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsak.contract.CloseKeep( + transaction, err := becdsak.contract.ReturnPartialSignerBonds( transactorOptions, ) if err != nil { return transaction, becdsak.errorResolver.ResolveError( err, becdsak.transactorOptions.From, - nil, - "closeKeep", + value, + "returnPartialSignerBonds", ) } becdsakLogger.Infof( - "submitted transaction closeKeep with id: [%v] and nonce [%v]", + "submitted transaction returnPartialSignerBonds with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -1440,20 +1242,20 @@ func (becdsak *BondedECDSAKeep) CloseKeep( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsak.contract.CloseKeep( + transaction, err := becdsak.contract.ReturnPartialSignerBonds( transactorOptions, ) if err != nil { return transaction, becdsak.errorResolver.ResolveError( err, becdsak.transactorOptions.From, - nil, - "closeKeep", + value, + "returnPartialSignerBonds", ) } becdsakLogger.Infof( - "submitted transaction closeKeep with id: [%v] and nonce [%v]", + "submitted transaction returnPartialSignerBonds with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -1468,32 +1270,33 @@ func (becdsak *BondedECDSAKeep) CloseKeep( } // Non-mutating call, not a transaction submission. -func (becdsak *BondedECDSAKeep) CallCloseKeep( +func (becdsak *BondedECDSAKeep) CallReturnPartialSignerBonds( + value *big.Int, blockNumber *big.Int, ) error { var result interface{} = nil err := ethutil.CallAtBlock( becdsak.transactorOptions.From, - blockNumber, nil, + blockNumber, value, becdsak.contractABI, becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "closeKeep", + "returnPartialSignerBonds", &result, ) return err } -func (becdsak *BondedECDSAKeep) CloseKeepGasEstimate() (uint64, error) { +func (becdsak *BondedECDSAKeep) ReturnPartialSignerBondsGasEstimate() (uint64, error) { var result uint64 result, err := ethutil.EstimateGas( becdsak.callerOptions.From, becdsak.contractAddress, - "closeKeep", + "returnPartialSignerBonds", becdsak.contractABI, becdsak.transactor, ) @@ -1502,12 +1305,17 @@ func (becdsak *BondedECDSAKeep) CloseKeepGasEstimate() (uint64, error) { } // Transaction submission. -func (becdsak *BondedECDSAKeep) SeizeSignerBonds( +func (becdsak *BondedECDSAKeep) Sign( + _digest [32]uint8, transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakLogger.Debug( - "submitting transaction seizeSignerBonds", + "submitting transaction sign", + "params: ", + fmt.Sprint( + _digest, + ), ) becdsak.transactionMutex.Lock() @@ -1532,20 +1340,22 @@ func (becdsak *BondedECDSAKeep) SeizeSignerBonds( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsak.contract.SeizeSignerBonds( + transaction, err := becdsak.contract.Sign( transactorOptions, + _digest, ) if err != nil { return transaction, becdsak.errorResolver.ResolveError( err, becdsak.transactorOptions.From, nil, - "seizeSignerBonds", + "sign", + _digest, ) } becdsakLogger.Infof( - "submitted transaction seizeSignerBonds with id: [%v] and nonce [%v]", + "submitted transaction sign with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -1556,20 +1366,22 @@ func (becdsak *BondedECDSAKeep) SeizeSignerBonds( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsak.contract.SeizeSignerBonds( + transaction, err := becdsak.contract.Sign( transactorOptions, + _digest, ) if err != nil { return transaction, becdsak.errorResolver.ResolveError( err, becdsak.transactorOptions.From, nil, - "seizeSignerBonds", + "sign", + _digest, ) } becdsakLogger.Infof( - "submitted transaction seizeSignerBonds with id: [%v] and nonce [%v]", + "submitted transaction sign with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -1584,7 +1396,8 @@ func (becdsak *BondedECDSAKeep) SeizeSignerBonds( } // Non-mutating call, not a transaction submission. -func (becdsak *BondedECDSAKeep) CallSeizeSignerBonds( +func (becdsak *BondedECDSAKeep) CallSign( + _digest [32]uint8, blockNumber *big.Int, ) error { var result interface{} = nil @@ -1596,70 +1409,236 @@ func (becdsak *BondedECDSAKeep) CallSeizeSignerBonds( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "seizeSignerBonds", + "sign", &result, + _digest, ) return err } -func (becdsak *BondedECDSAKeep) SeizeSignerBondsGasEstimate() (uint64, error) { +func (becdsak *BondedECDSAKeep) SignGasEstimate( + _digest [32]uint8, +) (uint64, error) { var result uint64 result, err := ethutil.EstimateGas( becdsak.callerOptions.From, becdsak.contractAddress, - "seizeSignerBonds", + "sign", becdsak.contractABI, becdsak.transactor, + _digest, ) return result, err } -// ----- Const Methods ------ +// Transaction submission. +func (becdsak *BondedECDSAKeep) Initialize( + _owner common.Address, + _members []common.Address, + _honestThreshold *big.Int, + _memberStake *big.Int, + _stakeLockDuration *big.Int, + _tokenStaking common.Address, + _keepBonding common.Address, + _keepFactory common.Address, -func (becdsak *BondedECDSAKeep) IsActive() (bool, error) { - var result bool - result, err := becdsak.contract.IsActive( - becdsak.callerOptions, + transactionOptions ...ethutil.TransactionOptions, +) (*types.Transaction, error) { + becdsakLogger.Debug( + "submitting transaction initialize", + "params: ", + fmt.Sprint( + _owner, + _members, + _honestThreshold, + _memberStake, + _stakeLockDuration, + _tokenStaking, + _keepBonding, + _keepFactory, + ), ) + becdsak.transactionMutex.Lock() + defer becdsak.transactionMutex.Unlock() + + // create a copy + transactorOptions := new(bind.TransactOpts) + *transactorOptions = *becdsak.transactorOptions + + if len(transactionOptions) > 1 { + return nil, fmt.Errorf( + "could not process multiple transaction options sets", + ) + } else if len(transactionOptions) > 0 { + transactionOptions[0].Apply(transactorOptions) + } + + nonce, err := becdsak.nonceManager.CurrentNonce() if err != nil { - return result, becdsak.errorResolver.ResolveError( + return nil, fmt.Errorf("failed to retrieve account nonce: %v", err) + } + + transactorOptions.Nonce = new(big.Int).SetUint64(nonce) + + transaction, err := becdsak.contract.Initialize( + transactorOptions, + _owner, + _members, + _honestThreshold, + _memberStake, + _stakeLockDuration, + _tokenStaking, + _keepBonding, + _keepFactory, + ) + if err != nil { + return transaction, becdsak.errorResolver.ResolveError( err, - becdsak.callerOptions.From, + becdsak.transactorOptions.From, nil, - "isActive", + "initialize", + _owner, + _members, + _honestThreshold, + _memberStake, + _stakeLockDuration, + _tokenStaking, + _keepBonding, + _keepFactory, ) } - return result, err + becdsakLogger.Infof( + "submitted transaction initialize with id: [%v] and nonce [%v]", + transaction.Hash().Hex(), + transaction.Nonce(), + ) + + go becdsak.miningWaiter.ForceMining( + transaction, + func(newGasPrice *big.Int) (*types.Transaction, error) { + transactorOptions.GasLimit = transaction.Gas() + transactorOptions.GasPrice = newGasPrice + + transaction, err := becdsak.contract.Initialize( + transactorOptions, + _owner, + _members, + _honestThreshold, + _memberStake, + _stakeLockDuration, + _tokenStaking, + _keepBonding, + _keepFactory, + ) + if err != nil { + return transaction, becdsak.errorResolver.ResolveError( + err, + becdsak.transactorOptions.From, + nil, + "initialize", + _owner, + _members, + _honestThreshold, + _memberStake, + _stakeLockDuration, + _tokenStaking, + _keepBonding, + _keepFactory, + ) + } + + becdsakLogger.Infof( + "submitted transaction initialize with id: [%v] and nonce [%v]", + transaction.Hash().Hex(), + transaction.Nonce(), + ) + + return transaction, nil + }, + ) + + becdsak.nonceManager.IncrementNonce() + + return transaction, err } -func (becdsak *BondedECDSAKeep) IsActiveAtBlock( +// Non-mutating call, not a transaction submission. +func (becdsak *BondedECDSAKeep) CallInitialize( + _owner common.Address, + _members []common.Address, + _honestThreshold *big.Int, + _memberStake *big.Int, + _stakeLockDuration *big.Int, + _tokenStaking common.Address, + _keepBonding common.Address, + _keepFactory common.Address, blockNumber *big.Int, -) (bool, error) { - var result bool +) error { + var result interface{} = nil err := ethutil.CallAtBlock( - becdsak.callerOptions.From, - blockNumber, - nil, + becdsak.transactorOptions.From, + blockNumber, nil, becdsak.contractABI, becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "isActive", + "initialize", &result, + _owner, + _members, + _honestThreshold, + _memberStake, + _stakeLockDuration, + _tokenStaking, + _keepBonding, + _keepFactory, ) - return result, err + return err } -func (becdsak *BondedECDSAKeep) PublicKey() ([]uint8, error) { - var result []uint8 - result, err := becdsak.contract.PublicKey( +func (becdsak *BondedECDSAKeep) InitializeGasEstimate( + _owner common.Address, + _members []common.Address, + _honestThreshold *big.Int, + _memberStake *big.Int, + _stakeLockDuration *big.Int, + _tokenStaking common.Address, + _keepBonding common.Address, + _keepFactory common.Address, +) (uint64, error) { + var result uint64 + + result, err := ethutil.EstimateGas( + becdsak.callerOptions.From, + becdsak.contractAddress, + "initialize", + becdsak.contractABI, + becdsak.transactor, + _owner, + _members, + _honestThreshold, + _memberStake, + _stakeLockDuration, + _tokenStaking, + _keepBonding, + _keepFactory, + ) + + return result, err +} + +// ----- Const Methods ------ + +func (becdsak *BondedECDSAKeep) IsActive() (bool, error) { + var result bool + result, err := becdsak.contract.IsActive( becdsak.callerOptions, ) @@ -1668,17 +1647,17 @@ func (becdsak *BondedECDSAKeep) PublicKey() ([]uint8, error) { err, becdsak.callerOptions.From, nil, - "publicKey", + "isActive", ) } return result, err } -func (becdsak *BondedECDSAKeep) PublicKeyAtBlock( +func (becdsak *BondedECDSAKeep) IsActiveAtBlock( blockNumber *big.Int, -) ([]uint8, error) { - var result []uint8 +) (bool, error) { + var result bool err := ethutil.CallAtBlock( becdsak.callerOptions.From, @@ -1688,17 +1667,20 @@ func (becdsak *BondedECDSAKeep) PublicKeyAtBlock( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "publicKey", + "isActive", &result, ) return result, err } -func (becdsak *BondedECDSAKeep) Digest() ([32]uint8, error) { - var result [32]uint8 - result, err := becdsak.contract.Digest( +func (becdsak *BondedECDSAKeep) IsAwaitingSignature( + _digest [32]uint8, +) (bool, error) { + var result bool + result, err := becdsak.contract.IsAwaitingSignature( becdsak.callerOptions, + _digest, ) if err != nil { @@ -1706,17 +1688,19 @@ func (becdsak *BondedECDSAKeep) Digest() ([32]uint8, error) { err, becdsak.callerOptions.From, nil, - "digest", + "isAwaitingSignature", + _digest, ) } return result, err } -func (becdsak *BondedECDSAKeep) DigestAtBlock( +func (becdsak *BondedECDSAKeep) IsAwaitingSignatureAtBlock( + _digest [32]uint8, blockNumber *big.Int, -) ([32]uint8, error) { - var result [32]uint8 +) (bool, error) { + var result bool err := ethutil.CallAtBlock( becdsak.callerOptions.From, @@ -1726,16 +1710,17 @@ func (becdsak *BondedECDSAKeep) DigestAtBlock( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "digest", + "isAwaitingSignature", &result, + _digest, ) return result, err } -func (becdsak *BondedECDSAKeep) GetMembers() ([]common.Address, error) { - var result []common.Address - result, err := becdsak.contract.GetMembers( +func (becdsak *BondedECDSAKeep) IsClosed() (bool, error) { + var result bool + result, err := becdsak.contract.IsClosed( becdsak.callerOptions, ) @@ -1744,17 +1729,17 @@ func (becdsak *BondedECDSAKeep) GetMembers() ([]common.Address, error) { err, becdsak.callerOptions.From, nil, - "getMembers", + "isClosed", ) } return result, err } -func (becdsak *BondedECDSAKeep) GetMembersAtBlock( +func (becdsak *BondedECDSAKeep) IsClosedAtBlock( blockNumber *big.Int, -) ([]common.Address, error) { - var result []common.Address +) (bool, error) { + var result bool err := ethutil.CallAtBlock( becdsak.callerOptions.From, @@ -1764,16 +1749,16 @@ func (becdsak *BondedECDSAKeep) GetMembersAtBlock( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "getMembers", + "isClosed", &result, ) return result, err } -func (becdsak *BondedECDSAKeep) IsTerminated() (bool, error) { - var result bool - result, err := becdsak.contract.IsTerminated( +func (becdsak *BondedECDSAKeep) Owner() (common.Address, error) { + var result common.Address + result, err := becdsak.contract.Owner( becdsak.callerOptions, ) @@ -1782,17 +1767,17 @@ func (becdsak *BondedECDSAKeep) IsTerminated() (bool, error) { err, becdsak.callerOptions.From, nil, - "isTerminated", + "owner", ) } return result, err } -func (becdsak *BondedECDSAKeep) IsTerminatedAtBlock( +func (becdsak *BondedECDSAKeep) OwnerAtBlock( blockNumber *big.Int, -) (bool, error) { - var result bool +) (common.Address, error) { + var result common.Address err := ethutil.CallAtBlock( becdsak.callerOptions.From, @@ -1802,20 +1787,17 @@ func (becdsak *BondedECDSAKeep) IsTerminatedAtBlock( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "isTerminated", + "owner", &result, ) return result, err } -func (becdsak *BondedECDSAKeep) IsAwaitingSignature( - _digest [32]uint8, -) (bool, error) { - var result bool - result, err := becdsak.contract.IsAwaitingSignature( +func (becdsak *BondedECDSAKeep) Digest() ([32]uint8, error) { + var result [32]uint8 + result, err := becdsak.contract.Digest( becdsak.callerOptions, - _digest, ) if err != nil { @@ -1823,19 +1805,17 @@ func (becdsak *BondedECDSAKeep) IsAwaitingSignature( err, becdsak.callerOptions.From, nil, - "isAwaitingSignature", - _digest, + "digest", ) } return result, err } -func (becdsak *BondedECDSAKeep) IsAwaitingSignatureAtBlock( - _digest [32]uint8, +func (becdsak *BondedECDSAKeep) DigestAtBlock( blockNumber *big.Int, -) (bool, error) { - var result bool +) ([32]uint8, error) { + var result [32]uint8 err := ethutil.CallAtBlock( becdsak.callerOptions.From, @@ -1845,21 +1825,17 @@ func (becdsak *BondedECDSAKeep) IsAwaitingSignatureAtBlock( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "isAwaitingSignature", + "digest", &result, - _digest, ) return result, err } -func (becdsak *BondedECDSAKeep) Members( - arg0 *big.Int, -) (common.Address, error) { - var result common.Address - result, err := becdsak.contract.Members( +func (becdsak *BondedECDSAKeep) GetMembers() ([]common.Address, error) { + var result []common.Address + result, err := becdsak.contract.GetMembers( becdsak.callerOptions, - arg0, ) if err != nil { @@ -1867,19 +1843,17 @@ func (becdsak *BondedECDSAKeep) Members( err, becdsak.callerOptions.From, nil, - "members", - arg0, + "getMembers", ) } return result, err } -func (becdsak *BondedECDSAKeep) MembersAtBlock( - arg0 *big.Int, +func (becdsak *BondedECDSAKeep) GetMembersAtBlock( blockNumber *big.Int, -) (common.Address, error) { - var result common.Address +) ([]common.Address, error) { + var result []common.Address err := ethutil.CallAtBlock( becdsak.callerOptions.From, @@ -1889,17 +1863,16 @@ func (becdsak *BondedECDSAKeep) MembersAtBlock( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "members", + "getMembers", &result, - arg0, ) return result, err } -func (becdsak *BondedECDSAKeep) CheckBondAmount() (*big.Int, error) { +func (becdsak *BondedECDSAKeep) MemberStake() (*big.Int, error) { var result *big.Int - result, err := becdsak.contract.CheckBondAmount( + result, err := becdsak.contract.MemberStake( becdsak.callerOptions, ) @@ -1908,14 +1881,14 @@ func (becdsak *BondedECDSAKeep) CheckBondAmount() (*big.Int, error) { err, becdsak.callerOptions.From, nil, - "checkBondAmount", + "memberStake", ) } return result, err } -func (becdsak *BondedECDSAKeep) CheckBondAmountAtBlock( +func (becdsak *BondedECDSAKeep) MemberStakeAtBlock( blockNumber *big.Int, ) (*big.Int, error) { var result *big.Int @@ -1928,17 +1901,20 @@ func (becdsak *BondedECDSAKeep) CheckBondAmountAtBlock( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "checkBondAmount", + "memberStake", &result, ) return result, err } -func (becdsak *BondedECDSAKeep) MemberStake() (*big.Int, error) { +func (becdsak *BondedECDSAKeep) Digests( + arg0 [32]uint8, +) (*big.Int, error) { var result *big.Int - result, err := becdsak.contract.MemberStake( + result, err := becdsak.contract.Digests( becdsak.callerOptions, + arg0, ) if err != nil { @@ -1946,14 +1922,16 @@ func (becdsak *BondedECDSAKeep) MemberStake() (*big.Int, error) { err, becdsak.callerOptions.From, nil, - "memberStake", + "digests", + arg0, ) } return result, err } -func (becdsak *BondedECDSAKeep) MemberStakeAtBlock( +func (becdsak *BondedECDSAKeep) DigestsAtBlock( + arg0 [32]uint8, blockNumber *big.Int, ) (*big.Int, error) { var result *big.Int @@ -1966,16 +1944,17 @@ func (becdsak *BondedECDSAKeep) MemberStakeAtBlock( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "memberStake", + "digests", &result, + arg0, ) return result, err } -func (becdsak *BondedECDSAKeep) GetOwner() (common.Address, error) { - var result common.Address - result, err := becdsak.contract.GetOwner( +func (becdsak *BondedECDSAKeep) PublicKey() ([]uint8, error) { + var result []uint8 + result, err := becdsak.contract.PublicKey( becdsak.callerOptions, ) @@ -1984,17 +1963,17 @@ func (becdsak *BondedECDSAKeep) GetOwner() (common.Address, error) { err, becdsak.callerOptions.From, nil, - "getOwner", + "publicKey", ) } return result, err } -func (becdsak *BondedECDSAKeep) GetOwnerAtBlock( +func (becdsak *BondedECDSAKeep) PublicKeyAtBlock( blockNumber *big.Int, -) (common.Address, error) { - var result common.Address +) ([]uint8, error) { + var result []uint8 err := ethutil.CallAtBlock( becdsak.callerOptions.From, @@ -2004,17 +1983,20 @@ func (becdsak *BondedECDSAKeep) GetOwnerAtBlock( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "getOwner", + "publicKey", &result, ) return result, err } -func (becdsak *BondedECDSAKeep) IsClosed() (bool, error) { - var result bool - result, err := becdsak.contract.IsClosed( +func (becdsak *BondedECDSAKeep) Members( + arg0 *big.Int, +) (common.Address, error) { + var result common.Address + result, err := becdsak.contract.Members( becdsak.callerOptions, + arg0, ) if err != nil { @@ -2022,17 +2004,19 @@ func (becdsak *BondedECDSAKeep) IsClosed() (bool, error) { err, becdsak.callerOptions.From, nil, - "isClosed", + "members", + arg0, ) } return result, err } -func (becdsak *BondedECDSAKeep) IsClosedAtBlock( +func (becdsak *BondedECDSAKeep) MembersAtBlock( + arg0 *big.Int, blockNumber *big.Int, -) (bool, error) { - var result bool +) (common.Address, error) { + var result common.Address err := ethutil.CallAtBlock( becdsak.callerOptions.From, @@ -2042,17 +2026,29 @@ func (becdsak *BondedECDSAKeep) IsClosedAtBlock( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "isClosed", + "members", &result, + arg0, ) return result, err } -func (becdsak *BondedECDSAKeep) Owner() (common.Address, error) { - var result common.Address - result, err := becdsak.contract.Owner( +func (becdsak *BondedECDSAKeep) CheckSignatureFraud( + _v uint8, + _r [32]uint8, + _s [32]uint8, + _signedDigest [32]uint8, + _preimage []uint8, +) (bool, error) { + var result bool + result, err := becdsak.contract.CheckSignatureFraud( becdsak.callerOptions, + _v, + _r, + _s, + _signedDigest, + _preimage, ) if err != nil { @@ -2060,17 +2056,27 @@ func (becdsak *BondedECDSAKeep) Owner() (common.Address, error) { err, becdsak.callerOptions.From, nil, - "owner", + "checkSignatureFraud", + _v, + _r, + _s, + _signedDigest, + _preimage, ) } return result, err } -func (becdsak *BondedECDSAKeep) OwnerAtBlock( +func (becdsak *BondedECDSAKeep) CheckSignatureFraudAtBlock( + _v uint8, + _r [32]uint8, + _s [32]uint8, + _signedDigest [32]uint8, + _preimage []uint8, blockNumber *big.Int, -) (common.Address, error) { - var result common.Address +) (bool, error) { + var result bool err := ethutil.CallAtBlock( becdsak.callerOptions.From, @@ -2080,8 +2086,13 @@ func (becdsak *BondedECDSAKeep) OwnerAtBlock( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "owner", + "checkSignatureFraud", &result, + _v, + _r, + _s, + _signedDigest, + _preimage, ) return result, err @@ -2131,9 +2142,9 @@ func (becdsak *BondedECDSAKeep) GetMemberETHBalanceAtBlock( return result, err } -func (becdsak *BondedECDSAKeep) GetOpenedTimestamp() (*big.Int, error) { - var result *big.Int - result, err := becdsak.contract.GetOpenedTimestamp( +func (becdsak *BondedECDSAKeep) GetOwner() (common.Address, error) { + var result common.Address + result, err := becdsak.contract.GetOwner( becdsak.callerOptions, ) @@ -2142,17 +2153,17 @@ func (becdsak *BondedECDSAKeep) GetOpenedTimestamp() (*big.Int, error) { err, becdsak.callerOptions.From, nil, - "getOpenedTimestamp", + "getOwner", ) } return result, err } -func (becdsak *BondedECDSAKeep) GetOpenedTimestampAtBlock( +func (becdsak *BondedECDSAKeep) GetOwnerAtBlock( blockNumber *big.Int, -) (*big.Int, error) { - var result *big.Int +) (common.Address, error) { + var result common.Address err := ethutil.CallAtBlock( becdsak.callerOptions.From, @@ -2162,7 +2173,45 @@ func (becdsak *BondedECDSAKeep) GetOpenedTimestampAtBlock( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "getOpenedTimestamp", + "getOwner", + &result, + ) + + return result, err +} + +func (becdsak *BondedECDSAKeep) GetPublicKey() ([]uint8, error) { + var result []uint8 + result, err := becdsak.contract.GetPublicKey( + becdsak.callerOptions, + ) + + if err != nil { + return result, becdsak.errorResolver.ResolveError( + err, + becdsak.callerOptions.From, + nil, + "getPublicKey", + ) + } + + return result, err +} + +func (becdsak *BondedECDSAKeep) GetPublicKeyAtBlock( + blockNumber *big.Int, +) ([]uint8, error) { + var result []uint8 + + err := ethutil.CallAtBlock( + becdsak.callerOptions.From, + blockNumber, + nil, + becdsak.contractABI, + becdsak.caller, + becdsak.errorResolver, + becdsak.contractAddress, + "getPublicKey", &result, ) @@ -2207,21 +2256,10 @@ func (becdsak *BondedECDSAKeep) HonestThresholdAtBlock( return result, err } -func (becdsak *BondedECDSAKeep) CheckSignatureFraud( - _v uint8, - _r [32]uint8, - _s [32]uint8, - _signedDigest [32]uint8, - _preimage []uint8, -) (bool, error) { +func (becdsak *BondedECDSAKeep) IsTerminated() (bool, error) { var result bool - result, err := becdsak.contract.CheckSignatureFraud( + result, err := becdsak.contract.IsTerminated( becdsak.callerOptions, - _v, - _r, - _s, - _signedDigest, - _preimage, ) if err != nil { @@ -2229,24 +2267,14 @@ func (becdsak *BondedECDSAKeep) CheckSignatureFraud( err, becdsak.callerOptions.From, nil, - "checkSignatureFraud", - _v, - _r, - _s, - _signedDigest, - _preimage, + "isTerminated", ) } return result, err } -func (becdsak *BondedECDSAKeep) CheckSignatureFraudAtBlock( - _v uint8, - _r [32]uint8, - _s [32]uint8, - _signedDigest [32]uint8, - _preimage []uint8, +func (becdsak *BondedECDSAKeep) IsTerminatedAtBlock( blockNumber *big.Int, ) (bool, error) { var result bool @@ -2259,25 +2287,17 @@ func (becdsak *BondedECDSAKeep) CheckSignatureFraudAtBlock( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "checkSignatureFraud", + "isTerminated", &result, - _v, - _r, - _s, - _signedDigest, - _preimage, ) return result, err } -func (becdsak *BondedECDSAKeep) Digests( - arg0 [32]uint8, -) (*big.Int, error) { +func (becdsak *BondedECDSAKeep) CheckBondAmount() (*big.Int, error) { var result *big.Int - result, err := becdsak.contract.Digests( + result, err := becdsak.contract.CheckBondAmount( becdsak.callerOptions, - arg0, ) if err != nil { @@ -2285,16 +2305,14 @@ func (becdsak *BondedECDSAKeep) Digests( err, becdsak.callerOptions.From, nil, - "digests", - arg0, + "checkBondAmount", ) } return result, err } -func (becdsak *BondedECDSAKeep) DigestsAtBlock( - arg0 [32]uint8, +func (becdsak *BondedECDSAKeep) CheckBondAmountAtBlock( blockNumber *big.Int, ) (*big.Int, error) { var result *big.Int @@ -2307,17 +2325,16 @@ func (becdsak *BondedECDSAKeep) DigestsAtBlock( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "digests", + "checkBondAmount", &result, - arg0, ) return result, err } -func (becdsak *BondedECDSAKeep) GetPublicKey() ([]uint8, error) { - var result []uint8 - result, err := becdsak.contract.GetPublicKey( +func (becdsak *BondedECDSAKeep) GetOpenedTimestamp() (*big.Int, error) { + var result *big.Int + result, err := becdsak.contract.GetOpenedTimestamp( becdsak.callerOptions, ) @@ -2326,17 +2343,17 @@ func (becdsak *BondedECDSAKeep) GetPublicKey() ([]uint8, error) { err, becdsak.callerOptions.From, nil, - "getPublicKey", + "getOpenedTimestamp", ) } return result, err } -func (becdsak *BondedECDSAKeep) GetPublicKeyAtBlock( +func (becdsak *BondedECDSAKeep) GetOpenedTimestampAtBlock( blockNumber *big.Int, -) ([]uint8, error) { - var result []uint8 +) (*big.Int, error) { + var result *big.Int err := ethutil.CallAtBlock( becdsak.callerOptions.From, @@ -2346,7 +2363,7 @@ func (becdsak *BondedECDSAKeep) GetPublicKeyAtBlock( becdsak.caller, becdsak.errorResolver, becdsak.contractAddress, - "getPublicKey", + "getOpenedTimestamp", &result, ) @@ -2392,121 +2409,67 @@ func (becdsak *BondedECDSAKeep) PastConflictingPublicKeySubmittedEvents( func (becdsak *BondedECDSAKeep) WatchConflictingPublicKeySubmitted( success bondedECDSAKeepConflictingPublicKeySubmittedFunc, - fail func(err error) error, submittingMemberFilter []common.Address, -) (subscription.EventSubscription, error) { - errorChan := make(chan error) - unsubscribeChan := make(chan struct{}) - - // Delay which must be preserved before a new resubscription attempt. - // There is no sense to resubscribe immediately after the fail of current - // subscription because the publisher must have some time to recover. - retryDelay := 5 * time.Second - - watch := func() { - failCallback := func(err error) error { - fail(err) - errorChan <- err // trigger resubscription signal - return err - } - - subscription, err := becdsak.subscribeConflictingPublicKeySubmitted( - success, - failCallback, - submittingMemberFilter, - ) - if err != nil { - errorChan <- err // trigger resubscription signal - return - } +) subscription.EventSubscription { + eventOccurred := make(chan *abi.BondedECDSAKeepConflictingPublicKeySubmitted) - // wait for unsubscription signal - <-unsubscribeChan - subscription.Unsubscribe() - } + ctx, cancelCtx := context.WithCancel(context.Background()) - // trigger the resubscriber goroutine + // TODO: Watch* function will soon accept channel as a parameter instead + // of the callback. This loop will be eliminated then. go func() { - go watch() // trigger first subscription - for { select { - case <-errorChan: - becdsakLogger.Warning( - "subscription to event ConflictingPublicKeySubmitted terminated with error; " + - "resubscription attempt will be performed after the retry delay", - ) - time.Sleep(retryDelay) - go watch() - case <-unsubscribeChan: - // shutdown the resubscriber goroutine on unsubscribe signal + case <-ctx.Done(): return - } - } - }() - - // closing the unsubscribeChan will trigger a unsubscribe signal and - // run unsubscription for all subscription instances - unsubscribeCallback := func() { - close(unsubscribeChan) - } - - return subscription.NewEventSubscription(unsubscribeCallback), nil -} - -func (becdsak *BondedECDSAKeep) subscribeConflictingPublicKeySubmitted( - success bondedECDSAKeepConflictingPublicKeySubmittedFunc, - fail func(err error) error, - submittingMemberFilter []common.Address, -) (subscription.EventSubscription, error) { - eventChan := make(chan *abi.BondedECDSAKeepConflictingPublicKeySubmitted) - eventSubscription, err := becdsak.contract.WatchConflictingPublicKeySubmitted( - nil, - eventChan, - submittingMemberFilter, - ) - if err != nil { - close(eventChan) - return eventSubscription, fmt.Errorf( - "error creating watch for ConflictingPublicKeySubmitted events: [%v]", - err, - ) - } - - var subscriptionMutex = &sync.Mutex{} - - go func() { - for { - select { - case event, subscribed := <-eventChan: - subscriptionMutex.Lock() - // if eventChan has been closed, it means we have unsubscribed - if !subscribed { - subscriptionMutex.Unlock() - return - } + case event := <-eventOccurred: success( event.SubmittingMember, event.ConflictingPublicKey, event.Raw.BlockNumber, ) - subscriptionMutex.Unlock() - case ee := <-eventSubscription.Err(): - fail(ee) - return } } }() - unsubscribeCallback := func() { - subscriptionMutex.Lock() - defer subscriptionMutex.Unlock() + subscribeFn := func(ctx context.Context) (event.Subscription, error) { + return becdsak.contract.WatchConflictingPublicKeySubmitted( + &bind.WatchOpts{Context: ctx}, + eventOccurred, + submittingMemberFilter, + ) + } + + thresholdViolatedFn := func(elapsed time.Duration) { + becdsakLogger.Errorf( + "subscription to event ConflictingPublicKeySubmitted had to be "+ + "retried [%s] since the last attempt; please inspect "+ + "Ethereum connectivity", + elapsed, + ) + } - eventSubscription.Unsubscribe() - close(eventChan) + subscriptionFailedFn := func(err error) { + becdsakLogger.Errorf( + "subscription to event ConflictingPublicKeySubmitted failed "+ + "with error: [%v]; resubscription attempt will be "+ + "performed", + err, + ) } - return subscription.NewEventSubscription(unsubscribeCallback), nil + sub := ethutil.WithResubscription( + becdsakSubscriptionBackoffMax, + subscribeFn, + becdsakSubscriptionAlertThreshold, + thresholdViolatedFn, + subscriptionFailedFn, + ) + + return subscription.NewEventSubscription(func() { + sub.Unsubscribe() + cancelCtx() + }) } type bondedECDSAKeepERC20RewardDistributedFunc func( @@ -2546,121 +2509,67 @@ func (becdsak *BondedECDSAKeep) PastERC20RewardDistributedEvents( func (becdsak *BondedECDSAKeep) WatchERC20RewardDistributed( success bondedECDSAKeepERC20RewardDistributedFunc, - fail func(err error) error, tokenFilter []common.Address, -) (subscription.EventSubscription, error) { - errorChan := make(chan error) - unsubscribeChan := make(chan struct{}) - - // Delay which must be preserved before a new resubscription attempt. - // There is no sense to resubscribe immediately after the fail of current - // subscription because the publisher must have some time to recover. - retryDelay := 5 * time.Second - - watch := func() { - failCallback := func(err error) error { - fail(err) - errorChan <- err // trigger resubscription signal - return err - } - - subscription, err := becdsak.subscribeERC20RewardDistributed( - success, - failCallback, - tokenFilter, - ) - if err != nil { - errorChan <- err // trigger resubscription signal - return - } +) subscription.EventSubscription { + eventOccurred := make(chan *abi.BondedECDSAKeepERC20RewardDistributed) - // wait for unsubscription signal - <-unsubscribeChan - subscription.Unsubscribe() - } + ctx, cancelCtx := context.WithCancel(context.Background()) - // trigger the resubscriber goroutine + // TODO: Watch* function will soon accept channel as a parameter instead + // of the callback. This loop will be eliminated then. go func() { - go watch() // trigger first subscription - for { select { - case <-errorChan: - becdsakLogger.Warning( - "subscription to event ERC20RewardDistributed terminated with error; " + - "resubscription attempt will be performed after the retry delay", - ) - time.Sleep(retryDelay) - go watch() - case <-unsubscribeChan: - // shutdown the resubscriber goroutine on unsubscribe signal + case <-ctx.Done(): return - } - } - }() - - // closing the unsubscribeChan will trigger a unsubscribe signal and - // run unsubscription for all subscription instances - unsubscribeCallback := func() { - close(unsubscribeChan) - } - - return subscription.NewEventSubscription(unsubscribeCallback), nil -} - -func (becdsak *BondedECDSAKeep) subscribeERC20RewardDistributed( - success bondedECDSAKeepERC20RewardDistributedFunc, - fail func(err error) error, - tokenFilter []common.Address, -) (subscription.EventSubscription, error) { - eventChan := make(chan *abi.BondedECDSAKeepERC20RewardDistributed) - eventSubscription, err := becdsak.contract.WatchERC20RewardDistributed( - nil, - eventChan, - tokenFilter, - ) - if err != nil { - close(eventChan) - return eventSubscription, fmt.Errorf( - "error creating watch for ERC20RewardDistributed events: [%v]", - err, - ) - } - - var subscriptionMutex = &sync.Mutex{} - - go func() { - for { - select { - case event, subscribed := <-eventChan: - subscriptionMutex.Lock() - // if eventChan has been closed, it means we have unsubscribed - if !subscribed { - subscriptionMutex.Unlock() - return - } + case event := <-eventOccurred: success( event.Token, event.Amount, event.Raw.BlockNumber, ) - subscriptionMutex.Unlock() - case ee := <-eventSubscription.Err(): - fail(ee) - return } } }() - unsubscribeCallback := func() { - subscriptionMutex.Lock() - defer subscriptionMutex.Unlock() + subscribeFn := func(ctx context.Context) (event.Subscription, error) { + return becdsak.contract.WatchERC20RewardDistributed( + &bind.WatchOpts{Context: ctx}, + eventOccurred, + tokenFilter, + ) + } + + thresholdViolatedFn := func(elapsed time.Duration) { + becdsakLogger.Errorf( + "subscription to event ERC20RewardDistributed had to be "+ + "retried [%s] since the last attempt; please inspect "+ + "Ethereum connectivity", + elapsed, + ) + } - eventSubscription.Unsubscribe() - close(eventChan) + subscriptionFailedFn := func(err error) { + becdsakLogger.Errorf( + "subscription to event ERC20RewardDistributed failed "+ + "with error: [%v]; resubscription attempt will be "+ + "performed", + err, + ) } - return subscription.NewEventSubscription(unsubscribeCallback), nil + sub := ethutil.WithResubscription( + becdsakSubscriptionBackoffMax, + subscribeFn, + becdsakSubscriptionAlertThreshold, + thresholdViolatedFn, + subscriptionFailedFn, + ) + + return subscription.NewEventSubscription(func() { + sub.Unsubscribe() + cancelCtx() + }) } type bondedECDSAKeepETHRewardDistributedFunc func( @@ -2697,141 +2606,94 @@ func (becdsak *BondedECDSAKeep) PastETHRewardDistributedEvents( func (becdsak *BondedECDSAKeep) WatchETHRewardDistributed( success bondedECDSAKeepETHRewardDistributedFunc, - fail func(err error) error, -) (subscription.EventSubscription, error) { - errorChan := make(chan error) - unsubscribeChan := make(chan struct{}) - - // Delay which must be preserved before a new resubscription attempt. - // There is no sense to resubscribe immediately after the fail of current - // subscription because the publisher must have some time to recover. - retryDelay := 5 * time.Second - - watch := func() { - failCallback := func(err error) error { - fail(err) - errorChan <- err // trigger resubscription signal - return err - } - - subscription, err := becdsak.subscribeETHRewardDistributed( - success, - failCallback, - ) - if err != nil { - errorChan <- err // trigger resubscription signal - return - } +) subscription.EventSubscription { + eventOccurred := make(chan *abi.BondedECDSAKeepETHRewardDistributed) - // wait for unsubscription signal - <-unsubscribeChan - subscription.Unsubscribe() - } + ctx, cancelCtx := context.WithCancel(context.Background()) - // trigger the resubscriber goroutine + // TODO: Watch* function will soon accept channel as a parameter instead + // of the callback. This loop will be eliminated then. go func() { - go watch() // trigger first subscription - for { select { - case <-errorChan: - becdsakLogger.Warning( - "subscription to event ETHRewardDistributed terminated with error; " + - "resubscription attempt will be performed after the retry delay", - ) - time.Sleep(retryDelay) - go watch() - case <-unsubscribeChan: - // shutdown the resubscriber goroutine on unsubscribe signal + case <-ctx.Done(): return + case event := <-eventOccurred: + success( + event.Amount, + event.Raw.BlockNumber, + ) } } }() - // closing the unsubscribeChan will trigger a unsubscribe signal and - // run unsubscription for all subscription instances - unsubscribeCallback := func() { - close(unsubscribeChan) + subscribeFn := func(ctx context.Context) (event.Subscription, error) { + return becdsak.contract.WatchETHRewardDistributed( + &bind.WatchOpts{Context: ctx}, + eventOccurred, + ) } - return subscription.NewEventSubscription(unsubscribeCallback), nil -} + thresholdViolatedFn := func(elapsed time.Duration) { + becdsakLogger.Errorf( + "subscription to event ETHRewardDistributed had to be "+ + "retried [%s] since the last attempt; please inspect "+ + "Ethereum connectivity", + elapsed, + ) + } -func (becdsak *BondedECDSAKeep) subscribeETHRewardDistributed( - success bondedECDSAKeepETHRewardDistributedFunc, - fail func(err error) error, -) (subscription.EventSubscription, error) { - eventChan := make(chan *abi.BondedECDSAKeepETHRewardDistributed) - eventSubscription, err := becdsak.contract.WatchETHRewardDistributed( - nil, - eventChan, - ) - if err != nil { - close(eventChan) - return eventSubscription, fmt.Errorf( - "error creating watch for ETHRewardDistributed events: [%v]", + subscriptionFailedFn := func(err error) { + becdsakLogger.Errorf( + "subscription to event ETHRewardDistributed failed "+ + "with error: [%v]; resubscription attempt will be "+ + "performed", err, ) } - var subscriptionMutex = &sync.Mutex{} - - go func() { - for { - select { - case event, subscribed := <-eventChan: - subscriptionMutex.Lock() - // if eventChan has been closed, it means we have unsubscribed - if !subscribed { - subscriptionMutex.Unlock() - return - } - success( - event.Amount, - event.Raw.BlockNumber, - ) - subscriptionMutex.Unlock() - case ee := <-eventSubscription.Err(): - fail(ee) - return - } - } - }() - - unsubscribeCallback := func() { - subscriptionMutex.Lock() - defer subscriptionMutex.Unlock() - - eventSubscription.Unsubscribe() - close(eventChan) - } + sub := ethutil.WithResubscription( + becdsakSubscriptionBackoffMax, + subscribeFn, + becdsakSubscriptionAlertThreshold, + thresholdViolatedFn, + subscriptionFailedFn, + ) - return subscription.NewEventSubscription(unsubscribeCallback), nil + return subscription.NewEventSubscription(func() { + sub.Unsubscribe() + cancelCtx() + }) } -type bondedECDSAKeepPublicKeyPublishedFunc func( - PublicKey []uint8, +type bondedECDSAKeepSignatureSubmittedFunc func( + Digest [32]uint8, + R [32]uint8, + S [32]uint8, + RecoveryID uint8, blockNumber uint64, ) -func (becdsak *BondedECDSAKeep) PastPublicKeyPublishedEvents( +func (becdsak *BondedECDSAKeep) PastSignatureSubmittedEvents( startBlock uint64, endBlock *uint64, -) ([]*abi.BondedECDSAKeepPublicKeyPublished, error) { - iterator, err := becdsak.contract.FilterPublicKeyPublished( + digestFilter [][32]uint8, +) ([]*abi.BondedECDSAKeepSignatureSubmitted, error) { + iterator, err := becdsak.contract.FilterSignatureSubmitted( &bind.FilterOpts{ Start: startBlock, End: endBlock, }, + digestFilter, ) if err != nil { return nil, fmt.Errorf( - "error retrieving past PublicKeyPublished events: [%v]", + "error retrieving past SignatureSubmitted events: [%v]", err, ) } - events := make([]*abi.BondedECDSAKeepPublicKeyPublished, 0) + events := make([]*abi.BondedECDSAKeepSignatureSubmitted, 0) for iterator.Next() { event := iterator.Event @@ -2841,118 +2703,71 @@ func (becdsak *BondedECDSAKeep) PastPublicKeyPublishedEvents( return events, nil } -func (becdsak *BondedECDSAKeep) WatchPublicKeyPublished( - success bondedECDSAKeepPublicKeyPublishedFunc, - fail func(err error) error, -) (subscription.EventSubscription, error) { - errorChan := make(chan error) - unsubscribeChan := make(chan struct{}) - - // Delay which must be preserved before a new resubscription attempt. - // There is no sense to resubscribe immediately after the fail of current - // subscription because the publisher must have some time to recover. - retryDelay := 5 * time.Second - - watch := func() { - failCallback := func(err error) error { - fail(err) - errorChan <- err // trigger resubscription signal - return err - } - - subscription, err := becdsak.subscribePublicKeyPublished( - success, - failCallback, - ) - if err != nil { - errorChan <- err // trigger resubscription signal - return - } +func (becdsak *BondedECDSAKeep) WatchSignatureSubmitted( + success bondedECDSAKeepSignatureSubmittedFunc, + digestFilter [][32]uint8, +) subscription.EventSubscription { + eventOccurred := make(chan *abi.BondedECDSAKeepSignatureSubmitted) - // wait for unsubscription signal - <-unsubscribeChan - subscription.Unsubscribe() - } + ctx, cancelCtx := context.WithCancel(context.Background()) - // trigger the resubscriber goroutine + // TODO: Watch* function will soon accept channel as a parameter instead + // of the callback. This loop will be eliminated then. go func() { - go watch() // trigger first subscription - for { select { - case <-errorChan: - becdsakLogger.Warning( - "subscription to event PublicKeyPublished terminated with error; " + - "resubscription attempt will be performed after the retry delay", - ) - time.Sleep(retryDelay) - go watch() - case <-unsubscribeChan: - // shutdown the resubscriber goroutine on unsubscribe signal + case <-ctx.Done(): return + case event := <-eventOccurred: + success( + event.Digest, + event.R, + event.S, + event.RecoveryID, + event.Raw.BlockNumber, + ) } } }() - // closing the unsubscribeChan will trigger a unsubscribe signal and - // run unsubscription for all subscription instances - unsubscribeCallback := func() { - close(unsubscribeChan) + subscribeFn := func(ctx context.Context) (event.Subscription, error) { + return becdsak.contract.WatchSignatureSubmitted( + &bind.WatchOpts{Context: ctx}, + eventOccurred, + digestFilter, + ) } - return subscription.NewEventSubscription(unsubscribeCallback), nil -} + thresholdViolatedFn := func(elapsed time.Duration) { + becdsakLogger.Errorf( + "subscription to event SignatureSubmitted had to be "+ + "retried [%s] since the last attempt; please inspect "+ + "Ethereum connectivity", + elapsed, + ) + } -func (becdsak *BondedECDSAKeep) subscribePublicKeyPublished( - success bondedECDSAKeepPublicKeyPublishedFunc, - fail func(err error) error, -) (subscription.EventSubscription, error) { - eventChan := make(chan *abi.BondedECDSAKeepPublicKeyPublished) - eventSubscription, err := becdsak.contract.WatchPublicKeyPublished( - nil, - eventChan, - ) - if err != nil { - close(eventChan) - return eventSubscription, fmt.Errorf( - "error creating watch for PublicKeyPublished events: [%v]", + subscriptionFailedFn := func(err error) { + becdsakLogger.Errorf( + "subscription to event SignatureSubmitted failed "+ + "with error: [%v]; resubscription attempt will be "+ + "performed", err, ) } - var subscriptionMutex = &sync.Mutex{} - - go func() { - for { - select { - case event, subscribed := <-eventChan: - subscriptionMutex.Lock() - // if eventChan has been closed, it means we have unsubscribed - if !subscribed { - subscriptionMutex.Unlock() - return - } - success( - event.PublicKey, - event.Raw.BlockNumber, - ) - subscriptionMutex.Unlock() - case ee := <-eventSubscription.Err(): - fail(ee) - return - } - } - }() - - unsubscribeCallback := func() { - subscriptionMutex.Lock() - defer subscriptionMutex.Unlock() - - eventSubscription.Unsubscribe() - close(eventChan) - } + sub := ethutil.WithResubscription( + becdsakSubscriptionBackoffMax, + subscribeFn, + becdsakSubscriptionAlertThreshold, + thresholdViolatedFn, + subscriptionFailedFn, + ) - return subscription.NewEventSubscription(unsubscribeCallback), nil + return subscription.NewEventSubscription(func() { + sub.Unsubscribe() + cancelCtx() + }) } type bondedECDSAKeepKeepClosedFunc func( @@ -2988,115 +2803,63 @@ func (becdsak *BondedECDSAKeep) PastKeepClosedEvents( func (becdsak *BondedECDSAKeep) WatchKeepClosed( success bondedECDSAKeepKeepClosedFunc, - fail func(err error) error, -) (subscription.EventSubscription, error) { - errorChan := make(chan error) - unsubscribeChan := make(chan struct{}) - - // Delay which must be preserved before a new resubscription attempt. - // There is no sense to resubscribe immediately after the fail of current - // subscription because the publisher must have some time to recover. - retryDelay := 5 * time.Second - - watch := func() { - failCallback := func(err error) error { - fail(err) - errorChan <- err // trigger resubscription signal - return err - } +) subscription.EventSubscription { + eventOccurred := make(chan *abi.BondedECDSAKeepKeepClosed) - subscription, err := becdsak.subscribeKeepClosed( - success, - failCallback, - ) - if err != nil { - errorChan <- err // trigger resubscription signal - return - } - - // wait for unsubscription signal - <-unsubscribeChan - subscription.Unsubscribe() - } + ctx, cancelCtx := context.WithCancel(context.Background()) - // trigger the resubscriber goroutine + // TODO: Watch* function will soon accept channel as a parameter instead + // of the callback. This loop will be eliminated then. go func() { - go watch() // trigger first subscription - for { select { - case <-errorChan: - becdsakLogger.Warning( - "subscription to event KeepClosed terminated with error; " + - "resubscription attempt will be performed after the retry delay", - ) - time.Sleep(retryDelay) - go watch() - case <-unsubscribeChan: - // shutdown the resubscriber goroutine on unsubscribe signal + case <-ctx.Done(): return + case event := <-eventOccurred: + success( + event.Raw.BlockNumber, + ) } } }() - // closing the unsubscribeChan will trigger a unsubscribe signal and - // run unsubscription for all subscription instances - unsubscribeCallback := func() { - close(unsubscribeChan) + subscribeFn := func(ctx context.Context) (event.Subscription, error) { + return becdsak.contract.WatchKeepClosed( + &bind.WatchOpts{Context: ctx}, + eventOccurred, + ) } - return subscription.NewEventSubscription(unsubscribeCallback), nil -} + thresholdViolatedFn := func(elapsed time.Duration) { + becdsakLogger.Errorf( + "subscription to event KeepClosed had to be "+ + "retried [%s] since the last attempt; please inspect "+ + "Ethereum connectivity", + elapsed, + ) + } -func (becdsak *BondedECDSAKeep) subscribeKeepClosed( - success bondedECDSAKeepKeepClosedFunc, - fail func(err error) error, -) (subscription.EventSubscription, error) { - eventChan := make(chan *abi.BondedECDSAKeepKeepClosed) - eventSubscription, err := becdsak.contract.WatchKeepClosed( - nil, - eventChan, - ) - if err != nil { - close(eventChan) - return eventSubscription, fmt.Errorf( - "error creating watch for KeepClosed events: [%v]", + subscriptionFailedFn := func(err error) { + becdsakLogger.Errorf( + "subscription to event KeepClosed failed "+ + "with error: [%v]; resubscription attempt will be "+ + "performed", err, ) } - var subscriptionMutex = &sync.Mutex{} - - go func() { - for { - select { - case event, subscribed := <-eventChan: - subscriptionMutex.Lock() - // if eventChan has been closed, it means we have unsubscribed - if !subscribed { - subscriptionMutex.Unlock() - return - } - success( - event.Raw.BlockNumber, - ) - subscriptionMutex.Unlock() - case ee := <-eventSubscription.Err(): - fail(ee) - return - } - } - }() - - unsubscribeCallback := func() { - subscriptionMutex.Lock() - defer subscriptionMutex.Unlock() - - eventSubscription.Unsubscribe() - close(eventChan) - } + sub := ethutil.WithResubscription( + becdsakSubscriptionBackoffMax, + subscribeFn, + becdsakSubscriptionAlertThreshold, + thresholdViolatedFn, + subscriptionFailedFn, + ) - return subscription.NewEventSubscription(unsubscribeCallback), nil + return subscription.NewEventSubscription(func() { + sub.Unsubscribe() + cancelCtx() + }) } type bondedECDSAKeepKeepTerminatedFunc func( @@ -3132,142 +2895,88 @@ func (becdsak *BondedECDSAKeep) PastKeepTerminatedEvents( func (becdsak *BondedECDSAKeep) WatchKeepTerminated( success bondedECDSAKeepKeepTerminatedFunc, - fail func(err error) error, -) (subscription.EventSubscription, error) { - errorChan := make(chan error) - unsubscribeChan := make(chan struct{}) - - // Delay which must be preserved before a new resubscription attempt. - // There is no sense to resubscribe immediately after the fail of current - // subscription because the publisher must have some time to recover. - retryDelay := 5 * time.Second - - watch := func() { - failCallback := func(err error) error { - fail(err) - errorChan <- err // trigger resubscription signal - return err - } +) subscription.EventSubscription { + eventOccurred := make(chan *abi.BondedECDSAKeepKeepTerminated) - subscription, err := becdsak.subscribeKeepTerminated( - success, - failCallback, - ) - if err != nil { - errorChan <- err // trigger resubscription signal - return - } - - // wait for unsubscription signal - <-unsubscribeChan - subscription.Unsubscribe() - } + ctx, cancelCtx := context.WithCancel(context.Background()) - // trigger the resubscriber goroutine + // TODO: Watch* function will soon accept channel as a parameter instead + // of the callback. This loop will be eliminated then. go func() { - go watch() // trigger first subscription - for { select { - case <-errorChan: - becdsakLogger.Warning( - "subscription to event KeepTerminated terminated with error; " + - "resubscription attempt will be performed after the retry delay", - ) - time.Sleep(retryDelay) - go watch() - case <-unsubscribeChan: - // shutdown the resubscriber goroutine on unsubscribe signal + case <-ctx.Done(): return + case event := <-eventOccurred: + success( + event.Raw.BlockNumber, + ) } } }() - // closing the unsubscribeChan will trigger a unsubscribe signal and - // run unsubscription for all subscription instances - unsubscribeCallback := func() { - close(unsubscribeChan) + subscribeFn := func(ctx context.Context) (event.Subscription, error) { + return becdsak.contract.WatchKeepTerminated( + &bind.WatchOpts{Context: ctx}, + eventOccurred, + ) } - return subscription.NewEventSubscription(unsubscribeCallback), nil -} + thresholdViolatedFn := func(elapsed time.Duration) { + becdsakLogger.Errorf( + "subscription to event KeepTerminated had to be "+ + "retried [%s] since the last attempt; please inspect "+ + "Ethereum connectivity", + elapsed, + ) + } -func (becdsak *BondedECDSAKeep) subscribeKeepTerminated( - success bondedECDSAKeepKeepTerminatedFunc, - fail func(err error) error, -) (subscription.EventSubscription, error) { - eventChan := make(chan *abi.BondedECDSAKeepKeepTerminated) - eventSubscription, err := becdsak.contract.WatchKeepTerminated( - nil, - eventChan, - ) - if err != nil { - close(eventChan) - return eventSubscription, fmt.Errorf( - "error creating watch for KeepTerminated events: [%v]", + subscriptionFailedFn := func(err error) { + becdsakLogger.Errorf( + "subscription to event KeepTerminated failed "+ + "with error: [%v]; resubscription attempt will be "+ + "performed", err, ) } - var subscriptionMutex = &sync.Mutex{} - - go func() { - for { - select { - case event, subscribed := <-eventChan: - subscriptionMutex.Lock() - // if eventChan has been closed, it means we have unsubscribed - if !subscribed { - subscriptionMutex.Unlock() - return - } - success( - event.Raw.BlockNumber, - ) - subscriptionMutex.Unlock() - case ee := <-eventSubscription.Err(): - fail(ee) - return - } - } - }() - - unsubscribeCallback := func() { - subscriptionMutex.Lock() - defer subscriptionMutex.Unlock() - - eventSubscription.Unsubscribe() - close(eventChan) - } + sub := ethutil.WithResubscription( + becdsakSubscriptionBackoffMax, + subscribeFn, + becdsakSubscriptionAlertThreshold, + thresholdViolatedFn, + subscriptionFailedFn, + ) - return subscription.NewEventSubscription(unsubscribeCallback), nil + return subscription.NewEventSubscription(func() { + sub.Unsubscribe() + cancelCtx() + }) } -type bondedECDSAKeepSignatureRequestedFunc func( - Digest [32]uint8, +type bondedECDSAKeepPublicKeyPublishedFunc func( + PublicKey []uint8, blockNumber uint64, ) -func (becdsak *BondedECDSAKeep) PastSignatureRequestedEvents( +func (becdsak *BondedECDSAKeep) PastPublicKeyPublishedEvents( startBlock uint64, endBlock *uint64, - digestFilter [][32]uint8, -) ([]*abi.BondedECDSAKeepSignatureRequested, error) { - iterator, err := becdsak.contract.FilterSignatureRequested( +) ([]*abi.BondedECDSAKeepPublicKeyPublished, error) { + iterator, err := becdsak.contract.FilterPublicKeyPublished( &bind.FilterOpts{ Start: startBlock, End: endBlock, }, - digestFilter, ) if err != nil { return nil, fmt.Errorf( - "error retrieving past SignatureRequested events: [%v]", + "error retrieving past PublicKeyPublished events: [%v]", err, ) } - events := make([]*abi.BondedECDSAKeepSignatureRequested, 0) + events := make([]*abi.BondedECDSAKeepPublicKeyPublished, 0) for iterator.Next() { event := iterator.Event @@ -3277,138 +2986,79 @@ func (becdsak *BondedECDSAKeep) PastSignatureRequestedEvents( return events, nil } -func (becdsak *BondedECDSAKeep) WatchSignatureRequested( - success bondedECDSAKeepSignatureRequestedFunc, - fail func(err error) error, - digestFilter [][32]uint8, -) (subscription.EventSubscription, error) { - errorChan := make(chan error) - unsubscribeChan := make(chan struct{}) - - // Delay which must be preserved before a new resubscription attempt. - // There is no sense to resubscribe immediately after the fail of current - // subscription because the publisher must have some time to recover. - retryDelay := 5 * time.Second - - watch := func() { - failCallback := func(err error) error { - fail(err) - errorChan <- err // trigger resubscription signal - return err - } - - subscription, err := becdsak.subscribeSignatureRequested( - success, - failCallback, - digestFilter, - ) - if err != nil { - errorChan <- err // trigger resubscription signal - return - } +func (becdsak *BondedECDSAKeep) WatchPublicKeyPublished( + success bondedECDSAKeepPublicKeyPublishedFunc, +) subscription.EventSubscription { + eventOccurred := make(chan *abi.BondedECDSAKeepPublicKeyPublished) - // wait for unsubscription signal - <-unsubscribeChan - subscription.Unsubscribe() - } + ctx, cancelCtx := context.WithCancel(context.Background()) - // trigger the resubscriber goroutine + // TODO: Watch* function will soon accept channel as a parameter instead + // of the callback. This loop will be eliminated then. go func() { - go watch() // trigger first subscription - for { select { - case <-errorChan: - becdsakLogger.Warning( - "subscription to event SignatureRequested terminated with error; " + - "resubscription attempt will be performed after the retry delay", - ) - time.Sleep(retryDelay) - go watch() - case <-unsubscribeChan: - // shutdown the resubscriber goroutine on unsubscribe signal + case <-ctx.Done(): return + case event := <-eventOccurred: + success( + event.PublicKey, + event.Raw.BlockNumber, + ) } } }() - // closing the unsubscribeChan will trigger a unsubscribe signal and - // run unsubscription for all subscription instances - unsubscribeCallback := func() { - close(unsubscribeChan) + subscribeFn := func(ctx context.Context) (event.Subscription, error) { + return becdsak.contract.WatchPublicKeyPublished( + &bind.WatchOpts{Context: ctx}, + eventOccurred, + ) } - return subscription.NewEventSubscription(unsubscribeCallback), nil -} + thresholdViolatedFn := func(elapsed time.Duration) { + becdsakLogger.Errorf( + "subscription to event PublicKeyPublished had to be "+ + "retried [%s] since the last attempt; please inspect "+ + "Ethereum connectivity", + elapsed, + ) + } -func (becdsak *BondedECDSAKeep) subscribeSignatureRequested( - success bondedECDSAKeepSignatureRequestedFunc, - fail func(err error) error, - digestFilter [][32]uint8, -) (subscription.EventSubscription, error) { - eventChan := make(chan *abi.BondedECDSAKeepSignatureRequested) - eventSubscription, err := becdsak.contract.WatchSignatureRequested( - nil, - eventChan, - digestFilter, - ) - if err != nil { - close(eventChan) - return eventSubscription, fmt.Errorf( - "error creating watch for SignatureRequested events: [%v]", + subscriptionFailedFn := func(err error) { + becdsakLogger.Errorf( + "subscription to event PublicKeyPublished failed "+ + "with error: [%v]; resubscription attempt will be "+ + "performed", err, ) } - var subscriptionMutex = &sync.Mutex{} - - go func() { - for { - select { - case event, subscribed := <-eventChan: - subscriptionMutex.Lock() - // if eventChan has been closed, it means we have unsubscribed - if !subscribed { - subscriptionMutex.Unlock() - return - } - success( - event.Digest, - event.Raw.BlockNumber, - ) - subscriptionMutex.Unlock() - case ee := <-eventSubscription.Err(): - fail(ee) - return - } - } - }() - - unsubscribeCallback := func() { - subscriptionMutex.Lock() - defer subscriptionMutex.Unlock() - - eventSubscription.Unsubscribe() - close(eventChan) - } + sub := ethutil.WithResubscription( + becdsakSubscriptionBackoffMax, + subscribeFn, + becdsakSubscriptionAlertThreshold, + thresholdViolatedFn, + subscriptionFailedFn, + ) - return subscription.NewEventSubscription(unsubscribeCallback), nil + return subscription.NewEventSubscription(func() { + sub.Unsubscribe() + cancelCtx() + }) } -type bondedECDSAKeepSignatureSubmittedFunc func( +type bondedECDSAKeepSignatureRequestedFunc func( Digest [32]uint8, - R [32]uint8, - S [32]uint8, - RecoveryID uint8, blockNumber uint64, ) -func (becdsak *BondedECDSAKeep) PastSignatureSubmittedEvents( +func (becdsak *BondedECDSAKeep) PastSignatureRequestedEvents( startBlock uint64, endBlock *uint64, digestFilter [][32]uint8, -) ([]*abi.BondedECDSAKeepSignatureSubmitted, error) { - iterator, err := becdsak.contract.FilterSignatureSubmitted( +) ([]*abi.BondedECDSAKeepSignatureRequested, error) { + iterator, err := becdsak.contract.FilterSignatureRequested( &bind.FilterOpts{ Start: startBlock, End: endBlock, @@ -3417,12 +3067,12 @@ func (becdsak *BondedECDSAKeep) PastSignatureSubmittedEvents( ) if err != nil { return nil, fmt.Errorf( - "error retrieving past SignatureSubmitted events: [%v]", + "error retrieving past SignatureRequested events: [%v]", err, ) } - events := make([]*abi.BondedECDSAKeepSignatureSubmitted, 0) + events := make([]*abi.BondedECDSAKeepSignatureRequested, 0) for iterator.Next() { event := iterator.Event @@ -3432,125 +3082,68 @@ func (becdsak *BondedECDSAKeep) PastSignatureSubmittedEvents( return events, nil } -func (becdsak *BondedECDSAKeep) WatchSignatureSubmitted( - success bondedECDSAKeepSignatureSubmittedFunc, - fail func(err error) error, +func (becdsak *BondedECDSAKeep) WatchSignatureRequested( + success bondedECDSAKeepSignatureRequestedFunc, digestFilter [][32]uint8, -) (subscription.EventSubscription, error) { - errorChan := make(chan error) - unsubscribeChan := make(chan struct{}) - - // Delay which must be preserved before a new resubscription attempt. - // There is no sense to resubscribe immediately after the fail of current - // subscription because the publisher must have some time to recover. - retryDelay := 5 * time.Second - - watch := func() { - failCallback := func(err error) error { - fail(err) - errorChan <- err // trigger resubscription signal - return err - } - - subscription, err := becdsak.subscribeSignatureSubmitted( - success, - failCallback, - digestFilter, - ) - if err != nil { - errorChan <- err // trigger resubscription signal - return - } +) subscription.EventSubscription { + eventOccurred := make(chan *abi.BondedECDSAKeepSignatureRequested) - // wait for unsubscription signal - <-unsubscribeChan - subscription.Unsubscribe() - } + ctx, cancelCtx := context.WithCancel(context.Background()) - // trigger the resubscriber goroutine + // TODO: Watch* function will soon accept channel as a parameter instead + // of the callback. This loop will be eliminated then. go func() { - go watch() // trigger first subscription - for { select { - case <-errorChan: - becdsakLogger.Warning( - "subscription to event SignatureSubmitted terminated with error; " + - "resubscription attempt will be performed after the retry delay", - ) - time.Sleep(retryDelay) - go watch() - case <-unsubscribeChan: - // shutdown the resubscriber goroutine on unsubscribe signal + case <-ctx.Done(): return + case event := <-eventOccurred: + success( + event.Digest, + event.Raw.BlockNumber, + ) } } }() - // closing the unsubscribeChan will trigger a unsubscribe signal and - // run unsubscription for all subscription instances - unsubscribeCallback := func() { - close(unsubscribeChan) + subscribeFn := func(ctx context.Context) (event.Subscription, error) { + return becdsak.contract.WatchSignatureRequested( + &bind.WatchOpts{Context: ctx}, + eventOccurred, + digestFilter, + ) } - return subscription.NewEventSubscription(unsubscribeCallback), nil -} + thresholdViolatedFn := func(elapsed time.Duration) { + becdsakLogger.Errorf( + "subscription to event SignatureRequested had to be "+ + "retried [%s] since the last attempt; please inspect "+ + "Ethereum connectivity", + elapsed, + ) + } -func (becdsak *BondedECDSAKeep) subscribeSignatureSubmitted( - success bondedECDSAKeepSignatureSubmittedFunc, - fail func(err error) error, - digestFilter [][32]uint8, -) (subscription.EventSubscription, error) { - eventChan := make(chan *abi.BondedECDSAKeepSignatureSubmitted) - eventSubscription, err := becdsak.contract.WatchSignatureSubmitted( - nil, - eventChan, - digestFilter, - ) - if err != nil { - close(eventChan) - return eventSubscription, fmt.Errorf( - "error creating watch for SignatureSubmitted events: [%v]", + subscriptionFailedFn := func(err error) { + becdsakLogger.Errorf( + "subscription to event SignatureRequested failed "+ + "with error: [%v]; resubscription attempt will be "+ + "performed", err, ) } - var subscriptionMutex = &sync.Mutex{} - - go func() { - for { - select { - case event, subscribed := <-eventChan: - subscriptionMutex.Lock() - // if eventChan has been closed, it means we have unsubscribed - if !subscribed { - subscriptionMutex.Unlock() - return - } - success( - event.Digest, - event.R, - event.S, - event.RecoveryID, - event.Raw.BlockNumber, - ) - subscriptionMutex.Unlock() - case ee := <-eventSubscription.Err(): - fail(ee) - return - } - } - }() - - unsubscribeCallback := func() { - subscriptionMutex.Lock() - defer subscriptionMutex.Unlock() - - eventSubscription.Unsubscribe() - close(eventChan) - } + sub := ethutil.WithResubscription( + becdsakSubscriptionBackoffMax, + subscribeFn, + becdsakSubscriptionAlertThreshold, + thresholdViolatedFn, + subscriptionFailedFn, + ) - return subscription.NewEventSubscription(unsubscribeCallback), nil + return subscription.NewEventSubscription(func() { + sub.Unsubscribe() + cancelCtx() + }) } type bondedECDSAKeepSlashingFailedFunc func( @@ -3586,113 +3179,61 @@ func (becdsak *BondedECDSAKeep) PastSlashingFailedEvents( func (becdsak *BondedECDSAKeep) WatchSlashingFailed( success bondedECDSAKeepSlashingFailedFunc, - fail func(err error) error, -) (subscription.EventSubscription, error) { - errorChan := make(chan error) - unsubscribeChan := make(chan struct{}) - - // Delay which must be preserved before a new resubscription attempt. - // There is no sense to resubscribe immediately after the fail of current - // subscription because the publisher must have some time to recover. - retryDelay := 5 * time.Second - - watch := func() { - failCallback := func(err error) error { - fail(err) - errorChan <- err // trigger resubscription signal - return err - } +) subscription.EventSubscription { + eventOccurred := make(chan *abi.BondedECDSAKeepSlashingFailed) - subscription, err := becdsak.subscribeSlashingFailed( - success, - failCallback, - ) - if err != nil { - errorChan <- err // trigger resubscription signal - return - } - - // wait for unsubscription signal - <-unsubscribeChan - subscription.Unsubscribe() - } + ctx, cancelCtx := context.WithCancel(context.Background()) - // trigger the resubscriber goroutine + // TODO: Watch* function will soon accept channel as a parameter instead + // of the callback. This loop will be eliminated then. go func() { - go watch() // trigger first subscription - for { select { - case <-errorChan: - becdsakLogger.Warning( - "subscription to event SlashingFailed terminated with error; " + - "resubscription attempt will be performed after the retry delay", - ) - time.Sleep(retryDelay) - go watch() - case <-unsubscribeChan: - // shutdown the resubscriber goroutine on unsubscribe signal + case <-ctx.Done(): return + case event := <-eventOccurred: + success( + event.Raw.BlockNumber, + ) } } }() - // closing the unsubscribeChan will trigger a unsubscribe signal and - // run unsubscription for all subscription instances - unsubscribeCallback := func() { - close(unsubscribeChan) + subscribeFn := func(ctx context.Context) (event.Subscription, error) { + return becdsak.contract.WatchSlashingFailed( + &bind.WatchOpts{Context: ctx}, + eventOccurred, + ) } - return subscription.NewEventSubscription(unsubscribeCallback), nil -} + thresholdViolatedFn := func(elapsed time.Duration) { + becdsakLogger.Errorf( + "subscription to event SlashingFailed had to be "+ + "retried [%s] since the last attempt; please inspect "+ + "Ethereum connectivity", + elapsed, + ) + } -func (becdsak *BondedECDSAKeep) subscribeSlashingFailed( - success bondedECDSAKeepSlashingFailedFunc, - fail func(err error) error, -) (subscription.EventSubscription, error) { - eventChan := make(chan *abi.BondedECDSAKeepSlashingFailed) - eventSubscription, err := becdsak.contract.WatchSlashingFailed( - nil, - eventChan, - ) - if err != nil { - close(eventChan) - return eventSubscription, fmt.Errorf( - "error creating watch for SlashingFailed events: [%v]", + subscriptionFailedFn := func(err error) { + becdsakLogger.Errorf( + "subscription to event SlashingFailed failed "+ + "with error: [%v]; resubscription attempt will be "+ + "performed", err, ) } - var subscriptionMutex = &sync.Mutex{} - - go func() { - for { - select { - case event, subscribed := <-eventChan: - subscriptionMutex.Lock() - // if eventChan has been closed, it means we have unsubscribed - if !subscribed { - subscriptionMutex.Unlock() - return - } - success( - event.Raw.BlockNumber, - ) - subscriptionMutex.Unlock() - case ee := <-eventSubscription.Err(): - fail(ee) - return - } - } - }() - - unsubscribeCallback := func() { - subscriptionMutex.Lock() - defer subscriptionMutex.Unlock() - - eventSubscription.Unsubscribe() - close(eventChan) - } + sub := ethutil.WithResubscription( + becdsakSubscriptionBackoffMax, + subscribeFn, + becdsakSubscriptionAlertThreshold, + thresholdViolatedFn, + subscriptionFailedFn, + ) - return subscription.NewEventSubscription(unsubscribeCallback), nil + return subscription.NewEventSubscription(func() { + sub.Unsubscribe() + cancelCtx() + }) } diff --git a/pkg/chain/gen/contract/BondedECDSAKeepFactory.go b/pkg/chain/gen/contract/BondedECDSAKeepFactory.go index db925479a..7ffd33ea2 100644 --- a/pkg/chain/gen/contract/BondedECDSAKeepFactory.go +++ b/pkg/chain/gen/contract/BondedECDSAKeepFactory.go @@ -4,6 +4,7 @@ package contract import ( + "context" "fmt" "math/big" "strings" @@ -15,6 +16,7 @@ import ( "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/event" "github.com/ipfs/go-log" @@ -28,6 +30,21 @@ import ( // included or excluded from logging at startup by name. var becdsakfLogger = log.Logger("keep-contract-BondedECDSAKeepFactory") +const ( + // Maximum backoff time between event resubscription attempts. + becdsakfSubscriptionBackoffMax = 2 * time.Minute + + // Threshold below which event resubscription emits an error to the logs. + // WS connection can be dropped at any moment and event resubscription will + // follow. However, if WS connection for event subscription is getting + // dropped too often, it may indicate something is wrong with Ethereum + // client. This constant defines the minimum lifetime of an event + // subscription required before the subscription failure happens and + // resubscription follows so that the resubscription does not emit an error + // to the logs alerting about potential problems with Ethereum client. + becdsakfSubscriptionAlertThreshold = 15 * time.Minute +) + type BondedECDSAKeepFactory struct { contract *abi.BondedECDSAKeepFactory contractAddress common.Address @@ -94,16 +111,16 @@ func NewBondedECDSAKeepFactory( // ----- Non-const Methods ------ // Transaction submission. -func (becdsakf *BondedECDSAKeepFactory) BeaconCallback( - _relayEntry *big.Int, +func (becdsakf *BondedECDSAKeepFactory) IsRecognized( + _delegatedAuthorityRecipient common.Address, transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakfLogger.Debug( - "submitting transaction beaconCallback", + "submitting transaction isRecognized", "params: ", fmt.Sprint( - _relayEntry, + _delegatedAuthorityRecipient, ), ) @@ -129,22 +146,22 @@ func (becdsakf *BondedECDSAKeepFactory) BeaconCallback( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsakf.contract.BeaconCallback( + transaction, err := becdsakf.contract.IsRecognized( transactorOptions, - _relayEntry, + _delegatedAuthorityRecipient, ) if err != nil { return transaction, becdsakf.errorResolver.ResolveError( err, becdsakf.transactorOptions.From, nil, - "beaconCallback", - _relayEntry, + "isRecognized", + _delegatedAuthorityRecipient, ) } becdsakfLogger.Infof( - "submitted transaction beaconCallback with id: [%v] and nonce [%v]", + "submitted transaction isRecognized with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -155,22 +172,22 @@ func (becdsakf *BondedECDSAKeepFactory) BeaconCallback( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsakf.contract.BeaconCallback( + transaction, err := becdsakf.contract.IsRecognized( transactorOptions, - _relayEntry, + _delegatedAuthorityRecipient, ) if err != nil { return transaction, becdsakf.errorResolver.ResolveError( err, becdsakf.transactorOptions.From, nil, - "beaconCallback", - _relayEntry, + "isRecognized", + _delegatedAuthorityRecipient, ) } becdsakfLogger.Infof( - "submitted transaction beaconCallback with id: [%v] and nonce [%v]", + "submitted transaction isRecognized with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -185,11 +202,11 @@ func (becdsakf *BondedECDSAKeepFactory) BeaconCallback( } // Non-mutating call, not a transaction submission. -func (becdsakf *BondedECDSAKeepFactory) CallBeaconCallback( - _relayEntry *big.Int, +func (becdsakf *BondedECDSAKeepFactory) CallIsRecognized( + _delegatedAuthorityRecipient common.Address, blockNumber *big.Int, -) error { - var result interface{} = nil +) (bool, error) { + var result bool err := ethutil.CallAtBlock( becdsakf.transactorOptions.From, @@ -198,43 +215,41 @@ func (becdsakf *BondedECDSAKeepFactory) CallBeaconCallback( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "beaconCallback", + "isRecognized", &result, - _relayEntry, + _delegatedAuthorityRecipient, ) - return err + return result, err } -func (becdsakf *BondedECDSAKeepFactory) BeaconCallbackGasEstimate( - _relayEntry *big.Int, +func (becdsakf *BondedECDSAKeepFactory) IsRecognizedGasEstimate( + _delegatedAuthorityRecipient common.Address, ) (uint64, error) { var result uint64 result, err := ethutil.EstimateGas( becdsakf.callerOptions.From, becdsakf.contractAddress, - "beaconCallback", + "isRecognized", becdsakf.contractABI, becdsakf.transactor, - _relayEntry, + _delegatedAuthorityRecipient, ) return result, err } // Transaction submission. -func (becdsakf *BondedECDSAKeepFactory) UpdateOperatorStatus( - _operator common.Address, +func (becdsakf *BondedECDSAKeepFactory) CreateSortitionPool( _application common.Address, transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakfLogger.Debug( - "submitting transaction updateOperatorStatus", + "submitting transaction createSortitionPool", "params: ", fmt.Sprint( - _operator, _application, ), ) @@ -261,9 +276,8 @@ func (becdsakf *BondedECDSAKeepFactory) UpdateOperatorStatus( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsakf.contract.UpdateOperatorStatus( + transaction, err := becdsakf.contract.CreateSortitionPool( transactorOptions, - _operator, _application, ) if err != nil { @@ -271,14 +285,13 @@ func (becdsakf *BondedECDSAKeepFactory) UpdateOperatorStatus( err, becdsakf.transactorOptions.From, nil, - "updateOperatorStatus", - _operator, + "createSortitionPool", _application, ) } becdsakfLogger.Infof( - "submitted transaction updateOperatorStatus with id: [%v] and nonce [%v]", + "submitted transaction createSortitionPool with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -289,9 +302,8 @@ func (becdsakf *BondedECDSAKeepFactory) UpdateOperatorStatus( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsakf.contract.UpdateOperatorStatus( + transaction, err := becdsakf.contract.CreateSortitionPool( transactorOptions, - _operator, _application, ) if err != nil { @@ -299,14 +311,13 @@ func (becdsakf *BondedECDSAKeepFactory) UpdateOperatorStatus( err, becdsakf.transactorOptions.From, nil, - "updateOperatorStatus", - _operator, + "createSortitionPool", _application, ) } becdsakfLogger.Infof( - "submitted transaction updateOperatorStatus with id: [%v] and nonce [%v]", + "submitted transaction createSortitionPool with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -321,12 +332,11 @@ func (becdsakf *BondedECDSAKeepFactory) UpdateOperatorStatus( } // Non-mutating call, not a transaction submission. -func (becdsakf *BondedECDSAKeepFactory) CallUpdateOperatorStatus( - _operator common.Address, +func (becdsakf *BondedECDSAKeepFactory) CallCreateSortitionPool( _application common.Address, blockNumber *big.Int, -) error { - var result interface{} = nil +) (common.Address, error) { + var result common.Address err := ethutil.CallAtBlock( becdsakf.transactorOptions.From, @@ -335,17 +345,15 @@ func (becdsakf *BondedECDSAKeepFactory) CallUpdateOperatorStatus( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "updateOperatorStatus", + "createSortitionPool", &result, - _operator, _application, ) - return err + return result, err } -func (becdsakf *BondedECDSAKeepFactory) UpdateOperatorStatusGasEstimate( - _operator common.Address, +func (becdsakf *BondedECDSAKeepFactory) CreateSortitionPoolGasEstimate( _application common.Address, ) (uint64, error) { var result uint64 @@ -353,10 +361,9 @@ func (becdsakf *BondedECDSAKeepFactory) UpdateOperatorStatusGasEstimate( result, err := ethutil.EstimateGas( becdsakf.callerOptions.From, becdsakf.contractAddress, - "updateOperatorStatus", + "createSortitionPool", becdsakf.contractABI, becdsakf.transactor, - _operator, _application, ) @@ -364,16 +371,16 @@ func (becdsakf *BondedECDSAKeepFactory) UpdateOperatorStatusGasEstimate( } // Transaction submission. -func (becdsakf *BondedECDSAKeepFactory) IsRecognized( - _delegatedAuthorityRecipient common.Address, +func (becdsakf *BondedECDSAKeepFactory) RegisterMemberCandidate( + _application common.Address, transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakfLogger.Debug( - "submitting transaction isRecognized", + "submitting transaction registerMemberCandidate", "params: ", fmt.Sprint( - _delegatedAuthorityRecipient, + _application, ), ) @@ -399,22 +406,22 @@ func (becdsakf *BondedECDSAKeepFactory) IsRecognized( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsakf.contract.IsRecognized( + transaction, err := becdsakf.contract.RegisterMemberCandidate( transactorOptions, - _delegatedAuthorityRecipient, + _application, ) if err != nil { return transaction, becdsakf.errorResolver.ResolveError( err, becdsakf.transactorOptions.From, nil, - "isRecognized", - _delegatedAuthorityRecipient, + "registerMemberCandidate", + _application, ) } becdsakfLogger.Infof( - "submitted transaction isRecognized with id: [%v] and nonce [%v]", + "submitted transaction registerMemberCandidate with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -425,22 +432,22 @@ func (becdsakf *BondedECDSAKeepFactory) IsRecognized( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsakf.contract.IsRecognized( + transaction, err := becdsakf.contract.RegisterMemberCandidate( transactorOptions, - _delegatedAuthorityRecipient, + _application, ) if err != nil { return transaction, becdsakf.errorResolver.ResolveError( err, becdsakf.transactorOptions.From, nil, - "isRecognized", - _delegatedAuthorityRecipient, + "registerMemberCandidate", + _application, ) } becdsakfLogger.Infof( - "submitted transaction isRecognized with id: [%v] and nonce [%v]", + "submitted transaction registerMemberCandidate with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -455,11 +462,11 @@ func (becdsakf *BondedECDSAKeepFactory) IsRecognized( } // Non-mutating call, not a transaction submission. -func (becdsakf *BondedECDSAKeepFactory) CallIsRecognized( - _delegatedAuthorityRecipient common.Address, +func (becdsakf *BondedECDSAKeepFactory) CallRegisterMemberCandidate( + _application common.Address, blockNumber *big.Int, -) (bool, error) { - var result bool +) error { + var result interface{} = nil err := ethutil.CallAtBlock( becdsakf.transactorOptions.From, @@ -468,43 +475,53 @@ func (becdsakf *BondedECDSAKeepFactory) CallIsRecognized( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "isRecognized", + "registerMemberCandidate", &result, - _delegatedAuthorityRecipient, + _application, ) - return result, err + return err } -func (becdsakf *BondedECDSAKeepFactory) IsRecognizedGasEstimate( - _delegatedAuthorityRecipient common.Address, +func (becdsakf *BondedECDSAKeepFactory) RegisterMemberCandidateGasEstimate( + _application common.Address, ) (uint64, error) { var result uint64 result, err := ethutil.EstimateGas( becdsakf.callerOptions.From, becdsakf.contractAddress, - "isRecognized", + "registerMemberCandidate", becdsakf.contractABI, becdsakf.transactor, - _delegatedAuthorityRecipient, + _application, ) return result, err } // Transaction submission. -func (becdsakf *BondedECDSAKeepFactory) RegisterMemberCandidate( - _application common.Address, +func (becdsakf *BondedECDSAKeepFactory) OpenKeep( + _groupSize *big.Int, + _honestThreshold *big.Int, + _owner common.Address, + _bond *big.Int, + _stakeLockDuration *big.Int, + value *big.Int, transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakfLogger.Debug( - "submitting transaction registerMemberCandidate", + "submitting transaction openKeep", "params: ", fmt.Sprint( - _application, + _groupSize, + _honestThreshold, + _owner, + _bond, + _stakeLockDuration, ), + "value: ", value, ) becdsakf.transactionMutex.Lock() @@ -514,6 +531,8 @@ func (becdsakf *BondedECDSAKeepFactory) RegisterMemberCandidate( transactorOptions := new(bind.TransactOpts) *transactorOptions = *becdsakf.transactorOptions + transactorOptions.Value = value + if len(transactionOptions) > 1 { return nil, fmt.Errorf( "could not process multiple transaction options sets", @@ -529,22 +548,30 @@ func (becdsakf *BondedECDSAKeepFactory) RegisterMemberCandidate( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsakf.contract.RegisterMemberCandidate( + transaction, err := becdsakf.contract.OpenKeep( transactorOptions, - _application, + _groupSize, + _honestThreshold, + _owner, + _bond, + _stakeLockDuration, ) if err != nil { return transaction, becdsakf.errorResolver.ResolveError( err, becdsakf.transactorOptions.From, - nil, - "registerMemberCandidate", - _application, + value, + "openKeep", + _groupSize, + _honestThreshold, + _owner, + _bond, + _stakeLockDuration, ) } becdsakfLogger.Infof( - "submitted transaction registerMemberCandidate with id: [%v] and nonce [%v]", + "submitted transaction openKeep with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -555,22 +582,30 @@ func (becdsakf *BondedECDSAKeepFactory) RegisterMemberCandidate( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsakf.contract.RegisterMemberCandidate( + transaction, err := becdsakf.contract.OpenKeep( transactorOptions, - _application, + _groupSize, + _honestThreshold, + _owner, + _bond, + _stakeLockDuration, ) if err != nil { return transaction, becdsakf.errorResolver.ResolveError( err, becdsakf.transactorOptions.From, - nil, - "registerMemberCandidate", - _application, + value, + "openKeep", + _groupSize, + _honestThreshold, + _owner, + _bond, + _stakeLockDuration, ) } becdsakfLogger.Infof( - "submitted transaction registerMemberCandidate with id: [%v] and nonce [%v]", + "submitted transaction openKeep with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -585,39 +620,56 @@ func (becdsakf *BondedECDSAKeepFactory) RegisterMemberCandidate( } // Non-mutating call, not a transaction submission. -func (becdsakf *BondedECDSAKeepFactory) CallRegisterMemberCandidate( - _application common.Address, +func (becdsakf *BondedECDSAKeepFactory) CallOpenKeep( + _groupSize *big.Int, + _honestThreshold *big.Int, + _owner common.Address, + _bond *big.Int, + _stakeLockDuration *big.Int, + value *big.Int, blockNumber *big.Int, -) error { - var result interface{} = nil +) (common.Address, error) { + var result common.Address err := ethutil.CallAtBlock( becdsakf.transactorOptions.From, - blockNumber, nil, + blockNumber, value, becdsakf.contractABI, becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "registerMemberCandidate", + "openKeep", &result, - _application, + _groupSize, + _honestThreshold, + _owner, + _bond, + _stakeLockDuration, ) - return err + return result, err } -func (becdsakf *BondedECDSAKeepFactory) RegisterMemberCandidateGasEstimate( - _application common.Address, +func (becdsakf *BondedECDSAKeepFactory) OpenKeepGasEstimate( + _groupSize *big.Int, + _honestThreshold *big.Int, + _owner common.Address, + _bond *big.Int, + _stakeLockDuration *big.Int, ) (uint64, error) { var result uint64 result, err := ethutil.EstimateGas( becdsakf.callerOptions.From, becdsakf.contractAddress, - "registerMemberCandidate", + "openKeep", becdsakf.contractABI, becdsakf.transactor, - _application, + _groupSize, + _honestThreshold, + _owner, + _bond, + _stakeLockDuration, ) return result, err @@ -774,26 +826,13 @@ func (becdsakf *BondedECDSAKeepFactory) SetMinimumBondableValueGasEstimate( } // Transaction submission. -func (becdsakf *BondedECDSAKeepFactory) OpenKeep( - _groupSize *big.Int, - _honestThreshold *big.Int, - _owner common.Address, - _bond *big.Int, - _stakeLockDuration *big.Int, +func (becdsakf *BondedECDSAKeepFactory) RequestNewGroupSelectionSeed( value *big.Int, transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakfLogger.Debug( - "submitting transaction openKeep", - "params: ", - fmt.Sprint( - _groupSize, - _honestThreshold, - _owner, - _bond, - _stakeLockDuration, - ), + "submitting transaction requestNewGroupSelectionSeed", "value: ", value, ) @@ -821,30 +860,20 @@ func (becdsakf *BondedECDSAKeepFactory) OpenKeep( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsakf.contract.OpenKeep( + transaction, err := becdsakf.contract.RequestNewGroupSelectionSeed( transactorOptions, - _groupSize, - _honestThreshold, - _owner, - _bond, - _stakeLockDuration, ) if err != nil { return transaction, becdsakf.errorResolver.ResolveError( err, becdsakf.transactorOptions.From, value, - "openKeep", - _groupSize, - _honestThreshold, - _owner, - _bond, - _stakeLockDuration, + "requestNewGroupSelectionSeed", ) } becdsakfLogger.Infof( - "submitted transaction openKeep with id: [%v] and nonce [%v]", + "submitted transaction requestNewGroupSelectionSeed with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -855,30 +884,20 @@ func (becdsakf *BondedECDSAKeepFactory) OpenKeep( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsakf.contract.OpenKeep( + transaction, err := becdsakf.contract.RequestNewGroupSelectionSeed( transactorOptions, - _groupSize, - _honestThreshold, - _owner, - _bond, - _stakeLockDuration, ) if err != nil { return transaction, becdsakf.errorResolver.ResolveError( err, becdsakf.transactorOptions.From, value, - "openKeep", - _groupSize, - _honestThreshold, - _owner, - _bond, - _stakeLockDuration, + "requestNewGroupSelectionSeed", ) } becdsakfLogger.Infof( - "submitted transaction openKeep with id: [%v] and nonce [%v]", + "submitted transaction requestNewGroupSelectionSeed with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -893,16 +912,11 @@ func (becdsakf *BondedECDSAKeepFactory) OpenKeep( } // Non-mutating call, not a transaction submission. -func (becdsakf *BondedECDSAKeepFactory) CallOpenKeep( - _groupSize *big.Int, - _honestThreshold *big.Int, - _owner common.Address, - _bond *big.Int, - _stakeLockDuration *big.Int, +func (becdsakf *BondedECDSAKeepFactory) CallRequestNewGroupSelectionSeed( value *big.Int, blockNumber *big.Int, -) (common.Address, error) { - var result common.Address +) error { + var result interface{} = nil err := ethutil.CallAtBlock( becdsakf.transactorOptions.From, @@ -911,53 +925,39 @@ func (becdsakf *BondedECDSAKeepFactory) CallOpenKeep( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "openKeep", + "requestNewGroupSelectionSeed", &result, - _groupSize, - _honestThreshold, - _owner, - _bond, - _stakeLockDuration, ) - return result, err + return err } -func (becdsakf *BondedECDSAKeepFactory) OpenKeepGasEstimate( - _groupSize *big.Int, - _honestThreshold *big.Int, - _owner common.Address, - _bond *big.Int, - _stakeLockDuration *big.Int, -) (uint64, error) { +func (becdsakf *BondedECDSAKeepFactory) RequestNewGroupSelectionSeedGasEstimate() (uint64, error) { var result uint64 result, err := ethutil.EstimateGas( becdsakf.callerOptions.From, becdsakf.contractAddress, - "openKeep", + "requestNewGroupSelectionSeed", becdsakf.contractABI, becdsakf.transactor, - _groupSize, - _honestThreshold, - _owner, - _bond, - _stakeLockDuration, ) return result, err } // Transaction submission. -func (becdsakf *BondedECDSAKeepFactory) CreateSortitionPool( +func (becdsakf *BondedECDSAKeepFactory) UpdateOperatorStatus( + _operator common.Address, _application common.Address, transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakfLogger.Debug( - "submitting transaction createSortitionPool", + "submitting transaction updateOperatorStatus", "params: ", fmt.Sprint( + _operator, _application, ), ) @@ -984,8 +984,9 @@ func (becdsakf *BondedECDSAKeepFactory) CreateSortitionPool( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsakf.contract.CreateSortitionPool( + transaction, err := becdsakf.contract.UpdateOperatorStatus( transactorOptions, + _operator, _application, ) if err != nil { @@ -993,13 +994,14 @@ func (becdsakf *BondedECDSAKeepFactory) CreateSortitionPool( err, becdsakf.transactorOptions.From, nil, - "createSortitionPool", + "updateOperatorStatus", + _operator, _application, ) } becdsakfLogger.Infof( - "submitted transaction createSortitionPool with id: [%v] and nonce [%v]", + "submitted transaction updateOperatorStatus with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -1010,8 +1012,9 @@ func (becdsakf *BondedECDSAKeepFactory) CreateSortitionPool( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsakf.contract.CreateSortitionPool( + transaction, err := becdsakf.contract.UpdateOperatorStatus( transactorOptions, + _operator, _application, ) if err != nil { @@ -1019,13 +1022,14 @@ func (becdsakf *BondedECDSAKeepFactory) CreateSortitionPool( err, becdsakf.transactorOptions.From, nil, - "createSortitionPool", + "updateOperatorStatus", + _operator, _application, ) } becdsakfLogger.Infof( - "submitted transaction createSortitionPool with id: [%v] and nonce [%v]", + "submitted transaction updateOperatorStatus with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -1040,11 +1044,12 @@ func (becdsakf *BondedECDSAKeepFactory) CreateSortitionPool( } // Non-mutating call, not a transaction submission. -func (becdsakf *BondedECDSAKeepFactory) CallCreateSortitionPool( +func (becdsakf *BondedECDSAKeepFactory) CallUpdateOperatorStatus( + _operator common.Address, _application common.Address, blockNumber *big.Int, -) (common.Address, error) { - var result common.Address +) error { + var result interface{} = nil err := ethutil.CallAtBlock( becdsakf.transactorOptions.From, @@ -1053,15 +1058,17 @@ func (becdsakf *BondedECDSAKeepFactory) CallCreateSortitionPool( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "createSortitionPool", + "updateOperatorStatus", &result, + _operator, _application, ) - return result, err + return err } -func (becdsakf *BondedECDSAKeepFactory) CreateSortitionPoolGasEstimate( +func (becdsakf *BondedECDSAKeepFactory) UpdateOperatorStatusGasEstimate( + _operator common.Address, _application common.Address, ) (uint64, error) { var result uint64 @@ -1069,9 +1076,10 @@ func (becdsakf *BondedECDSAKeepFactory) CreateSortitionPoolGasEstimate( result, err := ethutil.EstimateGas( becdsakf.callerOptions.From, becdsakf.contractAddress, - "createSortitionPool", + "updateOperatorStatus", becdsakf.contractABI, becdsakf.transactor, + _operator, _application, ) @@ -1079,14 +1087,17 @@ func (becdsakf *BondedECDSAKeepFactory) CreateSortitionPoolGasEstimate( } // Transaction submission. -func (becdsakf *BondedECDSAKeepFactory) RequestNewGroupSelectionSeed( - value *big.Int, +func (becdsakf *BondedECDSAKeepFactory) BeaconCallback( + _relayEntry *big.Int, transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakfLogger.Debug( - "submitting transaction requestNewGroupSelectionSeed", - "value: ", value, + "submitting transaction beaconCallback", + "params: ", + fmt.Sprint( + _relayEntry, + ), ) becdsakf.transactionMutex.Lock() @@ -1096,8 +1107,6 @@ func (becdsakf *BondedECDSAKeepFactory) RequestNewGroupSelectionSeed( transactorOptions := new(bind.TransactOpts) *transactorOptions = *becdsakf.transactorOptions - transactorOptions.Value = value - if len(transactionOptions) > 1 { return nil, fmt.Errorf( "could not process multiple transaction options sets", @@ -1113,20 +1122,22 @@ func (becdsakf *BondedECDSAKeepFactory) RequestNewGroupSelectionSeed( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsakf.contract.RequestNewGroupSelectionSeed( + transaction, err := becdsakf.contract.BeaconCallback( transactorOptions, + _relayEntry, ) if err != nil { return transaction, becdsakf.errorResolver.ResolveError( err, becdsakf.transactorOptions.From, - value, - "requestNewGroupSelectionSeed", + nil, + "beaconCallback", + _relayEntry, ) } becdsakfLogger.Infof( - "submitted transaction requestNewGroupSelectionSeed with id: [%v] and nonce [%v]", + "submitted transaction beaconCallback with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -1137,20 +1148,22 @@ func (becdsakf *BondedECDSAKeepFactory) RequestNewGroupSelectionSeed( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsakf.contract.RequestNewGroupSelectionSeed( + transaction, err := becdsakf.contract.BeaconCallback( transactorOptions, + _relayEntry, ) if err != nil { return transaction, becdsakf.errorResolver.ResolveError( err, becdsakf.transactorOptions.From, - value, - "requestNewGroupSelectionSeed", + nil, + "beaconCallback", + _relayEntry, ) } becdsakfLogger.Infof( - "submitted transaction requestNewGroupSelectionSeed with id: [%v] and nonce [%v]", + "submitted transaction beaconCallback with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -1165,35 +1178,39 @@ func (becdsakf *BondedECDSAKeepFactory) RequestNewGroupSelectionSeed( } // Non-mutating call, not a transaction submission. -func (becdsakf *BondedECDSAKeepFactory) CallRequestNewGroupSelectionSeed( - value *big.Int, +func (becdsakf *BondedECDSAKeepFactory) CallBeaconCallback( + _relayEntry *big.Int, blockNumber *big.Int, ) error { var result interface{} = nil err := ethutil.CallAtBlock( becdsakf.transactorOptions.From, - blockNumber, value, + blockNumber, nil, becdsakf.contractABI, becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "requestNewGroupSelectionSeed", + "beaconCallback", &result, + _relayEntry, ) return err } -func (becdsakf *BondedECDSAKeepFactory) RequestNewGroupSelectionSeedGasEstimate() (uint64, error) { +func (becdsakf *BondedECDSAKeepFactory) BeaconCallbackGasEstimate( + _relayEntry *big.Int, +) (uint64, error) { var result uint64 result, err := ethutil.EstimateGas( becdsakf.callerOptions.From, becdsakf.contractAddress, - "requestNewGroupSelectionSeed", + "beaconCallback", becdsakf.contractABI, becdsakf.transactor, + _relayEntry, ) return result, err @@ -1201,13 +1218,10 @@ func (becdsakf *BondedECDSAKeepFactory) RequestNewGroupSelectionSeedGasEstimate( // ----- Const Methods ------ -func (becdsakf *BondedECDSAKeepFactory) GetKeepAtIndex( - index *big.Int, -) (common.Address, error) { - var result common.Address - result, err := becdsakf.contract.GetKeepAtIndex( +func (becdsakf *BondedECDSAKeepFactory) GroupSelectionSeed() (*big.Int, error) { + var result *big.Int + result, err := becdsakf.contract.GroupSelectionSeed( becdsakf.callerOptions, - index, ) if err != nil { @@ -1215,19 +1229,17 @@ func (becdsakf *BondedECDSAKeepFactory) GetKeepAtIndex( err, becdsakf.callerOptions.From, nil, - "getKeepAtIndex", - index, + "groupSelectionSeed", ) } return result, err } -func (becdsakf *BondedECDSAKeepFactory) GetKeepAtIndexAtBlock( - index *big.Int, +func (becdsakf *BondedECDSAKeepFactory) GroupSelectionSeedAtBlock( blockNumber *big.Int, -) (common.Address, error) { - var result common.Address +) (*big.Int, error) { + var result *big.Int err := ethutil.CallAtBlock( becdsakf.callerOptions.From, @@ -1237,18 +1249,22 @@ func (becdsakf *BondedECDSAKeepFactory) GetKeepAtIndexAtBlock( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "getKeepAtIndex", + "groupSelectionSeed", &result, - index, ) return result, err } -func (becdsakf *BondedECDSAKeepFactory) MinimumBond() (*big.Int, error) { - var result *big.Int - result, err := becdsakf.contract.MinimumBond( +func (becdsakf *BondedECDSAKeepFactory) IsOperatorRegistered( + _operator common.Address, + _application common.Address, +) (bool, error) { + var result bool + result, err := becdsakf.contract.IsOperatorRegistered( becdsakf.callerOptions, + _operator, + _application, ) if err != nil { @@ -1256,17 +1272,21 @@ func (becdsakf *BondedECDSAKeepFactory) MinimumBond() (*big.Int, error) { err, becdsakf.callerOptions.From, nil, - "minimumBond", + "isOperatorRegistered", + _operator, + _application, ) } return result, err } -func (becdsakf *BondedECDSAKeepFactory) MinimumBondAtBlock( +func (becdsakf *BondedECDSAKeepFactory) IsOperatorRegisteredAtBlock( + _operator common.Address, + _application common.Address, blockNumber *big.Int, -) (*big.Int, error) { - var result *big.Int +) (bool, error) { + var result bool err := ethutil.CallAtBlock( becdsakf.callerOptions.From, @@ -1276,17 +1296,24 @@ func (becdsakf *BondedECDSAKeepFactory) MinimumBondAtBlock( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "minimumBond", + "isOperatorRegistered", &result, + _operator, + _application, ) return result, err } -func (becdsakf *BondedECDSAKeepFactory) GetKeepCount() (*big.Int, error) { - var result *big.Int - result, err := becdsakf.contract.GetKeepCount( +func (becdsakf *BondedECDSAKeepFactory) IsOperatorUpToDate( + _operator common.Address, + _application common.Address, +) (bool, error) { + var result bool + result, err := becdsakf.contract.IsOperatorUpToDate( becdsakf.callerOptions, + _operator, + _application, ) if err != nil { @@ -1294,17 +1321,21 @@ func (becdsakf *BondedECDSAKeepFactory) GetKeepCount() (*big.Int, error) { err, becdsakf.callerOptions.From, nil, - "getKeepCount", + "isOperatorUpToDate", + _operator, + _application, ) } return result, err } -func (becdsakf *BondedECDSAKeepFactory) GetKeepCountAtBlock( +func (becdsakf *BondedECDSAKeepFactory) IsOperatorUpToDateAtBlock( + _operator common.Address, + _application common.Address, blockNumber *big.Int, -) (*big.Int, error) { - var result *big.Int +) (bool, error) { + var result bool err := ethutil.CallAtBlock( becdsakf.callerOptions.From, @@ -1314,20 +1345,19 @@ func (becdsakf *BondedECDSAKeepFactory) GetKeepCountAtBlock( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "getKeepCount", + "isOperatorUpToDate", &result, + _operator, + _application, ) return result, err } -func (becdsakf *BondedECDSAKeepFactory) GetSortitionPoolWeight( - _application common.Address, -) (*big.Int, error) { +func (becdsakf *BondedECDSAKeepFactory) ReseedPool() (*big.Int, error) { var result *big.Int - result, err := becdsakf.contract.GetSortitionPoolWeight( + result, err := becdsakf.contract.ReseedPool( becdsakf.callerOptions, - _application, ) if err != nil { @@ -1335,16 +1365,14 @@ func (becdsakf *BondedECDSAKeepFactory) GetSortitionPoolWeight( err, becdsakf.callerOptions.From, nil, - "getSortitionPoolWeight", - _application, + "reseedPool", ) } return result, err } -func (becdsakf *BondedECDSAKeepFactory) GetSortitionPoolWeightAtBlock( - _application common.Address, +func (becdsakf *BondedECDSAKeepFactory) ReseedPoolAtBlock( blockNumber *big.Int, ) (*big.Int, error) { var result *big.Int @@ -1357,17 +1385,16 @@ func (becdsakf *BondedECDSAKeepFactory) GetSortitionPoolWeightAtBlock( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "getSortitionPoolWeight", + "reseedPool", &result, - _application, ) return result, err } -func (becdsakf *BondedECDSAKeepFactory) MasterKeepAddress() (common.Address, error) { - var result common.Address - result, err := becdsakf.contract.MasterKeepAddress( +func (becdsakf *BondedECDSAKeepFactory) CallbackGas() (*big.Int, error) { + var result *big.Int + result, err := becdsakf.contract.CallbackGas( becdsakf.callerOptions, ) @@ -1376,17 +1403,17 @@ func (becdsakf *BondedECDSAKeepFactory) MasterKeepAddress() (common.Address, err err, becdsakf.callerOptions.From, nil, - "masterKeepAddress", + "callbackGas", ) } return result, err } -func (becdsakf *BondedECDSAKeepFactory) MasterKeepAddressAtBlock( +func (becdsakf *BondedECDSAKeepFactory) CallbackGasAtBlock( blockNumber *big.Int, -) (common.Address, error) { - var result common.Address +) (*big.Int, error) { + var result *big.Int err := ethutil.CallAtBlock( becdsakf.callerOptions.From, @@ -1396,16 +1423,16 @@ func (becdsakf *BondedECDSAKeepFactory) MasterKeepAddressAtBlock( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "masterKeepAddress", + "callbackGas", &result, ) return result, err } -func (becdsakf *BondedECDSAKeepFactory) PoolStakeWeightDivisor() (*big.Int, error) { +func (becdsakf *BondedECDSAKeepFactory) NewEntryFeeEstimate() (*big.Int, error) { var result *big.Int - result, err := becdsakf.contract.PoolStakeWeightDivisor( + result, err := becdsakf.contract.NewEntryFeeEstimate( becdsakf.callerOptions, ) @@ -1414,14 +1441,14 @@ func (becdsakf *BondedECDSAKeepFactory) PoolStakeWeightDivisor() (*big.Int, erro err, becdsakf.callerOptions.From, nil, - "poolStakeWeightDivisor", + "newEntryFeeEstimate", ) } return result, err } -func (becdsakf *BondedECDSAKeepFactory) PoolStakeWeightDivisorAtBlock( +func (becdsakf *BondedECDSAKeepFactory) NewEntryFeeEstimateAtBlock( blockNumber *big.Int, ) (*big.Int, error) { var result *big.Int @@ -1434,20 +1461,20 @@ func (becdsakf *BondedECDSAKeepFactory) PoolStakeWeightDivisorAtBlock( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "poolStakeWeightDivisor", + "newEntryFeeEstimate", &result, ) return result, err } -func (becdsakf *BondedECDSAKeepFactory) GetKeepOpenedTimestamp( - _keep common.Address, -) (*big.Int, error) { - var result *big.Int - result, err := becdsakf.contract.GetKeepOpenedTimestamp( +func (becdsakf *BondedECDSAKeepFactory) Keeps( + arg0 *big.Int, +) (common.Address, error) { + var result common.Address + result, err := becdsakf.contract.Keeps( becdsakf.callerOptions, - _keep, + arg0, ) if err != nil { @@ -1455,19 +1482,19 @@ func (becdsakf *BondedECDSAKeepFactory) GetKeepOpenedTimestamp( err, becdsakf.callerOptions.From, nil, - "getKeepOpenedTimestamp", - _keep, + "keeps", + arg0, ) } return result, err } -func (becdsakf *BondedECDSAKeepFactory) GetKeepOpenedTimestampAtBlock( - _keep common.Address, +func (becdsakf *BondedECDSAKeepFactory) KeepsAtBlock( + arg0 *big.Int, blockNumber *big.Int, -) (*big.Int, error) { - var result *big.Int +) (common.Address, error) { + var result common.Address err := ethutil.CallAtBlock( becdsakf.callerOptions.From, @@ -1477,18 +1504,21 @@ func (becdsakf *BondedECDSAKeepFactory) GetKeepOpenedTimestampAtBlock( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "getKeepOpenedTimestamp", + "keeps", &result, - _keep, + arg0, ) return result, err } -func (becdsakf *BondedECDSAKeepFactory) GroupSelectionSeed() (*big.Int, error) { +func (becdsakf *BondedECDSAKeepFactory) GetSortitionPoolWeight( + _application common.Address, +) (*big.Int, error) { var result *big.Int - result, err := becdsakf.contract.GroupSelectionSeed( + result, err := becdsakf.contract.GetSortitionPoolWeight( becdsakf.callerOptions, + _application, ) if err != nil { @@ -1496,14 +1526,16 @@ func (becdsakf *BondedECDSAKeepFactory) GroupSelectionSeed() (*big.Int, error) { err, becdsakf.callerOptions.From, nil, - "groupSelectionSeed", + "getSortitionPoolWeight", + _application, ) } return result, err } -func (becdsakf *BondedECDSAKeepFactory) GroupSelectionSeedAtBlock( +func (becdsakf *BondedECDSAKeepFactory) GetSortitionPoolWeightAtBlock( + _application common.Address, blockNumber *big.Int, ) (*big.Int, error) { var result *big.Int @@ -1516,8 +1548,9 @@ func (becdsakf *BondedECDSAKeepFactory) GroupSelectionSeedAtBlock( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "groupSelectionSeed", + "getSortitionPoolWeight", &result, + _application, ) return result, err @@ -1567,15 +1600,13 @@ func (becdsakf *BondedECDSAKeepFactory) HasMinimumStakeAtBlock( return result, err } -func (becdsakf *BondedECDSAKeepFactory) IsOperatorRegistered( +func (becdsakf *BondedECDSAKeepFactory) BalanceOf( _operator common.Address, - _application common.Address, -) (bool, error) { - var result bool - result, err := becdsakf.contract.IsOperatorRegistered( +) (*big.Int, error) { + var result *big.Int + result, err := becdsakf.contract.BalanceOf( becdsakf.callerOptions, _operator, - _application, ) if err != nil { @@ -1583,21 +1614,19 @@ func (becdsakf *BondedECDSAKeepFactory) IsOperatorRegistered( err, becdsakf.callerOptions.From, nil, - "isOperatorRegistered", + "balanceOf", _operator, - _application, ) } return result, err } -func (becdsakf *BondedECDSAKeepFactory) IsOperatorRegisteredAtBlock( +func (becdsakf *BondedECDSAKeepFactory) BalanceOfAtBlock( _operator common.Address, - _application common.Address, blockNumber *big.Int, -) (bool, error) { - var result bool +) (*big.Int, error) { + var result *big.Int err := ethutil.CallAtBlock( becdsakf.callerOptions.From, @@ -1607,10 +1636,9 @@ func (becdsakf *BondedECDSAKeepFactory) IsOperatorRegisteredAtBlock( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "isOperatorRegistered", + "balanceOf", &result, _operator, - _application, ) return result, err @@ -1709,9 +1737,9 @@ func (becdsakf *BondedECDSAKeepFactory) IsOperatorEligibleAtBlock( return result, err } -func (becdsakf *BondedECDSAKeepFactory) ReseedPool() (*big.Int, error) { - var result *big.Int - result, err := becdsakf.contract.ReseedPool( +func (becdsakf *BondedECDSAKeepFactory) MasterKeepAddress() (common.Address, error) { + var result common.Address + result, err := becdsakf.contract.MasterKeepAddress( becdsakf.callerOptions, ) @@ -1720,17 +1748,17 @@ func (becdsakf *BondedECDSAKeepFactory) ReseedPool() (*big.Int, error) { err, becdsakf.callerOptions.From, nil, - "reseedPool", + "masterKeepAddress", ) } return result, err } -func (becdsakf *BondedECDSAKeepFactory) ReseedPoolAtBlock( +func (becdsakf *BondedECDSAKeepFactory) MasterKeepAddressAtBlock( blockNumber *big.Int, -) (*big.Int, error) { - var result *big.Int +) (common.Address, error) { + var result common.Address err := ethutil.CallAtBlock( becdsakf.callerOptions.From, @@ -1740,20 +1768,17 @@ func (becdsakf *BondedECDSAKeepFactory) ReseedPoolAtBlock( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "reseedPool", + "masterKeepAddress", &result, ) return result, err } -func (becdsakf *BondedECDSAKeepFactory) BalanceOf( - _operator common.Address, -) (*big.Int, error) { +func (becdsakf *BondedECDSAKeepFactory) NewGroupSelectionSeedFee() (*big.Int, error) { var result *big.Int - result, err := becdsakf.contract.BalanceOf( + result, err := becdsakf.contract.NewGroupSelectionSeedFee( becdsakf.callerOptions, - _operator, ) if err != nil { @@ -1761,16 +1786,14 @@ func (becdsakf *BondedECDSAKeepFactory) BalanceOf( err, becdsakf.callerOptions.From, nil, - "balanceOf", - _operator, + "newGroupSelectionSeedFee", ) } return result, err } -func (becdsakf *BondedECDSAKeepFactory) BalanceOfAtBlock( - _operator common.Address, +func (becdsakf *BondedECDSAKeepFactory) NewGroupSelectionSeedFeeAtBlock( blockNumber *big.Int, ) (*big.Int, error) { var result *big.Int @@ -1783,17 +1806,16 @@ func (becdsakf *BondedECDSAKeepFactory) BalanceOfAtBlock( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "balanceOf", + "newGroupSelectionSeedFee", &result, - _operator, ) return result, err } -func (becdsakf *BondedECDSAKeepFactory) NewEntryFeeEstimate() (*big.Int, error) { +func (becdsakf *BondedECDSAKeepFactory) OpenKeepFeeEstimate() (*big.Int, error) { var result *big.Int - result, err := becdsakf.contract.NewEntryFeeEstimate( + result, err := becdsakf.contract.OpenKeepFeeEstimate( becdsakf.callerOptions, ) @@ -1802,14 +1824,14 @@ func (becdsakf *BondedECDSAKeepFactory) NewEntryFeeEstimate() (*big.Int, error) err, becdsakf.callerOptions.From, nil, - "newEntryFeeEstimate", + "openKeepFeeEstimate", ) } return result, err } -func (becdsakf *BondedECDSAKeepFactory) NewEntryFeeEstimateAtBlock( +func (becdsakf *BondedECDSAKeepFactory) OpenKeepFeeEstimateAtBlock( blockNumber *big.Int, ) (*big.Int, error) { var result *big.Int @@ -1822,17 +1844,20 @@ func (becdsakf *BondedECDSAKeepFactory) NewEntryFeeEstimateAtBlock( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "newEntryFeeEstimate", + "openKeepFeeEstimate", &result, ) return result, err } -func (becdsakf *BondedECDSAKeepFactory) NewGroupSelectionSeedFee() (*big.Int, error) { +func (becdsakf *BondedECDSAKeepFactory) GetKeepOpenedTimestamp( + _keep common.Address, +) (*big.Int, error) { var result *big.Int - result, err := becdsakf.contract.NewGroupSelectionSeedFee( + result, err := becdsakf.contract.GetKeepOpenedTimestamp( becdsakf.callerOptions, + _keep, ) if err != nil { @@ -1840,14 +1865,16 @@ func (becdsakf *BondedECDSAKeepFactory) NewGroupSelectionSeedFee() (*big.Int, er err, becdsakf.callerOptions.From, nil, - "newGroupSelectionSeedFee", + "getKeepOpenedTimestamp", + _keep, ) } return result, err } -func (becdsakf *BondedECDSAKeepFactory) NewGroupSelectionSeedFeeAtBlock( +func (becdsakf *BondedECDSAKeepFactory) GetKeepOpenedTimestampAtBlock( + _keep common.Address, blockNumber *big.Int, ) (*big.Int, error) { var result *big.Int @@ -1860,17 +1887,21 @@ func (becdsakf *BondedECDSAKeepFactory) NewGroupSelectionSeedFeeAtBlock( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "newGroupSelectionSeedFee", + "getKeepOpenedTimestamp", &result, + _keep, ) return result, err } -func (becdsakf *BondedECDSAKeepFactory) CallbackGas() (*big.Int, error) { - var result *big.Int - result, err := becdsakf.contract.CallbackGas( +func (becdsakf *BondedECDSAKeepFactory) IsOperatorAuthorized( + _operator common.Address, +) (bool, error) { + var result bool + result, err := becdsakf.contract.IsOperatorAuthorized( becdsakf.callerOptions, + _operator, ) if err != nil { @@ -1878,17 +1909,19 @@ func (becdsakf *BondedECDSAKeepFactory) CallbackGas() (*big.Int, error) { err, becdsakf.callerOptions.From, nil, - "callbackGas", + "isOperatorAuthorized", + _operator, ) } return result, err } -func (becdsakf *BondedECDSAKeepFactory) CallbackGasAtBlock( +func (becdsakf *BondedECDSAKeepFactory) IsOperatorAuthorizedAtBlock( + _operator common.Address, blockNumber *big.Int, -) (*big.Int, error) { - var result *big.Int +) (bool, error) { + var result bool err := ethutil.CallAtBlock( becdsakf.callerOptions.From, @@ -1898,20 +1931,18 @@ func (becdsakf *BondedECDSAKeepFactory) CallbackGasAtBlock( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "callbackGas", + "isOperatorAuthorized", &result, + _operator, ) return result, err } -func (becdsakf *BondedECDSAKeepFactory) IsOperatorAuthorized( - _operator common.Address, -) (bool, error) { - var result bool - result, err := becdsakf.contract.IsOperatorAuthorized( +func (becdsakf *BondedECDSAKeepFactory) GetKeepCount() (*big.Int, error) { + var result *big.Int + result, err := becdsakf.contract.GetKeepCount( becdsakf.callerOptions, - _operator, ) if err != nil { @@ -1919,19 +1950,17 @@ func (becdsakf *BondedECDSAKeepFactory) IsOperatorAuthorized( err, becdsakf.callerOptions.From, nil, - "isOperatorAuthorized", - _operator, + "getKeepCount", ) } return result, err } -func (becdsakf *BondedECDSAKeepFactory) IsOperatorAuthorizedAtBlock( - _operator common.Address, +func (becdsakf *BondedECDSAKeepFactory) GetKeepCountAtBlock( blockNumber *big.Int, -) (bool, error) { - var result bool +) (*big.Int, error) { + var result *big.Int err := ethutil.CallAtBlock( becdsakf.callerOptions.From, @@ -1941,17 +1970,16 @@ func (becdsakf *BondedECDSAKeepFactory) IsOperatorAuthorizedAtBlock( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "isOperatorAuthorized", + "getKeepCount", &result, - _operator, ) return result, err } -func (becdsakf *BondedECDSAKeepFactory) OpenKeepFeeEstimate() (*big.Int, error) { +func (becdsakf *BondedECDSAKeepFactory) MinimumBond() (*big.Int, error) { var result *big.Int - result, err := becdsakf.contract.OpenKeepFeeEstimate( + result, err := becdsakf.contract.MinimumBond( becdsakf.callerOptions, ) @@ -1960,14 +1988,14 @@ func (becdsakf *BondedECDSAKeepFactory) OpenKeepFeeEstimate() (*big.Int, error) err, becdsakf.callerOptions.From, nil, - "openKeepFeeEstimate", + "minimumBond", ) } return result, err } -func (becdsakf *BondedECDSAKeepFactory) OpenKeepFeeEstimateAtBlock( +func (becdsakf *BondedECDSAKeepFactory) MinimumBondAtBlock( blockNumber *big.Int, ) (*big.Int, error) { var result *big.Int @@ -1980,22 +2008,17 @@ func (becdsakf *BondedECDSAKeepFactory) OpenKeepFeeEstimateAtBlock( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "openKeepFeeEstimate", + "minimumBond", &result, ) return result, err } -func (becdsakf *BondedECDSAKeepFactory) IsOperatorUpToDate( - _operator common.Address, - _application common.Address, -) (bool, error) { - var result bool - result, err := becdsakf.contract.IsOperatorUpToDate( +func (becdsakf *BondedECDSAKeepFactory) PoolStakeWeightDivisor() (*big.Int, error) { + var result *big.Int + result, err := becdsakf.contract.PoolStakeWeightDivisor( becdsakf.callerOptions, - _operator, - _application, ) if err != nil { @@ -2003,21 +2026,17 @@ func (becdsakf *BondedECDSAKeepFactory) IsOperatorUpToDate( err, becdsakf.callerOptions.From, nil, - "isOperatorUpToDate", - _operator, - _application, + "poolStakeWeightDivisor", ) } return result, err } -func (becdsakf *BondedECDSAKeepFactory) IsOperatorUpToDateAtBlock( - _operator common.Address, - _application common.Address, +func (becdsakf *BondedECDSAKeepFactory) PoolStakeWeightDivisorAtBlock( blockNumber *big.Int, -) (bool, error) { - var result bool +) (*big.Int, error) { + var result *big.Int err := ethutil.CallAtBlock( becdsakf.callerOptions.From, @@ -2027,22 +2046,20 @@ func (becdsakf *BondedECDSAKeepFactory) IsOperatorUpToDateAtBlock( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "isOperatorUpToDate", + "poolStakeWeightDivisor", &result, - _operator, - _application, ) return result, err } -func (becdsakf *BondedECDSAKeepFactory) Keeps( - arg0 *big.Int, +func (becdsakf *BondedECDSAKeepFactory) GetKeepAtIndex( + index *big.Int, ) (common.Address, error) { var result common.Address - result, err := becdsakf.contract.Keeps( + result, err := becdsakf.contract.GetKeepAtIndex( becdsakf.callerOptions, - arg0, + index, ) if err != nil { @@ -2050,16 +2067,16 @@ func (becdsakf *BondedECDSAKeepFactory) Keeps( err, becdsakf.callerOptions.From, nil, - "keeps", - arg0, + "getKeepAtIndex", + index, ) } return result, err } -func (becdsakf *BondedECDSAKeepFactory) KeepsAtBlock( - arg0 *big.Int, +func (becdsakf *BondedECDSAKeepFactory) GetKeepAtIndexAtBlock( + index *big.Int, blockNumber *big.Int, ) (common.Address, error) { var result common.Address @@ -2072,9 +2089,9 @@ func (becdsakf *BondedECDSAKeepFactory) KeepsAtBlock( becdsakf.caller, becdsakf.errorResolver, becdsakf.contractAddress, - "keeps", + "getKeepAtIndex", &result, - arg0, + index, ) return result, err @@ -2126,107 +2143,22 @@ func (becdsakf *BondedECDSAKeepFactory) PastBondedECDSAKeepCreatedEvents( func (becdsakf *BondedECDSAKeepFactory) WatchBondedECDSAKeepCreated( success bondedECDSAKeepFactoryBondedECDSAKeepCreatedFunc, - fail func(err error) error, keepAddressFilter []common.Address, ownerFilter []common.Address, applicationFilter []common.Address, -) (subscription.EventSubscription, error) { - errorChan := make(chan error) - unsubscribeChan := make(chan struct{}) - - // Delay which must be preserved before a new resubscription attempt. - // There is no sense to resubscribe immediately after the fail of current - // subscription because the publisher must have some time to recover. - retryDelay := 5 * time.Second - - watch := func() { - failCallback := func(err error) error { - fail(err) - errorChan <- err // trigger resubscription signal - return err - } - - subscription, err := becdsakf.subscribeBondedECDSAKeepCreated( - success, - failCallback, - keepAddressFilter, - ownerFilter, - applicationFilter, - ) - if err != nil { - errorChan <- err // trigger resubscription signal - return - } +) subscription.EventSubscription { + eventOccurred := make(chan *abi.BondedECDSAKeepFactoryBondedECDSAKeepCreated) - // wait for unsubscription signal - <-unsubscribeChan - subscription.Unsubscribe() - } + ctx, cancelCtx := context.WithCancel(context.Background()) - // trigger the resubscriber goroutine + // TODO: Watch* function will soon accept channel as a parameter instead + // of the callback. This loop will be eliminated then. go func() { - go watch() // trigger first subscription - for { select { - case <-errorChan: - becdsakfLogger.Warning( - "subscription to event BondedECDSAKeepCreated terminated with error; " + - "resubscription attempt will be performed after the retry delay", - ) - time.Sleep(retryDelay) - go watch() - case <-unsubscribeChan: - // shutdown the resubscriber goroutine on unsubscribe signal + case <-ctx.Done(): return - } - } - }() - - // closing the unsubscribeChan will trigger a unsubscribe signal and - // run unsubscription for all subscription instances - unsubscribeCallback := func() { - close(unsubscribeChan) - } - - return subscription.NewEventSubscription(unsubscribeCallback), nil -} - -func (becdsakf *BondedECDSAKeepFactory) subscribeBondedECDSAKeepCreated( - success bondedECDSAKeepFactoryBondedECDSAKeepCreatedFunc, - fail func(err error) error, - keepAddressFilter []common.Address, - ownerFilter []common.Address, - applicationFilter []common.Address, -) (subscription.EventSubscription, error) { - eventChan := make(chan *abi.BondedECDSAKeepFactoryBondedECDSAKeepCreated) - eventSubscription, err := becdsakf.contract.WatchBondedECDSAKeepCreated( - nil, - eventChan, - keepAddressFilter, - ownerFilter, - applicationFilter, - ) - if err != nil { - close(eventChan) - return eventSubscription, fmt.Errorf( - "error creating watch for BondedECDSAKeepCreated events: [%v]", - err, - ) - } - - var subscriptionMutex = &sync.Mutex{} - - go func() { - for { - select { - case event, subscribed := <-eventChan: - subscriptionMutex.Lock() - // if eventChan has been closed, it means we have unsubscribed - if !subscribed { - subscriptionMutex.Unlock() - return - } + case event := <-eventOccurred: success( event.KeepAddress, event.Members, @@ -2235,23 +2167,50 @@ func (becdsakf *BondedECDSAKeepFactory) subscribeBondedECDSAKeepCreated( event.HonestThreshold, event.Raw.BlockNumber, ) - subscriptionMutex.Unlock() - case ee := <-eventSubscription.Err(): - fail(ee) - return } } }() - unsubscribeCallback := func() { - subscriptionMutex.Lock() - defer subscriptionMutex.Unlock() + subscribeFn := func(ctx context.Context) (event.Subscription, error) { + return becdsakf.contract.WatchBondedECDSAKeepCreated( + &bind.WatchOpts{Context: ctx}, + eventOccurred, + keepAddressFilter, + ownerFilter, + applicationFilter, + ) + } + + thresholdViolatedFn := func(elapsed time.Duration) { + becdsakfLogger.Errorf( + "subscription to event BondedECDSAKeepCreated had to be "+ + "retried [%s] since the last attempt; please inspect "+ + "Ethereum connectivity", + elapsed, + ) + } - eventSubscription.Unsubscribe() - close(eventChan) + subscriptionFailedFn := func(err error) { + becdsakfLogger.Errorf( + "subscription to event BondedECDSAKeepCreated failed "+ + "with error: [%v]; resubscription attempt will be "+ + "performed", + err, + ) } - return subscription.NewEventSubscription(unsubscribeCallback), nil + sub := ethutil.WithResubscription( + becdsakfSubscriptionBackoffMax, + subscribeFn, + becdsakfSubscriptionAlertThreshold, + thresholdViolatedFn, + subscriptionFailedFn, + ) + + return subscription.NewEventSubscription(func() { + sub.Unsubscribe() + cancelCtx() + }) } type bondedECDSAKeepFactorySortitionPoolCreatedFunc func( @@ -2291,119 +2250,65 @@ func (becdsakf *BondedECDSAKeepFactory) PastSortitionPoolCreatedEvents( func (becdsakf *BondedECDSAKeepFactory) WatchSortitionPoolCreated( success bondedECDSAKeepFactorySortitionPoolCreatedFunc, - fail func(err error) error, applicationFilter []common.Address, -) (subscription.EventSubscription, error) { - errorChan := make(chan error) - unsubscribeChan := make(chan struct{}) - - // Delay which must be preserved before a new resubscription attempt. - // There is no sense to resubscribe immediately after the fail of current - // subscription because the publisher must have some time to recover. - retryDelay := 5 * time.Second - - watch := func() { - failCallback := func(err error) error { - fail(err) - errorChan <- err // trigger resubscription signal - return err - } +) subscription.EventSubscription { + eventOccurred := make(chan *abi.BondedECDSAKeepFactorySortitionPoolCreated) - subscription, err := becdsakf.subscribeSortitionPoolCreated( - success, - failCallback, - applicationFilter, - ) - if err != nil { - errorChan <- err // trigger resubscription signal - return - } - - // wait for unsubscription signal - <-unsubscribeChan - subscription.Unsubscribe() - } + ctx, cancelCtx := context.WithCancel(context.Background()) - // trigger the resubscriber goroutine + // TODO: Watch* function will soon accept channel as a parameter instead + // of the callback. This loop will be eliminated then. go func() { - go watch() // trigger first subscription - for { select { - case <-errorChan: - becdsakfLogger.Warning( - "subscription to event SortitionPoolCreated terminated with error; " + - "resubscription attempt will be performed after the retry delay", - ) - time.Sleep(retryDelay) - go watch() - case <-unsubscribeChan: - // shutdown the resubscriber goroutine on unsubscribe signal + case <-ctx.Done(): return - } - } - }() - - // closing the unsubscribeChan will trigger a unsubscribe signal and - // run unsubscription for all subscription instances - unsubscribeCallback := func() { - close(unsubscribeChan) - } - - return subscription.NewEventSubscription(unsubscribeCallback), nil -} - -func (becdsakf *BondedECDSAKeepFactory) subscribeSortitionPoolCreated( - success bondedECDSAKeepFactorySortitionPoolCreatedFunc, - fail func(err error) error, - applicationFilter []common.Address, -) (subscription.EventSubscription, error) { - eventChan := make(chan *abi.BondedECDSAKeepFactorySortitionPoolCreated) - eventSubscription, err := becdsakf.contract.WatchSortitionPoolCreated( - nil, - eventChan, - applicationFilter, - ) - if err != nil { - close(eventChan) - return eventSubscription, fmt.Errorf( - "error creating watch for SortitionPoolCreated events: [%v]", - err, - ) - } - - var subscriptionMutex = &sync.Mutex{} - - go func() { - for { - select { - case event, subscribed := <-eventChan: - subscriptionMutex.Lock() - // if eventChan has been closed, it means we have unsubscribed - if !subscribed { - subscriptionMutex.Unlock() - return - } + case event := <-eventOccurred: success( event.Application, event.SortitionPool, event.Raw.BlockNumber, ) - subscriptionMutex.Unlock() - case ee := <-eventSubscription.Err(): - fail(ee) - return } } }() - unsubscribeCallback := func() { - subscriptionMutex.Lock() - defer subscriptionMutex.Unlock() + subscribeFn := func(ctx context.Context) (event.Subscription, error) { + return becdsakf.contract.WatchSortitionPoolCreated( + &bind.WatchOpts{Context: ctx}, + eventOccurred, + applicationFilter, + ) + } + + thresholdViolatedFn := func(elapsed time.Duration) { + becdsakfLogger.Errorf( + "subscription to event SortitionPoolCreated had to be "+ + "retried [%s] since the last attempt; please inspect "+ + "Ethereum connectivity", + elapsed, + ) + } - eventSubscription.Unsubscribe() - close(eventChan) + subscriptionFailedFn := func(err error) { + becdsakfLogger.Errorf( + "subscription to event SortitionPoolCreated failed "+ + "with error: [%v]; resubscription attempt will be "+ + "performed", + err, + ) } - return subscription.NewEventSubscription(unsubscribeCallback), nil + sub := ethutil.WithResubscription( + becdsakfSubscriptionBackoffMax, + subscribeFn, + becdsakfSubscriptionAlertThreshold, + thresholdViolatedFn, + subscriptionFailedFn, + ) + + return subscription.NewEventSubscription(func() { + sub.Unsubscribe() + cancelCtx() + }) } diff --git a/pkg/chain/gen/contract/BondedECDSAKeepVendor.go b/pkg/chain/gen/contract/BondedECDSAKeepVendor.go index 315224eac..a8778641e 100644 --- a/pkg/chain/gen/contract/BondedECDSAKeepVendor.go +++ b/pkg/chain/gen/contract/BondedECDSAKeepVendor.go @@ -4,6 +4,7 @@ package contract import ( + "context" "fmt" "math/big" "strings" @@ -15,6 +16,7 @@ import ( "github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/event" "github.com/ipfs/go-log" @@ -28,6 +30,21 @@ import ( // included or excluded from logging at startup by name. var becdsakvLogger = log.Logger("keep-contract-BondedECDSAKeepVendor") +const ( + // Maximum backoff time between event resubscription attempts. + becdsakvSubscriptionBackoffMax = 2 * time.Minute + + // Threshold below which event resubscription emits an error to the logs. + // WS connection can be dropped at any moment and event resubscription will + // follow. However, if WS connection for event subscription is getting + // dropped too often, it may indicate something is wrong with Ethereum + // client. This constant defines the minimum lifetime of an event + // subscription required before the subscription failure happens and + // resubscription follows so that the resubscription does not emit an error + // to the logs alerting about potential problems with Ethereum client. + becdsakvSubscriptionAlertThreshold = 15 * time.Minute +) + type BondedECDSAKeepVendor struct { contract *abi.BondedECDSAKeepVendorImplV1 contractAddress common.Address @@ -94,17 +111,12 @@ func NewBondedECDSAKeepVendor( // ----- Non-const Methods ------ // Transaction submission. -func (becdsakv *BondedECDSAKeepVendor) UpgradeFactory( - _factory common.Address, +func (becdsakv *BondedECDSAKeepVendor) CompleteFactoryUpgrade( transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakvLogger.Debug( - "submitting transaction upgradeFactory", - "params: ", - fmt.Sprint( - _factory, - ), + "submitting transaction completeFactoryUpgrade", ) becdsakv.transactionMutex.Lock() @@ -129,22 +141,20 @@ func (becdsakv *BondedECDSAKeepVendor) UpgradeFactory( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsakv.contract.UpgradeFactory( + transaction, err := becdsakv.contract.CompleteFactoryUpgrade( transactorOptions, - _factory, ) if err != nil { return transaction, becdsakv.errorResolver.ResolveError( err, becdsakv.transactorOptions.From, nil, - "upgradeFactory", - _factory, + "completeFactoryUpgrade", ) } becdsakvLogger.Infof( - "submitted transaction upgradeFactory with id: [%v] and nonce [%v]", + "submitted transaction completeFactoryUpgrade with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -155,22 +165,20 @@ func (becdsakv *BondedECDSAKeepVendor) UpgradeFactory( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsakv.contract.UpgradeFactory( + transaction, err := becdsakv.contract.CompleteFactoryUpgrade( transactorOptions, - _factory, ) if err != nil { return transaction, becdsakv.errorResolver.ResolveError( err, becdsakv.transactorOptions.From, nil, - "upgradeFactory", - _factory, + "completeFactoryUpgrade", ) } becdsakvLogger.Infof( - "submitted transaction upgradeFactory with id: [%v] and nonce [%v]", + "submitted transaction completeFactoryUpgrade with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -185,8 +193,7 @@ func (becdsakv *BondedECDSAKeepVendor) UpgradeFactory( } // Non-mutating call, not a transaction submission. -func (becdsakv *BondedECDSAKeepVendor) CallUpgradeFactory( - _factory common.Address, +func (becdsakv *BondedECDSAKeepVendor) CallCompleteFactoryUpgrade( blockNumber *big.Int, ) error { var result interface{} = nil @@ -198,38 +205,41 @@ func (becdsakv *BondedECDSAKeepVendor) CallUpgradeFactory( becdsakv.caller, becdsakv.errorResolver, becdsakv.contractAddress, - "upgradeFactory", + "completeFactoryUpgrade", &result, - _factory, ) return err } -func (becdsakv *BondedECDSAKeepVendor) UpgradeFactoryGasEstimate( - _factory common.Address, -) (uint64, error) { +func (becdsakv *BondedECDSAKeepVendor) CompleteFactoryUpgradeGasEstimate() (uint64, error) { var result uint64 result, err := ethutil.EstimateGas( becdsakv.callerOptions.From, becdsakv.contractAddress, - "upgradeFactory", + "completeFactoryUpgrade", becdsakv.contractABI, becdsakv.transactor, - _factory, ) return result, err } // Transaction submission. -func (becdsakv *BondedECDSAKeepVendor) CompleteFactoryUpgrade( +func (becdsakv *BondedECDSAKeepVendor) Initialize( + registryAddress common.Address, + factory common.Address, transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakvLogger.Debug( - "submitting transaction completeFactoryUpgrade", + "submitting transaction initialize", + "params: ", + fmt.Sprint( + registryAddress, + factory, + ), ) becdsakv.transactionMutex.Lock() @@ -254,20 +264,24 @@ func (becdsakv *BondedECDSAKeepVendor) CompleteFactoryUpgrade( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsakv.contract.CompleteFactoryUpgrade( + transaction, err := becdsakv.contract.Initialize( transactorOptions, + registryAddress, + factory, ) if err != nil { return transaction, becdsakv.errorResolver.ResolveError( err, becdsakv.transactorOptions.From, nil, - "completeFactoryUpgrade", + "initialize", + registryAddress, + factory, ) } becdsakvLogger.Infof( - "submitted transaction completeFactoryUpgrade with id: [%v] and nonce [%v]", + "submitted transaction initialize with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -278,20 +292,24 @@ func (becdsakv *BondedECDSAKeepVendor) CompleteFactoryUpgrade( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsakv.contract.CompleteFactoryUpgrade( + transaction, err := becdsakv.contract.Initialize( transactorOptions, + registryAddress, + factory, ) if err != nil { return transaction, becdsakv.errorResolver.ResolveError( err, becdsakv.transactorOptions.From, nil, - "completeFactoryUpgrade", + "initialize", + registryAddress, + factory, ) } becdsakvLogger.Infof( - "submitted transaction completeFactoryUpgrade with id: [%v] and nonce [%v]", + "submitted transaction initialize with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -306,7 +324,9 @@ func (becdsakv *BondedECDSAKeepVendor) CompleteFactoryUpgrade( } // Non-mutating call, not a transaction submission. -func (becdsakv *BondedECDSAKeepVendor) CallCompleteFactoryUpgrade( +func (becdsakv *BondedECDSAKeepVendor) CallInitialize( + registryAddress common.Address, + factory common.Address, blockNumber *big.Int, ) error { var result interface{} = nil @@ -318,40 +338,45 @@ func (becdsakv *BondedECDSAKeepVendor) CallCompleteFactoryUpgrade( becdsakv.caller, becdsakv.errorResolver, becdsakv.contractAddress, - "completeFactoryUpgrade", + "initialize", &result, + registryAddress, + factory, ) return err } -func (becdsakv *BondedECDSAKeepVendor) CompleteFactoryUpgradeGasEstimate() (uint64, error) { +func (becdsakv *BondedECDSAKeepVendor) InitializeGasEstimate( + registryAddress common.Address, + factory common.Address, +) (uint64, error) { var result uint64 result, err := ethutil.EstimateGas( becdsakv.callerOptions.From, becdsakv.contractAddress, - "completeFactoryUpgrade", + "initialize", becdsakv.contractABI, becdsakv.transactor, + registryAddress, + factory, ) return result, err } // Transaction submission. -func (becdsakv *BondedECDSAKeepVendor) Initialize( - registryAddress common.Address, - factory common.Address, +func (becdsakv *BondedECDSAKeepVendor) UpgradeFactory( + _factory common.Address, transactionOptions ...ethutil.TransactionOptions, ) (*types.Transaction, error) { becdsakvLogger.Debug( - "submitting transaction initialize", + "submitting transaction upgradeFactory", "params: ", fmt.Sprint( - registryAddress, - factory, + _factory, ), ) @@ -377,24 +402,22 @@ func (becdsakv *BondedECDSAKeepVendor) Initialize( transactorOptions.Nonce = new(big.Int).SetUint64(nonce) - transaction, err := becdsakv.contract.Initialize( + transaction, err := becdsakv.contract.UpgradeFactory( transactorOptions, - registryAddress, - factory, + _factory, ) if err != nil { return transaction, becdsakv.errorResolver.ResolveError( err, becdsakv.transactorOptions.From, nil, - "initialize", - registryAddress, - factory, + "upgradeFactory", + _factory, ) } becdsakvLogger.Infof( - "submitted transaction initialize with id: [%v] and nonce [%v]", + "submitted transaction upgradeFactory with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -405,24 +428,22 @@ func (becdsakv *BondedECDSAKeepVendor) Initialize( transactorOptions.GasLimit = transaction.Gas() transactorOptions.GasPrice = newGasPrice - transaction, err := becdsakv.contract.Initialize( + transaction, err := becdsakv.contract.UpgradeFactory( transactorOptions, - registryAddress, - factory, + _factory, ) if err != nil { return transaction, becdsakv.errorResolver.ResolveError( err, becdsakv.transactorOptions.From, nil, - "initialize", - registryAddress, - factory, + "upgradeFactory", + _factory, ) } becdsakvLogger.Infof( - "submitted transaction initialize with id: [%v] and nonce [%v]", + "submitted transaction upgradeFactory with id: [%v] and nonce [%v]", transaction.Hash().Hex(), transaction.Nonce(), ) @@ -437,9 +458,8 @@ func (becdsakv *BondedECDSAKeepVendor) Initialize( } // Non-mutating call, not a transaction submission. -func (becdsakv *BondedECDSAKeepVendor) CallInitialize( - registryAddress common.Address, - factory common.Address, +func (becdsakv *BondedECDSAKeepVendor) CallUpgradeFactory( + _factory common.Address, blockNumber *big.Int, ) error { var result interface{} = nil @@ -451,29 +471,26 @@ func (becdsakv *BondedECDSAKeepVendor) CallInitialize( becdsakv.caller, becdsakv.errorResolver, becdsakv.contractAddress, - "initialize", + "upgradeFactory", &result, - registryAddress, - factory, + _factory, ) return err } -func (becdsakv *BondedECDSAKeepVendor) InitializeGasEstimate( - registryAddress common.Address, - factory common.Address, +func (becdsakv *BondedECDSAKeepVendor) UpgradeFactoryGasEstimate( + _factory common.Address, ) (uint64, error) { var result uint64 result, err := ethutil.EstimateGas( becdsakv.callerOptions.From, becdsakv.contractAddress, - "initialize", + "upgradeFactory", becdsakv.contractABI, becdsakv.transactor, - registryAddress, - factory, + _factory, ) return result, err @@ -481,9 +498,9 @@ func (becdsakv *BondedECDSAKeepVendor) InitializeGasEstimate( // ----- Const Methods ------ -func (becdsakv *BondedECDSAKeepVendor) Initialized() (bool, error) { - var result bool - result, err := becdsakv.contract.Initialized( +func (becdsakv *BondedECDSAKeepVendor) FactoryUpgradeTimeDelay() (*big.Int, error) { + var result *big.Int + result, err := becdsakv.contract.FactoryUpgradeTimeDelay( becdsakv.callerOptions, ) @@ -492,17 +509,17 @@ func (becdsakv *BondedECDSAKeepVendor) Initialized() (bool, error) { err, becdsakv.callerOptions.From, nil, - "initialized", + "factoryUpgradeTimeDelay", ) } return result, err } -func (becdsakv *BondedECDSAKeepVendor) InitializedAtBlock( +func (becdsakv *BondedECDSAKeepVendor) FactoryUpgradeTimeDelayAtBlock( blockNumber *big.Int, -) (bool, error) { - var result bool +) (*big.Int, error) { + var result *big.Int err := ethutil.CallAtBlock( becdsakv.callerOptions.From, @@ -512,16 +529,16 @@ func (becdsakv *BondedECDSAKeepVendor) InitializedAtBlock( becdsakv.caller, becdsakv.errorResolver, becdsakv.contractAddress, - "initialized", + "factoryUpgradeTimeDelay", &result, ) return result, err } -func (becdsakv *BondedECDSAKeepVendor) SelectFactory() (common.Address, error) { - var result common.Address - result, err := becdsakv.contract.SelectFactory( +func (becdsakv *BondedECDSAKeepVendor) Initialized() (bool, error) { + var result bool + result, err := becdsakv.contract.Initialized( becdsakv.callerOptions, ) @@ -530,17 +547,17 @@ func (becdsakv *BondedECDSAKeepVendor) SelectFactory() (common.Address, error) { err, becdsakv.callerOptions.From, nil, - "selectFactory", + "initialized", ) } return result, err } -func (becdsakv *BondedECDSAKeepVendor) SelectFactoryAtBlock( +func (becdsakv *BondedECDSAKeepVendor) InitializedAtBlock( blockNumber *big.Int, -) (common.Address, error) { - var result common.Address +) (bool, error) { + var result bool err := ethutil.CallAtBlock( becdsakv.callerOptions.From, @@ -550,16 +567,16 @@ func (becdsakv *BondedECDSAKeepVendor) SelectFactoryAtBlock( becdsakv.caller, becdsakv.errorResolver, becdsakv.contractAddress, - "selectFactory", + "initialized", &result, ) return result, err } -func (becdsakv *BondedECDSAKeepVendor) FactoryUpgradeTimeDelay() (*big.Int, error) { - var result *big.Int - result, err := becdsakv.contract.FactoryUpgradeTimeDelay( +func (becdsakv *BondedECDSAKeepVendor) SelectFactory() (common.Address, error) { + var result common.Address + result, err := becdsakv.contract.SelectFactory( becdsakv.callerOptions, ) @@ -568,17 +585,17 @@ func (becdsakv *BondedECDSAKeepVendor) FactoryUpgradeTimeDelay() (*big.Int, erro err, becdsakv.callerOptions.From, nil, - "factoryUpgradeTimeDelay", + "selectFactory", ) } return result, err } -func (becdsakv *BondedECDSAKeepVendor) FactoryUpgradeTimeDelayAtBlock( +func (becdsakv *BondedECDSAKeepVendor) SelectFactoryAtBlock( blockNumber *big.Int, -) (*big.Int, error) { - var result *big.Int +) (common.Address, error) { + var result common.Address err := ethutil.CallAtBlock( becdsakv.callerOptions.From, @@ -588,7 +605,7 @@ func (becdsakv *BondedECDSAKeepVendor) FactoryUpgradeTimeDelayAtBlock( becdsakv.caller, becdsakv.errorResolver, becdsakv.contractAddress, - "factoryUpgradeTimeDelay", + "selectFactory", &result, ) @@ -631,116 +648,64 @@ func (becdsakv *BondedECDSAKeepVendor) PastFactoryUpgradeCompletedEvents( func (becdsakv *BondedECDSAKeepVendor) WatchFactoryUpgradeCompleted( success bondedECDSAKeepVendorFactoryUpgradeCompletedFunc, - fail func(err error) error, -) (subscription.EventSubscription, error) { - errorChan := make(chan error) - unsubscribeChan := make(chan struct{}) - - // Delay which must be preserved before a new resubscription attempt. - // There is no sense to resubscribe immediately after the fail of current - // subscription because the publisher must have some time to recover. - retryDelay := 5 * time.Second - - watch := func() { - failCallback := func(err error) error { - fail(err) - errorChan <- err // trigger resubscription signal - return err - } - - subscription, err := becdsakv.subscribeFactoryUpgradeCompleted( - success, - failCallback, - ) - if err != nil { - errorChan <- err // trigger resubscription signal - return - } +) subscription.EventSubscription { + eventOccurred := make(chan *abi.BondedECDSAKeepVendorImplV1FactoryUpgradeCompleted) - // wait for unsubscription signal - <-unsubscribeChan - subscription.Unsubscribe() - } + ctx, cancelCtx := context.WithCancel(context.Background()) - // trigger the resubscriber goroutine + // TODO: Watch* function will soon accept channel as a parameter instead + // of the callback. This loop will be eliminated then. go func() { - go watch() // trigger first subscription - for { select { - case <-errorChan: - becdsakvLogger.Warning( - "subscription to event FactoryUpgradeCompleted terminated with error; " + - "resubscription attempt will be performed after the retry delay", - ) - time.Sleep(retryDelay) - go watch() - case <-unsubscribeChan: - // shutdown the resubscriber goroutine on unsubscribe signal + case <-ctx.Done(): return + case event := <-eventOccurred: + success( + event.Factory, + event.Raw.BlockNumber, + ) } } }() - // closing the unsubscribeChan will trigger a unsubscribe signal and - // run unsubscription for all subscription instances - unsubscribeCallback := func() { - close(unsubscribeChan) + subscribeFn := func(ctx context.Context) (event.Subscription, error) { + return becdsakv.contract.WatchFactoryUpgradeCompleted( + &bind.WatchOpts{Context: ctx}, + eventOccurred, + ) } - return subscription.NewEventSubscription(unsubscribeCallback), nil -} + thresholdViolatedFn := func(elapsed time.Duration) { + becdsakvLogger.Errorf( + "subscription to event FactoryUpgradeCompleted had to be "+ + "retried [%s] since the last attempt; please inspect "+ + "Ethereum connectivity", + elapsed, + ) + } -func (becdsakv *BondedECDSAKeepVendor) subscribeFactoryUpgradeCompleted( - success bondedECDSAKeepVendorFactoryUpgradeCompletedFunc, - fail func(err error) error, -) (subscription.EventSubscription, error) { - eventChan := make(chan *abi.BondedECDSAKeepVendorImplV1FactoryUpgradeCompleted) - eventSubscription, err := becdsakv.contract.WatchFactoryUpgradeCompleted( - nil, - eventChan, - ) - if err != nil { - close(eventChan) - return eventSubscription, fmt.Errorf( - "error creating watch for FactoryUpgradeCompleted events: [%v]", + subscriptionFailedFn := func(err error) { + becdsakvLogger.Errorf( + "subscription to event FactoryUpgradeCompleted failed "+ + "with error: [%v]; resubscription attempt will be "+ + "performed", err, ) } - var subscriptionMutex = &sync.Mutex{} - - go func() { - for { - select { - case event, subscribed := <-eventChan: - subscriptionMutex.Lock() - // if eventChan has been closed, it means we have unsubscribed - if !subscribed { - subscriptionMutex.Unlock() - return - } - success( - event.Factory, - event.Raw.BlockNumber, - ) - subscriptionMutex.Unlock() - case ee := <-eventSubscription.Err(): - fail(ee) - return - } - } - }() - - unsubscribeCallback := func() { - subscriptionMutex.Lock() - defer subscriptionMutex.Unlock() - - eventSubscription.Unsubscribe() - close(eventChan) - } + sub := ethutil.WithResubscription( + becdsakvSubscriptionBackoffMax, + subscribeFn, + becdsakvSubscriptionAlertThreshold, + thresholdViolatedFn, + subscriptionFailedFn, + ) - return subscription.NewEventSubscription(unsubscribeCallback), nil + return subscription.NewEventSubscription(func() { + sub.Unsubscribe() + cancelCtx() + }) } type bondedECDSAKeepVendorFactoryUpgradeStartedFunc func( @@ -778,115 +743,63 @@ func (becdsakv *BondedECDSAKeepVendor) PastFactoryUpgradeStartedEvents( func (becdsakv *BondedECDSAKeepVendor) WatchFactoryUpgradeStarted( success bondedECDSAKeepVendorFactoryUpgradeStartedFunc, - fail func(err error) error, -) (subscription.EventSubscription, error) { - errorChan := make(chan error) - unsubscribeChan := make(chan struct{}) - - // Delay which must be preserved before a new resubscription attempt. - // There is no sense to resubscribe immediately after the fail of current - // subscription because the publisher must have some time to recover. - retryDelay := 5 * time.Second - - watch := func() { - failCallback := func(err error) error { - fail(err) - errorChan <- err // trigger resubscription signal - return err - } +) subscription.EventSubscription { + eventOccurred := make(chan *abi.BondedECDSAKeepVendorImplV1FactoryUpgradeStarted) - subscription, err := becdsakv.subscribeFactoryUpgradeStarted( - success, - failCallback, - ) - if err != nil { - errorChan <- err // trigger resubscription signal - return - } - - // wait for unsubscription signal - <-unsubscribeChan - subscription.Unsubscribe() - } + ctx, cancelCtx := context.WithCancel(context.Background()) - // trigger the resubscriber goroutine + // TODO: Watch* function will soon accept channel as a parameter instead + // of the callback. This loop will be eliminated then. go func() { - go watch() // trigger first subscription - for { select { - case <-errorChan: - becdsakvLogger.Warning( - "subscription to event FactoryUpgradeStarted terminated with error; " + - "resubscription attempt will be performed after the retry delay", - ) - time.Sleep(retryDelay) - go watch() - case <-unsubscribeChan: - // shutdown the resubscriber goroutine on unsubscribe signal + case <-ctx.Done(): return - } - } - }() - - // closing the unsubscribeChan will trigger a unsubscribe signal and - // run unsubscription for all subscription instances - unsubscribeCallback := func() { - close(unsubscribeChan) - } - - return subscription.NewEventSubscription(unsubscribeCallback), nil -} - -func (becdsakv *BondedECDSAKeepVendor) subscribeFactoryUpgradeStarted( - success bondedECDSAKeepVendorFactoryUpgradeStartedFunc, - fail func(err error) error, -) (subscription.EventSubscription, error) { - eventChan := make(chan *abi.BondedECDSAKeepVendorImplV1FactoryUpgradeStarted) - eventSubscription, err := becdsakv.contract.WatchFactoryUpgradeStarted( - nil, - eventChan, - ) - if err != nil { - close(eventChan) - return eventSubscription, fmt.Errorf( - "error creating watch for FactoryUpgradeStarted events: [%v]", - err, - ) - } - - var subscriptionMutex = &sync.Mutex{} - - go func() { - for { - select { - case event, subscribed := <-eventChan: - subscriptionMutex.Lock() - // if eventChan has been closed, it means we have unsubscribed - if !subscribed { - subscriptionMutex.Unlock() - return - } + case event := <-eventOccurred: success( event.Factory, event.Timestamp, event.Raw.BlockNumber, ) - subscriptionMutex.Unlock() - case ee := <-eventSubscription.Err(): - fail(ee) - return } } }() - unsubscribeCallback := func() { - subscriptionMutex.Lock() - defer subscriptionMutex.Unlock() + subscribeFn := func(ctx context.Context) (event.Subscription, error) { + return becdsakv.contract.WatchFactoryUpgradeStarted( + &bind.WatchOpts{Context: ctx}, + eventOccurred, + ) + } + + thresholdViolatedFn := func(elapsed time.Duration) { + becdsakvLogger.Errorf( + "subscription to event FactoryUpgradeStarted had to be "+ + "retried [%s] since the last attempt; please inspect "+ + "Ethereum connectivity", + elapsed, + ) + } - eventSubscription.Unsubscribe() - close(eventChan) + subscriptionFailedFn := func(err error) { + becdsakvLogger.Errorf( + "subscription to event FactoryUpgradeStarted failed "+ + "with error: [%v]; resubscription attempt will be "+ + "performed", + err, + ) } - return subscription.NewEventSubscription(unsubscribeCallback), nil + sub := ethutil.WithResubscription( + becdsakvSubscriptionBackoffMax, + subscribeFn, + becdsakvSubscriptionAlertThreshold, + thresholdViolatedFn, + subscriptionFailedFn, + ) + + return subscription.NewEventSubscription(func() { + sub.Unsubscribe() + cancelCtx() + }) }