From c3b0582cc93cbfda03623df8b4f6875552335909 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 2 Jun 2022 08:17:52 -0400 Subject: [PATCH] feat: fetch curated communities from smart contract on optimism (#2685) --- contracts/contracts.go | 18 ++ contracts/directory/address.go | 21 ++ contracts/directory/directory.go | 333 +++++++++++++++++++++++++++++ contracts/directory/directory.sol | 14 ++ contracts/directory/doc.go | 3 + multiaccounts/settings/database.go | 8 + node/status_node_services.go | 4 +- protocol/communities/manager.go | 31 +++ protocol/messenger.go | 95 ++++---- protocol/messenger_communities.go | 128 +++++++++++ protocol/messenger_config.go | 9 + services/ext/api.go | 7 + services/ext/service.go | 12 +- services/wakuext/api_test.go | 6 +- services/wakuext/service.go | 11 +- services/wakuv2ext/service.go | 12 +- 16 files changed, 650 insertions(+), 62 deletions(-) create mode 100644 contracts/directory/address.go create mode 100644 contracts/directory/directory.go create mode 100644 contracts/directory/directory.sol create mode 100644 contracts/directory/doc.go diff --git a/contracts/contracts.go b/contracts/contracts.go index 3202cd2e8..c69cb4842 100644 --- a/contracts/contracts.go +++ b/contracts/contracts.go @@ -2,6 +2,7 @@ package contracts import ( "github.com/ethereum/go-ethereum/common" + "github.com/status-im/status-go/contracts/directory" "github.com/status-im/status-go/contracts/registrar" "github.com/status-im/status-go/contracts/resolver" "github.com/status-im/status-go/contracts/snt" @@ -120,3 +121,20 @@ func (c *ContractMaker) NewStickerPack(chainID uint64) (*stickers.StickerPack, e backend, ) } + +func (c *ContractMaker) NewDirectory(chainID uint64) (*directory.Directory, error) { + contractAddr, err := directory.ContractAddress(chainID) + if err != nil { + return nil, err + } + + backend, err := c.RPCClient.EthClient(chainID) + if err != nil { + return nil, err + } + + return directory.NewDirectory( + contractAddr, + backend, + ) +} diff --git a/contracts/directory/address.go b/contracts/directory/address.go new file mode 100644 index 000000000..b6ec5e14d --- /dev/null +++ b/contracts/directory/address.go @@ -0,0 +1,21 @@ +package directory + +import ( + "errors" + + "github.com/ethereum/go-ethereum/common" +) + +var errorNotAvailableOnChainID = errors.New("not available for chainID") + +var contractAddressByChainID = map[uint64]common.Address{ + 69: common.HexToAddress("0x33534cc18D50ab082324A98eE69A7cCe47b75C49"), // optimism kovan testnet +} + +func ContractAddress(chainID uint64) (common.Address, error) { + addr, exists := contractAddressByChainID[chainID] + if !exists { + return *new(common.Address), errorNotAvailableOnChainID + } + return addr, nil +} diff --git a/contracts/directory/directory.go b/contracts/directory/directory.go new file mode 100644 index 000000000..5fe7967fd --- /dev/null +++ b/contracts/directory/directory.go @@ -0,0 +1,333 @@ +// Code generated - DO NOT EDIT. +// This file is a generated binding and any manual changes will be lost. + +package directory + +import ( + "math/big" + "strings" + + ethereum "github.com/ethereum/go-ethereum" + "github.com/ethereum/go-ethereum/accounts/abi" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/event" +) + +// Reference imports to suppress errors if they are not otherwise used. +var ( + _ = big.NewInt + _ = strings.NewReader + _ = ethereum.NotFound + _ = bind.Bind + _ = common.Big1 + _ = types.BloomLookup + _ = event.NewSubscription +) + +// DirectoryABI is the input ABI used to generate the binding from. +const DirectoryABI = "[{\"inputs\":[{\"internalType\":\"bytes\",\"name\":\"community\",\"type\":\"bytes\"}],\"name\":\"addCommunity\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"uint256\",\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"communities\",\"outputs\":[{\"internalType\":\"bytes\",\"name\":\"\",\"type\":\"bytes\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"name\":\"getCommunities\",\"outputs\":[{\"internalType\":\"bytes[]\",\"name\":\"\",\"type\":\"bytes[]\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"bytes\",\"name\":\"community\",\"type\":\"bytes\"}],\"name\":\"isCommunityInDirectory\",\"outputs\":[{\"internalType\":\"bool\",\"name\":\"\",\"type\":\"bool\"}],\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":\"bytes\",\"name\":\"community\",\"type\":\"bytes\"}],\"name\":\"removeCommunity\",\"outputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]" + +// DirectoryFuncSigs maps the 4-byte function signature to its string representation. +var DirectoryFuncSigs = map[string]string{ + "74837935": "addCommunity(bytes)", + "e590b56a": "communities(uint256)", + "c251b565": "getCommunities()", + "b3dbb52a": "isCommunityInDirectory(bytes)", + "3c01b93c": "removeCommunity(bytes)", +} + +// DirectoryBin is the compiled bytecode used for deploying new contracts. +var DirectoryBin = "0x608060405234801561001057600080fd5b5061033b806100206000396000f3fe608060405234801561001057600080fd5b50600436106100575760003560e01c80633c01b93c1461005c578063748379351461005c578063b3dbb52a14610070578063c251b5651461009b578063e590b56a146100aa575b600080fd5b61006e61006a366004610176565b5050565b005b61008661007e366004610176565b600092915050565b60405190151581526020015b60405180910390f35b60606040516100929190610235565b6100bd6100b8366004610297565b6100ca565b60405161009291906102b0565b600081815481106100da57600080fd5b9060005260206000200160009150905080546100f5906102ca565b80601f0160208091040260200160405190810160405280929190818152602001828054610121906102ca565b801561016e5780601f106101435761010080835404028352916020019161016e565b820191906000526020600020905b81548152906001019060200180831161015157829003601f168201915b505050505081565b6000806020838503121561018957600080fd5b823567ffffffffffffffff808211156101a157600080fd5b818501915085601f8301126101b557600080fd5b8135818111156101c457600080fd5b8660208285010111156101d657600080fd5b60209290920196919550909350505050565b6000815180845260005b8181101561020e576020818501810151868301820152016101f2565b81811115610220576000602083870101525b50601f01601f19169290920160200192915050565b6000602080830181845280855180835260408601915060408160051b870101925083870160005b8281101561028a57603f198886030184526102788583516101e8565b9450928501929085019060010161025c565b5092979650505050505050565b6000602082840312156102a957600080fd5b5035919050565b6020815260006102c360208301846101e8565b9392505050565b600181811c908216806102de57607f821691505b602082108114156102ff57634e487b7160e01b600052602260045260246000fd5b5091905056fea2646970667358221220f39ece68bf28d27ae1776133faa6ead8c1a3f73d975864e0cf6295808b53284664736f6c634300080b0033" + +// DeployDirectory deploys a new Ethereum contract, binding an instance of Directory to it. +func DeployDirectory(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *Directory, error) { + parsed, err := abi.JSON(strings.NewReader(DirectoryABI)) + if err != nil { + return common.Address{}, nil, nil, err + } + + address, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex(DirectoryBin), backend) + if err != nil { + return common.Address{}, nil, nil, err + } + return address, tx, &Directory{DirectoryCaller: DirectoryCaller{contract: contract}, DirectoryTransactor: DirectoryTransactor{contract: contract}, DirectoryFilterer: DirectoryFilterer{contract: contract}}, nil +} + +// Directory is an auto generated Go binding around an Ethereum contract. +type Directory struct { + DirectoryCaller // Read-only binding to the contract + DirectoryTransactor // Write-only binding to the contract + DirectoryFilterer // Log filterer for contract events +} + +// DirectoryCaller is an auto generated read-only Go binding around an Ethereum contract. +type DirectoryCaller struct { + contract *bind.BoundContract // Generic contract wrapper for the low level calls +} + +// DirectoryTransactor is an auto generated write-only Go binding around an Ethereum contract. +type DirectoryTransactor struct { + contract *bind.BoundContract // Generic contract wrapper for the low level calls +} + +// DirectoryFilterer is an auto generated log filtering Go binding around an Ethereum contract events. +type DirectoryFilterer struct { + contract *bind.BoundContract // Generic contract wrapper for the low level calls +} + +// DirectorySession is an auto generated Go binding around an Ethereum contract, +// with pre-set call and transact options. +type DirectorySession struct { + Contract *Directory // Generic contract binding to set the session for + CallOpts bind.CallOpts // Call options to use throughout this session + TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session +} + +// DirectoryCallerSession is an auto generated read-only Go binding around an Ethereum contract, +// with pre-set call options. +type DirectoryCallerSession struct { + Contract *DirectoryCaller // Generic contract caller binding to set the session for + CallOpts bind.CallOpts // Call options to use throughout this session +} + +// DirectoryTransactorSession is an auto generated write-only Go binding around an Ethereum contract, +// with pre-set transact options. +type DirectoryTransactorSession struct { + Contract *DirectoryTransactor // Generic contract transactor binding to set the session for + TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session +} + +// DirectoryRaw is an auto generated low-level Go binding around an Ethereum contract. +type DirectoryRaw struct { + Contract *Directory // Generic contract binding to access the raw methods on +} + +// DirectoryCallerRaw is an auto generated low-level read-only Go binding around an Ethereum contract. +type DirectoryCallerRaw struct { + Contract *DirectoryCaller // Generic read-only contract binding to access the raw methods on +} + +// DirectoryTransactorRaw is an auto generated low-level write-only Go binding around an Ethereum contract. +type DirectoryTransactorRaw struct { + Contract *DirectoryTransactor // Generic write-only contract binding to access the raw methods on +} + +// NewDirectory creates a new instance of Directory, bound to a specific deployed contract. +func NewDirectory(address common.Address, backend bind.ContractBackend) (*Directory, error) { + contract, err := bindDirectory(address, backend, backend, backend) + if err != nil { + return nil, err + } + return &Directory{DirectoryCaller: DirectoryCaller{contract: contract}, DirectoryTransactor: DirectoryTransactor{contract: contract}, DirectoryFilterer: DirectoryFilterer{contract: contract}}, nil +} + +// NewDirectoryCaller creates a new read-only instance of Directory, bound to a specific deployed contract. +func NewDirectoryCaller(address common.Address, caller bind.ContractCaller) (*DirectoryCaller, error) { + contract, err := bindDirectory(address, caller, nil, nil) + if err != nil { + return nil, err + } + return &DirectoryCaller{contract: contract}, nil +} + +// NewDirectoryTransactor creates a new write-only instance of Directory, bound to a specific deployed contract. +func NewDirectoryTransactor(address common.Address, transactor bind.ContractTransactor) (*DirectoryTransactor, error) { + contract, err := bindDirectory(address, nil, transactor, nil) + if err != nil { + return nil, err + } + return &DirectoryTransactor{contract: contract}, nil +} + +// NewDirectoryFilterer creates a new log filterer instance of Directory, bound to a specific deployed contract. +func NewDirectoryFilterer(address common.Address, filterer bind.ContractFilterer) (*DirectoryFilterer, error) { + contract, err := bindDirectory(address, nil, nil, filterer) + if err != nil { + return nil, err + } + return &DirectoryFilterer{contract: contract}, nil +} + +// bindDirectory binds a generic wrapper to an already deployed contract. +func bindDirectory(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor, filterer bind.ContractFilterer) (*bind.BoundContract, error) { + parsed, err := abi.JSON(strings.NewReader(DirectoryABI)) + if err != nil { + return nil, err + } + return bind.NewBoundContract(address, parsed, caller, transactor, filterer), nil +} + +// Call invokes the (constant) contract method with params as input values and +// sets the output to result. The result type might be a single field for simple +// returns, a slice of interfaces for anonymous returns and a struct for named +// returns. +func (_Directory *DirectoryRaw) Call(opts *bind.CallOpts, result *[]interface{}, method string, params ...interface{}) error { + return _Directory.Contract.DirectoryCaller.contract.Call(opts, result, method, params...) +} + +// Transfer initiates a plain transaction to move funds to the contract, calling +// its default method if one is available. +func (_Directory *DirectoryRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) { + return _Directory.Contract.DirectoryTransactor.contract.Transfer(opts) +} + +// Transact invokes the (paid) contract method with params as input values. +func (_Directory *DirectoryRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { + return _Directory.Contract.DirectoryTransactor.contract.Transact(opts, method, params...) +} + +// Call invokes the (constant) contract method with params as input values and +// sets the output to result. The result type might be a single field for simple +// returns, a slice of interfaces for anonymous returns and a struct for named +// returns. +func (_Directory *DirectoryCallerRaw) Call(opts *bind.CallOpts, result *[]interface{}, method string, params ...interface{}) error { + return _Directory.Contract.contract.Call(opts, result, method, params...) +} + +// Transfer initiates a plain transaction to move funds to the contract, calling +// its default method if one is available. +func (_Directory *DirectoryTransactorRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) { + return _Directory.Contract.contract.Transfer(opts) +} + +// Transact invokes the (paid) contract method with params as input values. +func (_Directory *DirectoryTransactorRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { + return _Directory.Contract.contract.Transact(opts, method, params...) +} + +// Communities is a free data retrieval call binding the contract method 0xe590b56a. +// +// Solidity: function communities(uint256 ) view returns(bytes) +func (_Directory *DirectoryCaller) Communities(opts *bind.CallOpts, arg0 *big.Int) ([]byte, error) { + var out []interface{} + err := _Directory.contract.Call(opts, &out, "communities", arg0) + + if err != nil { + return *new([]byte), err + } + + out0 := *abi.ConvertType(out[0], new([]byte)).(*[]byte) + + return out0, err + +} + +// Communities is a free data retrieval call binding the contract method 0xe590b56a. +// +// Solidity: function communities(uint256 ) view returns(bytes) +func (_Directory *DirectorySession) Communities(arg0 *big.Int) ([]byte, error) { + return _Directory.Contract.Communities(&_Directory.CallOpts, arg0) +} + +// Communities is a free data retrieval call binding the contract method 0xe590b56a. +// +// Solidity: function communities(uint256 ) view returns(bytes) +func (_Directory *DirectoryCallerSession) Communities(arg0 *big.Int) ([]byte, error) { + return _Directory.Contract.Communities(&_Directory.CallOpts, arg0) +} + +// GetCommunities is a free data retrieval call binding the contract method 0xc251b565. +// +// Solidity: function getCommunities() view returns(bytes[]) +func (_Directory *DirectoryCaller) GetCommunities(opts *bind.CallOpts) ([][]byte, error) { + var out []interface{} + err := _Directory.contract.Call(opts, &out, "getCommunities") + + if err != nil { + return *new([][]byte), err + } + + out0 := *abi.ConvertType(out[0], new([][]byte)).(*[][]byte) + + return out0, err + +} + +// GetCommunities is a free data retrieval call binding the contract method 0xc251b565. +// +// Solidity: function getCommunities() view returns(bytes[]) +func (_Directory *DirectorySession) GetCommunities() ([][]byte, error) { + return _Directory.Contract.GetCommunities(&_Directory.CallOpts) +} + +// GetCommunities is a free data retrieval call binding the contract method 0xc251b565. +// +// Solidity: function getCommunities() view returns(bytes[]) +func (_Directory *DirectoryCallerSession) GetCommunities() ([][]byte, error) { + return _Directory.Contract.GetCommunities(&_Directory.CallOpts) +} + +// IsCommunityInDirectory is a free data retrieval call binding the contract method 0xb3dbb52a. +// +// Solidity: function isCommunityInDirectory(bytes community) view returns(bool) +func (_Directory *DirectoryCaller) IsCommunityInDirectory(opts *bind.CallOpts, community []byte) (bool, error) { + var out []interface{} + err := _Directory.contract.Call(opts, &out, "isCommunityInDirectory", community) + + if err != nil { + return *new(bool), err + } + + out0 := *abi.ConvertType(out[0], new(bool)).(*bool) + + return out0, err + +} + +// IsCommunityInDirectory is a free data retrieval call binding the contract method 0xb3dbb52a. +// +// Solidity: function isCommunityInDirectory(bytes community) view returns(bool) +func (_Directory *DirectorySession) IsCommunityInDirectory(community []byte) (bool, error) { + return _Directory.Contract.IsCommunityInDirectory(&_Directory.CallOpts, community) +} + +// IsCommunityInDirectory is a free data retrieval call binding the contract method 0xb3dbb52a. +// +// Solidity: function isCommunityInDirectory(bytes community) view returns(bool) +func (_Directory *DirectoryCallerSession) IsCommunityInDirectory(community []byte) (bool, error) { + return _Directory.Contract.IsCommunityInDirectory(&_Directory.CallOpts, community) +} + +// AddCommunity is a paid mutator transaction binding the contract method 0x74837935. +// +// Solidity: function addCommunity(bytes community) returns() +func (_Directory *DirectoryTransactor) AddCommunity(opts *bind.TransactOpts, community []byte) (*types.Transaction, error) { + return _Directory.contract.Transact(opts, "addCommunity", community) +} + +// AddCommunity is a paid mutator transaction binding the contract method 0x74837935. +// +// Solidity: function addCommunity(bytes community) returns() +func (_Directory *DirectorySession) AddCommunity(community []byte) (*types.Transaction, error) { + return _Directory.Contract.AddCommunity(&_Directory.TransactOpts, community) +} + +// AddCommunity is a paid mutator transaction binding the contract method 0x74837935. +// +// Solidity: function addCommunity(bytes community) returns() +func (_Directory *DirectoryTransactorSession) AddCommunity(community []byte) (*types.Transaction, error) { + return _Directory.Contract.AddCommunity(&_Directory.TransactOpts, community) +} + +// RemoveCommunity is a paid mutator transaction binding the contract method 0x3c01b93c. +// +// Solidity: function removeCommunity(bytes community) returns() +func (_Directory *DirectoryTransactor) RemoveCommunity(opts *bind.TransactOpts, community []byte) (*types.Transaction, error) { + return _Directory.contract.Transact(opts, "removeCommunity", community) +} + +// RemoveCommunity is a paid mutator transaction binding the contract method 0x3c01b93c. +// +// Solidity: function removeCommunity(bytes community) returns() +func (_Directory *DirectorySession) RemoveCommunity(community []byte) (*types.Transaction, error) { + return _Directory.Contract.RemoveCommunity(&_Directory.TransactOpts, community) +} + +// RemoveCommunity is a paid mutator transaction binding the contract method 0x3c01b93c. +// +// Solidity: function removeCommunity(bytes community) returns() +func (_Directory *DirectoryTransactorSession) RemoveCommunity(community []byte) (*types.Transaction, error) { + return _Directory.Contract.RemoveCommunity(&_Directory.TransactOpts, community) +} diff --git a/contracts/directory/directory.sol b/contracts/directory/directory.sol new file mode 100644 index 000000000..77be0ad61 --- /dev/null +++ b/contracts/directory/directory.sol @@ -0,0 +1,14 @@ +pragma solidity ^0.8.5; + +contract Directory { + + bytes[] public communities; + + function isCommunityInDirectory(bytes calldata community) public view returns (bool) { } + + function getCommunities() public view returns (bytes[] memory) { } + + function addCommunity(bytes calldata community) public { } + + function removeCommunity(bytes calldata community) public { } +} diff --git a/contracts/directory/doc.go b/contracts/directory/doc.go new file mode 100644 index 000000000..c3fd39e89 --- /dev/null +++ b/contracts/directory/doc.go @@ -0,0 +1,3 @@ +package directory + +//go:generate abigen -sol directory.sol -pkg directory -out directory.go diff --git a/multiaccounts/settings/database.go b/multiaccounts/settings/database.go index 19fc933ba..6d9e7be82 100644 --- a/multiaccounts/settings/database.go +++ b/multiaccounts/settings/database.go @@ -545,3 +545,11 @@ func (db *Database) GetWalletRootAddress() (rst types.Address, err error) { } return } + +func (db *Database) TestNetworksEnabled() (rst bool, err error) { + err = db.makeSelectRow(TestNetworksEnabled).Scan(&rst) + if err == sql.ErrNoRows { + return rst, nil + } + return +} diff --git a/node/status_node_services.go b/node/status_node_services.go index 2499a4fce..adf5dea15 100644 --- a/node/status_node_services.go +++ b/node/status_node_services.go @@ -173,7 +173,7 @@ func (b *StatusNode) wakuExtService(config *params.NodeConfig) (*wakuext.Service } if b.wakuExtSrvc == nil { - b.wakuExtSrvc = wakuext.New(*config, b.nodeBridge(), ext.EnvelopeSignalHandler{}, b.db) + b.wakuExtSrvc = wakuext.New(*config, b.nodeBridge(), b.rpcClient, ext.EnvelopeSignalHandler{}, b.db) } b.wakuExtSrvc.SetP2PServer(b.gethNode.Server()) @@ -185,7 +185,7 @@ func (b *StatusNode) wakuV2ExtService(config *params.NodeConfig) (*wakuv2ext.Ser return nil, errors.New("geth node not initialized") } if b.wakuV2ExtSrvc == nil { - b.wakuV2ExtSrvc = wakuv2ext.New(*config, b.nodeBridge(), ext.EnvelopeSignalHandler{}, b.db) + b.wakuV2ExtSrvc = wakuv2ext.New(*config, b.nodeBridge(), b.rpcClient, ext.EnvelopeSignalHandler{}, b.db) } b.wakuV2ExtSrvc.SetP2PServer(b.gethNode.Server()) diff --git a/protocol/communities/manager.go b/protocol/communities/manager.go index 18ba056e5..7e8e10605 100644 --- a/protocol/communities/manager.go +++ b/protocol/communities/manager.go @@ -238,6 +238,37 @@ func (m *Manager) All() ([]*Community, error) { return m.persistence.AllCommunities(m.identity) } +type KnownCommunitiesResponse struct { + ContractCommunities []string `json:"contractCommunities"` + Descriptions map[string]*Community `json:"communities"` + UnknownCommunities []string `json:"unknownCommunities"` +} + +func (m *Manager) GetStoredDescriptionForCommunities(communityIDs []types.HexBytes) (response *KnownCommunitiesResponse, err error) { + response = &KnownCommunitiesResponse{ + Descriptions: make(map[string]*Community), + } + + for i := range communityIDs { + communityID := communityIDs[i].String() + var community *Community + community, err = m.GetByID(communityIDs[i]) + if err != nil { + return + } + + response.ContractCommunities = append(response.ContractCommunities, communityID) + + if community != nil { + response.Descriptions[community.IDString()] = community + } else { + response.UnknownCommunities = append(response.UnknownCommunities, communityID) + } + } + + return +} + func (m *Manager) Joined() ([]*Community, error) { return m.persistence.JoinedCommunities(m.identity) } diff --git a/protocol/messenger.go b/protocol/messenger.go index 3eb8a41e9..83ce95c88 100644 --- a/protocol/messenger.go +++ b/protocol/messenger.go @@ -15,6 +15,7 @@ import ( "sync" "time" + "github.com/status-im/status-go/contracts" "github.com/status-im/status-go/services/browsers" "github.com/pkg/errors" @@ -89,45 +90,49 @@ var messageCacheIntervalMs uint64 = 1000 * 60 * 60 * 48 // Similarly, it needs to expose an interface to manage // mailservers because they can also be managed by the user. type Messenger struct { - node types.Node - server *p2p.Server - peerStore *mailservers.PeerStore - config *config - identity *ecdsa.PrivateKey - persistence *sqlitePersistence - transport *transport.Transport - encryptor *encryption.Protocol - sender *common.MessageSender - ensVerifier *ens.Verifier - anonMetricsClient *anonmetrics.Client - anonMetricsServer *anonmetrics.Server - pushNotificationClient *pushnotificationclient.Client - pushNotificationServer *pushnotificationserver.Server - communitiesManager *communities.Manager - logger *zap.Logger - verifyTransactionClient EthClient - featureFlags common.FeatureFlags - shutdownTasks []func() error - shouldPublishContactCode bool - systemMessagesTranslations *systemMessageTranslationsMap - allChats *chatMap - allContacts *contactMap - allInstallations *installationMap - modifiedInstallations *stringBoolMap - installationID string - mailserverCycle mailserverCycle - database *sql.DB - multiAccounts *multiaccounts.Database - mailservers *mailserversDB.Database - settings *accounts.Database - account *multiaccounts.Account - mailserversDatabase *mailserversDB.Database - browserDatabase *browsers.Database - httpServer *server.Server - quit chan struct{} - requestedCommunities map[string]*transport.Filter + node types.Node + server *p2p.Server + peerStore *mailservers.PeerStore + config *config + identity *ecdsa.PrivateKey + persistence *sqlitePersistence + transport *transport.Transport + encryptor *encryption.Protocol + sender *common.MessageSender + ensVerifier *ens.Verifier + anonMetricsClient *anonmetrics.Client + anonMetricsServer *anonmetrics.Server + pushNotificationClient *pushnotificationclient.Client + pushNotificationServer *pushnotificationserver.Server + communitiesManager *communities.Manager + logger *zap.Logger + verifyTransactionClient EthClient + featureFlags common.FeatureFlags + shutdownTasks []func() error + shouldPublishContactCode bool + systemMessagesTranslations *systemMessageTranslationsMap + allChats *chatMap + allContacts *contactMap + allInstallations *installationMap + modifiedInstallations *stringBoolMap + installationID string + mailserverCycle mailserverCycle + database *sql.DB + multiAccounts *multiaccounts.Database + mailservers *mailserversDB.Database + settings *accounts.Database + account *multiaccounts.Account + mailserversDatabase *mailserversDB.Database + browserDatabase *browsers.Database + httpServer *server.Server + quit chan struct{} + + requestedCommunitiesLock sync.RWMutex + requestedCommunities map[string]*transport.Filter + connectionState connection.State telemetryClient *telemetry.Client + contractMaker *contracts.ContractMaker downloadHistoryArchiveTasksWaitGroup sync.WaitGroup // TODO(samyoul) Determine if/how the remaining usage of this mutex can be removed mutex sync.Mutex @@ -425,12 +430,16 @@ func NewMessenger( peers: make(map[string]peerStatus), availabilitySubscriptions: make([]chan struct{}, 0), }, - mailserversDatabase: c.mailserversDatabase, - account: c.account, - quit: make(chan struct{}), - requestedCommunities: make(map[string]*transport.Filter), - browserDatabase: c.browserDatabase, - httpServer: c.httpServer, + mailserversDatabase: c.mailserversDatabase, + account: c.account, + quit: make(chan struct{}), + requestedCommunitiesLock: sync.RWMutex{}, + requestedCommunities: make(map[string]*transport.Filter), + browserDatabase: c.browserDatabase, + httpServer: c.httpServer, + contractMaker: &contracts.ContractMaker{ + RPCClient: c.rpcClient, + }, shutdownTasks: []func() error{ ensVerifier.Stop, pushNotificationClient.Stop, diff --git a/protocol/messenger_communities.go b/protocol/messenger_communities.go index 7c2767ca1..988954dbe 100644 --- a/protocol/messenger_communities.go +++ b/protocol/messenger_communities.go @@ -9,6 +9,8 @@ import ( "github.com/golang/protobuf/proto" "go.uber.org/zap" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/status-im/status-go/eth-node/crypto" "github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/protocol/common" @@ -199,6 +201,43 @@ func (m *Messenger) JoinedCommunities() ([]*communities.Community, error) { return m.communitiesManager.Joined() } +func (m *Messenger) CuratedCommunities() (*communities.KnownCommunitiesResponse, error) { + testNetworksEnabled, err := m.settings.TestNetworksEnabled() + if err != nil { + return nil, err + } + + chainID := uint64(10) // Optimism (mainnet) + if testNetworksEnabled { + chainID = 69 // Optimism (kovan) + } + + directory, err := m.contractMaker.NewDirectory(chainID) + if err != nil { + return nil, err + } + + callOpts := &bind.CallOpts{Context: context.Background(), Pending: false} + + communities, err := directory.GetCommunities(callOpts) + if err != nil { + return nil, err + } + var communityIDs []types.HexBytes + for _, c := range communities { + communityIDs = append(communityIDs, c) + } + + response, err := m.communitiesManager.GetStoredDescriptionForCommunities(communityIDs) + if err != nil { + return nil, err + } + + go m.requestCommunitiesFromMailserver(response.UnknownCommunities) + + return response, nil +} + func (m *Messenger) JoinCommunity(ctx context.Context, communityID types.HexBytes) (*MessengerResponse, error) { mr, err := m.joinCommunity(ctx, communityID) if err != nil { @@ -921,6 +960,9 @@ func (m *Messenger) RequestCommunityInfoFromMailserverAsync(communityID string) // RequestCommunityInfoFromMailserver installs filter for community and requests its details // from mailserver. When response received it will be passed through signals handler func (m *Messenger) requestCommunityInfoFromMailserver(communityID string, waitForResponse bool) (*communities.Community, error) { + m.requestedCommunitiesLock.Lock() + defer m.requestedCommunitiesLock.Unlock() + if _, ok := m.requestedCommunities[communityID]; ok { return nil, nil } @@ -1000,6 +1042,92 @@ func (m *Messenger) requestCommunityInfoFromMailserver(communityID string, waitF return community, nil } +// RequestCommunityInfoFromMailserver installs filter for community and requests its details +// from mailserver. When response received it will be passed through signals handler +func (m *Messenger) requestCommunitiesFromMailserver(communityIDs []string) { + m.requestedCommunitiesLock.Lock() + defer m.requestedCommunitiesLock.Unlock() + + var topics []types.TopicType + for _, communityID := range communityIDs { + if _, ok := m.requestedCommunities[communityID]; ok { + continue + } + + //If filter wasn't installed we create it and remember for deinstalling after + //response received + filter := m.transport.FilterByChatID(communityID) + if filter == nil { + filters, err := m.transport.InitPublicFilters([]string{communityID}) + if err != nil { + m.logger.Error("Can't install filter for community", zap.Error(err)) + continue + } + if len(filters) != 1 { + m.logger.Error("Unexpected amount of filters created") + continue + } + filter = filters[0] + m.requestedCommunities[communityID] = filter + } else { + //we don't remember filter id associated with community because it was already installed + m.requestedCommunities[communityID] = nil + } + topics = append(topics, filter.Topic) + } + + to := uint32(m.transport.GetCurrentTime() / 1000) + from := to - oneMonthInSeconds + + _, err := m.performMailserverRequest(func() (*MessengerResponse, error) { + batch := MailserverBatch{From: from, To: to, Topics: topics} + m.logger.Info("Requesting historic") + err := m.processMailserverBatch(batch) + return nil, err + }) + + if err != nil { + m.logger.Error("Err performing mailserver request", zap.Error(err)) + return + } + + ctx := context.Background() + ctx, cancel := context.WithTimeout(ctx, 15*time.Second) + defer cancel() + + fetching := true + for fetching { + select { + case <-time.After(200 * time.Millisecond): + allLoaded := true + for _, c := range communityIDs { + community, err := m.communitiesManager.GetByIDString(c) + if err != nil { + m.logger.Error("Error loading community", zap.Error(err)) + break + } + + if community == nil || community.Name() == "" || community.DescriptionText() == "" { + allLoaded = false + break + } + } + + if allLoaded { + fetching = false + } + + case <-ctx.Done(): + fetching = false + } + } + + for _, c := range communityIDs { + m.forgetCommunityRequest(c) + } + +} + // forgetCommunityRequest removes community from requested ones and removes filter func (m *Messenger) forgetCommunityRequest(communityID string) { filter, ok := m.requestedCommunities[communityID] diff --git a/protocol/messenger_config.go b/protocol/messenger_config.go index b9ce52544..e6b2ba71b 100644 --- a/protocol/messenger_config.go +++ b/protocol/messenger_config.go @@ -4,6 +4,7 @@ import ( "database/sql" "encoding/json" + "github.com/status-im/status-go/rpc" "github.com/status-im/status-go/server" "github.com/status-im/status-go/services/browsers" @@ -70,6 +71,7 @@ type config struct { browserDatabase *browsers.Database torrentConfig *params.TorrentConfig httpServer *server.Server + rpcClient *rpc.Client verifyTransactionClient EthClient verifyENSURL string @@ -284,3 +286,10 @@ func WithHTTPServer(s *server.Server) Option { return nil } } + +func WithRPCClient(r *rpc.Client) Option { + return func(c *config) error { + c.rpcClient = r + return nil + } +} diff --git a/services/ext/api.go b/services/ext/api.go index 48134feb5..7626b20b3 100644 --- a/services/ext/api.go +++ b/services/ext/api.go @@ -352,6 +352,13 @@ func (api *PublicAPI) JoinedCommunities(parent context.Context) ([]*communities. return api.service.messenger.JoinedCommunities() } +// CuratedCommunities returns the list of curated communities stored in the smart contract. If a community is +// already known by the node, its description will be returned and and will asynchronously retrieve the +// description for the communities it does not know +func (api *PublicAPI) CuratedCommunities(parent context.Context) (*communities.KnownCommunitiesResponse, error) { + return api.service.messenger.CuratedCommunities() +} + // JoinCommunity joins a community with the given ID func (api *PublicAPI) JoinCommunity(parent context.Context, communityID types.HexBytes) (*protocol.MessengerResponse, error) { return api.service.messenger.JoinCommunity(parent, communityID) diff --git a/services/ext/service.go b/services/ext/service.go index 268da0b4e..c3d82c3e6 100644 --- a/services/ext/service.go +++ b/services/ext/service.go @@ -23,7 +23,8 @@ import ( "github.com/ethereum/go-ethereum/node" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/enode" - "github.com/ethereum/go-ethereum/rpc" + gethrpc "github.com/ethereum/go-ethereum/rpc" + "github.com/status-im/status-go/rpc" "github.com/status-im/status-go/connection" "github.com/status-im/status-go/db" @@ -61,6 +62,7 @@ type Service struct { cancelMessenger chan struct{} storage db.TransactionalStorage n types.Node + rpcClient *rpc.Client config params.NodeConfig mailMonitor *MailRequestMonitor server *p2p.Server @@ -76,6 +78,7 @@ var _ node.Lifecycle = (*Service)(nil) func New( config params.NodeConfig, n types.Node, + rpcClient *rpc.Client, ldb *leveldb.DB, mailMonitor *MailRequestMonitor, eventSub mailservers.EnvelopeEventSubscriber, @@ -85,6 +88,7 @@ func New( return &Service{ storage: db.NewLevelDBStorage(ldb), n: n, + rpcClient: rpcClient, config: config, mailMonitor: mailMonitor, peerStore: peerStore, @@ -144,7 +148,7 @@ func (s *Service) InitProtocol(nodeName string, identity *ecdsa.PrivateKey, db * s.multiAccountsDB = multiAccountDb s.account = acc - options, err := buildMessengerOptions(s.config, identity, db, httpServer, s.multiAccountsDB, acc, envelopesMonitorConfig, s.accountsDB, logger, &MessengerSignalsHandler{}) + options, err := buildMessengerOptions(s.config, identity, db, httpServer, s.rpcClient, s.multiAccountsDB, acc, envelopesMonitorConfig, s.accountsDB, logger, &MessengerSignalsHandler{}) if err != nil { return err } @@ -348,7 +352,7 @@ func (s *Service) Protocols() []p2p.Protocol { } // APIs returns a list of new APIs. -func (s *Service) APIs() []rpc.API { +func (s *Service) APIs() []gethrpc.API { panic("this is abstract service, use shhext or wakuext implementation") } @@ -391,6 +395,7 @@ func buildMessengerOptions( identity *ecdsa.PrivateKey, db *sql.DB, httpServer *server.Server, + rpcClient *rpc.Client, multiAccounts *multiaccounts.Database, account *multiaccounts.Account, envelopesMonitorConfig *transport.EnvelopesMonitorConfig, @@ -412,6 +417,7 @@ func buildMessengerOptions( protocol.WithClusterConfig(config.ClusterConfig), protocol.WithTorrentConfig(&config.TorrentConfig), protocol.WithHTTPServer(httpServer), + protocol.WithRPCClient(rpcClient), } if config.ShhextConfig.DataSyncEnabled { diff --git a/services/wakuext/api_test.go b/services/wakuext/api_test.go index cf467902b..b514fc6ba 100644 --- a/services/wakuext/api_test.go +++ b/services/wakuext/api_test.go @@ -61,7 +61,7 @@ func TestRequestMessagesErrors(t *testing.T) { }, } nodeWrapper := ext.NewTestNodeWrapper(nil, waku) - service := New(config, nodeWrapper, handler, nil) + service := New(config, nodeWrapper, nil, handler, nil) api := NewPublicAPI(service) const mailServerPeer = "enode://b7e65e1bedc2499ee6cbd806945af5e7df0e59e4070c96821570bd581473eade24a489f5ec95d060c0db118c879403ab88d827d3766978f28708989d35474f87@[::]:51920" @@ -121,7 +121,7 @@ func TestInitProtocol(t *testing.T) { require.NoError(t, err) nodeWrapper := ext.NewTestNodeWrapper(nil, waku) - service := New(config, nodeWrapper, nil, db) + service := New(config, nodeWrapper, nil, nil, db) tmpdir, err := ioutil.TempDir("", "test-shhext-service-init-protocol") require.NoError(t, err) @@ -187,7 +187,7 @@ func (s *ShhExtSuite) createAndAddNode() { db, err := leveldb.Open(storage.NewMemStorage(), nil) s.Require().NoError(err) nodeWrapper := ext.NewTestNodeWrapper(nil, gethbridge.NewGethWakuWrapper(w)) - service := New(config, nodeWrapper, nil, db) + service := New(config, nodeWrapper, nil, nil, db) sqlDB, err := appdatabase.InitializeDB(fmt.Sprintf("%s/%d", s.dir, idx), "password") s.Require().NoError(err) diff --git a/services/wakuext/service.go b/services/wakuext/service.go index c3d86510d..cd1ff9316 100644 --- a/services/wakuext/service.go +++ b/services/wakuext/service.go @@ -3,7 +3,8 @@ package wakuext import ( "github.com/syndtr/goleveldb/leveldb" - "github.com/ethereum/go-ethereum/rpc" + gethrpc "github.com/ethereum/go-ethereum/rpc" + "github.com/status-im/status-go/rpc" "github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/params" @@ -15,7 +16,7 @@ type Service struct { w types.Waku } -func New(config params.NodeConfig, n types.Node, handler ext.EnvelopeEventsHandler, ldb *leveldb.DB) *Service { +func New(config params.NodeConfig, n types.Node, rpcClient *rpc.Client, handler ext.EnvelopeEventsHandler, ldb *leveldb.DB) *Service { w, err := n.GetWaku(nil) if err != nil { panic(err) @@ -27,7 +28,7 @@ func New(config params.NodeConfig, n types.Node, handler ext.EnvelopeEventsHandl requestsRegistry := ext.NewRequestsRegistry(delay) mailMonitor := ext.NewMailRequestMonitor(w, handler, requestsRegistry) return &Service{ - Service: ext.New(config, n, ldb, mailMonitor, w), + Service: ext.New(config, n, rpcClient, ldb, mailMonitor, w), w: w, } } @@ -37,8 +38,8 @@ func (s *Service) PublicWakuAPI() types.PublicWakuAPI { } // APIs returns a list of new APIs. -func (s *Service) APIs() []rpc.API { - apis := []rpc.API{ +func (s *Service) APIs() []gethrpc.API { + apis := []gethrpc.API{ { Namespace: "wakuext", Version: "1.0", diff --git a/services/wakuv2ext/service.go b/services/wakuv2ext/service.go index 45a4311d8..b57be687f 100644 --- a/services/wakuv2ext/service.go +++ b/services/wakuv2ext/service.go @@ -3,10 +3,10 @@ package wakuv2ext import ( "github.com/syndtr/goleveldb/leveldb" - "github.com/ethereum/go-ethereum/rpc" - + gethrpc "github.com/ethereum/go-ethereum/rpc" "github.com/status-im/status-go/eth-node/types" "github.com/status-im/status-go/params" + "github.com/status-im/status-go/rpc" "github.com/status-im/status-go/services/ext" ) @@ -15,7 +15,7 @@ type Service struct { w types.Waku } -func New(config params.NodeConfig, n types.Node, handler ext.EnvelopeEventsHandler, ldb *leveldb.DB) *Service { +func New(config params.NodeConfig, n types.Node, rpcClient *rpc.Client, handler ext.EnvelopeEventsHandler, ldb *leveldb.DB) *Service { w, err := n.GetWakuV2(nil) if err != nil { panic(err) @@ -27,7 +27,7 @@ func New(config params.NodeConfig, n types.Node, handler ext.EnvelopeEventsHandl requestsRegistry := ext.NewRequestsRegistry(delay) mailMonitor := ext.NewMailRequestMonitor(w, handler, requestsRegistry) return &Service{ - Service: ext.New(config, n, ldb, mailMonitor, w), + Service: ext.New(config, n, rpcClient, ldb, mailMonitor, w), w: w, } } @@ -37,8 +37,8 @@ func (s *Service) PublicWakuAPI() types.PublicWakuAPI { } // APIs returns a list of new APIs. -func (s *Service) APIs() []rpc.API { - apis := []rpc.API{ +func (s *Service) APIs() []gethrpc.API { + apis := []gethrpc.API{ { Namespace: "wakuext", Version: "1.0",