Skip to content

Commit

Permalink
update headless acc service
Browse files Browse the repository at this point in the history
  • Loading branch information
area363 committed Oct 31, 2023
1 parent 41b7f30 commit 5aa3c3d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
13 changes: 13 additions & 0 deletions NineChronicles.Headless/Services/RedisAccessControlService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using StackExchange.Redis;
using Libplanet.Crypto;
using Nekoyume.Blockchain;
Expand Down Expand Up @@ -26,5 +27,17 @@ public bool IsAccessDenied(Address address)

return result;
}

public int? GetTxQuota(Address address)
{
RedisValue result = _db.StringGet(address.ToString());
if (!result.IsNull)
{
Log.ForContext("Source", nameof(IAccessControlService))
.Debug("\"{Address}\" access level: {level}", address, result);
}

return Convert.ToInt32(result);
}
}
}
19 changes: 18 additions & 1 deletion NineChronicles.Headless/Services/SQLiteAccessControlService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Microsoft.Data.Sqlite;
using Libplanet.Crypto;
using Nekoyume.Blockchain;
Expand All @@ -8,9 +9,11 @@ namespace NineChronicles.Headless.Services
public class SQLiteAccessControlService : IAccessControlService
{
private const string CreateTableSql =
"CREATE TABLE IF NOT EXISTS blocklist (address VARCHAR(42))";
"CREATE TABLE IF NOT EXISTS blocklist (address VARCHAR(42), quota INT)";
private const string CheckAccessSql =
"SELECT EXISTS(SELECT 1 FROM blocklist WHERE address=@Address)";
private const string GetTxQuotaSql =
"SELECT quota FROM blocklist WHERE address=@Address";

protected readonly string _connectionString;

Expand Down Expand Up @@ -46,5 +49,19 @@ public bool IsAccessDenied(Address address)

return result;
}

public int? GetTxQuota(Address address)
{
using var connection = new SqliteConnection(_connectionString);
connection.Open();

using var command = connection.CreateCommand();
command.CommandText = GetTxQuotaSql;
command.Parameters.AddWithValue("@Address", address.ToString());

var queryResult = command.ExecuteScalar();

return Convert.ToInt32(queryResult);
}
}
}

0 comments on commit 5aa3c3d

Please sign in to comment.