Skip to content

Instantly share code, notes, and snippets.

@zelig
Last active November 29, 2015 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zelig/0140294ffbb64d28a683 to your computer and use it in GitHub Desktop.
Save zelig/0140294ffbb64d28a683 to your computer and use it in GitHub Desktop.
node stack, rpc/ipc/js interfaces and module Api registration

@karalabe @bas-vk @fjl guys I was thinking of the ipcjson integration in the new stack design. SO the idea is that we should make ipc interfaces (http, js, etc) services now that they do not depend on other packages. These services would be registered just like others (in wrapper). Then other services when they are initialised would look if they find an ipcapi capable service and register their API module(s) (more than one, e.g., ethereum will insert at least eth and miner, but probably also natspec, registrar, etc)

// in ipc package
type Api interface {
  // whatever we need to describe an Api
  Autocomplete()
  Methods()
  Name()
  Version()
}

type Interface interface {
  Register(Api) error
}

// in the node wrapper 
var cool *CoolService
node.Register(&cool, func (ctx node.ServiceContext) (Service, error) {
  return 
})

// in cool package
// CoolService implements node.Service and rpc.Api interface
type CoolService struct {}

func NewCool(ctx node.ServiceContext) (*CoolService, error) {
self := &CoolService{}
  console := ctx.Service(&Ipc.Console{}).(*Ipc.Console)
  console.Register(self)
}

This is the most flexible one, allows services to decide that they register an api only on console, not remote comms (for instance node admin api or apis with file system access). But it also assumes that ipc.Interface services are registered first.

Alternatively and additionally it could also be declarative like Protocols:

// Apis implements node.Service
func (self *CoolService) Apis() []ipc.Api {
  return []ipc.Api{ipc.Api(self), ipc.Api(anotherApi)}
}

and node.Node#Register would Register all these Apis with all ExternalInterface services it finds. Further fine tuning with this declarative patter would be that the ipc.Api implementations specify a list of allowed dinterfaces

// Interfaces implements ipc.Api
func (self *CoolService) Interfaces() []ipc.InterfaceType {
  return []ipc.InterfaceType{ipc.Console, ipc.HttpRpc, }
}

Then node.Node#Start would first initialise services, then iterate over ipc.Interface services and Register each Api of each service as long as the allowed interface type matches.

i think this wuold be very cool. BTW, I deliberately did not look at your code Bas, cos wanted to test if we are organicallly on the same page ;)

oh, and the whole package should be called interfaces not rpc. rpc/console etc are interface types that abstracted out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment