## Description
This PR adds a `ModuleDB` interface which can be used directly by Cosmos SDK modules. A simplified bank example with Mint/Send/Burn functionality against Balance and Supply tables is included in the tests.
This PR also:
* adds simplified `Get` and `Has` methods to `Table` which use the primary key values in the message instead of `...interface{}`
* adds a stable deterministic proto JSON marshaler and updates the `Entry.String` methods to use it because the golden tests are not deterministic without this. This code is currently internal but can be extracted to a public `codec` or `cosmos-proto` package eventually.
---
### Author Checklist
*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*
I have...
- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed
### Reviewers Checklist
*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*
I have...
- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
126 lines
3.3 KiB
Go
126 lines
3.3 KiB
Go
package ormdb
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/binary"
|
|
"math"
|
|
|
|
"google.golang.org/protobuf/reflect/protoregistry"
|
|
|
|
"github.com/cosmos/cosmos-sdk/orm/encoding/encodeutil"
|
|
|
|
"github.com/cosmos/cosmos-sdk/orm/encoding/ormkv"
|
|
"github.com/cosmos/cosmos-sdk/orm/types/ormerrors"
|
|
|
|
"google.golang.org/protobuf/proto"
|
|
"google.golang.org/protobuf/reflect/protoreflect"
|
|
|
|
"github.com/cosmos/cosmos-sdk/orm/model/ormtable"
|
|
)
|
|
|
|
type fileDescriptorDBOptions struct {
|
|
Prefix []byte
|
|
ID uint32
|
|
TypeResolver ormtable.TypeResolver
|
|
JSONValidator func(proto.Message) error
|
|
GetBackend func(context.Context) (ormtable.Backend, error)
|
|
GetReadBackend func(context.Context) (ormtable.ReadBackend, error)
|
|
}
|
|
|
|
type fileDescriptorDB struct {
|
|
id uint32
|
|
prefix []byte
|
|
tablesById map[uint32]ormtable.Table
|
|
tablesByName map[protoreflect.FullName]ormtable.Table
|
|
fileDescriptor protoreflect.FileDescriptor
|
|
}
|
|
|
|
func newFileDescriptorDB(fileDescriptor protoreflect.FileDescriptor, options fileDescriptorDBOptions) (*fileDescriptorDB, error) {
|
|
prefix := encodeutil.AppendVarUInt32(options.Prefix, options.ID)
|
|
|
|
schema := &fileDescriptorDB{
|
|
id: options.ID,
|
|
prefix: prefix,
|
|
tablesById: map[uint32]ormtable.Table{},
|
|
tablesByName: map[protoreflect.FullName]ormtable.Table{},
|
|
fileDescriptor: fileDescriptor,
|
|
}
|
|
|
|
resolver := options.TypeResolver
|
|
if resolver == nil {
|
|
resolver = protoregistry.GlobalTypes
|
|
}
|
|
|
|
messages := fileDescriptor.Messages()
|
|
n := messages.Len()
|
|
for i := 0; i < n; i++ {
|
|
messageDescriptor := messages.Get(i)
|
|
tableName := messageDescriptor.FullName()
|
|
messageType, err := resolver.FindMessageByName(tableName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
table, err := ormtable.Build(ormtable.Options{
|
|
Prefix: prefix,
|
|
MessageType: messageType,
|
|
TypeResolver: resolver,
|
|
JSONValidator: options.JSONValidator,
|
|
GetReadBackend: options.GetReadBackend,
|
|
GetBackend: options.GetBackend,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
id := table.ID()
|
|
if _, ok := schema.tablesById[id]; ok {
|
|
return nil, ormerrors.InvalidTableId.Wrapf("duplicate ID %d for %s", id, tableName)
|
|
}
|
|
schema.tablesById[id] = table
|
|
|
|
if _, ok := schema.tablesByName[tableName]; ok {
|
|
return nil, ormerrors.InvalidTableDefinition.Wrapf("duplicate table %s", tableName)
|
|
}
|
|
schema.tablesByName[tableName] = table
|
|
}
|
|
|
|
return schema, nil
|
|
}
|
|
|
|
func (f fileDescriptorDB) DecodeEntry(k, v []byte) (ormkv.Entry, error) {
|
|
r := bytes.NewReader(k)
|
|
err := encodeutil.SkipPrefix(r, f.prefix)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
id, err := binary.ReadUvarint(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if id > math.MaxUint32 {
|
|
return nil, ormerrors.UnexpectedDecodePrefix.Wrapf("uint32 varint id out of range %d", id)
|
|
}
|
|
|
|
table, ok := f.tablesById[uint32(id)]
|
|
if !ok {
|
|
return nil, ormerrors.UnexpectedDecodePrefix.Wrapf("can't find table with id %d", id)
|
|
}
|
|
|
|
return table.DecodeEntry(k, v)
|
|
}
|
|
|
|
func (f fileDescriptorDB) EncodeEntry(entry ormkv.Entry) (k, v []byte, err error) {
|
|
table, ok := f.tablesByName[entry.GetTableName()]
|
|
if !ok {
|
|
return nil, nil, ormerrors.BadDecodeEntry.Wrapf("can't find table %s", entry.GetTableName())
|
|
}
|
|
|
|
return table.EncodeEntry(entry)
|
|
}
|
|
|
|
var _ ormkv.EntryCodec = fileDescriptorDB{}
|