Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truth container now accepts a timestamp instead of block height #111

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ These below are excerpts of the configuration (with some parts omitted for brevi
"lossFunctionEntrypointName": "apiAdapter",
"minStake": "100000000",
"groundTruthParameters": {
"GroundTruthEndpoint": "http://localhost:8888/gt/{Token}/{BlockHeight}",
"GroundTruthEndpoint": "http://localhost:8888/gt/{Token}/{BlockTime}",
"Token": "ETHUSD"
},
"lossFunctionParameters": {
Expand Down Expand Up @@ -290,7 +290,7 @@ These below are excerpts of the configuration (with some parts omitted for brevi
"lossFunctionEntrypointName": "apiAdapter",
"minStake": "100000000",
"groundTruthParameters": {
"GroundTruthEndpoint": "http://localhost:8888/gt/{Token}/{BlockHeight}",
"GroundTruthEndpoint": "http://localhost:8888/gt/{Token}/{BlockTime}",
"Token": "ETHUSD"
},
"lossFunctionParameters": {
Expand Down
2 changes: 1 addition & 1 deletion adapter/api/apiadapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Reputer: []lib.ReputerConfig{
"loopSeconds": 30,
"minStake": 100000,
"groundTruthParameters": {
"GroundTruthEndpoint": "http://localhost:8888/gt/{Token}/{BlockHeight}",
"GroundTruthEndpoint": "http://localhost:8888/gt/{Token}/{BlockTime}",
"Token": "ETHUSD"
},
"lossFunctionParameters": {
Expand Down
18 changes: 16 additions & 2 deletions adapter/api/apiadapter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"strconv"
"strings"
"time"

alloraMath "github.com/allora-network/allora-chain/math"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -44,6 +45,19 @@ func replaceExtendedPlaceholders(urlTemplate string, params map[string]string, b
return urlTemplate
}

func replaceExtendedPlaceholdersWithTime(urlTemplate string, params map[string]string, blockTime time.Time, topicId uint64) string {
blockTimeUnix := blockTime.Unix()
blockTimeAsString := strconv.FormatInt(blockTimeUnix, 10)
topicIdAsString := strconv.FormatUint(topicId, 10)
defaultParams := map[string]string{
"BlockTime": blockTimeAsString,
"TopicId": topicIdAsString,
}
urlTemplate = replacePlaceholders(urlTemplate, defaultParams)
urlTemplate = replacePlaceholders(urlTemplate, params)
return urlTemplate
}

func requestEndpoint(url string) (string, error) {
// make request to url
resp, err := http.Get(url) // nolint: gosec
Expand Down Expand Up @@ -130,11 +144,11 @@ func (a *AlloraAdapter) CalcForecast(node lib.WorkerConfig, blockHeight int64) (
return nodeValues, nil
}

func (a *AlloraAdapter) GroundTruth(node lib.ReputerConfig, blockHeight int64) (lib.Truth, error) {
func (a *AlloraAdapter) GroundTruth(node lib.ReputerConfig, blockTime time.Time) (lib.Truth, error) {
log := log.With().Str("actorType", "reputer").Uint64("topicId", node.TopicId).Logger()

urlTemplate := node.GroundTruthParameters["GroundTruthEndpoint"]
url := replaceExtendedPlaceholders(urlTemplate, node.GroundTruthParameters, blockHeight, node.TopicId)
url := replaceExtendedPlaceholdersWithTime(urlTemplate, node.GroundTruthParameters, blockTime, node.TopicId)
log.Debug().Str("url", url).Msg("Ground truth endpoint")
groundTruth, err := requestEndpoint(url)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions adapter/api/source/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def get_forecast():
return jsonify([nv.__dict__ for nv in node_values])


@app.route('/truth/<token>/<blockheight>', methods=['GET'])
def get_truth(token, blockheight):
@app.route('/truth/<token>/<blockTime>', methods=['GET'])
def get_truth(token, blockTime):
random_float = str(random.uniform(0.0, 100.0))
return random_float

Expand Down
2 changes: 1 addition & 1 deletion config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"lossFunctionEntrypointName": "apiAdapter",
"minStake": 100000000,
"groundTruthParameters": {
"GroundTruthEndpoint": "http://localhost:8888/gt/{Token}/{BlockHeight}",
"GroundTruthEndpoint": "http://localhost:8888/gt/{Token}/{BlockTime}",
"Token": "ETHUSD"
},
"lossFunctionParameters": {
Expand Down
4 changes: 3 additions & 1 deletion lib/domain_entrypoint.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package lib

import "time"

type Truth = string

type AlloraAdapter interface {
Name() string
CalcInference(WorkerConfig, int64) (string, error)
CalcForecast(WorkerConfig, int64) ([]NodeValue, error)
GroundTruth(ReputerConfig, int64) (Truth, error)
GroundTruth(ReputerConfig, time.Time) (Truth, error)
LossFunction(ReputerConfig, string, string, map[string]string) (string, error)
IsLossFunctionNeverNegative(ReputerConfig, map[string]string) (bool, error)
CanInfer() bool
Expand Down
9 changes: 9 additions & 0 deletions lib/repo_query_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lib

import (
"context"
"time"

emissionstypes "github.com/allora-network/allora-chain/x/emissions/types"
"github.com/cosmos/cosmos-sdk/types/query"
Expand All @@ -28,3 +29,11 @@ func (node *NodeConfig) GetReputerValuesAtBlock(ctx context.Context, topicId emi

return resp.NetworkInferences, nil
}

func (node *NodeConfig) GetBlockTime(ctx context.Context, nonce BlockHeight) (time.Time, error) {
header, err := node.Chain.Client.RPC.Header(ctx, &nonce)
if err != nil {
return time.Time{}, err
}
return header.Header.Time, nil
}
15 changes: 14 additions & 1 deletion usecase/build_commit_reputer_payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/json"
"errors"
"fmt"
"time"

errorsmod "cosmossdk.io/errors"
alloraMath "github.com/allora-network/allora-chain/math"
Expand Down Expand Up @@ -38,7 +39,19 @@ func (suite *UseCaseSuite) BuildCommitReputerPayload(ctx context.Context, repute
}
valueBundle.Reputer = suite.RPCManager.GetCurrentNode().Wallet.Address

sourceTruth, err := reputer.GroundTruthEntrypoint.GroundTruth(reputer, nonce)
blockTime, err := RunWithNodeRetry(
ctx,
suite.RPCManager,
func(node *lib.NodeConfig) (time.Time, error) {
return node.GetBlockTime(ctx, nonce)
},
"get block time",
)
if err != nil {
return errorsmod.Wrapf(err, "error getting block time, topicId: %d, blockHeight: %d", reputer.TopicId, nonce)
}

sourceTruth, err := reputer.GroundTruthEntrypoint.GroundTruth(reputer, blockTime)
if err != nil {
return errorsmod.Wrapf(err, "error getting source truth from reputer, topicId: %d, blockHeight: %d", reputer.TopicId, nonce)
}
Expand Down
5 changes: 3 additions & 2 deletions usecase/mock_allora_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package usecase

import (
"allora_offchain_node/lib"
"time"

"github.com/stretchr/testify/mock"
)
Expand All @@ -25,8 +26,8 @@ func (m *MockAlloraAdapter) CalcForecast(config lib.WorkerConfig, timestamp int6
return args.Get(0).([]lib.NodeValue), args.Error(1) // nolint: forcetypeassert
}

func (m *MockAlloraAdapter) GroundTruth(config lib.ReputerConfig, timestamp int64) (lib.Truth, error) {
args := m.Called(config, timestamp)
func (m *MockAlloraAdapter) GroundTruth(config lib.ReputerConfig, blockTime time.Time) (lib.Truth, error) {
args := m.Called(config, blockTime)
return args.Get(0).(lib.Truth), args.Error(1) // nolint: forcetypeassert
}

Expand Down