Skip to content

Commit

Permalink
Merge pull request #32 from initia-labs/feat/allowed-publishers
Browse files Browse the repository at this point in the history
feat: add allowed_publishers params
  • Loading branch information
beer-1 authored Dec 12, 2023
2 parents 5f92ad1 + c31a2ab commit f6769ff
Show file tree
Hide file tree
Showing 29 changed files with 366 additions and 100 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/initia-labs/OPinit v0.1.0-beta.3
// we also need to update `LIBINITIAVM_VERSION` of images/private/Dockerfile#5
github.com/initia-labs/initiavm v0.1.2-beta.4
github.com/initia-labs/initiavm v0.1.2-beta.5
github.com/novifinancial/serde-reflection/serde-generate/runtime/golang v0.0.0-20220519162058-e5cd3c3b3f3a
github.com/pelletier/go-toml v1.9.5
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -787,8 +787,8 @@ github.com/initia-labs/OPinit v0.1.0-beta.3 h1:DY/ItJTUAujNhqrzjgAFFzSLn/IGrIN1F
github.com/initia-labs/OPinit v0.1.0-beta.3/go.mod h1:MPnFU2x4xzeT3L8K3lc0wkOl09o/oekEabPHIwWdqJg=
github.com/initia-labs/cosmos-sdk v0.47.7-0.20231208103644-7bba6b092b8c h1:XdQVkEp/PoWGz/ykzQmiKmBK50X8+ToV/briOTz2vT0=
github.com/initia-labs/cosmos-sdk v0.47.7-0.20231208103644-7bba6b092b8c/go.mod h1:fIhY9qlXZNvlWz8lAB2YcSRf2dCAi8VEJrq5GY/v2yE=
github.com/initia-labs/initiavm v0.1.2-beta.4 h1:RTBJ2hl8DSwLTi7rWx15beslfaqLm/bnEzaEFxsuGZ8=
github.com/initia-labs/initiavm v0.1.2-beta.4/go.mod h1:aQt4lImZWF9xj7Fm978n0IoeDdKWXTg1CSq6O4WoSD8=
github.com/initia-labs/initiavm v0.1.2-beta.5 h1:/pmtEdgSfNBhuifpQ4VEc/GxBhJCZ8LxzAAhPmnUTT0=
github.com/initia-labs/initiavm v0.1.2-beta.5/go.mod h1:aQt4lImZWF9xj7Fm978n0IoeDdKWXTg1CSq6O4WoSD8=
github.com/jackpal/go-nat-pmp v1.0.2/go.mod h1:QPH045xvCAeXUZOxsnwmrtiCoxIr9eob+4orBN1SBKc=
github.com/jedisct1/go-minisign v0.0.0-20190909160543-45766022959e/go.mod h1:G1CVv03EnqU1wYL2dFwXxW2An0az9JTl/ZsqXQeBlkU=
github.com/jessevdk/go-flags v0.0.0-20141203071132-1679536dcc89/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
Expand Down
2 changes: 1 addition & 1 deletion images/private/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM golang:1.19.1-alpine3.16 AS go-builder
#ARG arch=x86_64

# See https://github.com/initia-labs/initiavm/releases
ARG LIBINITIAVM_VERSION=v0.1.2-beta.4
ARG LIBINITIAVM_VERSION=v0.1.2-beta.5

# this comes from standard alpine nightly file
# https://github.com/rust-lang/docker-rust-nightly/blob/master/alpine3.12/Dockerfile
Expand Down
7 changes: 7 additions & 0 deletions proto/initia/move/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ message Params {
(gogoproto.nullable) = false,
(amino.dont_omitempty) = true
];

// It is a list of addresses with permission to distribute contracts,
// and an empty list is interpreted as allowing anyone to distribute.
repeated string allowed_publishers = 5 [
(gogoproto.moretags) = "yaml:\"allowed_publishers\"",
(amino.dont_omitempty) = true
];
}

// RawParams defines the raw params to store.
Expand Down
2 changes: 1 addition & 1 deletion x/bank/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func _createTestInput(
moduleBytes, err := precompile.ReadStdlib()
require.NoError(t, err)

err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.ArbitraryEnabled)
err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.ArbitraryEnabled, moveParams.AllowedPublishers)
require.NoError(t, err)

