Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Govern TX #231

Draft
wants to merge 107 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
107 commits
Select commit Hold shift + click to select a range
9b3a3bc
POC govern-tx created
nivida Nov 16, 2020
84d98e4
Whitelist POC etc. implemented
nivida Nov 16, 2020
591be32
code-style improvements
nivida Nov 16, 2020
961e756
code style improvements, index.ts updated for auth, Authenticator upd…
nivida Nov 16, 2020
cb9401e
draft Authenticator implemented
nivida Nov 16, 2020
0154840
typo fixed
nivida Nov 16, 2020
a0f9161
draft fastify auth plugin installed
nivida Nov 16, 2020
6b77717
naming checked, index updated for AuthPlugin, and usful todo comments…
nivida Nov 16, 2020
fac287a
typo fixed
nivida Nov 16, 2020
ff120f0
Authenticator simplified
nivida Nov 16, 2020
315b3c1
auth-plugin improved
nivida Nov 16, 2020
2a3ffec
Auth structure simplified
nivida Nov 17, 2020
175d8d1
fastify configurations updated
nivida Nov 17, 2020
367d104
basic Configuration class created and started to define the fastify r…
nivida Nov 17, 2020
6a620ae
dummy configuration added
nivida Nov 17, 2020
2a56016
definition of routes improved
nivida Nov 17, 2020
0854ae6
index.ts cleaned up and Boostrap created
nivida Nov 17, 2020
b69520e
simple Database adapter implemented
nivida Nov 17, 2020
108af5e
whitelist actions with the corresponding validations implemented
nivida Nov 17, 2020
ea624f4
condition fixed
nivida Nov 17, 2020
d97a4ef
types updated and Admin DB entity created
nivida Nov 17, 2020
09974c8
Authenticator updated
nivida Nov 17, 2020
a1e8a99
initiation of Authenticator in Bootstrap updated for admin role handl…
nivida Nov 17, 2020
03ce0a9
typo fixed and funcDoc improved in Authenticator
nivida Nov 17, 2020
8e65ab0
Authenticator cleaned up
nivida Nov 17, 2020
b186491
codestyle++
nivida Nov 17, 2020
1fde923
conditions improved in Authenticator
nivida Nov 17, 2020
f2eef73
codestyle++, request handling updated
nivida Nov 23, 2020
ba8dfff
wallet draft implementation
nivida Nov 23, 2020
f4983da
Draft provider added, actions updated, and server initiation in Boots…
nivida Nov 23, 2020
0df7f8b
codestyle and types improved
nivida Nov 23, 2020
08d5c3c
typo fixed
nivida Nov 23, 2020
f7dbaf4
transaction handling updated
nivida Nov 23, 2020
a497e80
not required interface definition removed in ContractFunction
nivida Nov 23, 2020
2720ce4
draft for submitter manipulation added
nivida Nov 23, 2020
c6a344b
ToDos added to think about tomorrow
nivida Nov 23, 2020
761a270
correct encoding/decoding API implemented from ethers.js
nivida Nov 24, 2020
269aa54
ABI items added and type definitions improved
nivida Nov 24, 2020
29b6eef
typescript related improvements
nivida Nov 24, 2020
657ac34
basic response validation schema added
nivida Nov 24, 2020
824d31b
Configuration constructor improved and index.ts updated
nivida Nov 24, 2020
43dd4e2
types fixed and TODO's updated
nivida Nov 24, 2020
f2b0288
TODO's removed, Authenticator simplified, and dependencies updated
nivida Nov 30, 2020
c49dc62
DB diagramm, README, and package.json commands added
nivida Nov 30, 2020
4a581d4
BootstrapTest template added and jest.config file created
nivida Dec 1, 2020
073d9ae
draft test cases for BootstrapTest implemented and Configuration updated
nivida Dec 4, 2020
b1fca8c
mock classes added to BootstrapTest
nivida Dec 4, 2020
eba5cd7
types updated, postgres dep checked, BootstrapTest updated
nivida Dec 4, 2020
f8d49f2
BootstrapTest implemented
nivida Dec 4, 2020
b3d5a92
unused import removed in BootstrapTest
nivida Dec 4, 2020
8ffb8ee
Test template for Authenticator added
nivida Dec 7, 2020
b922984
Authenticator whitelist check fixed and AuthenticatorTest implemented
nivida Dec 7, 2020
f6c8484
expectations added to AuthenticatorTest
nivida Dec 7, 2020
7bbb682
Configuration VO excluded from coverage
nivida Dec 7, 2020
86b1038
DatabaseTest added
nivida Dec 7, 2020
09ba82e
DatabaseTest simplified
nivida Dec 7, 2020
df2318c
Mock types handling in AuthenticatorTest improved and AdminTest templ…
nivida Dec 7, 2020
bb3ddb3
AdminTest added
nivida Dec 7, 2020
6d4c893
Whitelist.deleteItem fixed and WhitelistTest template added
nivida Dec 7, 2020
9e64044
Whitelist.addItem type updated and WhitelistTest added
nivida Dec 7, 2020
da15584
ProviderTest template added
nivida Dec 7, 2020
8ac1d58
existing tests improved and Provider test case template added
nivida Dec 9, 2020
b700171
ProviderTest implemented
nivida Dec 9, 2020
abfef93
AuthenticatorTest and ProviderTest improved. Transaction tests added
nivida Dec 9, 2020
db1ee85
WalletTest implemented
nivida Dec 10, 2020
cd70b87
types updated in AddItemAction and AddItemActionTest implemented
nivida Dec 10, 2020
7dbe265
code style improved and DeleteItemActionTest implemented
nivida Dec 10, 2020
b68dca7
GetListActionTest implemented
nivida Dec 10, 2020
429f2df
AbstractActionTest implemented and test names updated of transaction,…
nivida Dec 10, 2020
304dfad
AbstractTransaction fixed and AbstractTransactionTest created
nivida Dec 10, 2020
3136ce4
ContractFunctionTest implemented
nivida Dec 10, 2020
a19e260
AbstractActionTest updated and AbstractWhitelistActionTest implemented
nivida Dec 10, 2020
c10ec79
basic docker-compose added and init.sql to create the empty tables
nivida Dec 10, 2020
025b608
config setup implemented
nivida Dec 10, 2020
4ea093f
types added to config, index.ts improved, and e2e config etc. added
nivida Dec 10, 2020
6da8bc8
db_model.png updated
nivida Dec 10, 2020
e4c75dc
type def fixed in AddItemAction, dev.config updated, and Provider fixed
nivida Dec 10, 2020
52d063a
missing funcSig added to ContractFunction encoding
nivida Dec 10, 2020
21eed44
ContractFunction class improved
nivida Dec 10, 2020
d7f4dc9
ContractFunctionTest updated
nivida Dec 10, 2020
ff32f42
init.sql updated and tx limit per account implemented
nivida Dec 11, 2020
9ccbb94
CreateTransaction implemented and admin handling improved
nivida Dec 11, 2020
01671f3
Whitelist.limitReached implemented and Authenticator updated
nivida Dec 11, 2020
cdf1081
AdminItem interface updated
nivida Dec 11, 2020
0f3417c
request handling simplified
nivida Dec 11, 2020
06681d2
types improved
nivida Dec 11, 2020
6c0056d
unused import removed and important TODO added to Authenticator
nivida Dec 11, 2020
d50f988
Authenticator error handling updated to not leak any internal informa…
nivida Dec 11, 2020
eba373d
changes from Auth testing
nivida Dec 11, 2020
6ee9493
init.sql corrected to postgres sql syntax
nivida Dec 11, 2020
5f12051
Authenticator updated
nivida Dec 11, 2020
fd5b732
preHandler does already have the body parsed.. postman autocomplete c…
nivida Dec 11, 2020
3f26462
request body handling adjusted for whitelist actions
nivida Dec 11, 2020
0429fa0
Authenticator updated to handle whitelist and tx actions, sql schema …
nivida Dec 11, 2020
eb6c386
DeleteItemAction.validateRequest fixed and removing of function sig h…
nivida Dec 11, 2020
088a97c
postgres DB schema extended
nivida Dec 11, 2020
2153a23
query in Admin entity fixed
nivida Dec 11, 2020
5433b46
DB schema simplified. No need for a ID if the PK is anyways unique
nivida Dec 11, 2020
777a814
Whitelist.addItem query fixed
nivida Dec 11, 2020
753e5c7
DB model for documentation updated
nivida Dec 11, 2020
fa8642e
types improved for Database and his implemented entities
nivida Dec 11, 2020
64b5243
Whitelist.ts queries improved
nivida Dec 11, 2020
f55b8c5
generics added and code style improvment
nivida Dec 11, 2020
1285e50
code style improved (EOF)
nivida Dec 11, 2020
9816d2f
code style improvements
nivida Dec 14, 2020
98609ed
Wallet types updated and Whitelist GET request fixed
nivida Dec 15, 2020
621e855
fix govern-tx tests
novaknole1 Feb 17, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/govern-tx/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage/
dev-data/
66 changes: 66 additions & 0 deletions packages/govern-tx/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Govern TX

