Skip to content

Commit

Permalink
Merge branch 'main' into copyExecutables
Browse files Browse the repository at this point in the history
  • Loading branch information
dwertent authored Sep 20, 2024
2 parents 62cdcde + 2d808b8 commit 33ccebf
Show file tree
Hide file tree
Showing 51 changed files with 569 additions and 537 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CREATE TABLE transactions (
contract TEXT NOT NULL,
"from" TEXT NOT NULL,
sequence_id UUID,
domain_id TEXT,
domain_name TEXT,
schema_id TEXT,
assembled_round BIGINT,
attestation_plan TEXT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ BEGIN;
CREATE TABLE schemas (
"id" TEXT NOT NULL,
"created_at" BIGINT,
"domain_id" TEXT,
"domain_name" TEXT,
"type" TEXT,
"signature" TEXT,
"definition" TEXT,
"labels" TEXT,
PRIMARY KEY ("domain_id", "id")
PRIMARY KEY ("domain_name", "id")
);
COMMIT;
14 changes: 8 additions & 6 deletions core/go/db/migrations/postgres/000003_create_states_table.up.sql
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
BEGIN;

CREATE TABLE states (
"id" TEXT NOT NULL,
"created_at" BIGINT NOT NULL,
"domain_id" TEXT,
"schema" TEXT,
"data" TEXT,
"id" TEXT NOT NULL,
"created_at" BIGINT NOT NULL,
"domain_name" TEXT,
"schema" TEXT,
"contract_address" TEXT,
"data" TEXT,
PRIMARY KEY ("id"),
FOREIGN KEY ("domain_id", "schema") REFERENCES schemas ("domain_id", "id") ON DELETE CASCADE
FOREIGN KEY ("domain_name", "schema") REFERENCES schemas ("domain_name", "id") ON DELETE CASCADE
);
CREATE INDEX states_by_domain ON states("domain_name", "schema", "contract_address");

CREATE TABLE state_labels (
"state" TEXT NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CREATE TABLE transactions (
contract VARCHAR NOT NULL,
"from" VARCHAR NOT NULL,
sequence_id UUID,
domain_id VARCHAR,
domain_name VARCHAR,
schema_id VARCHAR,
payload_json VARCHAR,
payload_rlp VARCHAR,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
CREATE TABLE schemas (
"id" VARCHAR NOT NULL,
"created_at" BIGINT,
"domain_id" VARCHAR,
"domain_name" VARCHAR,
"type" VARCHAR,
"signature" VARCHAR,
"definition" VARCHAR,
"labels" VARCHAR,
PRIMARY KEY ("domain_id", "id")
PRIMARY KEY ("domain_name", "id")
);
14 changes: 8 additions & 6 deletions core/go/db/migrations/sqlite/000003_create_states_table.up.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
CREATE TABLE states (
"id" VARCHAR NOT NULL,
"created_at" BIGINT NOT NULL,
"domain_id" VARCHAR,
"schema" VARCHAR,
"data" VARCHAR,
"id" VARCHAR NOT NULL,
"created_at" BIGINT NOT NULL,
"domain_name" VARCHAR,
"schema" VARCHAR,
"contract_address" VARCHAR,
"data" VARCHAR,
PRIMARY KEY ("id"),
FOREIGN KEY ("domain_id", "schema") REFERENCES schemas ("domain_id", "id") ON DELETE CASCADE
FOREIGN KEY ("domain_name", "schema") REFERENCES schemas ("domain_name", "id") ON DELETE CASCADE
);
CREATE INDEX states_by_domain ON states("domain_name", "schema", "contract_address");

CREATE TABLE state_labels (
"state" VARCHAR NOT NULL,
Expand Down
2 changes: 1 addition & 1 deletion core/go/internal/components/domainmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type DomainManagerToDomain interface {
type DomainManager interface {
ManagerLifecycle
ConfiguredDomains() map[string]*PluginConfig
DomainRegistered(name string, id uuid.UUID, toDomain DomainManagerToDomain) (fromDomain plugintk.DomainCallbacks, err error)
DomainRegistered(name string, toDomain DomainManagerToDomain) (fromDomain plugintk.DomainCallbacks, err error)
GetDomainByName(ctx context.Context, name string) (Domain, error)
GetSmartContractByAddress(ctx context.Context, addr tktypes.EthAddress) (DomainSmartContract, error)
WaitForDeploy(ctx context.Context, txID uuid.UUID) (DomainSmartContract, error)
Expand Down
12 changes: 6 additions & 6 deletions core/go/internal/domainmgr/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"sync"
"sync/atomic"

"github.com/google/uuid"
"github.com/hyperledger/firefly-common/pkg/i18n"
"github.com/hyperledger/firefly-signer/pkg/abi"
"github.com/hyperledger/firefly-signer/pkg/eip712"
Expand All @@ -46,7 +45,6 @@ type domain struct {

conf *DomainConfig
dm *domainManager
id uuid.UUID
name string
api components.DomainManagerToDomain
registryAddress *tktypes.EthAddress
Expand All @@ -62,13 +60,12 @@ type domain struct {
initDone chan struct{}
}

func (dm *domainManager) newDomain(id uuid.UUID, name string, conf *DomainConfig, toDomain components.DomainManagerToDomain) *domain {
func (dm *domainManager) newDomain(name string, conf *DomainConfig, toDomain components.DomainManagerToDomain) *domain {
d := &domain{
dm: dm,
conf: conf,
initRetry: retry.NewRetryIndefinite(&conf.Init.Retry),
name: name,
id: id,
api: toDomain,
initDone: make(chan struct{}),
registryAddress: tktypes.MustEthAddress(conf.RegistryAddress), // check earlier in startup
Expand Down Expand Up @@ -117,7 +114,6 @@ func (d *domain) processDomainConfig(confRes *prototk.ConfigureDomainResponse) (
}
}
return &prototk.InitDomainRequest{
DomainUuid: d.id.String(),
AbiStateSchemas: schemasProto,
}, nil
}
Expand Down Expand Up @@ -196,9 +192,13 @@ func (d *domain) FindAvailableStates(ctx context.Context, req *prototk.FindAvail
if err != nil {
return nil, i18n.WrapError(ctx, err, msgs.MsgDomainInvalidQueryJSON)
}
addr, err := tktypes.ParseEthAddress(req.ContractAddress)
if err != nil {
return nil, i18n.WrapError(ctx, err, msgs.MsgDomainErrorParsingAddress)
}

var states []*statestore.State
err = d.dm.stateStore.RunInDomainContext(d.name, func(ctx context.Context, dsi statestore.DomainStateInterface) (err error) {
err = d.dm.stateStore.RunInDomainContext(d.name, *addr, func(ctx context.Context, dsi statestore.DomainStateInterface) (err error) {
states, err = dsi.FindAvailableStates(req.SchemaId, &query)
return err
})
Expand Down
39 changes: 25 additions & 14 deletions core/go/internal/domainmgr/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ func newTestDomain(t *testing.T, realDB bool, domainConfig *prototk.DomainConfig
}

func registerTestDomain(t *testing.T, dm *domainManager, tp *testPlugin) {
domainID := uuid.New()
_, err := dm.DomainRegistered("test1", domainID, tp)
_, err := dm.DomainRegistered("test1", tp)
require.NoError(t, err)

da, err := dm.GetDomainByName(context.Background(), "test1")
Expand Down Expand Up @@ -238,9 +237,6 @@ func TestDoubleRegisterReplaces(t *testing.T) {
byName, err := dm.GetDomainByName(ctx, "test1")
require.NoError(t, err)
assert.Same(t, tp1.d, byName)
byUUID := dm.domainsByID[tp1.d.id]
require.NoError(t, err)
assert.Same(t, tp1.d, byUUID)

}

Expand Down Expand Up @@ -288,8 +284,7 @@ func TestDomainConfigureFail(t *testing.T) {
},
})

domainID := uuid.New()
_, err := dm.DomainRegistered("test1", domainID, tp)
_, err := dm.DomainRegistered("test1", tp)
require.NoError(t, err)

da, err := dm.GetDomainByName(ctx, "test1")
Expand Down Expand Up @@ -320,6 +315,18 @@ func TestDomainFindAvailableStatesBadQuery(t *testing.T) {
assert.Regexp(t, "PD011608", err)
}

func TestDomainFindAvailableStatesBadAddress(t *testing.T) {
ctx, _, tp, done := newTestDomain(t, false, goodDomainConf(), mockSchemas())
defer done()
assert.Nil(t, tp.d.initError.Load())
_, err := tp.d.FindAvailableStates(ctx, &prototk.FindAvailableStatesRequest{
SchemaId: "12345",
ContractAddress: "0x",
QueryJson: `{}`,
})
assert.Regexp(t, "PD011641", err)
}

func TestDomainFindAvailableStatesFail(t *testing.T) {
ctx, _, tp, done := newTestDomain(t, false, goodDomainConf(), func(mc *mockComponents) {
mc.stateStore.On("EnsureABISchemas", mock.Anything, "test1", mock.Anything).Return([]statestore.Schema{}, nil)
Expand All @@ -328,13 +335,14 @@ func TestDomainFindAvailableStatesFail(t *testing.T) {
defer done()
assert.Nil(t, tp.d.initError.Load())
_, err := tp.d.FindAvailableStates(ctx, &prototk.FindAvailableStatesRequest{
SchemaId: "12345",
QueryJson: `{}`,
ContractAddress: tktypes.RandAddress().String(),
SchemaId: "12345",
QueryJson: `{}`,
})
assert.Regexp(t, "pop", err)
}

func storeState(t *testing.T, dm *domainManager, tp *testPlugin, txID uuid.UUID, amount *ethtypes.HexInteger) *fakeState {
func storeState(t *testing.T, dm *domainManager, tp *testPlugin, contractAddress tktypes.EthAddress, txID uuid.UUID, amount *ethtypes.HexInteger) *fakeState {
state := &fakeState{
Salt: tktypes.Bytes32(tktypes.RandBytes(32)),
Owner: tktypes.EthAddress(tktypes.RandBytes(20)),
Expand All @@ -343,7 +351,7 @@ func storeState(t *testing.T, dm *domainManager, tp *testPlugin, txID uuid.UUID,
stateJSON, err := json.Marshal(state)
require.NoError(t, err)

err = dm.stateStore.RunInDomainContextFlush("test1", func(ctx context.Context, dsi statestore.DomainStateInterface) error {
err = dm.stateStore.RunInDomainContextFlush("test1", contractAddress, func(ctx context.Context, dsi statestore.DomainStateInterface) error {
newStates, err := dsi.UpsertStates(&txID, []*statestore.StateUpsert{
{
SchemaID: tp.stateSchemas[0].Id,
Expand All @@ -364,11 +372,13 @@ func TestDomainFindAvailableStatesOK(t *testing.T) {
assert.Nil(t, tp.d.initError.Load())

txID := uuid.New()
state1 := storeState(t, dm, tp, txID, ethtypes.NewHexIntegerU64(100000000))
contractAddress := tktypes.RandAddress()
state1 := storeState(t, dm, tp, *contractAddress, txID, ethtypes.NewHexIntegerU64(100000000))

// Filter match
states, err := tp.d.FindAvailableStates(ctx, &prototk.FindAvailableStatesRequest{
SchemaId: tp.stateSchemas[0].Id,
ContractAddress: contractAddress.String(),
SchemaId: tp.stateSchemas[0].Id,
QueryJson: `{
"eq": [
{ "field": "owner", "value": "` + state1.Owner.String() + `" }
Expand All @@ -380,7 +390,8 @@ func TestDomainFindAvailableStatesOK(t *testing.T) {

// Filter miss
states, err = tp.d.FindAvailableStates(ctx, &prototk.FindAvailableStatesRequest{
SchemaId: tp.stateSchemas[0].Id,
ContractAddress: contractAddress.String(),
SchemaId: tp.stateSchemas[0].Id,
QueryJson: `{
"eq": [
{ "field": "owner", "value": "` + tktypes.EthAddress(tktypes.RandBytes(20)).String() + `" }
Expand Down
10 changes: 3 additions & 7 deletions core/go/internal/domainmgr/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func NewDomainManager(bgCtx context.Context, conf *DomainManagerConfig) componen
return &domainManager{
bgCtx: bgCtx,
conf: conf,
domainsByID: make(map[uuid.UUID]*domain),
domainsByName: make(map[string]*domain),
domainsByAddress: make(map[tktypes.EthAddress]*domain),
contractWaiter: inflight.NewInflightManager[uuid.UUID, *PrivateSmartContract](uuid.Parse),
Expand All @@ -76,7 +75,6 @@ type domainManager struct {
blockIndexer blockindexer.BlockIndexer
ethClientFactory ethclient.EthClientFactory

domainsByID map[uuid.UUID]*domain
domainsByName map[string]*domain
domainsByAddress map[tktypes.EthAddress]*domain

Expand Down Expand Up @@ -123,7 +121,7 @@ func (dm *domainManager) Start() error { return nil }
func (dm *domainManager) Stop() {
dm.mux.Lock()
var allDomains []*domain
for _, d := range dm.domainsByID {
for _, d := range dm.domainsByName {
allDomains = append(allDomains, d)
}
dm.mux.Unlock()
Expand All @@ -136,7 +134,6 @@ func (dm *domainManager) Stop() {
func (dm *domainManager) cleanupDomain(d *domain) {
// must not hold the domain lock when running this
d.close()
delete(dm.domainsByID, d.id)
delete(dm.domainsByName, d.name)
delete(dm.domainsByAddress, *d.RegistryAddress())
}
Expand All @@ -149,7 +146,7 @@ func (dm *domainManager) ConfiguredDomains() map[string]*components.PluginConfig
return pluginConf
}

func (dm *domainManager) DomainRegistered(name string, id uuid.UUID, toDomain components.DomainManagerToDomain) (fromDomain plugintk.DomainCallbacks, err error) {
func (dm *domainManager) DomainRegistered(name string, toDomain components.DomainManagerToDomain) (fromDomain plugintk.DomainCallbacks, err error) {
dm.mux.Lock()
defer dm.mux.Unlock()

Expand All @@ -171,8 +168,7 @@ func (dm *domainManager) DomainRegistered(name string, id uuid.UUID, toDomain co
}

// Initialize
d := dm.newDomain(id, name, conf, toDomain)
dm.domainsByID[id] = d
d := dm.newDomain(name, conf, toDomain)
dm.domainsByName[name] = d
go d.init()
return d, nil
Expand Down
10 changes: 5 additions & 5 deletions core/go/internal/domainmgr/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ func newTestDomainManager(t *testing.T, realDB bool, conf *DomainManagerConfig,
require.NoError(t, mp.Mock.ExpectationsWereMet())
}
componentMocks.On("StateStore").Return(mc.stateStore)
mridc := mc.stateStore.On("RunInDomainContext", mock.Anything, mock.Anything)
mridc := mc.stateStore.On("RunInDomainContext", mock.Anything, mock.Anything, mock.Anything)
mridc.Run(func(args mock.Arguments) {
mridc.Return((args[1].(statestore.DomainContextFunction))(
mridc.Return((args[2].(statestore.DomainContextFunction))(
ctx, mc.domainStateInterface,
))
}).Maybe()
mridcf := mc.stateStore.On("RunInDomainContextFlush", mock.Anything, mock.Anything)
mridcf := mc.stateStore.On("RunInDomainContextFlush", mock.Anything, mock.Anything, mock.Anything)
mridcf.Run(func(args mock.Arguments) {
mridcf.Return((args[1].(statestore.DomainContextFunction))(
mridcf.Return((args[2].(statestore.DomainContextFunction))(
ctx, mc.domainStateInterface,
))
}).Maybe()
Expand Down Expand Up @@ -147,7 +147,7 @@ func TestDomainRegisteredNotFound(t *testing.T) {
})
defer done()

_, err := dm.DomainRegistered("unknown", uuid.New(), nil)
_, err := dm.DomainRegistered("unknown", nil)
assert.Regexp(t, "PD011600", err)
}

Expand Down
6 changes: 3 additions & 3 deletions core/go/internal/domainmgr/private_smart_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (dc *domainContract) WritePotentialStates(ctx context.Context, tx *componen

var states []*statestore.State
if len(newStatesToWrite) > 0 {
err := dc.dm.stateStore.RunInDomainContext(domain.name, func(ctx context.Context, dsi statestore.DomainStateInterface) (err error) {
err := dc.dm.stateStore.RunInDomainContext(domain.name, dc.info.Address, func(ctx context.Context, dsi statestore.DomainStateInterface) (err error) {
states, err = dsi.UpsertStates(&tx.ID, newStatesToWrite)
return err
})
Expand Down Expand Up @@ -254,7 +254,7 @@ func (dc *domainContract) LockStates(ctx context.Context, tx *components.Private
})
}

return dc.dm.stateStore.RunInDomainContext(dc.d.name, func(ctx context.Context, dsi statestore.DomainStateInterface) error {
return dc.dm.stateStore.RunInDomainContext(dc.d.name, dc.info.Address, func(ctx context.Context, dsi statestore.DomainStateInterface) error {
// Heavy lifting is all done for us by the state store
_, err := dsi.UpsertStates(&tx.ID, txLockedStateUpserts)
if err == nil && len(readStateUpserts) > 0 {
Expand Down Expand Up @@ -416,7 +416,7 @@ func (dc *domainContract) loadStates(ctx context.Context, refs []*prototk.StateR
stateIDs[i] = stateID
}
statesByID := make(map[tktypes.Bytes32]*statestore.State)
err := dc.dm.stateStore.RunInDomainContext(dc.d.name, func(ctx context.Context, dsi statestore.DomainStateInterface) error {
err := dc.dm.stateStore.RunInDomainContext(dc.d.name, dc.info.Address, func(ctx context.Context, dsi statestore.DomainStateInterface) error {
for schemaID, stateIDs := range rawIDsBySchema {
statesForSchema, err := dsi.FindAvailableStates(schemaID, &query.QueryJSON{
Statements: query.Statements{
Expand Down
Loading

0 comments on commit 33ccebf

Please sign in to comment.