Skip to content

Commit 46cc59e

Browse files
authored
Merge pull request #324 from CovenantSQL/feature/faucet_v3
Faucet api update & cql command improvement
2 parents a612291 + 0b42360 commit 46cc59e

30 files changed

Lines changed: 771 additions & 141 deletions

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,12 @@ status:
3333

3434

3535
builder: status
36+
# alpine image libmusl is not compatible with golang race detector
37+
# also alpine libmusl is required for building static binaries to avoid glibc getaddrinfo panic
3638
docker build \
3739
--tag $(BUILDER):$(VERSION) \
3840
--tag $(BUILDER):latest \
39-
--build-arg BUILD_ARG=use_all_cores \
41+
--build-arg BUILD_ARG=release \
4042
-f docker/builder.Dockerfile \
4143
.
4244

@@ -183,6 +185,11 @@ client: bin/cql bin/cql.test bin/cql-fuse bin/cql-mysql-adapter bin/cql-faucet
183185

184186
all: bp miner client
185187

188+
build-release: bin/cqld bin/cql-minerd bin/cql bin/cql-fuse bin/cql-mysql-adapter bin/cql-faucet
189+
190+
release:
191+
make -j$(JOBS) build-release
192+
186193
clean:
187194
rm -rf bin/cql*
188195
rm -f *.cover.out

blockproducer/chain_io.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package blockproducer
1818