The govern-tx package does include the transaction service of the govern project.
This service can get deployed on any server or cloud infrastructure who has nodejs installed.

## Commands

- ``yarn dev`` - Starts the docker containers and the server in dev mode
- ``yarn start`` - Starts the docker containers and the server in production mode
- ``yarn start:server`` - Starts the server without starting the docker containers
- ``yarn start:containers`` - Starts the docker containers
- ``yarn stop:containers`` - Stops the docker containers
- ``yarn test`` - Does execute the unit and e2e tests of this package

## Postgres DB diagramm

![DB Diagramm](./assets/db_model.png)

## Directory Structure

Overall are the thoughts to have a modularized structure with the separation between abstract classes and concrete implementations with ``lib`` and ``src``.
Means ``lib`` and ``src`` should (If abstraction is required for all module) have the same base structure. This is important to increase the readability.

```
# All concrete implementations are located in the ./src/ folder
./src/

# Authentication handler used in the fastify pre-handler
./src/auth

# Configratuon object with the required validations of the input
./src/config

# DB wrapper and entities used in the server actions
./src/db

# Provider class used to connect to a Ethereum node
./src/provider

# All transactions related action commands
./src/transactions

# The wallet we use to sign the transactions
./src/wallet

# All whitelist related CRUD action commands
./src/whitelist

# This class is used to boot the entire server and is used in ./src/index.ts
./Bootstrap.ts

# All abstract classes and helpers are located in the ./lib/ folder
./lib/

# Base abstraction for all actions
./lib/AbstractAction.ts

# Base abstraction for all transaction related actions and VO for contract function encoding
./lib/transactions

# Base abstraction for all whiteliste related CRUD actions
./lib/whitelist

# Home of all tests (e2e & unit)
./test
```
Binary file added packages/govern-tx/assets/db_model.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions packages/govern-tx/config/dev.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Config } from '../src/config/Configuration';

