forked from cerc-io/ipld-eth-server
Rename handlers to transformers
This commit is contained in:
parent
a11fb709b2
commit
02b23ef748
@ -1,21 +1,21 @@
|
||||
# Handlers
|
||||
# Transformers
|
||||
|
||||
## Description
|
||||
Handlers must be defined in order to define what events should trigger data updates and how those are performed.
|
||||
Transformers must be defined in order to define what events should trigger data updates and how those are performed.
|
||||
|
||||
## Interface
|
||||
|
||||
### Initializer
|
||||
Accepts DB and Blockchain from Vulcanize and returns a new handler. E.g. for a new object "Cup":
|
||||
`func NewCupHandler(db *postgres.DB, blockchain core.ContractDataFetcher) handlers.Handler`
|
||||
Accepts DB and Blockchain from Vulcanize and returns a new transformer. E.g. for a new object "Cup":
|
||||
`func NewCupTransformer(db *postgres.DB, blockchain core.ContractDataFetcher) transformers.Transformer`
|
||||
|
||||
### Execute
|
||||
Triggers operations to take in response to a given log event.
|
||||
Can persist data from logs, fetch and persist arbitrary data from outside services (e.g. contract state), or take any number of other actions. E.g.:
|
||||
`func (cupHandler *CupHandler) Execute() error`
|
||||
`func (cupTransformer *CupTransformer) Execute() error`
|
||||
|
||||
## Additional Requirements
|
||||
Handlers must define log filters and create them so that relevant watched events can be identified and retrieved. E.g.:
|
||||
Transformers must define log filters and create them so that relevant watched events can be identified and retrieved. E.g.:
|
||||
```$xslt
|
||||
{
|
||||
Name: "CupsBite",
|
@ -6,15 +6,15 @@ import (
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
)
|
||||
|
||||
type Handler interface {
|
||||
type Transformer interface {
|
||||
Execute() error
|
||||
}
|
||||
|
||||
type HandlerInitializer func(db *postgres.DB, blockchain core.Blockchain) Handler
|
||||
type TransformerInitializer func(db *postgres.DB, blockchain core.Blockchain) Transformer
|
||||
|
||||
func HexToInt64(byteString string) int64 {
|
||||
intHash := common.HexToHash(byteString)
|
||||
return intHash.Big().Int64()
|
||||
value := common.HexToHash(byteString)
|
||||
return value.Big().Int64()
|
||||
}
|
||||
|
||||
func HexToString(byteString string) string {
|
@ -6,22 +6,22 @@ import (
|
||||
)
|
||||
|
||||
type Watcher struct {
|
||||
Handlers []Handler
|
||||
Transformers []Transformer
|
||||
DB postgres.DB
|
||||
Blockchain core.Blockchain
|
||||
}
|
||||
|
||||
func (watcher *Watcher) AddHandlers(us []HandlerInitializer) {
|
||||
for _, handlerInitializer := range us {
|
||||
handler := handlerInitializer(&watcher.DB, watcher.Blockchain)
|
||||
watcher.Handlers = append(watcher.Handlers, handler)
|
||||
func (watcher *Watcher) AddTransformers(us []TransformerInitializer) {
|
||||
for _, transformerInitializer := range us {
|
||||
transformer := transformerInitializer(&watcher.DB, watcher.Blockchain)
|
||||
watcher.Transformers = append(watcher.Transformers, transformer)
|
||||
}
|
||||
}
|
||||
|
||||
func (watcher *Watcher) Execute() error {
|
||||
var err error
|
||||
for _, handler := range watcher.Handlers {
|
||||
err = handler.Execute()
|
||||
for _, transformer := range watcher.Transformers {
|
||||
err = transformer.Execute()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
@ -10,12 +10,12 @@ import (
|
||||
"github.com/vulcanize/vulcanizedb/pkg/datastore/postgres"
|
||||
)
|
||||
|
||||
type MockHandler struct {
|
||||
type MockTransformer struct {
|
||||
executeWasCalled bool
|
||||
executeError error
|
||||
}
|
||||
|
||||
func (mh *MockHandler) Execute() error {
|
||||
func (mh *MockTransformer) Execute() error {
|
||||
if mh.executeError != nil {
|
||||
return mh.executeError
|
||||
}
|
||||
@ -23,47 +23,47 @@ func (mh *MockHandler) Execute() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func fakeHandlerInitializer(db *postgres.DB, blockchain core.Blockchain) shared.Handler {
|
||||
return &MockHandler{}
|
||||
func fakeTransformerInitializer(db *postgres.DB, blockchain core.Blockchain) shared.Transformer {
|
||||
return &MockTransformer{}
|
||||
}
|
||||
|
||||
var _ = Describe("Watcher", func() {
|
||||
It("Adds handlers", func() {
|
||||
It("Adds transformers", func() {
|
||||
watcher := shared.Watcher{}
|
||||
|
||||
watcher.AddHandlers([]shared.HandlerInitializer{fakeHandlerInitializer})
|
||||
watcher.AddTransformers([]shared.TransformerInitializer{fakeTransformerInitializer})
|
||||
|
||||
Expect(len(watcher.Handlers)).To(Equal(1))
|
||||
Expect(watcher.Handlers).To(ConsistOf(&MockHandler{}))
|
||||
Expect(len(watcher.Transformers)).To(Equal(1))
|
||||
Expect(watcher.Transformers).To(ConsistOf(&MockTransformer{}))
|
||||
})
|
||||
|
||||
It("Adds handlers from multiple sources", func() {
|
||||
It("Adds transformers from multiple sources", func() {
|
||||
watcher := shared.Watcher{}
|
||||
|
||||
watcher.AddHandlers([]shared.HandlerInitializer{fakeHandlerInitializer})
|
||||
watcher.AddHandlers([]shared.HandlerInitializer{fakeHandlerInitializer})
|
||||
watcher.AddTransformers([]shared.TransformerInitializer{fakeTransformerInitializer})
|
||||
watcher.AddTransformers([]shared.TransformerInitializer{fakeTransformerInitializer})
|
||||
|
||||
Expect(len(watcher.Handlers)).To(Equal(2))
|
||||
Expect(len(watcher.Transformers)).To(Equal(2))
|
||||
})
|
||||
|
||||
It("Executes each handler", func() {
|
||||
It("Executes each transformer", func() {
|
||||
watcher := shared.Watcher{}
|
||||
fakeHandler := &MockHandler{}
|
||||
watcher.Handlers = []shared.Handler{fakeHandler}
|
||||
fakeTransformer := &MockTransformer{}
|
||||
watcher.Transformers = []shared.Transformer{fakeTransformer}
|
||||
|
||||
watcher.Execute()
|
||||
|
||||
Expect(fakeHandler.executeWasCalled).To(BeTrue())
|
||||
Expect(fakeTransformer.executeWasCalled).To(BeTrue())
|
||||
})
|
||||
|
||||
It("Returns an error if handler returns an error", func() {
|
||||
It("Returns an error if transformer returns an error", func() {
|
||||
watcher := shared.Watcher{}
|
||||
fakeHandler := &MockHandler{executeError: errors.New("Something bad happened")}
|
||||
watcher.Handlers = []shared.Handler{fakeHandler}
|
||||
fakeTransformer := &MockTransformer{executeError: errors.New("Something bad happened")}
|
||||
watcher.Transformers = []shared.Transformer{fakeTransformer}
|
||||
|
||||
err := watcher.Execute()
|
||||
|
||||
Expect(err).To(HaveOccurred())
|
||||
Expect(fakeHandler.executeWasCalled).To(BeFalse())
|
||||
Expect(fakeTransformer.executeWasCalled).To(BeFalse())
|
||||
})
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user