Use WakuV2 in MessengerCommunitiesTokenPermissionsSuite

This commit is contained in:
Vitaliy Vlasov 2023-10-08 09:02:53 +03:00
parent 6798d1ac5c
commit 1fedf5503a
2 changed files with 120 additions and 18 deletions

View File

@ -5,6 +5,7 @@ import (
"crypto/ecdsa"
"encoding/json"
"errors"
"os"
"io/ioutil"
"sync"
@ -20,6 +21,7 @@ import (
"github.com/status-im/status-go/account"
"github.com/status-im/status-go/account/generator"
"github.com/status-im/status-go/appdatabase"
gethbridge "github.com/status-im/status-go/eth-node/bridge/geth"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/multiaccounts"
@ -34,6 +36,7 @@ import (
"github.com/status-im/status-go/services/communitytokens"
walletToken "github.com/status-im/status-go/services/wallet/token"
"github.com/status-im/status-go/t/helpers"
waku "github.com/status-im/status-go/wakuv2"
"github.com/status-im/status-go/walletdatabase"
)
@ -122,6 +125,81 @@ func (c *CollectiblesServiceMock) DeploymentSignatureDigest(chainID uint64, addr
return gethcommon.Hex2Bytes("ccbb375343347491706cf4b43796f7b96ccc89c9e191a8b78679daeba1684ec7"), nil
}
func newWakuV2(s *suite.Suite, logger *zap.Logger, useLocalWaku bool) *waku.Waku {
config := &waku.Config{}
var onPeerStats func(connStatus types.ConnStatus)
var connStatusChan chan struct{}
if !useLocalWaku {
enrTreeAddress := testENRBootstrap
envEnrTreeAddress := os.Getenv("ENRTREE_ADDRESS")
if envEnrTreeAddress != "" {
enrTreeAddress = envEnrTreeAddress
}
config.EnableDiscV5 = true
config.DiscV5BootstrapNodes = []string{enrTreeAddress}
config.DiscoveryLimit = 20
config.WakuNodes = []string{enrTreeAddress}
connStatusChan = make(chan struct{})
terminator := sync.Once{}
onPeerStats = func(connStatus types.ConnStatus) {
if connStatus.IsOnline {
terminator.Do(func() {
connStatusChan <- struct{}{}
})
}
}
}
waku, err := waku.New("", "", config, logger, nil, nil, nil, onPeerStats)
s.Require().NoError(err)
s.Require().NoError(waku.Start())
if !useLocalWaku {
select {
case <-time.After(30 * time.Second):
s.Require().Fail("timeout elapsed")
case <-connStatusChan:
// proceed, peers found
close(connStatusChan)
}
}
return waku
}
func createWakuNetwork(s *suite.Suite, parentLogger *zap.Logger, nodeNames []string) []types.Waku {
nodes := make([]*waku.Waku, len(nodeNames))
for i, name := range nodeNames {
logger := parentLogger.With(zap.String("name", name+"-waku"))
node := newWakuV2(s, logger, true)
nodes[i] = node
}
// Setup local network graph
for i := 0; i < len(nodes); i++ {
for j := 0; j < len(nodes); j++ {
if i == j {
continue
}
addrs := nodes[j].ListenAddresses()
s.Require().Greater(len(addrs), 0)
_, err := nodes[i].AddRelayPeer(addrs[0])
s.Require().NoError(err)
err = nodes[i].DialPeer(addrs[0])
s.Require().NoError(err)
}
}
wrappers := make([]types.Waku, len(nodes))
for i, n := range nodes {
wrappers[i] = gethbridge.NewGethWakuV2Wrapper(n)
}
return wrappers
}
func newMessenger(s *suite.Suite, shh types.Waku, logger *zap.Logger, password string, walletAddresses []string,
mockedBalances *map[uint64]map[gethcommon.Address]map[gethcommon.Address]*hexutil.Big, collectiblesService communitytokens.ServiceInterface) *Messenger {
accountsManagerMock := &AccountManagerMock{}

View File

@ -22,11 +22,11 @@ import (
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/tt"
"github.com/status-im/status-go/waku"
)
const testChainID1 = 1
const testENRBootstrap = "enrtree://AL65EKLJAUXKKPG43HVTML5EFFWEZ7L4LOKTLZCLJASG4DSESQZEC@prod.status.nodes.status.im"
const ownerPassword = "123456"
const alicePassword = "qwerty"
const bobPassword = "bob123"
@ -108,7 +108,7 @@ func (tckd *TestCommunitiesKeyDistributor) waitOnKeyDistribution(condition func(
}
func TestMessengerCommunitiesTokenPermissionsSuite(t *testing.T) {
//suite.Run(t, new(MessengerCommunitiesTokenPermissionsSuite))
suite.Run(t, new(MessengerCommunitiesTokenPermissionsSuite))
}
type MessengerCommunitiesTokenPermissionsSuite struct {
@ -116,9 +116,11 @@ type MessengerCommunitiesTokenPermissionsSuite struct {
owner *Messenger
bob *Messenger
alice *Messenger
// If one wants to send messages between different instances of Messenger,
// a single Waku service should be shared.
shh types.Waku
ownerWaku types.Waku
bobWaku types.Waku
aliceWaku types.Waku
logger *zap.Logger
mockedBalances map[uint64]map[gethcommon.Address]map[gethcommon.Address]*hexutil.Big // chainID, account, token, balance
@ -128,15 +130,20 @@ type MessengerCommunitiesTokenPermissionsSuite struct {
func (s *MessengerCommunitiesTokenPermissionsSuite) SetupTest() {
s.logger = tt.MustCreateTestLogger()
config := waku.DefaultConfig
config.MinimumAcceptedPoW = 0
shh := waku.New(&config, s.logger)
s.shh = gethbridge.NewGethWakuWrapper(shh)
s.Require().NoError(shh.Start())
wakuNodes := createWakuNetwork(&s.Suite, s.logger, []string{"owner", "bob", "alice"})
ownerLogger := s.logger.With(zap.String("name", "owner"))
s.ownerWaku = wakuNodes[0]
s.owner = s.newMessenger(ownerPassword, []string{ownerAddress}, s.ownerWaku, ownerLogger)
bobLogger := s.logger.With(zap.String("name", "bob"))
s.bobWaku = wakuNodes[1]
s.bob = s.newMessenger(bobPassword, []string{bobAddress}, s.bobWaku, bobLogger)
aliceLogger := s.logger.With(zap.String("name", "alice"))
s.aliceWaku = wakuNodes[2]
s.alice = s.newMessenger(alicePassword, []string{aliceAddress1, aliceAddress2}, s.aliceWaku, aliceLogger)
s.owner = s.newMessenger(ownerPassword, []string{ownerAddress})
s.bob = s.newMessenger(bobPassword, []string{bobAddress})
s.alice = s.newMessenger(alicePassword, []string{aliceAddress1, aliceAddress2})
_, err := s.owner.Start()
s.Require().NoError(err)
_, err = s.bob.Start()
@ -149,17 +156,34 @@ func (s *MessengerCommunitiesTokenPermissionsSuite) SetupTest() {
s.mockedBalances[testChainID1][gethcommon.HexToAddress(aliceAddress1)] = make(map[gethcommon.Address]*hexutil.Big)
s.mockedBalances[testChainID1][gethcommon.HexToAddress(aliceAddress2)] = make(map[gethcommon.Address]*hexutil.Big)
s.mockedBalances[testChainID1][gethcommon.HexToAddress(bobAddress)] = make(map[gethcommon.Address]*hexutil.Big)
}
func (s *MessengerCommunitiesTokenPermissionsSuite) TearDownTest() {
s.Require().NoError(s.owner.Shutdown())
s.Require().NoError(s.bob.Shutdown())
s.Require().NoError(s.alice.Shutdown())
if s.owner != nil {
s.Require().NoError(s.owner.Shutdown())
}
if s.ownerWaku != nil {
s.Require().NoError(gethbridge.GetGethWakuV2From(s.ownerWaku).Stop())
}
if s.bob != nil {
s.Require().NoError(s.bob.Shutdown())
}
if s.bobWaku != nil {
s.Require().NoError(gethbridge.GetGethWakuV2From(s.bobWaku).Stop())
}
if s.alice != nil {
s.Require().NoError(s.alice.Shutdown())
}
if s.aliceWaku != nil {
s.Require().NoError(gethbridge.GetGethWakuV2From(s.aliceWaku).Stop())
}
_ = s.logger.Sync()
}
func (s *MessengerCommunitiesTokenPermissionsSuite) newMessenger(password string, walletAddresses []string) *Messenger {
return newMessenger(&s.Suite, s.shh, s.logger, password, walletAddresses, &s.mockedBalances, s.collectiblesServiceMock)
func (s *MessengerCommunitiesTokenPermissionsSuite) newMessenger(password string, walletAddresses []string, waku types.Waku, logger *zap.Logger) *Messenger {
return newMessenger(&s.Suite, waku, logger, password, walletAddresses, &s.mockedBalances, s.collectiblesServiceMock)
}
func (s *MessengerCommunitiesTokenPermissionsSuite) joinCommunity(community *communities.Community, user *Messenger, password string, addresses []string) {