export const config: Config = {
ethereum: {
publicKey: '', // Add default EOA
contracts: {
GovernQueue: '', // Add deployment address can get calculated if deployed with create2
GovernBaseFactory: ''
},
url: 'http://localhost:8545',
blockConfirmations: 42
},
database: {
user: 'govern-tx',
host: 'localhost',
password: 'tx-service',
database: 'govern-tx',
port: 5432
},
server: {
host: '0.0.0.0',
port: 4040
}
}
24 changes: 24 additions & 0 deletions packages/govern-tx/config/prod.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Config } from '../src/config/Configuration';

export const config: Config = {
ethereum: {
publicKey: '', // Add default EOA
contracts: {
GovernQueue: '', // Add deployment address can get calculated if deployed with create2
GovernBaseFactory: ''
},
url: 'localhost:8545',
blockConfirmations: 42
},
database: {
user: 'govern-tx',
host: 'localhost',
password: 'tx-service',
database: 'govern-tx',
port: 5432
},
server: {
host: '0.0.0.0',
port: 4040
}
}
18 changes: 18 additions & 0 deletions packages/govern-tx/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: "3"
services:
postgres:
image: postgres
ports:
- "5432:5432"
environment:
POSTGRES_USER: govern-tx
POSTGRES_PASSWORD: tx-service
POSTGRES_DB: govern-tx
user: "${HOST_UID}:${HOST_GID}"
volumes:
- ./dev-data/postgres:/var/lib/postgresql/data
- ./postgres:/docker-entrypoint-initdb.d/
ganache:
image: trufflesuite/ganache-cli
ports:
- "8545:8545"
4 changes: 4 additions & 0 deletions packages/govern-tx/jest.config.e2e.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const jestConfig = require('./jest.config.js')
jestConfig.testMatch = ['/**/**Test.e2e.ts']

module.exports = jestConfig;
25 changes: 25 additions & 0 deletions packages/govern-tx/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
rootDir: './',
preset: 'ts-jest',
notifyMode: 'success-change',
collectCoverage: true,
coverageDirectory: './coverage/',
coverageThreshold: {
global: {
functions: 80,
lines: 80,
statements: 80
}
},
notify: true,
clearMocks: true,
resetMocks: true,
resetModules: true,
testMatch: ['/**/**Test.ts'],
bail: true,
coveragePathIgnorePatterns: [
'node_modules',
'dist'
]
}