faucet := NewTestFaucet(t, ctx, bankKeeper, *moveKeeper, authtypes.Minter, initialTotalSupply()...)
Expand Down
2 changes: 1 addition & 1 deletion x/distribution/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func _createTestInput(
moduleBytes, err := precompile.ReadStdlib()
require.NoError(t, err)

err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.ArbitraryEnabled)
err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.ArbitraryEnabled, moveParams.AllowedPublishers)
require.NoError(t, err)

faucet := NewTestFaucet(t, ctx, bankKeeper, *moveKeeper, authtypes.Minter, initialTotalSupply()...)
Expand Down
2 changes: 1 addition & 1 deletion x/gov/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func _createTestInput(
moduleBytes, err := precompile.ReadStdlib()
require.NoError(t, err)

err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.ArbitraryEnabled)
err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.ArbitraryEnabled, moveParams.AllowedPublishers)
require.NoError(t, err)

faucet := NewTestFaucet(t, ctx, bankKeeper, *moveKeeper, authtypes.Minter, initialTotalSupply()...)
Expand Down
9 changes: 9 additions & 0 deletions x/move/keeper/bank_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func Test_GetBalance(t *testing.T) {
require.NoError(t, err)

amount, err = moveBankKeeper.GetBalance(ctx, twoAddr, bondDenom)
require.NoError(t, err)
require.Equal(t, mintAmount, amount)
}

