Skip to content

Commit

Permalink
add wasm module
Browse files Browse the repository at this point in the history
  • Loading branch information
slandymani committed Oct 9, 2024
1 parent 8dbef43 commit 3a01548
Show file tree
Hide file tree
Showing 12 changed files with 1,515 additions and 0 deletions.
53 changes: 53 additions & 0 deletions database/schema/14-wasm.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
CREATE TYPE ACCESS_CONFIG AS
(
permission INT,
addresses TEXT
);

CREATE TABLE wasm_params
(
one_row_id BOOLEAN NOT NULL DEFAULT TRUE PRIMARY KEY,
code_upload_access ACCESS_CONFIG NOT NULL,
instantiate_default_permission INT NOT NULL,
height BIGINT NOT NULL
);

CREATE TABLE wasm_code
(
sender TEXT NULL,
byte_code BYTEA NOT NULL,
instantiate_permission ACCESS_CONFIG NULL,
code_id BIGINT NOT NULL UNIQUE,
height BIGINT NOT NULL
);
CREATE INDEX wasm_code_height_index ON wasm_code (height);

CREATE TABLE wasm_contract
(
sender TEXT NULL,
creator TEXT NOT NULL REFERENCES account (address),
admin TEXT NULL,
code_id BIGINT NOT NULL REFERENCES wasm_code (code_id),
label TEXT NULL,
raw_contract_message JSONB NOT NULL DEFAULT '{}'::JSONB,
funds COIN[] NOT NULL DEFAULT '{}',
contract_address TEXT NOT NULL UNIQUE,
data TEXT NULL,
instantiated_at TIMESTAMP NOT NULL,
contract_info_extension TEXT NULL,
contract_states JSONB NOT NULL DEFAULT '{}'::JSONB,
height BIGINT NOT NULL
);
CREATE INDEX wasm_contract_height_index ON wasm_contract (height);

CREATE TABLE wasm_execute_contract
(
sender TEXT NOT NULL,
contract_address TEXT NOT NULL REFERENCES wasm_contract (contract_address),
raw_contract_message JSONB NOT NULL DEFAULT '{}'::JSONB,
funds COIN[] NOT NULL DEFAULT '{}',
data TEXT NULL,
executed_at TIMESTAMP NOT NULL,
height BIGINT NOT NULL
);
CREATE INDEX execute_contract_height_index ON wasm_execute_contract (height);
205 changes: 205 additions & 0 deletions database/types/wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
package types

import (
"database/sql/driver"
"fmt"
"time"

wasmtypes "github.com/ODIN-PROTOCOL/wasmd/x/wasm/types"
)

// DbAccessConfig represents the information stored inside the database about a single access_config
type DbAccessConfig struct {
Permission int `db:"permission"`
Addresses []string `db:"addresses"`
}

// NewDbAccessConfig builds a DbAccessConfig starting from an CosmWasm type AccessConfig
func NewDbAccessConfig(accessCfg *wasmtypes.AccessConfig) DbAccessConfig {
return DbAccessConfig{
Permission: int(accessCfg.Permission),
Addresses: accessCfg.Addresses,
}
}

// Value implements driver.Valuer
func (cfg *DbAccessConfig) Value() (driver.Value, error) {
if cfg != nil {
return fmt.Sprintf("(%d,%s)", cfg.Permission, cfg.Addresses), nil
}

return fmt.Sprintf("(%d,%s)", wasmtypes.AccessTypeUnspecified, ""), nil
}

// Equal tells whether a and b represent the same access_config
func (cfg *DbAccessConfig) Equal(b *DbAccessConfig) bool {
return cfg.Permission == b.Permission
}

// ===================== Params =====================

// WasmParams represents the CosmWasm code in x/wasm module
type WasmParams struct {
CodeUploadAccess *DbAccessConfig `db:"code_upload_access"`
InstantiateDefaultPermission int32 `db:"instantiate_default_permission"`
Height int64 `db:"height"`
}

// NewWasmParams allows to build a new x/wasm params instance
func NewWasmParams(
codeUploadAccess *DbAccessConfig, instantiateDefaultPermission int32, height int64,
) WasmParams {
return WasmParams{
CodeUploadAccess: codeUploadAccess,
InstantiateDefaultPermission: instantiateDefaultPermission,
Height: height,
}
}

// ===================== Code =====================

// WasmCodeRow represents a single row inside the "wasm_code" table
type WasmCodeRow struct {
Sender string `db:"sender"`
WasmByteCode string `db:"wasm_byte_code"`
InstantiatePermission *DbAccessConfig `db:"instantiate_permission"`
CodeID int64 `db:"code_id"`
Height int64 `db:"height"`
}

// NewWasmCodeRow allows to easily create a new NewWasmCodeRow
func NewWasmCodeRow(
sender string,
wasmByteCode string,
instantiatePermission *DbAccessConfig,
codeID int64,
height int64,
) WasmCodeRow {
return WasmCodeRow{
Sender: sender,
WasmByteCode: wasmByteCode,
InstantiatePermission: instantiatePermission,
CodeID: codeID,
Height: height,
}
}

