Skip to content

Commit

Permalink
Merge pull request #45 from initia-labs/feat/update-initiation-1
Browse files Browse the repository at this point in the history
Feat/update initiation 1
  • Loading branch information
ALPAC-4 authored Jul 16, 2024
2 parents 24722cd + 973b11a commit 57edee8
Show file tree
Hide file tree
Showing 21 changed files with 373 additions and 232 deletions.
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Config } from '@jest/types';

// Jest configuration
const config: Config.InitialOptions = {
roots: ['<rootDir>/src', '<rootDir>/src/test'],
roots: ['<rootDir>/src'],
transform: {
'^.+\\.ts?$': 'ts-jest',
},
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 22 additions & 1 deletion src/controller/executor/ClaimTxController.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { Context } from 'koa'
import { KoaController, Get, Controller } from 'koa-joi-controllers'
import {
KoaController,
Get,
Controller,
Validator,
Validate
} from 'koa-joi-controllers'
import { ErrorTypes } from '../../lib/error'
import { error, success } from '../../lib/response'
import { getClaimTxList } from '../../service'
import { responses, routeConfig, z } from 'koa-swagger-decorator'
import { GetClaimResponse } from '../../swagger/executor_model'
import { wrapControllerFunction } from '../../lib/metricsMiddleware'
import { INIT_ACCOUNT_REGEX } from '../../lib/constants'

const Joi = Validator.Joi

@Controller('')
export class ClaimTxController extends KoaController {
Expand Down Expand Up @@ -33,6 +42,18 @@ export class ClaimTxController extends KoaController {
}
})
@responses(GetClaimResponse)
@Validate({
query: {
address: Joi.string()
.optional()
.regex(INIT_ACCOUNT_REGEX)
.description('User address'),
sequence: Joi.number().optional(),
limit: Joi.number().optional().default(20),
offset: Joi.number().optional().default(0),
descending: Joi.boolean().optional().default(true)
}
})
@Get('/tx/claim')
async getClaimTxList(ctx: Context): Promise<void> {
await wrapControllerFunction('get_claim_tx_list', async (ctx) => {
Expand Down
23 changes: 22 additions & 1 deletion src/controller/executor/DepositTxController.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { Context } from 'koa'
import { KoaController, Get, Controller } from 'koa-joi-controllers'
import {
KoaController,
Get,
Controller,
Validator,
Validate
} from 'koa-joi-controllers'
import { ErrorTypes } from '../../lib/error'
import { error, success } from '../../lib/response'
import { getDepositTxList } from '../../service'
import { responses, routeConfig, z } from 'koa-swagger-decorator'
import { GetDepositResponse } from '../../swagger/executor_model'
import { wrapControllerFunction } from '../../lib/metricsMiddleware'
import { INIT_ACCOUNT_REGEX } from '../../lib/constants'

const Joi = Validator.Joi

@Controller('')
export class DepositTxController extends KoaController {
Expand Down Expand Up @@ -33,6 +42,18 @@ export class DepositTxController extends KoaController {
}
})
@responses(GetDepositResponse)
@Validate({
query: {
address: Joi.string()
.optional()
.regex(INIT_ACCOUNT_REGEX)
.description('User address'),
sequence: Joi.number().optional(),
limit: Joi.number().optional().default(20),
offset: Joi.number().optional().default(0),
descending: Joi.boolean().optional().default(true)
}
})
@Get('/tx/deposit')
async getDepositTxList(ctx: Context): Promise<void> {
await wrapControllerFunction('get_deposit_tx_list', async (ctx) => {
Expand Down
18 changes: 17 additions & 1 deletion src/controller/executor/OutputController.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import { Context } from 'koa'
import { KoaController, Get, Controller } from 'koa-joi-controllers'
import {
KoaController,
Get,
Controller,
Validator,
Validate
} from 'koa-joi-controllers'
import { ErrorTypes } from '../../lib/error'
import { error, success } from '../../lib/response'
import { responses, routeConfig, z } from 'koa-swagger-decorator'
import { getOutputList } from '../../service'
import { GetOutputResponse } from '../../swagger/executor_model'
import { wrapControllerFunction } from '../../lib/metricsMiddleware'

const Joi = Validator.Joi

@Controller('')
export class OutputController extends KoaController {
@routeConfig({
Expand All @@ -32,6 +40,14 @@ export class OutputController extends KoaController {
}
})
@responses(GetOutputResponse)
@Validate({
query: {
output_index: Joi.number().optional(),
limit: Joi.number().optional().default(20),
offset: Joi.number().optional().default(0),
descending: Joi.boolean().optional().default(true)
}
})
@Get('/output')
async getgetOutputList(ctx: Context): Promise<void> {
await wrapControllerFunction('get_output_list', async (ctx) => {
Expand Down
23 changes: 22 additions & 1 deletion src/controller/executor/WithdrawalTxController.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { Context } from 'koa'
import { KoaController, Get, Controller } from 'koa-joi-controllers'
import {
KoaController,
Get,
Controller,
Validator,
Validate
} from 'koa-joi-controllers'
import { ErrorTypes } from '../../lib/error'
import { error, success } from '../../lib/response'
import { getWithdrawalTxList } from '../../service'
import { responses, routeConfig, z } from 'koa-swagger-decorator'
import { GetWithdrawalResponse } from '../../swagger/executor_model'
import { wrapControllerFunction } from '../../lib/metricsMiddleware'
import { INIT_ACCOUNT_REGEX } from '../../lib/constants'

const Joi = Validator.Joi

@Controller('')
export class WithdrawalTxController extends KoaController {
Expand Down Expand Up @@ -33,6 +42,18 @@ export class WithdrawalTxController extends KoaController {
}
})
@responses(GetWithdrawalResponse)
@Validate({
query: {
address: Joi.string()
.optional()
.regex(INIT_ACCOUNT_REGEX)
.description('User address'),
sequence: Joi.number().optional(),
limit: Joi.number().optional().default(20),
offset: Joi.number().optional().default(0),
descending: Joi.boolean().optional().default(true)
}
})
@Get('/tx/withdrawal')
async getWithdrawalTxList(ctx: Context): Promise<void> {
await wrapControllerFunction('get_withdrawal_tx_list', async (ctx) => {
Expand Down
6 changes: 0 additions & 6 deletions src/controller/metrics/MetricsController.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import { Context } from 'koa'
import { KoaController, Get, Controller } from 'koa-joi-controllers'
import { routeConfig } from 'koa-swagger-decorator'
import { Prometheus } from '../../lib/metrics'

@Controller('')
export class MetricsController extends KoaController {
@routeConfig({
method: 'get',
path: '/metrics',
tags: ['Metrics']
})
@Get('/metrics')
async getMetrics(ctx: Context): Promise<void> {
try {
Expand Down
5 changes: 5 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const INIT_BECH32_REGEX = /^init1(?:[a-z0-9]){38}/
export const INIT_HEX_REGEX = /0x(?:[a-f0-9]*){1,64}/
export const INIT_ACCOUNT_REGEX = new RegExp(
INIT_BECH32_REGEX.source + '|' + INIT_HEX_REGEX.source
)
69 changes: 65 additions & 4 deletions src/lib/storage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,70 @@ const v1 = [
receiver: 'init174knscjg688ddtxj8smyjz073r3w5mmsp3m0m2',
l1_denom: 'uinit',
amount: BigInt(1000000)
},
{
bridge_id: BigInt(1),
sequence: BigInt(4),
sender: 'init1wzenw7r2t2ra39k4l9yqq95pw55ap4sm4vsa9g',
receiver: 'init174knscjg688ddtxj8smyjz073r3w5mmsp3m0m2',
l1_denom: 'uinit',
amount: BigInt(1000231200)
},
{
bridge_id: BigInt(1),
sequence: BigInt(5),
sender: 'init1wzenw7r2t2ra39k4l9yqq95pw55ap4sm4vsa9g',
receiver: 'init174knscjg688ddtxj8smyjz073r3w5mmsp3m0m2',
l1_denom: 'uinit',
amount: BigInt(32340000)
},
{
bridge_id: BigInt(1),
sequence: BigInt(6),
sender: 'init1wzenw7r2t2ra39k4l9yqq95pw55ap4sm4vsa9g',
receiver: 'init174knscjg688ddtxj8smyjz073r3w5mmsp3m0m2',
l1_denom: 'uinit',
amount: BigInt(101230000)
}
]

describe('WithdrawStorage', () => {
it('getmerkleproof', async () => {
const tx = {
bridge_id: BigInt(1),
sequence: BigInt(4),
sender: '0000000000000000000000000000000000000004',
receiver: '0000000000000000000000000000000000000001',
l1_denom: 'l1denom',
amount: BigInt(3000000)
}
const bridge_id_buf = Buffer.alloc(8)
bridge_id_buf.writeBigInt64BE(tx.bridge_id)

const sequence_buf = Buffer.alloc(8)
sequence_buf.writeBigInt64BE(tx.sequence)

const amount_buf = Buffer.alloc(8)
amount_buf.writeBigInt64BE(tx.amount)

const result = sha3_256(
Buffer.concat([
bridge_id_buf,
sequence_buf,
Buffer.from(tx.sender, 'hex'),
Buffer.from('|'),
Buffer.from(tx.receiver, 'hex'),
Buffer.from('|'),
Buffer.from(tx.l1_denom, 'utf8'),
Buffer.from('|'),
amount_buf
])
).toString('base64')
expect(
result == 'F+mzhRVdcwLS5tk2NDB2MbgMm7A0nk39G+NGEjXpTV0='
).toBeTruthy()
})

it('verify v1', async () => {
const airdrop = new WithdrawStorage(v1)
const target = v1[0]
Expand All @@ -47,11 +107,12 @@ describe('WithdrawStorage', () => {
])
).toString('base64')
expect(airdrop.verify(merkleProof, target)).toBeTruthy()

expect(merkleRoot).toEqual('EYgpXs1b+Z3AdGqjjtJHylrGzCjXtBKDD2UTPXelUk4=')
expect(merkleRoot).toEqual('VcN+0UZbTtGyyLfQtAHW+bCv5ixadyyT0ZZ26aUT1JY=')
expect(merkleProof).toEqual([
'5eJNy8mEqvyhysgWCqi7JQ7K602FtSpz+wDRNQitQMc='
'gnUeNU3EnW4iBOk8wounvu98aTER0BP5dOD0lkuwBBE=',
'yE4zjliK5P9sfdzR2iNh6nYHmD+mjDK6dONuZ3QlVcA=',
'GQXXUQ5P/egGvbAHkYfWHIAfgyCEmnjz/fUMKrWCEn8='
])
expect(outputRoot).toEqual('euaoJcFRXfV/6F0AiC0vYwXUY4NPHfCn9LbFMPieNsA=')
expect(outputRoot).toEqual('0cg24XcpDwTIFXHY4jNyxg2EQS5RUqcMvlMJeuI5rf4=')
})
})
11 changes: 11 additions & 0 deletions src/lib/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { sha3_256 } from './util'
import { WithdrawalTx } from './types'
import { AccAddress } from 'initia-l1'

const SPLITTER = Buffer.from('|', 'utf8')

function convertHexToBase64(hex: string): string {
return Buffer.from(hex, 'hex').toString('base64')
}
Expand All @@ -26,8 +28,11 @@ export class WithdrawStorage {
bridge_id_buf,
sequence_buf,
AccAddress.toBuffer(tx.sender),
SPLITTER,
AccAddress.toBuffer(tx.receiver),
SPLITTER,
Buffer.from(tx.l1_denom, 'utf8'),
SPLITTER,
amount_buf
])
)
Expand Down Expand Up @@ -57,8 +62,11 @@ export class WithdrawStorage {
bridge_id_buf,
sequence_buf,
AccAddress.toBuffer(tx.sender),
SPLITTER,
AccAddress.toBuffer(tx.receiver),
SPLITTER,
Buffer.from(tx.l1_denom, 'utf8'),
SPLITTER,
amount_buf
])
)
Expand Down Expand Up @@ -91,8 +99,11 @@ export class WithdrawStorage {
bridge_id_buf,
sequence_buf,
AccAddress.toBuffer(tx.sender),
SPLITTER,
AccAddress.toBuffer(tx.receiver),
SPLITTER,
Buffer.from(tx.l1_denom, 'utf8'),
SPLITTER,
amount_buf
])
)
Expand Down
4 changes: 0 additions & 4 deletions src/orm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import ExecutorUnconfirmedTxEntity from './executor/UnconfirmedTxEntity'
import RecordEntity from './batch/RecordEntity'
import BatchTxEntity from './batch/BatchTxEntity'

import OutputTxEntity from './output/OutputTxEntity'

import ChallengerDepositTxEntity from './challenger/DepositTxEntity'
import ChallengerWithdrawalTxEntity from './challenger/WithdrawalTxEntity'
import ChallengerFinalizeDepositTxEntity from './challenger/FinalizeDepositTxEntity'
Expand All @@ -18,7 +16,6 @@ import ChallengerOutputEntity from './challenger/OutputEntity'
import ChallengedOutputEntity from './challenger/DeletedOutputEntity'
import ChallengeEntity from './challenger/ChallengeEntity'

export * from './output/OutputTxEntity'
export * from './batch/RecordEntity'
export * from './batch/BatchTxEntity'

Expand All @@ -40,7 +37,6 @@ export * from './challenger/ChallengeEntity'
export {
RecordEntity,
BatchTxEntity,
OutputTxEntity,
StateEntity,
ExecutorWithdrawalTxEntity,
ExecutorDepositTxEntity,
Expand Down
14 changes: 0 additions & 14 deletions src/orm/output/OutputTxEntity.ts

This file was deleted.

Loading

0 comments on commit 57edee8

Please sign in to comment.