Skip to content

Commit

Permalink
slight refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
atvanguard committed Mar 8, 2024
1 parent 5f9200b commit e7e8da5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 42 deletions.
1 change: 1 addition & 0 deletions plugin/evm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ type Config struct {
// Testing apis enabled
TestingApiEnabled bool `json:"testing-api-enabled"`

// NodeType is the type of node among the following: "tresor", "kitkat", "berghain", meaning validator only, matching engine, rpc node respectively
NodeType string `json:"node-type"`

// LoadFromSnapshotEnabled = true if the node should load the memory db from a snapshot
Expand Down
28 changes: 24 additions & 4 deletions plugin/evm/limit_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os"
"runtime"
"runtime/debug"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -76,17 +77,28 @@ type limitOrderProcesser struct {
tradingAPI *orderbook.TradingAPI
}

func NewLimitOrderProcesser(ctx *snow.Context, txPool *txpool.TxPool, shutdownChan <-chan struct{}, shutdownWg *sync.WaitGroup, backend *eth.EthAPIBackend, blockChain *core.BlockChain, hubbleDB database.Database, validatorPrivateKey string, config Config) (LimitOrderProcesser, error) {
func NewLimitOrderProcesser(ctx *snow.Context, txPool *txpool.TxPool, shutdownChan <-chan struct{}, shutdownWg *sync.WaitGroup, backend *eth.EthAPIBackend, blockChain *core.BlockChain, hubbleDB database.Database, config Config) (LimitOrderProcesser, error) {
log.Info("**** NewLimitOrderProcesser")

configService := orderbook.NewConfigService(blockChain)
memoryDb := orderbook.NewInMemoryDatabase(configService)

nodeType, err := stringToNodeType(config.NodeType)
if err != nil {
return nil, err
}

configService := orderbook.NewConfigService(blockChain)
memoryDb := orderbook.NewInMemoryDatabase(configService)
var validatorPrivateKey string
if nodeType == Kitkat || nodeType == Kitkat_Berghain {
validatorPrivateKey, err = loadPrivateKeyFromFile(config.ValidatorPrivateKeyFile)
if err != nil {
panic(fmt.Sprint("please specify correct path for validator-private-key-file in chain.json ", err))
}
if validatorPrivateKey == "" {
panic("validator private key is empty")
}
}
lotp := orderbook.NewLimitOrderTxProcessor(txPool, memoryDb, backend, validatorPrivateKey)

signedObAddy := configService.GetSignedOrderbookContract()
contractEventProcessor := orderbook.NewContractEventsProcessor(memoryDb, signedObAddy)

Expand Down Expand Up @@ -134,6 +146,14 @@ func NewLimitOrderProcesser(ctx *snow.Context, txPool *txpool.TxPool, shutdownCh
}, nil
}

func loadPrivateKeyFromFile(keyFile string) (string, error) {
key, err := os.ReadFile(keyFile)
if err != nil {
return "", err
}
return strings.TrimSuffix(string(key), "\n"), nil
}

func stringToNodeType(nodeTypeString string) (NodeType, error) {
switch nodeTypeString {
case string(Tresor):
Expand Down
48 changes: 10 additions & 38 deletions plugin/evm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,16 @@ func (vm *VM) initializeChain(lastAcceptedHash common.Hash, ethConfig ethconfig.
vm.blockChain = vm.eth.BlockChain()
vm.miner = vm.eth.Miner()

vm.limitOrderProcesser, err = vm.NewLimitOrderProcesser()
vm.limitOrderProcesser, err = NewLimitOrderProcesser(
vm.ctx,
vm.txPool,
vm.shutdownChan,
&vm.shutdownWg,
vm.eth.APIBackend,
vm.blockChain,
vm.hubbleDB,
vm.config,
)
if err != nil {
return err
}
Expand Down Expand Up @@ -1170,40 +1179,3 @@ func attachEthService(handler *rpc.Server, apis []rpc.API, names []string) error

return nil
}

func (vm *VM) NewLimitOrderProcesser() (LimitOrderProcesser, error) {
nodeType, err := stringToNodeType(vm.config.NodeType)
if err != nil {
return nil, err
}

var validatorPrivateKey string
if nodeType == Kitkat || nodeType == Kitkat_Berghain {
validatorPrivateKey, err = loadPrivateKeyFromFile(vm.config.ValidatorPrivateKeyFile)
if err != nil {
panic(fmt.Sprint("please specify correct path for validator-private-key-file in chain.json ", err))
}
if validatorPrivateKey == "" {
panic("validator private key is empty")
}
}
return NewLimitOrderProcesser(
vm.ctx,
vm.txPool,
vm.shutdownChan,
&vm.shutdownWg,
vm.eth.APIBackend,
vm.blockChain,
vm.hubbleDB,
validatorPrivateKey,
vm.config,
)
}

func loadPrivateKeyFromFile(keyFile string) (string, error) {
key, err := os.ReadFile(keyFile)
if err != nil {
return "", err
}
return strings.TrimSuffix(string(key), "\n"), nil
}

0 comments on commit e7e8da5

Please sign in to comment.