Skip to content

Commit

Permalink
add kvasir service
Browse files Browse the repository at this point in the history
  • Loading branch information
slandymani committed Sep 23, 2024
1 parent 19452f4 commit 27541e9
Show file tree
Hide file tree
Showing 15 changed files with 1,507 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/kvasir/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"github.com/ODIN-PROTOCOL/odin-core/kvasir"
)

func main() {
kvasir.Main()
}
20 changes: 20 additions & 0 deletions kvasir/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package kvasir

import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func configCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "config [key] [value]",
Aliases: []string{"c"},
Short: "Set kvasir configuration environment",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
viper.Set(args[0], args[1])
return viper.WriteConfig()
},
}
return cmd
}
98 changes: 98 additions & 0 deletions kvasir/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package kvasir

import (
"sync/atomic"
"time"

"github.com/ODIN-PROTOCOL/odin-core/kvasir/executor"
wasmtypes "github.com/ODIN-PROTOCOL/wasmd/x/wasm/types"
rpcclient "github.com/cometbft/cometbft/rpc/client"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"google.golang.org/grpc"
)

type Request struct {
ChosenValidators []string `json:"chosen_validators"`
Metadata string `json:"metadata"`
PartsReceived uint32 `json:"parts_received"`
PartsRequested uint32 `json:"parts_requested"`
Payed sdk.Coins `json:"payed"`
RequestHeight uint64 `json:"request_Height"`
RequestID uint64 `json:"request_id"`
Sender string `json:"sender"`
Status string `json:"status"`
}

type RequestKey struct {
ContractAddress string
RequestID uint64
}

type ReportMsgWithKey struct {
result []byte
contractAddress string
msg *wasmtypes.RawContractMessage
execVersion string
keyIndex int64
requestID uint64
}

type Context struct {
client rpcclient.Client
validator sdk.ValAddress
validatorAccAddr sdk.AccAddress
gasPrices string
keys []*keyring.Record
executor executor.Executor
broadcastTimeout time.Duration
maxTry uint64
rpcPollInterval time.Duration
maxReport uint64
ipfs string
grpc *grpc.ClientConn

pendingMsgs chan ReportMsgWithKey
freeKeys chan int64
keyRoundRobinIndex int64 // Must use in conjunction with sync/atomic

pendingRequests map[RequestKey]bool

contracts []string

metricsEnabled bool
handlingGauge int64
pendingGauge int64
errorCount int64
submittedCount int64
home string
}

func (c *Context) nextKeyIndex() int64 {
keyIndex := atomic.AddInt64(&c.keyRoundRobinIndex, 1) % int64(len(c.keys))
return keyIndex
}

func (c *Context) updateHandlingGauge(amount int64) {
if c.metricsEnabled {
atomic.AddInt64(&c.handlingGauge, amount)
}
}

func (c *Context) updatePendingGauge(amount int64) {
if c.metricsEnabled {
atomic.AddInt64(&c.pendingGauge, amount)
}
}

func (c *Context) updateErrorCount(amount int64) {
if c.metricsEnabled {
atomic.AddInt64(&c.errorCount, amount)
}
}

func (c *Context) updateSubmittedCount(amount int64) {
if c.metricsEnabled {
atomic.AddInt64(&c.submittedCount, amount)
}
}
27 changes: 27 additions & 0 deletions kvasir/event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package kvasir

import (
abci "github.com/cometbft/cometbft/abci/types"
)

type rawRequest struct {
contract string
requestID uint64
calldata string
}

// GetEventValues returns the list of all values in the given log with the given type and key.
func GetEventValues(log []abci.Event, evType string, evKey string) (res []string) {
for _, ev := range log {
if ev.Type != evType {
continue
}

for _, attr := range ev.Attributes {
if attr.Key == evKey {
res = append(res, attr.Value)
}
}
}
return res
}
Loading

0 comments on commit 27541e9

Please sign in to comment.