diff --git a/node/pkg/reporter/types.go b/node/pkg/reporter/types.go index abf998887..0fd27081c 100644 --- a/node/pkg/reporter/types.go +++ b/node/pkg/reporter/types.go @@ -161,12 +161,3 @@ func makeGetLatestGlobalAggregatesQuery(configIds []int32) string { return q } - -func makeGetProofsQuery(aggregates []GlobalAggregate) string { - placeHolders := make([]string, len(aggregates)) - for i, agg := range aggregates { - placeHolders[i] = fmt.Sprintf("('%d', %d)", agg.ConfigID, agg.Round) - } - - return fmt.Sprintf("SELECT config_id, round, proof FROM proofs WHERE (config_id, round) IN (%s);", strings.Join(placeHolders, ",")) -} diff --git a/node/pkg/reporter/utils.go b/node/pkg/reporter/utils.go index a01c45ea7..5adb1abf6 100644 --- a/node/pkg/reporter/utils.go +++ b/node/pkg/reporter/utils.go @@ -1,14 +1,12 @@ package reporter import ( - "bytes" "context" "math" "math/big" "time" "bisonai.com/orakl/node/pkg/chain/helper" - chainUtils "bisonai.com/orakl/node/pkg/chain/utils" "bisonai.com/orakl/node/pkg/common/keys" "bisonai.com/orakl/node/pkg/db" errorSentinel "bisonai.com/orakl/node/pkg/error" @@ -61,31 +59,6 @@ func GetLastSubmission(ctx context.Context, submissionPairs map[int32]Submission return db.MGetObject[GlobalAggregate](ctx, keyList) } -func StoreLastSubmission(ctx context.Context, aggregates []GlobalAggregate) error { - vals := make(map[string]any) - - for _, agg := range aggregates { - if agg.ConfigID == 0 { - log.Error().Str("Player", "Reporter").Int32("ConfigID", agg.ConfigID).Msg("skipping invalid aggregate") - continue - } - vals[keys.LastSubmissionKey(agg.ConfigID)] = agg - } - - if len(vals) == 0 { - return errorSentinel.ErrReporterEmptyValidAggregates - } - return db.MSetObject(ctx, vals) -} - -func ProofsToMap(proofs []Proof) map[int32][]byte { - m := make(map[int32][]byte) - for _, proof := range proofs { - m[proof.ConfigID] = proof.Proof - } - return m -} - func MakeContractArgsWithProofs(aggregates []GlobalAggregate, submissionPairs map[int32]SubmissionPair) ([][32]byte, []*big.Int, []*big.Int, error) { if len(aggregates) == 0 { return nil, nil, nil, errorSentinel.ErrReporterEmptyAggregatesParam @@ -117,57 +90,6 @@ func MakeContractArgsWithProofs(aggregates []GlobalAggregate, submissionPairs ma return feedHash, values, timestamps, nil } -func FilterInvalidAggregates(aggregates []GlobalAggregate, submissionPairs map[int32]SubmissionPair) []GlobalAggregate { - validAggregates := make([]GlobalAggregate, 0, len(aggregates)) - for _, aggregate := range aggregates { - if IsAggValid(aggregate, submissionPairs) { - validAggregates = append(validAggregates, aggregate) - } - } - return validAggregates -} - -func IsAggValid(aggregate GlobalAggregate, submissionPairs map[int32]SubmissionPair) bool { - lastSubmission := submissionPairs[aggregate.ConfigID].LastSubmission - return lastSubmission == 0 || aggregate.Round > lastSubmission -} - -func GetProofs(ctx context.Context, aggregates []GlobalAggregate) ([]Proof, error) { - result, err := GetProofsRdb(ctx, aggregates) - if err != nil { - log.Warn().Str("Player", "Reporter").Err(err).Msg("getProofsRdb failed, trying to get from pgsql") - return GetProofsPgsql(ctx, aggregates) - } - return result, nil -} - -func GetProofsRdb(ctx context.Context, aggregates []GlobalAggregate) ([]Proof, error) { - keyList := []string{} - for _, agg := range aggregates { - keyList = append(keyList, keys.ProofKey(agg.ConfigID, agg.Round)) - } - return db.MGetObject[Proof](ctx, keyList) -} - -func GetProofsPgsql(ctx context.Context, aggregates []GlobalAggregate) ([]Proof, error) { - q := makeGetProofsQuery(aggregates) - return db.QueryRows[Proof](ctx, q, nil) -} - -func GetProofsAsMap(ctx context.Context, aggregates []GlobalAggregate) (map[int32][]byte, error) { - proofs, err := GetProofs(ctx, aggregates) - if err != nil { - log.Error().Str("Player", "Reporter").Err(err).Msg("submit without proofs") - return nil, err - } - - if len(proofs) < len(aggregates) { - log.Error().Str("Player", "Reporter").Msg("proofs not found for all aggregates") - return nil, errorSentinel.ErrReporterMissingProof - } - return ProofsToMap(proofs), nil -} - func GetLatestGlobalAggregates(ctx context.Context, submissionPairs map[int32]SubmissionPair) ([]GlobalAggregate, error) { result, err := GetLatestGlobalAggregatesRdb(ctx, submissionPairs) if err != nil { @@ -197,15 +119,6 @@ func GetLatestGlobalAggregatesRdb(ctx context.Context, submissionPairs map[int32 return db.MGetObject[GlobalAggregate](ctx, keyList) } -func ValidateAggregateTimestampValues(aggregates []GlobalAggregate) bool { - for _, agg := range aggregates { - if agg.Timestamp.IsZero() { - return false - } - } - return true -} - func ReadOnchainWhitelist(ctx context.Context, chainHelper *helper.ChainHelper, contractAddress string, contractFunction string) ([]common.Address, error) { result, err := chainHelper.ReadContract(ctx, contractAddress, contractFunction) if err != nil { @@ -227,116 +140,6 @@ func ReadOnchainWhitelist(ctx context.Context, chainHelper *helper.ChainHelper, return arr, nil } -func CheckForNonWhitelistedSigners(signers []common.Address, whitelist []common.Address) error { - for _, signer := range signers { - if !isWhitelisted(signer, whitelist) { - log.Error().Str("Player", "Reporter").Str("signer", signer.Hex()).Msg("non-whitelisted signer") - return errorSentinel.ErrReporterSignerNotWhitelisted - } - } - return nil -} - -func isWhitelisted(signer common.Address, whitelist []common.Address) bool { - for _, w := range whitelist { - if w == signer { - return true - } - } - return false -} - -func GetSignerMap(signers []common.Address, proofChunks [][]byte) map[common.Address][]byte { - signerMap := make(map[common.Address][]byte) - for i, signer := range signers { - signerMap[signer] = proofChunks[i] - } - return signerMap -} - -func GetSignerListFromProofs(hash []byte, proofChunks [][]byte) ([]common.Address, error) { - signers := []common.Address{} - for _, p := range proofChunks { - signer, err := chainUtils.RecoverSigner(hash, p) - if err != nil { - return nil, err - } - signers = append(signers, signer) - } - - return signers, nil -} - -func SplitProofToChunk(proof []byte) ([][]byte, error) { - if len(proof) == 0 { - return nil, errorSentinel.ErrReporterEmptyProofParam - } - - if len(proof)%65 != 0 { - return nil, errorSentinel.ErrReporterInvalidProofLength - } - - proofs := make([][]byte, 0, len(proof)/65) - for i := 0; i < len(proof); i += 65 { - proofs = append(proofs, proof[i:i+65]) - } - - return proofs, nil -} - -func RemoveDuplicateProof(proof []byte) []byte { - proofs, err := SplitProofToChunk(proof) - if err != nil { - return []byte{} - } - - uniqueProofs := make(map[string][]byte) - for _, p := range proofs { - uniqueProofs[string(p)] = p - } - - result := make([][]byte, 0, len(uniqueProofs)*65) - for _, p := range uniqueProofs { - result = append(result, p) - } - - return bytes.Join(result, nil) -} - -func UpsertProofs(ctx context.Context, aggregates []GlobalAggregate, proofMap map[int32][]byte) error { - upsertRows := make([][]any, 0, len(aggregates)) - for _, agg := range aggregates { - proof, ok := proofMap[agg.ConfigID] - if !ok { - continue - } - upsertRows = append(upsertRows, []any{agg.ConfigID, agg.Round, proof}) - } - - err := db.BulkUpsert(ctx, "proofs", []string{"config_id", "round", "proof"}, upsertRows, []string{"config_id", "round"}, []string{"proof"}) - if err != nil { - log.Error().Str("Player", "Reporter").Err(err).Msg("failed to upsert proofs") - } - return err -} - -func UpdateProofs(ctx context.Context, aggregates []GlobalAggregate, proofMap map[int32][]byte) error { - rows := make([][]any, 0, len(aggregates)) - for _, agg := range aggregates { - proof, ok := proofMap[agg.ConfigID] - if !ok { - continue - } - rows = append(rows, []any{proof, agg.ConfigID, agg.Round}) - } - - err := db.BulkUpdate(ctx, "proofs", []string{"proof"}, rows, []string{"config_id", "round"}) - if err != nil { - log.Error().Str("Player", "Reporter").Err(err).Msg("failed to update proofs") - } - return err -} - func GetDeviationThreshold(submissionInterval time.Duration) float64 { if submissionInterval <= 15*time.Second { return MIN_DEVIATION_THRESHOLD