75 changes: 75 additions & 0 deletions packages/govern-tx/lib/AbstractAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { FastifySchema, FastifyRequest } from 'fastify'

export interface Params {
message: string | any,
signature: string,
}

export default abstract class AbstractAction<T> {
/**
* The given request by the user
*
* @var {Request} parameters
*/
protected request: FastifyRequest;

/**
* @param {Request} parameters
*
* @constructor
*/
constructor(request: FastifyRequest) {
this.request = this.validateRequest(request);
}

/**
* Validates the given request body.
*
* @method validateRequest
*
* @param {Request} request
*
* @returns {Request}
*
* @protected
*/
protected validateRequest(request: FastifyRequest): FastifyRequest {
return request;
}

/**
* Executes the actual action
*
* @method execute
*
* @returns {Promise<any>}
*
* @public
*/
public abstract execute(): Promise<T>

/**
* Returns the required request schema
*
* @property schema
*
* @returns {FastifySchema}
*/
public static get schema(): FastifySchema {
return {
body: {
type: 'object',
required: ['message', 'signature'],
properties: {
message: {
oneOf: [
{ type: 'string'},
{ type: 'object'}
]
},
signature: { type: 'string' }
}
}
} as FastifySchema
}
}
109 changes: 109 additions & 0 deletions packages/govern-tx/lib/transactions/AbstractTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { FastifySchema, FastifyRequest } from 'fastify';
import { TransactionReceipt } from '@ethersproject/abstract-provider'
import { JsonFragment } from '@ethersproject/abi';
import AbstractAction from '../AbstractAction'
import ContractFunction from '../transactions/ContractFunction'
import Provider from '../../src/provider/Provider'
import { EthereumOptions } from '../../src/config/Configuration';
import Whitelist from '../../src/db/Whitelist';
import { Params } from '../AbstractAction';
import { AuthenticatedRequest } from '../../src/auth/Authenticator';

export default abstract class AbstractTransaction extends AbstractAction<TransactionReceipt> {
/**
* The function ABI used to create a transaction
*
* @property {Object} functionABI
*
* @protected
*/
protected functionABI: JsonFragment

/**
* The contract name
*
* @property {string} contract
*
* @protected
*/
protected contract: string

/**
* @param {EthereumOptions} config - Ethereum related configurations
* @param {Provider} provider - The Ethereum provider object
* @param {Whitelist} whitelist - Whitelist DB adapter
* @param {Request} request - The request body given by the user
* @param {string} publicKey - The public key of the user
*
* @constructor
*/
constructor(
private config: EthereumOptions,
private provider: Provider,
private whitelist: Whitelist,
request: FastifyRequest,
) {
super(request)
}

/**
* Executes the transaction and returns the TransactionReceipt
*
* @method execute
*
* @returns {Promise<TransactionReceipt>}
*
* @public
*/
public async execute(): Promise<TransactionReceipt> {
const contractFunction = new ContractFunction(
this.functionABI,
(this.request.params as Params).message
)
contractFunction.functionArguments[0].payload.submitter = this.config.publicKey

let receipt: TransactionReceipt = await this.provider.sendTransaction(this.contract, contractFunction)

if(!(this.request as AuthenticatedRequest).admin) {
this.whitelist.increaseExecutionCounter((this.request as AuthenticatedRequest).publicKey)
}

return receipt;
}

/**
* TODO: Test BigNumber handling of the response and the fastify schema validation
*
* Returns the schema of a transaction command
*
* @property {FastifySchema} schema
*
* @returns {FastifySchema}
*/
public static get schema(): FastifySchema {
const schema = AbstractAction.schema

schema.response = {
200: {
type: 'object',
properties: {
to: { type: 'string' },
from: { type: 'string' },
contractAddress: { type: 'string' },
transactionIndex: { type: 'number' },
gasUsed: { type: 'object' }, // BigNumber
logsBloom: { type: 'string' },
blockHash: { type: 'string' },
transactionHash: { type: 'string' },
logs: { type: 'array' },
confirmations: { type: 'number' },
cumulativeGasUsed: { type: 'object'}, // BigNumber
byzantium: { type: 'boolean' },
status: { type: 'number' }
}
}
}

return schema
}
}
Loading