// Equals return true if one WasmCodeRow representing the same row as the original one
func (a WasmCodeRow) Equals(b WasmCodeRow) bool {
return a.Sender == b.Sender &&
a.WasmByteCode == b.WasmByteCode &&
a.InstantiatePermission.Equal(b.InstantiatePermission) &&
a.CodeID == b.CodeID &&
a.Height == b.Height
}

// ===================== Contract =====================

// WasmContractRow represents a single row inside the "wasm_contract" table
type WasmContractRow struct {
Sender string `db:"sender"`
Creator string `db:"creator"`
Admin string `db:"admin"`
CodeID int64 `db:"code_id"`
Label string `db:"label"`
RawContractMessage string `db:"raw_contract_message"`
Funds *DbCoins `db:"funds"`
ContractAddress string `db:"contract_address"`
Data string `db:"data"`
InstantiatedAt time.Time `db:"instantiated_at"`
ContractInfoExtension string `db:"contract_info_extension"`
ContractStates string `db:"contract_states"`
Height int64 `db:"height"`
}

// NewWasmContractRow allows to easily create a new WasmContractRow
func NewWasmContractRow(
sender string,
admin string,
codeID int64,
label string,
rawContractMessage string,
funds *DbCoins,
contractAddress string,
data string,
instantiatedAt time.Time,
creator string,
contractInfoExtension string,
height int64,
) WasmContractRow {
return WasmContractRow{
Sender: sender,
Admin: admin,
CodeID: codeID,
Label: label,
RawContractMessage: rawContractMessage,
Funds: funds,
ContractAddress: contractAddress,
Data: data,
InstantiatedAt: instantiatedAt,
Creator: creator,
ContractInfoExtension: contractInfoExtension,
Height: height,
}
}

// Equals return true if one WasmContractRow representing the same row as the original one
func (a WasmContractRow) Equals(b WasmContractRow) bool {
return a.Sender == b.Sender &&
a.Creator == b.Creator &&
a.Admin == b.Admin &&
a.CodeID == b.CodeID &&
a.Label == b.Label &&
a.RawContractMessage == b.RawContractMessage &&
a.Funds.Equal(b.Funds) &&
a.ContractAddress == b.ContractAddress &&
a.Data == b.Data &&
a.InstantiatedAt == b.InstantiatedAt &&
a.ContractInfoExtension == b.ContractInfoExtension &&
a.Height == b.Height
}

// ===================== Execute Contract =====================

// WasmExecuteContractRow represents a single row inside the "wasm_execute_contract" table
type WasmExecuteContractRow struct {
Sender string `db:"sender"`
ContractAddress string `db:"contract_address"`
RawContractMessage string `db:"raw_contract_message"`
Funds *DbCoins `db:"funds"`
Data string `db:"data"`
ExecutedAt time.Time `db:"executed_at"`
Height int64 `db:"height"`
}

// NewWasmExecuteContractRow allows to easily create a new WasmExecuteContractRow
func NewWasmExecuteContractRow(
sender string,
contractAddress string,
rawContractMessage string,
funds *DbCoins,
data string,
executedAt time.Time,
height int64,
) WasmExecuteContractRow {
return WasmExecuteContractRow{
Sender: sender,
RawContractMessage: rawContractMessage,
Funds: funds,
ContractAddress: contractAddress,
Data: data,
ExecutedAt: executedAt,
Height: height,
}
}

// Equals return true if one WasmExecuteContractRow representing the same row as the original one
func (a WasmExecuteContractRow) Equals(b WasmExecuteContractRow) bool {
return a.Sender == b.Sender &&
a.ContractAddress == b.ContractAddress &&
a.RawContractMessage == b.RawContractMessage &&
a.Funds.Equal(b.Funds) &&
a.Data == b.Data &&
a.ExecutedAt == b.ExecutedAt &&
a.Height == b.Height
}
35 changes: 35 additions & 0 deletions database/utils/wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package utils

import "github.com/forbole/callisto/v4/types"

func SplitWasmContracts(contracts []types.WasmContract, paramsNumber int) [][]types.WasmContract {
maxBalancesPerSlice := maxPostgreSQLParams / paramsNumber
slices := make([][]types.WasmContract, len(contracts)/maxBalancesPerSlice+1)

sliceIndex := 0
for index, contract := range contracts {
slices[sliceIndex] = append(slices[sliceIndex], contract)

if index > 0 && index%(maxBalancesPerSlice-1) == 0 {
sliceIndex++
}
}

return slices
}

func SplitWasmExecuteContracts(executeContracts []types.WasmExecuteContract, paramsNumber int) [][]types.WasmExecuteContract {
maxBalancesPerSlice := maxPostgreSQLParams / paramsNumber
slices := make([][]types.WasmExecuteContract, len(executeContracts)/maxBalancesPerSlice+1)

sliceIndex := 0
for index, executeContract := range executeContracts {
slices[sliceIndex] = append(slices[sliceIndex], executeContract)

if index > 0 && index%(maxBalancesPerSlice-1) == 0 {
sliceIndex++
}
}

return slices
}
Loading

0 comments on commit 3a01548

Please sign in to comment.