Easy oracle deployment in Go

Yesterday during the haeckathon in Sofia I started to work on a library for the Go programming language for easy building of aeternity oracles.

The idea is to launch aeternity oracles in an idiomatic way fir Go, which is the pattern of defining/registering handlers followed by listening/serving. This way if one can write an http server in Go, one can also write an aeternity oracle without worrying about any complexity.

So with this library oracles can be created like this:

func helloQueryHandler(r *goraecle.Request) (string, error) {
 name, ok := r.Args["name"] // Get name or set to "stranger" if not provided
 if !ok {
  name = "stranger"
 }

 // Error handling
 if ok && strings.Contains(strings.ToLower(name.(string)), "vitalik") {
  return "", fmt.Errorf("Go away!")
 }

 // Return an answer
 return fmt.Sprintf("Hello, %s", name), nil
}

func main() {
 goraecle.HandleFunc("hello", helloQueryHandler) // Register handler for hello query

 // Start the oracle
 if err := goraecle.ListenAndServe(ownerPrivateKey, "ae_uat", "http://sdk-testnet.aepps.com"); err != nil {
  log.Fatalf("Failed to start oracle: %v", err)
 }
}

I’ll tidy up my code and push it to Github later.

Just to elaborate a bit: In this particular example queries are handled by the default raw query handler and formatted in the following way: “hello:name=arjan,profession=developer”. However, the data format can easily be changed by writing a customized RawQueryHandler. Also muxers can be customized. And anything can be an oracle handler by implementing the Handler interface, for example:

type ComplexObject struct {
  number int
}

func (o ComplexObject) ServeOracle(r *goraecle.Request) (string, error) {
  return fmt.Sprintf("The number is: %d", o.number), nil
}

Registering such a handler is again similar to other Go servers:

complex := ComplexObject{number: 42}
goraecle.Handle("complex", complex)

Please let me know what you think of the idea?

I’d like to continue this project and either contribute it as a pull request to the Go SDK (if it would fit in the SDK) or launch it as an open sourced library if it’s too niche for the SDK. So I’d also like to ask people to tell me any desired features for a library like this.

2 Likes

Thank you @arjan.chain for your participation :slight_smile: I’ll add your idea to the collection of hackathon projects.

Best,
Albena