1919
import (
20+
"database/sql"
21+
2022
pi "github.com/CovenantSQL/CovenantSQL/blockproducer/interfaces"
2123
"github.com/CovenantSQL/CovenantSQL/crypto/hash"
2224
"github.com/CovenantSQL/CovenantSQL/proto"
@@ -142,6 +144,60 @@ func (c *Chain) queryTxState(hash hash.Hash) (state pi.TransactionState, err err
142144
return pi.TransactionStateNotFound, nil
143145
}
144146

147+
func (c *Chain) queryAccountSQLChainProfiles(account proto.AccountAddress) (profiles []*types.SQLChainProfile, err error) {
148+
var dbs []proto.DatabaseID
149+
150+
dbs, err = func() (dbs []proto.DatabaseID, err error) {
151+
c.RLock()
152+
defer c.RUnlock()
153+
154+
var (
155+
id string
156+
rows *sql.Rows
157+
querySQL = `SELECT "id" FROM "indexed_shardChains" WHERE "account" = ?`
158+
)
159+
160+
rows, err = c.storage.Reader().Query(querySQL, account.String())
161+
162+
if err != nil {
163+
return
164+
}
165+
166+
defer func() {
167+
_ = rows.Close()
168+
}()
169+
170+
for rows.Next() {
171+
err = rows.Scan(&id)
172+
if err != nil {
173+
return
174+
}
175+
176+
dbs = append(dbs, proto.DatabaseID(id))
177+
}
178+
179+
return
180+
}()
181+
182+
if err != nil {
183+
return
184+
}
185+
186+
var (
187+
profile *types.SQLChainProfile
188+
ok bool
189+
)
190+
191+
for _, db := range dbs {
192+
profile, ok = c.loadSQLChainProfile(db)
193+
if ok {
194+
profiles = append(profiles, profile)
195+
}
196+
}
197+
198+
return
199+
}
200+
145201
func (c *Chain) immutableNextNonce(addr proto.AccountAddress) (n pi.AccountNonce, err error) {
146202
c.RLock()
147203
defer c.RUnlock()

blockproducer/chain_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,19 @@ func TestChain(t *testing.T) {
214214
_, loaded = chain.immutable.loadOrStoreProviderObject(addr1, &types.ProviderProfile{})
215215
So(loaded, ShouldBeFalse)
216216
_, loaded = chain.immutable.loadOrStoreSQLChainObject(dbid1, &types.SQLChainProfile{
217-
Miners: []*types.MinerInfo{&types.MinerInfo{Address: addr2}},
217+
ID: dbid1,
218+
Miners: []*types.MinerInfo{{Address: addr2}},
219+
Users: []*types.SQLChainUser{{Address: addr1, Permission: &types.UserPermission{Role: types.Admin}}},
218220
})
219221
So(loaded, ShouldBeFalse)
220222
_, loaded = chain.immutable.loadOrStoreAccountObject(addr2, &types.Account{
221223
Address: addr2,
222224
TokenBalance: [types.SupportTokenNumber]uint64{100, 100, 100, 100, 100},
223225
})
224226
So(loaded, ShouldBeFalse)
227+
228+
sps := chain.immutable.compileChanges(nil)
229+
_ = store(chain.storage, sps, nil)
225230
chain.immutable.commit()
226231

227232
err = rpcService.QuerySQLChainProfile(
@@ -237,6 +242,12 @@ func TestChain(t *testing.T) {
237242
So(err, ShouldBeNil)
238243
So(queryBalanceResp.OK, ShouldBeTrue)
239244
So(queryBalanceResp.Balance, ShouldEqual, 100)
245+
246+
// query for account sqlchain profiles
247+
var profilesResp = new(types.QueryAccountSQLChainProfilesResp)
248+
_ = rpcService.QueryAccountSQLChainProfiles(&types.QueryAccountSQLChainProfilesReq{Addr: addr1}, profilesResp)
249+
So(profilesResp.Profiles, ShouldNotBeEmpty)
250+
So(profilesResp.Profiles[0].ID, ShouldEqual, dbid1)
240251
})
241252

242253
Convey("Chain APIs should return correct result of tx state", func() {

blockproducer/rpc.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,16 @@ func (s *ChainRPCService) QueryTxState(
127127
resp.State = state
128128
return
129129
}
130+
131+
// QueryAccountSQLChainProfiles is the RPC method to query account sqlchain profiles.
132+
func (s *ChainRPCService) QueryAccountSQLChainProfiles(
133+
req *types.QueryAccountSQLChainProfilesReq, resp *types.QueryAccountSQLChainProfilesResp) (err error,
134+
) {
135+
var profiles []*types.SQLChainProfile
136+
if profiles, err = s.chain.queryAccountSQLChainProfiles(req.Addr); err != nil {
137+
return
138+
}
139+
resp.Addr = req.Addr
140+
resp.Profiles = profiles
141+
return
142+
}

blockproducer/storage.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ var (
7171
UNIQUE ("address", "id")
7272
);`,
7373

74+
`CREATE INDEX IF NOT EXISTS "idx__shardChain__id" ON "shardChain" ("id");`,
75+
7476
`CREATE TABLE IF NOT EXISTS "provider" (
7577
"address" TEXT,
7678
"encoded" BLOB,
@@ -108,6 +110,14 @@ var (
108110
`CREATE INDEX IF NOT EXISTS "idx__indexed_transactions__timestamp" ON "indexed_transactions" ("timestamp" DESC);`,
109111
`CREATE INDEX IF NOT EXISTS "idx__indexed_transactions__tx_type__timestamp" ON "indexed_transactions" ("tx_type", "timestamp" DESC);`,
110112
`CREATE INDEX IF NOT EXISTS "idx__indexed_transactions__address__timestamp" ON "indexed_transactions" ("address", "timestamp" DESC);`,
113+
114+
`CREATE TABLE IF NOT EXISTS "indexed_shardChains" (
115+
"account" TEXT,
116+
"address" TEXT,
117+
"id" TEXT,
118+
UNIQUE("account", "address", "id")
119+
);`,
120+
`CREATE INDEX IF NOT EXISTS "idx__indexed_shardChains__id" ON "indexed_shardChains" ("id");`,
111121
}
112122
)
113123

@@ -322,6 +332,29 @@ func updateShardChain(profile *types.SQLChainProfile) storageProcedure {
322332
profile.Address.String(),
323333
string(profile.ID),
324334
enc.Bytes())
335+
if err != nil {
336+
return
337+
}
338+
339+
for _, u := range profile.Users {
340+
if u.Permission.Role == types.Void {
341+
// remove index
342+
_, err = tx.Exec(`DELETE FROM "indexed_shardChains" WHERE "account" = ? AND "address" = ?`,
343+
u.Address.String(),
344+
profile.Address.String())
345+
} else {
346+
_, err = tx.Exec(`INSERT OR REPLACE INTO "indexed_shardChains" ("account", "address", "id")
347+
VALUES(?, ?, ?)`,
348+
u.Address.String(),
349+
profile.Address.String(),
350+
profile.ID)
351+
}
352+
353+
if err != nil {
354+
return
355+
}
356+
}
357+
325358
return
326359
}
327360
}
@@ -332,6 +365,10 @@ func deleteShardChain(id proto.DatabaseID) storageProcedure {
332365
"profile_database_id": id,
333366
}).Debug("deleting profile")
334367
_, err = tx.Exec(`DELETE FROM "shardChain" WHERE "id"=?`, id)
368+
if err != nil {
369+
return
370+
}
371+
_, err = tx.Exec(`DELETE FROM "indexed_shardChains" WHERE "id" = ?`, id)
335372
return
336373
}
337374
}

0 commit comments

Comments
 (0)