2021-08-06 15:40:23 +00:00
|
|
|
package communities
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"database/sql"
|
2023-06-21 11:20:43 +00:00
|
|
|
"math/big"
|
2021-08-06 15:40:23 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2022-03-08 15:25:00 +00:00
|
|
|
"github.com/status-im/status-go/appdatabase"
|
2021-08-06 15:40:23 +00:00
|
|
|
"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"
|
2023-07-07 13:03:37 +00:00
|
|
|
"github.com/status-im/status-go/protocol/communities/token"
|
2021-08-06 15:40:23 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
"github.com/status-im/status-go/protocol/sqlite"
|
2023-06-21 11:20:43 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/bigint"
|
2023-08-11 11:28:45 +00:00
|
|
|
"github.com/status-im/status-go/t/helpers"
|
2021-08-06 15:40:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPersistenceSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(PersistenceSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type PersistenceSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
db *Persistence
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PersistenceSuite) SetupTest() {
|
|
|
|
s.db = nil
|
|
|
|
|
2023-08-11 11:28:45 +00:00
|
|
|
db, err := helpers.SetupTestMemorySQLDB(appdatabase.DbInitializer{})
|
2021-08-06 15:40:23 +00:00
|
|
|
s.NoError(err, "creating sqlite db instance")
|
|
|
|
|
2022-03-08 15:25:00 +00:00
|
|
|
err = sqlite.Migrate(db)
|
|
|
|
s.NoError(err, "protocol migrate")
|
|
|
|
|
2021-08-06 15:40:23 +00:00
|
|
|
s.db = &Persistence{db: db}
|
|
|
|
}
|
|
|
|
|
2022-09-20 19:57:39 +00:00
|
|
|
func (s *PersistenceSuite) TestSaveCommunity() {
|
|
|
|
id, err := crypto.GenerateKey()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// there is one community inserted by default
|
|
|
|
communities, err := s.db.AllCommunities(&id.PublicKey)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(communities, 1)
|
|
|
|
|
|
|
|
community := Community{
|
|
|
|
config: &Config{
|
|
|
|
PrivateKey: id,
|
|
|
|
ID: &id.PublicKey,
|
|
|
|
Joined: true,
|
|
|
|
Spectated: true,
|
|
|
|
Verified: true,
|
2023-07-19 12:14:42 +00:00
|
|
|
Muted: true,
|
|
|
|
MuteTill: time.Time{},
|
2022-09-20 19:57:39 +00:00
|
|
|
CommunityDescription: &protobuf.CommunityDescription{},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
s.Require().NoError(s.db.SaveCommunity(&community))
|
|
|
|
|
|
|
|
communities, err = s.db.AllCommunities(&id.PublicKey)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(communities, 2)
|
|
|
|
s.Equal(types.HexBytes(crypto.CompressPubkey(&id.PublicKey)), communities[1].ID())
|
|
|
|
s.Equal(true, communities[1].Joined())
|
|
|
|
s.Equal(true, communities[1].Spectated())
|
|
|
|
s.Equal(true, communities[1].Verified())
|
2023-07-19 12:14:42 +00:00
|
|
|
s.Equal(true, communities[1].Muted())
|
|
|
|
s.Equal(time.Time{}, communities[1].MuteTill())
|
2022-09-20 19:57:39 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 15:40:23 +00:00
|
|
|
func (s *PersistenceSuite) TestShouldHandleSyncCommunity() {
|
|
|
|
sc := &protobuf.SyncCommunity{
|
|
|
|
Id: []byte("0x123456"),
|
|
|
|
Description: []byte("this is a description"),
|
|
|
|
Joined: true,
|
|
|
|
Verified: true,
|
|
|
|
Clock: uint64(time.Now().Unix()),
|
|
|
|
}
|
|
|
|
|
|
|
|
// check an empty db to see if a community should be synced
|
|
|
|
should, err := s.db.ShouldHandleSyncCommunity(sc)
|
|
|
|
s.NoError(err, "SaveSyncCommunity")
|
|
|
|
s.True(should)
|
|
|
|
|
|
|
|
// add a new community to the db
|
|
|
|
err = s.db.saveRawCommunityRow(fromSyncCommunityProtobuf(sc))
|
|
|
|
s.NoError(err, "saveRawCommunityRow")
|
|
|
|
|
|
|
|
rcrs, err := s.db.getAllCommunitiesRaw()
|
|
|
|
s.NoError(err, "should have no error from getAllCommunitiesRaw")
|
|
|
|
s.Len(rcrs, 2, "length of all communities raw should be 2")
|
|
|
|
|
|
|
|
// check again to see is the community should be synced
|
|
|
|
sc.Clock--
|
|
|
|
should, err = s.db.ShouldHandleSyncCommunity(sc)
|
|
|
|
s.NoError(err, "SaveSyncCommunity")
|
|
|
|
s.False(should)
|
|
|
|
|
|
|
|
// check again to see is the community should be synced
|
|
|
|
sc.Clock++
|
|
|
|
sc.Clock++
|
|
|
|
should, err = s.db.ShouldHandleSyncCommunity(sc)
|
|
|
|
s.NoError(err, "SaveSyncCommunity")
|
|
|
|
s.True(should)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PersistenceSuite) TestSetSyncClock() {
|
|
|
|
sc := &protobuf.SyncCommunity{
|
|
|
|
Id: []byte("0x123456"),
|
|
|
|
Description: []byte("this is a description"),
|
|
|
|
Joined: true,
|
|
|
|
Verified: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a new community to the db
|
|
|
|
err := s.db.saveRawCommunityRow(fromSyncCommunityProtobuf(sc))
|
|
|
|
s.NoError(err, "saveRawCommunityRow")
|
|
|
|
|
|
|
|
// retrieve row from db synced_at must be zero
|
|
|
|
rcr, err := s.db.getRawCommunityRow(sc.Id)
|
|
|
|
s.NoError(err, "getRawCommunityRow")
|
|
|
|
s.Zero(rcr.SyncedAt, "synced_at must be zero value")
|
|
|
|
|
|
|
|
// Set the synced_at value
|
|
|
|
clock := uint64(time.Now().Unix())
|
|
|
|
err = s.db.SetSyncClock(sc.Id, clock)
|
|
|
|
s.NoError(err, "SetSyncClock")
|
|
|
|
|
|
|
|
// Retrieve row from db and check clock matches synced_at value
|
|
|
|
rcr, err = s.db.getRawCommunityRow(sc.Id)
|
|
|
|
s.NoError(err, "getRawCommunityRow")
|
|
|
|
s.Equal(clock, rcr.SyncedAt, "synced_at must equal the value of the clock")
|
|
|
|
|
|
|
|
// Set Synced At with an older clock value
|
|
|
|
olderClock := clock - uint64(256)
|
|
|
|
err = s.db.SetSyncClock(sc.Id, olderClock)
|
|
|
|
s.NoError(err, "SetSyncClock")
|
|
|
|
|
|
|
|
// Retrieve row from db and check olderClock matches synced_at value
|
|
|
|
rcr, err = s.db.getRawCommunityRow(sc.Id)
|
|
|
|
s.NoError(err, "getRawCommunityRow")
|
|
|
|
s.NotEqual(olderClock, rcr.SyncedAt, "synced_at must not equal the value of the olderClock value")
|
|
|
|
|
|
|
|
// Set Synced At with a newer clock value
|
|
|
|
newerClock := clock + uint64(512)
|
|
|
|
err = s.db.SetSyncClock(sc.Id, newerClock)
|
|
|
|
s.NoError(err, "SetSyncClock")
|
|
|
|
|
|
|
|
// Retrieve row from db and check olderClock matches synced_at value
|
|
|
|
rcr, err = s.db.getRawCommunityRow(sc.Id)
|
|
|
|
s.NoError(err, "getRawCommunityRow")
|
|
|
|
s.Equal(newerClock, rcr.SyncedAt, "synced_at must equal the value of the newerClock value")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PersistenceSuite) TestSetPrivateKey() {
|
|
|
|
sc := &protobuf.SyncCommunity{
|
|
|
|
Id: []byte("0x123456"),
|
|
|
|
Description: []byte("this is a description"),
|
|
|
|
Joined: true,
|
|
|
|
Verified: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a new community to the db with no private key
|
|
|
|
err := s.db.saveRawCommunityRow(fromSyncCommunityProtobuf(sc))
|
|
|
|
s.NoError(err, "saveRawCommunityRow")
|
|
|
|
|
|
|
|
// retrieve row from db, private key must be zero
|
|
|
|
rcr, err := s.db.getRawCommunityRow(sc.Id)
|
|
|
|
s.NoError(err, "getRawCommunityRow")
|
|
|
|
s.Zero(rcr.PrivateKey, "private key must be zero value")
|
|
|
|
|
|
|
|
// Set private key
|
|
|
|
pk, err := crypto.GenerateKey()
|
|
|
|
s.NoError(err, "crypto.GenerateKey")
|
|
|
|
err = s.db.SetPrivateKey(sc.Id, pk)
|
|
|
|
s.NoError(err, "SetPrivateKey")
|
|
|
|
|
|
|
|
// retrieve row from db again, private key must match the given key
|
|
|
|
rcr, err = s.db.getRawCommunityRow(sc.Id)
|
|
|
|
s.NoError(err, "getRawCommunityRow")
|
|
|
|
s.Equal(crypto.FromECDSA(pk), rcr.PrivateKey, "private key must match given key")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PersistenceSuite) TestJoinedAndPendingCommunitiesWithRequests() {
|
|
|
|
identity, err := crypto.GenerateKey()
|
|
|
|
s.NoError(err, "crypto.GenerateKey shouldn't give any error")
|
|
|
|
|
|
|
|
clock := uint64(time.Now().Unix())
|
|
|
|
|
|
|
|
// Add a new community that we have joined
|
|
|
|
com := s.makeNewCommunity(identity)
|
|
|
|
com.Join()
|
2022-06-01 07:55:48 +00:00
|
|
|
sc, err := com.ToSyncCommunityProtobuf(clock, nil)
|
2021-08-06 15:40:23 +00:00
|
|
|
s.NoError(err, "Community.ToSyncCommunityProtobuf shouldn't give any error")
|
|
|
|
err = s.db.saveRawCommunityRow(fromSyncCommunityProtobuf(sc))
|
|
|
|
s.NoError(err, "saveRawCommunityRow")
|
|
|
|
|
|
|
|
// Add a new community that we have requested to join, but not yet joined
|
|
|
|
com2 := s.makeNewCommunity(identity)
|
|
|
|
err = s.db.SaveCommunity(com2)
|
|
|
|
s.NoError(err, "SaveCommunity shouldn't give any error")
|
|
|
|
|
|
|
|
rtj := &RequestToJoin{
|
|
|
|
ID: types.HexBytes{1, 2, 3, 4, 5, 6, 7, 8},
|
|
|
|
PublicKey: common.PubkeyToHex(&identity.PublicKey),
|
|
|
|
Clock: clock,
|
|
|
|
CommunityID: com2.ID(),
|
|
|
|
State: RequestToJoinStatePending,
|
|
|
|
}
|
|
|
|
err = s.db.SaveRequestToJoin(rtj)
|
|
|
|
s.NoError(err, "SaveRequestToJoin shouldn't give any error")
|
|
|
|
|
|
|
|
comms, err := s.db.JoinedAndPendingCommunitiesWithRequests(&identity.PublicKey)
|
|
|
|
s.NoError(err, "JoinedAndPendingCommunitiesWithRequests shouldn't give any error")
|
|
|
|
s.Len(comms, 2, "Should have 2 communities")
|
|
|
|
|
|
|
|
for _, comm := range comms {
|
|
|
|
switch comm.IDString() {
|
|
|
|
case com.IDString():
|
|
|
|
s.Len(comm.RequestsToJoin(), 0, "Should have no RequestsToJoin")
|
|
|
|
case com2.IDString():
|
|
|
|
rtjs := comm.RequestsToJoin()
|
|
|
|
s.Len(rtjs, 1, "Should have one RequestsToJoin")
|
|
|
|
s.Equal(rtjs[0], rtj, "RequestToJoin should match the Request stored in the db")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-22 10:10:31 +00:00
|
|
|
func (s *PersistenceSuite) TestSaveRequestToLeave() {
|
|
|
|
rtl := &RequestToLeave{
|
|
|
|
ID: []byte("0x123456"),
|
|
|
|
PublicKey: "0xffffff",
|
|
|
|
Clock: 2,
|
|
|
|
CommunityID: []byte("0x654321"),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := s.db.SaveRequestToLeave(rtl)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// older clocks should not be saved
|
|
|
|
rtl.Clock = 1
|
|
|
|
err = s.db.SaveRequestToLeave(rtl)
|
|
|
|
s.Error(err)
|
|
|
|
}
|
|
|
|
|
2021-08-06 15:40:23 +00:00
|
|
|
func (s *PersistenceSuite) makeNewCommunity(identity *ecdsa.PrivateKey) *Community {
|
|
|
|
comPrivKey, err := crypto.GenerateKey()
|
|
|
|
s.NoError(err, "crypto.GenerateKey shouldn't give any error")
|
|
|
|
|
|
|
|
com, err := New(Config{
|
|
|
|
MemberIdentity: &identity.PublicKey,
|
|
|
|
PrivateKey: comPrivKey,
|
|
|
|
ID: &comPrivKey.PublicKey,
|
|
|
|
})
|
|
|
|
s.NoError(err, "New shouldn't give any error")
|
|
|
|
|
|
|
|
md, err := com.MarshaledDescription()
|
|
|
|
s.NoError(err, "Community.MarshaledDescription shouldn't give any error")
|
2023-07-10 15:35:15 +00:00
|
|
|
com.config.CommunityDescriptionProtocolMessage = md
|
2021-08-06 15:40:23 +00:00
|
|
|
|
|
|
|
return com
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PersistenceSuite) TestGetSyncedRawCommunity() {
|
|
|
|
sc := &protobuf.SyncCommunity{
|
|
|
|
Id: []byte("0x123456"),
|
|
|
|
Description: []byte("this is a description"),
|
|
|
|
Joined: true,
|
|
|
|
Verified: true,
|
2022-09-20 19:57:39 +00:00
|
|
|
Spectated: true,
|
2021-08-06 15:40:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add a new community to the db
|
|
|
|
err := s.db.saveRawCommunityRowWithoutSyncedAt(fromSyncCommunityProtobuf(sc))
|
|
|
|
s.NoError(err, "saveRawCommunityRow")
|
|
|
|
|
|
|
|
// retrieve row from db synced_at must be zero
|
|
|
|
rcr, err := s.db.getRawCommunityRow(sc.Id)
|
|
|
|
s.NoError(err, "getRawCommunityRow")
|
|
|
|
s.Zero(rcr.SyncedAt, "synced_at must be zero value")
|
|
|
|
|
|
|
|
// retrieve synced row from db, should fail
|
|
|
|
src, err := s.db.getSyncedRawCommunity(sc.Id)
|
|
|
|
s.EqualError(err, sql.ErrNoRows.Error())
|
|
|
|
s.Nil(src)
|
|
|
|
|
|
|
|
// Set the synced_at value
|
|
|
|
clock := uint64(time.Now().Unix())
|
|
|
|
err = s.db.SetSyncClock(sc.Id, clock)
|
|
|
|
s.NoError(err, "SetSyncClock")
|
|
|
|
|
|
|
|
// retrieve row from db synced_at must not be zero
|
|
|
|
rcr, err = s.db.getRawCommunityRow(sc.Id)
|
|
|
|
s.NoError(err, "getRawCommunityRow")
|
|
|
|
s.NotZero(rcr.SyncedAt, "synced_at must be zero value")
|
|
|
|
|
|
|
|
// retrieve synced row from db, should succeed
|
|
|
|
src, err = s.db.getSyncedRawCommunity(sc.Id)
|
|
|
|
s.NoError(err)
|
|
|
|
s.NotNil(src)
|
|
|
|
s.Equal(clock, src.SyncedAt)
|
|
|
|
}
|
2022-03-08 15:25:00 +00:00
|
|
|
|
|
|
|
func (s *PersistenceSuite) TestGetCommunitiesSettings() {
|
|
|
|
settings := []CommunitySettings{
|
|
|
|
{CommunityID: "0x01", HistoryArchiveSupportEnabled: false},
|
|
|
|
{CommunityID: "0x02", HistoryArchiveSupportEnabled: true},
|
|
|
|
{CommunityID: "0x03", HistoryArchiveSupportEnabled: false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range settings {
|
|
|
|
stg := settings[i]
|
|
|
|
err := s.db.SaveCommunitySettings(stg)
|
|
|
|
s.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rst, err := s.db.GetCommunitiesSettings()
|
|
|
|
s.NoError(err)
|
|
|
|
s.Equal(settings, rst)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PersistenceSuite) TestSaveCommunitySettings() {
|
|
|
|
settings := CommunitySettings{CommunityID: "0x01", HistoryArchiveSupportEnabled: false}
|
|
|
|
err := s.db.SaveCommunitySettings(settings)
|
|
|
|
s.NoError(err)
|
|
|
|
rst, err := s.db.GetCommunitiesSettings()
|
|
|
|
s.NoError(err)
|
|
|
|
s.Equal(1, len(rst))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PersistenceSuite) TestDeleteCommunitySettings() {
|
|
|
|
settings := CommunitySettings{CommunityID: "0x01", HistoryArchiveSupportEnabled: false}
|
|
|
|
|
|
|
|
err := s.db.SaveCommunitySettings(settings)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
rst, err := s.db.GetCommunitiesSettings()
|
|
|
|
s.NoError(err)
|
|
|
|
s.Equal(1, len(rst))
|
|
|
|
s.NoError(s.db.DeleteCommunitySettings(types.HexBytes{0x01}))
|
|
|
|
rst2, err := s.db.GetCommunitiesSettings()
|
|
|
|
s.NoError(err)
|
|
|
|
s.Equal(0, len(rst2))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *PersistenceSuite) TestUpdateCommunitySettings() {
|
|
|
|
settings := []CommunitySettings{
|
|
|
|
{CommunityID: "0x01", HistoryArchiveSupportEnabled: true},
|
|
|
|
{CommunityID: "0x02", HistoryArchiveSupportEnabled: false},
|
|
|
|
}
|
|
|
|
|
|
|
|
s.NoError(s.db.SaveCommunitySettings(settings[0]))
|
|
|
|
s.NoError(s.db.SaveCommunitySettings(settings[1]))
|
|
|
|
|
|
|
|
settings[0].HistoryArchiveSupportEnabled = true
|
|
|
|
settings[1].HistoryArchiveSupportEnabled = false
|
|
|
|
|
|
|
|
s.NoError(s.db.UpdateCommunitySettings(settings[0]))
|
|
|
|
s.NoError(s.db.UpdateCommunitySettings(settings[1]))
|
|
|
|
|
|
|
|
rst, err := s.db.GetCommunitiesSettings()
|
|
|
|
s.NoError(err)
|
|
|
|
s.Equal(settings, rst)
|
|
|
|
}
|
2023-01-27 13:27:24 +00:00
|
|
|
|
!refactor: introduce `SaveCommunityToken()` and change `AddCommunityToken()`
**This is a breaking change!**
Prior to this commit we had `AddCommunityToken(token *communities,
croppedImage CroppedImage)` that we used to
1. add a `CommunityToken` to the user's database and
2. to create a `CommunityTokenMetadata` from it which is then added to
the community's `CommunityDescription` and published to its members
However, I've then discovered that we need to separate these two things,
such that we can deploy a community token, then add it to the database
only for tracking purposes, **then** add it to the community description
(and propagate to members) once we know that the deploy tx indeed went
through.
To implement this, this commit introduces a new API
`SaveCommunityToken(token *communities.CommunityToken, croppedImage
CroppedImage)` which adds the token to the database only and doesn't
touch the community description.
The `AddCommunityToken` API is then changed that it's exclusively used
for adding an already saved `CommunityToken` to the community
description so it can be published to members. Hence, the signature is
now `AddCommunityToken(communityID string, chainID int, address
string)`, which makes this a breaking change.
Clients that used `AddCommunityToken()` before now need to ensure that
they first call `SaveCommunityToken()` as `AddCommunityToken()` will
fail otherwise.
2023-07-25 11:35:17 +00:00
|
|
|
func (s *PersistenceSuite) TestGetCommunityToken() {
|
|
|
|
tokens, err := s.db.GetCommunityTokens("123")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(tokens, 0)
|
|
|
|
|
2023-07-07 13:03:37 +00:00
|
|
|
tokenERC721 := token.CommunityToken{
|
!refactor: introduce `SaveCommunityToken()` and change `AddCommunityToken()`
**This is a breaking change!**
Prior to this commit we had `AddCommunityToken(token *communities,
croppedImage CroppedImage)` that we used to
1. add a `CommunityToken` to the user's database and
2. to create a `CommunityTokenMetadata` from it which is then added to
the community's `CommunityDescription` and published to its members
However, I've then discovered that we need to separate these two things,
such that we can deploy a community token, then add it to the database
only for tracking purposes, **then** add it to the community description
(and propagate to members) once we know that the deploy tx indeed went
through.
To implement this, this commit introduces a new API
`SaveCommunityToken(token *communities.CommunityToken, croppedImage
CroppedImage)` which adds the token to the database only and doesn't
touch the community description.
The `AddCommunityToken` API is then changed that it's exclusively used
for adding an already saved `CommunityToken` to the community
description so it can be published to members. Hence, the signature is
now `AddCommunityToken(communityID string, chainID int, address
string)`, which makes this a breaking change.
Clients that used `AddCommunityToken()` before now need to ensure that
they first call `SaveCommunityToken()` as `AddCommunityToken()` will
fail otherwise.
2023-07-25 11:35:17 +00:00
|
|
|
CommunityID: "123",
|
|
|
|
TokenType: protobuf.CommunityTokenType_ERC721,
|
|
|
|
Address: "0x123",
|
|
|
|
Name: "StatusToken",
|
|
|
|
Symbol: "STT",
|
|
|
|
Description: "desc",
|
|
|
|
Supply: &bigint.BigInt{Int: big.NewInt(123)},
|
|
|
|
InfiniteSupply: false,
|
|
|
|
Transferable: true,
|
|
|
|
RemoteSelfDestruct: true,
|
|
|
|
ChainID: 1,
|
2023-07-07 13:03:37 +00:00
|
|
|
DeployState: token.InProgress,
|
!refactor: introduce `SaveCommunityToken()` and change `AddCommunityToken()`
**This is a breaking change!**
Prior to this commit we had `AddCommunityToken(token *communities,
croppedImage CroppedImage)` that we used to
1. add a `CommunityToken` to the user's database and
2. to create a `CommunityTokenMetadata` from it which is then added to
the community's `CommunityDescription` and published to its members
However, I've then discovered that we need to separate these two things,
such that we can deploy a community token, then add it to the database
only for tracking purposes, **then** add it to the community description
(and propagate to members) once we know that the deploy tx indeed went
through.
To implement this, this commit introduces a new API
`SaveCommunityToken(token *communities.CommunityToken, croppedImage
CroppedImage)` which adds the token to the database only and doesn't
touch the community description.
The `AddCommunityToken` API is then changed that it's exclusively used
for adding an already saved `CommunityToken` to the community
description so it can be published to members. Hence, the signature is
now `AddCommunityToken(communityID string, chainID int, address
string)`, which makes this a breaking change.
Clients that used `AddCommunityToken()` before now need to ensure that
they first call `SaveCommunityToken()` as `AddCommunityToken()` will
fail otherwise.
2023-07-25 11:35:17 +00:00
|
|
|
Base64Image: "ABCD",
|
|
|
|
}
|
|
|
|
|
|
|
|
err = s.db.AddCommunityToken(&tokenERC721)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
token, err := s.db.GetCommunityToken("123", 1, "0x123")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Equal(&tokenERC721, token)
|
|
|
|
}
|
|
|
|
|
2023-01-27 13:27:24 +00:00
|
|
|
func (s *PersistenceSuite) TestGetCommunityTokens() {
|
2023-03-02 17:33:30 +00:00
|
|
|
tokens, err := s.db.GetCommunityTokens("123")
|
2023-01-27 13:27:24 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(tokens, 0)
|
|
|
|
|
2023-07-07 13:03:37 +00:00
|
|
|
tokenERC721 := token.CommunityToken{
|
2023-01-27 13:27:24 +00:00
|
|
|
CommunityID: "123",
|
2023-02-20 11:57:33 +00:00
|
|
|
TokenType: protobuf.CommunityTokenType_ERC721,
|
2023-01-27 13:27:24 +00:00
|
|
|
Address: "0x123",
|
|
|
|
Name: "StatusToken",
|
|
|
|
Symbol: "STT",
|
|
|
|
Description: "desc",
|
2023-06-21 11:20:43 +00:00
|
|
|
Supply: &bigint.BigInt{Int: big.NewInt(123)},
|
2023-01-27 13:27:24 +00:00
|
|
|
InfiniteSupply: false,
|
|
|
|
Transferable: true,
|
|
|
|
RemoteSelfDestruct: true,
|
|
|
|
ChainID: 1,
|
2023-07-07 13:03:37 +00:00
|
|
|
DeployState: token.InProgress,
|
2023-01-27 13:27:24 +00:00
|
|
|
Base64Image: "ABCD",
|
2023-07-18 08:33:45 +00:00
|
|
|
Deployer: "0xDep1",
|
|
|
|
PrivilegesLevel: token.OwnerLevel,
|
2023-01-27 13:27:24 +00:00
|
|
|
}
|
|
|
|
|
2023-07-07 13:03:37 +00:00
|
|
|
tokenERC20 := token.CommunityToken{
|
2023-01-27 13:27:24 +00:00
|
|
|
CommunityID: "345",
|
2023-06-14 07:47:54 +00:00
|
|
|
TokenType: protobuf.CommunityTokenType_ERC20,
|
2023-01-27 13:27:24 +00:00
|
|
|
Address: "0x345",
|
|
|
|
Name: "StatusToken",
|
|
|
|
Symbol: "STT",
|
|
|
|
Description: "desc",
|
2023-06-21 11:20:43 +00:00
|
|
|
Supply: &bigint.BigInt{Int: big.NewInt(345)},
|
2023-01-27 13:27:24 +00:00
|
|
|
InfiniteSupply: false,
|
|
|
|
Transferable: true,
|
|
|
|
RemoteSelfDestruct: true,
|
|
|
|
ChainID: 2,
|
2023-07-07 13:03:37 +00:00
|
|
|
DeployState: token.Failed,
|
2023-01-27 13:27:24 +00:00
|
|
|
Base64Image: "QWERTY",
|
2023-06-14 07:47:54 +00:00
|
|
|
Decimals: 21,
|
2023-07-18 08:33:45 +00:00
|
|
|
Deployer: "0xDep2",
|
|
|
|
PrivilegesLevel: token.CommunityLevel,
|
2023-01-27 13:27:24 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 07:47:54 +00:00
|
|
|
err = s.db.AddCommunityToken(&tokenERC721)
|
2023-01-27 13:27:24 +00:00
|
|
|
s.Require().NoError(err)
|
2023-06-14 07:47:54 +00:00
|
|
|
err = s.db.AddCommunityToken(&tokenERC20)
|
2023-01-27 13:27:24 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2023-03-02 17:33:30 +00:00
|
|
|
tokens, err = s.db.GetCommunityTokens("123")
|
2023-01-27 13:27:24 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(tokens, 1)
|
2023-06-14 07:47:54 +00:00
|
|
|
s.Require().Equal(tokenERC721, *tokens[0])
|
2023-01-27 13:27:24 +00:00
|
|
|
|
2023-07-07 13:03:37 +00:00
|
|
|
err = s.db.UpdateCommunityTokenState(1, "0x123", token.Deployed)
|
2023-01-27 13:27:24 +00:00
|
|
|
s.Require().NoError(err)
|
2023-03-02 17:33:30 +00:00
|
|
|
tokens, err = s.db.GetCommunityTokens("123")
|
2023-01-27 13:27:24 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(tokens, 1)
|
2023-07-07 13:03:37 +00:00
|
|
|
s.Require().Equal(token.Deployed, tokens[0].DeployState)
|
2023-06-14 07:47:54 +00:00
|
|
|
|
|
|
|
tokens, err = s.db.GetCommunityTokens("345")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(tokens, 1)
|
|
|
|
s.Require().Equal(tokenERC20, *tokens[0])
|
2023-07-18 08:33:45 +00:00
|
|
|
|
|
|
|
err = s.db.UpdateCommunityTokenAddress(1, "0x123", "0x123-newAddr")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
tokens, err = s.db.GetCommunityTokens("123")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(tokens, 1)
|
|
|
|
s.Require().Equal("0x123-newAddr", tokens[0].Address)
|
2023-01-27 13:27:24 +00:00
|
|
|
}
|
2023-06-22 06:54:58 +00:00
|
|
|
|
|
|
|
func (s *PersistenceSuite) TestSaveCheckChannelPermissionResponse() {
|
|
|
|
|
|
|
|
viewAndPostPermissionResults := make(map[string]*PermissionTokenCriteriaResult)
|
|
|
|
viewAndPostPermissionResults["one"] = &PermissionTokenCriteriaResult{
|
|
|
|
Criteria: []bool{true, true, true, true},
|
|
|
|
}
|
|
|
|
viewAndPostPermissionResults["two"] = &PermissionTokenCriteriaResult{
|
|
|
|
Criteria: []bool{false},
|
|
|
|
}
|
|
|
|
chatID := "some-chat-id"
|
|
|
|
communityID := "some-community-id"
|
|
|
|
|
|
|
|
checkChannelPermissionResponse := &CheckChannelPermissionsResponse{
|
|
|
|
ViewOnlyPermissions: &CheckChannelViewOnlyPermissionsResult{
|
|
|
|
Satisfied: true,
|
|
|
|
Permissions: make(map[string]*PermissionTokenCriteriaResult),
|
|
|
|
},
|
|
|
|
ViewAndPostPermissions: &CheckChannelViewAndPostPermissionsResult{
|
|
|
|
Satisfied: true,
|
|
|
|
Permissions: viewAndPostPermissionResults,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err := s.db.SaveCheckChannelPermissionResponse(communityID, chatID, checkChannelPermissionResponse)
|
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
responses, err := s.db.GetCheckChannelPermissionResponses(communityID)
|
|
|
|
s.NoError(err)
|
|
|
|
s.Require().Len(responses, 1)
|
|
|
|
s.Require().NotNil(responses[chatID])
|
|
|
|
s.Require().True(responses[chatID].ViewOnlyPermissions.Satisfied)
|
|
|
|
s.Require().Len(responses[chatID].ViewOnlyPermissions.Permissions, 0)
|
|
|
|
s.Require().True(responses[chatID].ViewAndPostPermissions.Satisfied)
|
|
|
|
s.Require().Len(responses[chatID].ViewAndPostPermissions.Permissions, 2)
|
|
|
|
s.Require().Equal(responses[chatID].ViewAndPostPermissions.Permissions["one"].Criteria, []bool{true, true, true, true})
|
|
|
|
s.Require().Equal(responses[chatID].ViewAndPostPermissions.Permissions["two"].Criteria, []bool{false})
|
|
|
|
}
|