Expand All @@ -51,6 +52,8 @@ func Test_AccountCoinStore(t *testing.T) {
require.NoError(t, err)

coinStore, err := moveBankKeeper.GetUserStores(ctx, twoAddr)
require.NoError(t, err)

iter := coinStore.Iterator(nil, nil)
defer iter.Close()

Expand All @@ -63,6 +66,8 @@ func Test_AccountCoinStore(t *testing.T) {

value := iter.Value()
storeAddr, err := vmtypes.NewAccountAddressFromBytes(value)
require.NoError(t, err)

mt, amount, err := moveBankKeeper.Balance(ctx, storeAddr)
require.NoError(t, err)
require.Equal(t, sdk.NewInt(1), amount)
Expand All @@ -87,6 +92,8 @@ func Test_AccountCoinStore(t *testing.T) {

value := iter2.Value()
storeAddr, err := vmtypes.NewAccountAddressFromBytes(value)
require.NoError(t, err)

mt, amount, err := moveBankKeeper.Balance(ctx, storeAddr)
require.NoError(t, err)
require.Equal(t, sdk.NewInt(mintAmount+1), amount)
Expand Down Expand Up @@ -145,6 +152,8 @@ func Test_GetIssuers(t *testing.T) {
}

issuers, err := moveBankKeeper.GetIssuers(ctx)
require.NoError(t, err)

iter := issuers.Iterator(nil, nil)
defer iter.Close()

Expand Down
53 changes: 52 additions & 1 deletion x/move/keeper/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ func NewCodeKeeper(k *Keeper) CodeKeeper {
return CodeKeeper{k}
}

// Load the code params from the move store
func (k CodeKeeper) GetParams(ctx sdk.Context) (bool, []vmtypes.AccountAddress, error) {
bz, err := k.GetResourceBytes(ctx, vmtypes.StdAddress, vmtypes.StructTag{
Address: vmtypes.StdAddress,
Module: vmtypes.Identifier(types.MoveModuleNameCode),
Name: vmtypes.Identifier(types.ResourceNameModuleStore),
TypeArgs: []vmtypes.TypeTag{},
})
if err != nil {
return false, nil, err
}

return types.ReadCodeModuleStore(bz)
}

// Load the allow_arbitrary flag from the move store
func (k CodeKeeper) GetAllowArbitrary(ctx sdk.Context) (bool, error) {
bz, err := k.GetResourceBytes(ctx, vmtypes.StdAddress, vmtypes.StructTag{
Expand All @@ -28,7 +43,7 @@ func (k CodeKeeper) GetAllowArbitrary(ctx sdk.Context) (bool, error) {
return false, err
}

return vmtypes.NewDeserializer(bz).DeserializeBool()
return vmtypes.NewDeserializer(bz[:1]).DeserializeBool()
}

// Store the allow_arbitrary flag to move store.
Expand All @@ -51,6 +66,42 @@ func (k CodeKeeper) SetAllowArbitrary(ctx sdk.Context, allow bool) error {
)
}

// Load the allowed_publishers from the move store
func (k CodeKeeper) GetAllowedPublishers(ctx sdk.Context) ([]vmtypes.AccountAddress, error) {
bz, err := k.GetResourceBytes(ctx, vmtypes.StdAddress, vmtypes.StructTag{
Address: vmtypes.StdAddress,
Module: vmtypes.Identifier(types.MoveModuleNameCode),
Name: vmtypes.Identifier(types.ResourceNameModuleStore),
TypeArgs: []vmtypes.TypeTag{},
})
if err != nil {
return nil, err
}

_, allowedPublishers, err := types.ReadCodeModuleStore(bz)
return allowedPublishers, err
}

// Store the allowed_publishers to move store.
func (k CodeKeeper) SetAllowedPublishers(ctx sdk.Context, allowedPublishers []vmtypes.AccountAddress) error {
bz, err := vmtypes.SerializeAddressVector(allowedPublishers)
if err != nil {
return err
}

return k.ExecuteEntryFunction(
ctx,
vmtypes.StdAddress,
vmtypes.StdAddress,
types.MoveModuleNameCode,
types.FunctionNameCodeSetAllowedPublishers,
[]vmtypes.TypeTag{},
[][]byte{
bz,
},
)
}

// GetUpgradePolicy reads upgrade policy from the code module.
func (k CodeKeeper) GetUpgradePolicy(ctx sdk.Context, addr vmtypes.AccountAddress, name string) (types.UpgradePolicy, error) {
st := vmtypes.StructTag{
Expand Down
47 changes: 47 additions & 0 deletions x/move/keeper/code_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package keeper_test

import (
"testing"

"github.com/initia-labs/initia/x/move/keeper"
"github.com/initia-labs/initia/x/move/types"
vmtypes "github.com/initia-labs/initiavm/types"

"github.com/stretchr/testify/require"
)

func Test_CodeKeeper_GetParams(t *testing.T) {
ctx, input := createDefaultTestInput(t)

allowArbitrary, allowedPublishers, err := keeper.NewCodeKeeper(&input.MoveKeeper).GetParams(ctx)
require.NoError(t, err)
require.Equal(t, types.DefaultArbitraryEnabled, allowArbitrary)
require.Empty(t, allowedPublishers)
}

func Test_CodeKeeper_SetParams(t *testing.T) {
ctx, input := createDefaultTestInput(t)
codeKeeper := keeper.NewCodeKeeper(&input.MoveKeeper)

err := codeKeeper.SetAllowArbitrary(ctx, false)
require.NoError(t, err)

err = codeKeeper.SetAllowedPublishers(ctx, []vmtypes.AccountAddress{vmtypes.StdAddress, vmtypes.TestAddress})
require.NoError(t, err)

allowArbitrary, err := codeKeeper.GetAllowArbitrary(ctx)
require.NoError(t, err)
require.False(t, allowArbitrary)

allowedPublishers, err := codeKeeper.GetAllowedPublishers(ctx)
require.NoError(t, err)
require.Equal(t, []vmtypes.AccountAddress{vmtypes.StdAddress, vmtypes.TestAddress}, allowedPublishers)
}

func Test_CodeKeeper_MustContains_StdAddr(t *testing.T) {
ctx, input := createDefaultTestInput(t)
codeKeeper := keeper.NewCodeKeeper(&input.MoveKeeper)

err := codeKeeper.SetAllowedPublishers(ctx, []vmtypes.AccountAddress{vmtypes.TestAddress})
require.Error(t, err)
}
2 changes: 1 addition & 1 deletion x/move/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ func _createTestInput(

// append test module
moduleBytes = append(moduleBytes, basicCoinModule)
err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.ArbitraryEnabled)
err = moveKeeper.Initialize(ctx, moduleBytes, moveParams.ArbitraryEnabled, moveParams.AllowedPublishers)
require.NoError(t, err)

faucet := NewTestFaucet(t, ctx, bankKeeper, *moveKeeper, authtypes.Minter, initialTotalSupply()...)
Expand Down
2 changes: 0 additions & 2 deletions x/move/keeper/dex.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ func (k DexKeeper) deleteDexPair(
) {
kvStore := ctx.KVStore(k.storeKey)
kvStore.Delete(types.GetDexPairKey(metadataQuote))

return
}

// HasDexPair check whether types.DexPair exists or not with
Expand Down
2 changes: 2 additions & 0 deletions x/move/keeper/dex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ func TestDexPair(t *testing.T) {
require.False(t, found)

metadataQuote, err := types.MetadataAddressFromDenom(denom)
require.NoError(t, err)
metadataLP, err := types.MetadataAddressFromDenom(denomLP)
require.NoError(t, err)

// invalid metadata
dexPair := types.DexPair{
Expand Down
15 changes: 13 additions & 2 deletions x/move/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func (k Keeper) Initialize(
ctx sdk.Context,
moduleBytes [][]byte,
allowArbitrary bool,
allowedPublishers []string,
) error {
ctx = ctx.WithTxBytes(make([]byte, 32))

Expand All @@ -30,10 +31,20 @@ func (k Keeper) Initialize(
modules[i] = vmtypes.NewModule(moduleBz)
}

_allowedPublishers := make([]vmtypes.AccountAddress, len(allowedPublishers))
for i, addr := range allowedPublishers {
addr, err := types.AccAddressFromString(addr)
if err != nil {
return err
}

_allowedPublishers[i] = addr
}

// The default upgrade policy is compatible when it's not set,
// so skip the registration at initialize.
kvStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.PrefixKeyVMStore)
if err := k.moveVM.Initialize(kvStore, api, env, vmtypes.NewModuleBundle(modules...), allowArbitrary); err != nil {
if err := k.moveVM.Initialize(kvStore, api, env, vmtypes.NewModuleBundle(modules...), allowArbitrary, _allowedPublishers); err != nil {
return err
}

Expand All @@ -51,7 +62,7 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) ([]abc
k.SetExecutionCounter(ctx, genState.ExecutionCounter)

if len(genState.GetModules()) == 0 {
if err := k.Initialize(ctx, genState.GetStdlibs(), params.ArbitraryEnabled); err != nil {
if err := k.Initialize(ctx, genState.GetStdlibs(), params.ArbitraryEnabled, params.AllowedPublishers); err != nil {
return nil, err
}
}
Expand Down
22 changes: 9 additions & 13 deletions x/move/keeper/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,8 @@ func (k Keeper) ExecuteEntryFunctionWithMultiSenders(
}

sendersStr := make([]string, len(senders))
signers := make([][]byte, len(senders))
for i, signer := range senders {
signers[i] = signer[:]
sendersStr[i] = signer.String()
for i, sender := range senders {
sendersStr[i] = sender.String()
}

api := NewApi(k, ctx)
Expand All @@ -172,7 +170,7 @@ func (k Keeper) ExecuteEntryFunctionWithMultiSenders(
api,
env,
gasForRuntime,
signers,
senders,
payload,
)

Expand Down Expand Up @@ -200,14 +198,14 @@ func (k Keeper) ExecuteEntryFunctionWithMultiSenders(

func (k Keeper) ExecuteScript(
ctx sdk.Context,
sender sdk.AccAddress,
sender vmtypes.AccountAddress,
byteCodes []byte,
typeArgs []vmtypes.TypeTag,
args [][]byte,
) error {
return k.ExecuteScriptWithMultiSenders(
ctx,
[]sdk.AccAddress{sender},
[]vmtypes.AccountAddress{sender},
byteCodes,
typeArgs,
args,
Expand All @@ -216,7 +214,7 @@ func (k Keeper) ExecuteScript(

func (k Keeper) ExecuteScriptWithMultiSenders(
ctx sdk.Context,
senders []sdk.AccAddress,
senders []vmtypes.AccountAddress,
byteCodes []byte,
typeArgs []vmtypes.TypeTag,
args [][]byte,
Expand Down Expand Up @@ -251,10 +249,8 @@ func (k Keeper) ExecuteScriptWithMultiSenders(
}

sendersStr := make([]string, len(senders))
signers := make([][]byte, len(senders))
for i, signer := range senders {
signers[i] = signer
sendersStr[i] = signer.String()
for i, sender := range senders {
sendersStr[i] = sender.String()
}

api := NewApi(k, ctx)
Expand All @@ -269,7 +265,7 @@ func (k Keeper) ExecuteScriptWithMultiSenders(
api,
env,
gasForRuntime,
signers,
senders,
payload,
)

Expand Down
Loading

0 comments on commit f6769ff

Please sign in to comment.