Skip to content

Commit

Permalink
Remove RemoteRegistry reference from smartContractGW
Browse files Browse the repository at this point in the history
Route all calls through ContractStore instead.

Signed-off-by: Andrew Richardson <[email protected]>
  • Loading branch information
awrichar committed Sep 22, 2021
1 parent dcb8111 commit f0cd139
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 80 deletions.
18 changes: 6 additions & 12 deletions internal/contractgateway/smartcontractgw.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,8 @@ func NewSmartContractGateway(conf *SmartContractGatewayConf, txnConf *tx.TxnProc
baseURL, _ = url.Parse("http://localhost:8080")
}
log.Infof("OpenAPI Smart Contract Gateway configured with base URL '%s'", baseURL.String())
rr := contractregistry.NewRemoteRegistry(&conf.RemoteRegistry)
gw := &smartContractGW{
conf: conf,
rr: rr,
baseSwaggerConf: &openapi.ABI2SwaggerConf{
ExternalHost: baseURL.Host,
ExternalRootPath: baseURL.Path,
Expand All @@ -156,14 +154,12 @@ func NewSmartContractGateway(conf *SmartContractGatewayConf, txnConf *tx.TxnProc
},
ws: ws,
}
gw.cs, err = contractregistry.NewContractStore(&contractregistry.ContractStoreConf{
rr := contractregistry.NewRemoteRegistry(&conf.RemoteRegistry)
gw.cs = contractregistry.NewContractStore(&contractregistry.ContractStoreConf{
BaseURL: conf.BaseURL,
StoragePath: conf.StoragePath,
}, rr)
if err != nil {
return nil, err
}
if err = gw.rr.Init(); err != nil {
if err = gw.cs.Init(); err != nil {
return nil, err
}
syncDispatcher := newSyncDispatcher(processor)
Expand All @@ -175,15 +171,13 @@ func NewSmartContractGateway(conf *SmartContractGatewayConf, txnConf *tx.TxnProc
}
}
gw.r2e = newREST2eth(gw, gw.cs, rpc, gw.sm, processor, asyncDispatcher, syncDispatcher)
gw.cs.Init()
return gw, nil
}

type smartContractGW struct {
conf *SmartContractGatewayConf
sm events.SubscriptionManager
cs contractregistry.ContractStore
rr contractregistry.RemoteRegistry
r2e *rest2eth
ws ws.WebSocketChannels
baseSwaggerConf *openapi.ABI2SwaggerConf
Expand Down Expand Up @@ -219,7 +213,7 @@ func (g *smartContractGW) PostDeploy(msg *messages.TransactionReceipt) error {
var err error
if isRemote {
if msg.RegisterAs != "" {
err = g.rr.RegisterInstance(msg.RegisterAs, "0x"+addrHexNo0x)
err = g.cs.AddRemoteInstance(msg.RegisterAs, "0x"+addrHexNo0x)
}
} else {
_, err = g.cs.AddContract(addrHexNo0x, requestID, registeredName, msg.RegisterAs)
Expand Down Expand Up @@ -1163,8 +1157,8 @@ func (g *smartContractGW) Shutdown() {
if g.sm != nil {
g.sm.Close()
}
if g.rr != nil {
g.rr.Close()
if g.cs != nil {
g.cs.Close()
}
}

Expand Down
16 changes: 9 additions & 7 deletions internal/contractgateway/smartcontractgw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ func TestRemoteRegistrySwaggerOrABI(t *testing.T) {
router.ServeHTTP(res, req)
assert.Equal(500, res.Code)

mcs.On("Close").Return()
scgw.Shutdown()
mcs.AssertExpectations(t)
}
Expand Down Expand Up @@ -552,6 +553,7 @@ func TestRemoteRegistryBadABI(t *testing.T) {
json.NewDecoder(res.Body).Decode(&msg)
assert.Regexp("Invalid ABI", msg["error"])

mcs.On("Close").Return()
scgw.Shutdown()
mcs.AssertExpectations(t)
}
Expand Down Expand Up @@ -753,8 +755,8 @@ func TestPostDeployRemoteRegisteredName(t *testing.T) {
},
nil, nil, nil, nil,
)
mrr := &contractregistrymocks.RemoteRegistry{}
s.(*smartContractGW).rr = mrr
mcs := &contractregistrymocks.ContractStore{}
s.(*smartContractGW).cs = mcs

contractAddr := ethbind.API.HexToAddress("0x0123456789AbcdeF0123456789abCdef01234567")
scgw := s.(*smartContractGW)
Expand All @@ -774,7 +776,7 @@ func TestPostDeployRemoteRegisteredName(t *testing.T) {
RegisterAs: "lobster",
}

mrr.On("RegisterInstance", "lobster", "0x0123456789abcdef0123456789abcdef01234567").Return(nil)
mcs.On("AddRemoteInstance", "lobster", "0x0123456789abcdef0123456789abcdef01234567").Return(nil)

deployFile := path.Join(dir, "abi_message1.deploy.json")
deployMsg := &messages.DeployContract{}
Expand All @@ -785,7 +787,7 @@ func TestPostDeployRemoteRegisteredName(t *testing.T) {

assert.Equal("http://localhost/api/v1/instances/lobster?openapi", replyMsg.ContractSwagger)

mrr.AssertExpectations(t)
mcs.AssertExpectations(t)
}

func TestPostDeployRemoteRegisteredNameNotSuccess(t *testing.T) {
Expand All @@ -802,8 +804,8 @@ func TestPostDeployRemoteRegisteredNameNotSuccess(t *testing.T) {
},
nil, nil, nil, nil,
)
mrr := &contractregistrymocks.RemoteRegistry{}
s.(*smartContractGW).rr = mrr
mcs := &contractregistrymocks.ContractStore{}
s.(*smartContractGW).cs = mcs

contractAddr := ethbind.API.HexToAddress("0x0123456789AbcdeF0123456789abCdef01234567")
scgw := s.(*smartContractGW)
Expand Down Expand Up @@ -832,7 +834,7 @@ func TestPostDeployRemoteRegisteredNameNotSuccess(t *testing.T) {

assert.Empty(replyMsg.ContractSwagger)

mrr.AssertExpectations(t)
mcs.AssertExpectations(t)
}

func TestPostDeployMissingContractAddress(t *testing.T) {
Expand Down
30 changes: 22 additions & 8 deletions internal/contractregistry/contractstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ type ContractResolver interface {

type ContractStore interface {
ContractResolver
Init()
Init() error
Close()
AddContract(addrHexNo0x, abiID, pathName, registerAs string) (*ContractInfo, error)
AddABI(id string, deployMsg *messages.DeployContract, createdTime time.Time) *ABIInfo
AddRemoteInstance(lookupStr, address string) error
GetLocalABIInfo(abiID string) (*ABIInfo, error)
ListContracts() []messages.TimeSortable
ListABIs() []messages.TimeSortable
Expand All @@ -72,18 +74,14 @@ type contractStore struct {
abiCache *lru.Cache
}

func NewContractStore(conf *ContractStoreConf, rr RemoteRegistry) (cs ContractStore, err error) {
c := &contractStore{
func NewContractStore(conf *ContractStoreConf, rr RemoteRegistry) ContractStore {
return &contractStore{
conf: conf,
rr: rr,
contractIndex: make(map[string]messages.TimeSortable),
contractRegistrations: make(map[string]*ContractInfo),
abiIndex: make(map[string]messages.TimeSortable),
}
if c.abiCache, err = lru.New(DefaultABICacheSize); err != nil {
return nil, ethconnecterrors.Errorf(ethconnecterrors.RESTGatewayResourceErr, err)
}
return c, nil
}

// ContractInfo is the minimal data structure we keep in memory, indexed by address
Expand Down Expand Up @@ -255,7 +253,7 @@ func (cs *contractStore) loadDeployMsg(abiID string) (*messages.DeployContract,
return msg, nil
}

func (cs *contractStore) Init() {
func (cs *contractStore) buildIndex() {
log.Infof("Building installed smart contract index")
legacyContractMatcher, _ := regexp.Compile(`^contract_([0-9a-z]{40})\.swagger\.json$`)
instanceMatcher, _ := regexp.Compile(`^contract_([0-9a-z]{40})\.instance\.json$`)
Expand All @@ -281,6 +279,18 @@ func (cs *contractStore) Init() {
log.Infof("Smart contract index built. %d entries", len(cs.contractIndex))
}

func (cs *contractStore) Init() (err error) {
if cs.abiCache, err = lru.New(DefaultABICacheSize); err != nil {
return ethconnecterrors.Errorf(ethconnecterrors.RESTGatewayResourceErr, err)
}
cs.buildIndex()
return cs.rr.Init()
}

func (cs *contractStore) Close() {
cs.rr.Close()
}

func (cs *contractStore) migrateLegacyContract(address, fileName string, createdTime time.Time) {
swaggerFile, err := os.OpenFile(fileName, os.O_RDONLY, 0)
if err != nil {
Expand Down Expand Up @@ -404,6 +414,10 @@ func (cs *contractStore) AddABI(id string, deployMsg *messages.DeployContract, c
return info
}

func (cs *contractStore) AddRemoteInstance(lookupStr, address string) error {
return cs.rr.RegisterInstance(lookupStr, address)
}

func (cs *contractStore) ListContracts() []messages.TimeSortable {
cs.idxLock.Lock()
retval := make([]messages.TimeSortable, 0, len(cs.contractIndex))
Expand Down
Loading

0 comments on commit f0cd139

Please sign in to comment.