Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
Gas optimized create with raw Table
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Feb 17, 2020
1 parent 36449ef commit 2963287
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
40 changes: 28 additions & 12 deletions incubator/group/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Keeper struct {
key sdk.StoreKey

// Group Table
groupTable orm.AutoUInt64Table
groupTable orm.Table
groupByAdminIndex orm.Index

// Group Member Table
Expand All @@ -60,6 +60,7 @@ type Keeper struct {
voteTable orm.NaturalKeyTable
voteByProposalBaseIndex orm.UInt64Index
voteByVoterIndex orm.Index
groupSeq orm.Sequence
}

func NewGroupKeeper(storeKey sdk.StoreKey) Keeper {
Expand All @@ -68,7 +69,8 @@ func NewGroupKeeper(storeKey sdk.StoreKey) Keeper {
//
// Group Table
//
groupTableBuilder := orm.NewAutoUInt64TableBuilder(GroupTablePrefix, GroupTableSeqPrefix, storeKey, &GroupMetadata{})
groupTableBuilder := orm.NewTableBuilder(GroupTablePrefix, storeKey, &GroupMetadata{}, orm.FixLengthIndexKeys(orm.EncodedSeqLength))
k.groupSeq = orm.NewSequence(storeKey, GroupTableSeqPrefix)
k.groupByAdminIndex = orm.NewIndex(groupTableBuilder, GroupByAdminIndexPrefix, func(val interface{}) ([]orm.RowID, error) {
return []orm.RowID{val.(*GroupMetadata).Admin.Bytes()}, nil
})
Expand Down Expand Up @@ -137,17 +139,17 @@ func NewGroupKeeper(storeKey sdk.StoreKey) Keeper {
}

func (k Keeper) CreateGroup(ctx orm.HasKVStore, admin sdk.AccAddress, members []Member, comment string) (GroupID, error) {
var val = GroupID(k.groupTable.Sequence().PeekNextVal(ctx))
id, err := k.groupTable.Create(ctx, &GroupMetadata{
Group: val,
id := k.groupSeq.NextVal(ctx)
var groupID = GroupID(id)
err := k.groupTable.Create(ctx, orm.EncodeSequence(id), &GroupMetadata{
Group: groupID,
Admin: admin,
Comment: comment,
Version: 0,
})
if err != nil {
return 0, errors.Wrap(err, "could not create group")
}
groupID := GroupID(id)

for i := range members {
m := members[i]
Expand All @@ -163,19 +165,33 @@ func (k Keeper) CreateGroup(ctx orm.HasKVStore, admin sdk.AccAddress, members []
return groupID, nil
}

func (k Keeper) UpdateGroupMembers(ctx orm.HasKVStore, group GroupID, membersUpdates []Member) error {
func (k Keeper) UpdateGroupMembers(ctx orm.HasKVStore, groupID GroupID, membersUpdates []Member) error {
panic("implement me")
}

func (k Keeper) UpdateGroupAdmin(ctx orm.HasKVStore, group GroupID, newAdmin sdk.AccAddress) error {
panic("implement me")
func (k Keeper) UpdateGroupAdmin(ctx orm.HasKVStore, groupID GroupID, newAdmin sdk.AccAddress) error {
var obj GroupMetadata
rowID := orm.EncodeSequence(uint64(groupID))
err := k.groupTable.GetOne(ctx, rowID, &obj)
if err != nil {
return err
}
obj.Admin = newAdmin
return k.groupTable.Save(ctx, rowID, &obj)
}

func (k Keeper) UpdateGroupComment(ctx orm.HasKVStore, group GroupID, newComment string) error {
panic("implement me")
func (k Keeper) UpdateGroupComment(ctx orm.HasKVStore, groupID GroupID, newComment string) error {
var obj GroupMetadata
rowID := orm.EncodeSequence(uint64(groupID))
err := k.groupTable.GetOne(ctx, rowID, &obj)
if err != nil {
return err
}
obj.Comment = newComment
return k.groupTable.Save(ctx, rowID, &obj)
}

//func (k Keeper) CreateGroupAccount(ctx orm.HasKVStore, admin sdk.AccAddress, group GroupID, policy DecisionPolicy, comment string) (sdk.AccAddress, error) {
//func (k Keeper) CreateGroupAccount(ctx orm.HasKVStore, admin sdk.AccAddress, groupID GroupID, policy DecisionPolicy, comment string) (sdk.AccAddress, error) {
// panic("implement me")
//}
//
Expand Down
3 changes: 1 addition & 2 deletions incubator/group/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ func (m MsgCreateGroup) GetSigners() []sdk.AccAddress {

// GetSignBytes returns the bytes for the message signer to sign on
func (m MsgCreateGroup) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(m)
return sdk.MustSortJSON(bz)
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(m))
}

// ValidateBasic does a sanity check on the provided data
Expand Down

0 comments on commit 2963287

Please sign in to comment.