Skip to content

Commit

Permalink
refactor(x/tally): remove reveal data length limit and outdated std d…
Browse files Browse the repository at this point in the history
…ev filters in favor of new ones
  • Loading branch information
hacheigriega committed Jan 27, 2025
1 parent 8320c5e commit ea5d1eb
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 418 deletions.
2 changes: 0 additions & 2 deletions proto/sedachain/tally/v1/tally.proto
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,4 @@ message Params {
];
// MaxResultSize is the maximum size of the result of a data request in bytes.
uint32 max_result_size = 8;
// MaxRevealDataLength is the maximum length of a reveal data.
uint32 max_reveal_data_length = 9;
}
4 changes: 2 additions & 2 deletions x/tally/keeper/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ func BuildFilter(filterInput string, replicationFactor uint16, params types.Para
case filterTypeNone:
filter, err = types.NewFilterNone(params.FilterGasCostNone, gasMeter)
case filterTypeMode:
filter, err = types.NewFilterMode(input, params.FilterGasCostMultiplierMode, replicationFactor, int(params.MaxRevealDataLength), gasMeter)
filter, err = types.NewFilterMode(input, params.FilterGasCostMultiplierMode, replicationFactor, gasMeter)
case filterTypeStdDev:
filter, err = types.NewFilterStdDev(input, params.FilterGasCostMultiplierStdDev, replicationFactor, int(params.MaxRevealDataLength), gasMeter)
filter, err = types.NewFilterStdDev(input, params.FilterGasCostMultiplierStdDev, replicationFactor, gasMeter)
default:
return nil, types.ErrInvalidFilterType
}
Expand Down
162 changes: 66 additions & 96 deletions x/tally/keeper/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,33 +492,33 @@ func TestFilter(t *testing.T) {
wantErr: types.ErrNoConsensus,
},
{
name: "Standard deviation filter uint64",
tallyInputAsHex: "02000000000016E36003000000000000000D242E726573756C742E74657874", // max_sigma = 1.5, number_type = uint64, json_path = $.result.text
outliers: []bool{true, false, false, false, false, true},
name: "Standard deviation filter 32-bit integer",
tallyInputAsHex: "02000000000016E36000000000000000000D242E726573756C742E74657874", // sigma_multiplier = 1.5, number_type = 0x00, json_path = $.result.text
outliers: make([]bool, 6),
reveals: []types.RevealBody{
{Reveal: `{"result": {"text": 4, "number": 0}}`},
{Reveal: `{"result": {"text": 5, "number": 10}}`},
{Reveal: `{"result": {"text": 6, "number": 101}}`},
{Reveal: `{"result": {"text": 7, "number": 0}}`},
{Reveal: `{"result": {"text": 8, "number": 0}}`},
{Reveal: `{"result": {"text": 9, "number": 0}}`},
{Reveal: `{"result": {"text": 4}}`},
{Reveal: `{"result": {"text": 5}}`},
{Reveal: `{"result": {"text": 6}}`},
{Reveal: `{"result": {"text": 7}}`},
{Reveal: `{"result": {"text": 8}}`},
{Reveal: `{"result": {"text": 9}}`},
},
consensus: true,
consensus: false,
consPubKeys: nil,
tallyGasUsed: defaultParams.GasCostBase + defaultParams.FilterGasCostMultiplierStdDev*6,
wantErr: nil,
wantErr: types.ErrNoConsensus,
},
{
name: "Standard deviation filter int64",
tallyInputAsHex: "02000000000016E36001000000000000000D242E726573756C742E74657874", // max_sigma = 1.5, number_type = int64, json_path = $.result.text
outliers: []bool{true, false, false, false, false, true},
name: "Standard deviation filter 64-bit integer",
tallyInputAsHex: "0200000000001E848001000000000000000D242E726573756C742E74657874", // sigma_multiplier = 2.0, number_type = 0x01, json_path = $.result.text
outliers: []bool{false, false, false, false, false, true},
reveals: []types.RevealBody{
{Reveal: `{"result": {"text": 4, "number": 0}}`},
{Reveal: `{"result": {"text": 5, "number": 10}}`},
{Reveal: `{"result": {"text": 6, "number": 101}}`},
{Reveal: `{"result": {"text": 7, "number": 0}}`},
{Reveal: `{"result": {"text": 8, "number": 0}}`},
{Reveal: `{"result": {"text": 9, "number": 0}}`},
{Reveal: `{"result": {"text": 4}}`},
{Reveal: `{"result": {"text": 5}}`},
{Reveal: `{"result": {"text": 6}}`},
{Reveal: `{"result": {"text": 7}}`},
{Reveal: `{"result": {"text": 8}}`},
{Reveal: `{"result": {"text": 9}}`},
},
consensus: true,
consPubKeys: nil,
Expand All @@ -527,7 +527,7 @@ func TestFilter(t *testing.T) {
},
{
name: "Standard deviation filter - Single reveal",
tallyInputAsHex: "02000000000016E36001000000000000000D242E726573756C742E74657874", // max_sigma = 1.5, number_type = uint64, json_path = $.result.text
tallyInputAsHex: "02000000000016E36001000000000000000D242E726573756C742E74657874", // sigma_multiplier = 1.5, number_type = 0x01, json_path = $.result.text
outliers: []bool{false},
reveals: []types.RevealBody{
{Reveal: `{"result": {"text": 4, "number": 0}}`},
Expand All @@ -538,61 +538,27 @@ func TestFilter(t *testing.T) {
wantErr: nil,
},
{
name: "Standard deviation filter - One corrupt reveal",
tallyInputAsHex: "02000000000016E36001000000000000000D242E726573756C742E74657874", // max_sigma = 1.5, number_type = uint64, json_path = $.result.text
outliers: make([]bool, 6),
reveals: []types.RevealBody{
{Reveal: `{"result": {"text": 4, "number": 0}}`},
{Reveal: `{"result": {"text": 5, "number": 10}}`},
{Reveal: `{"result": {"text": 6, "number": 101}}`},
{Reveal: `{"result": {"text": 7, "number": 0}}`},
{Reveal: `{"result": {"number": 0}}`}, // corrupt
{Reveal: `{"result": {"text": 9, "number": 0}}`},
},
consensus: false,
consPubKeys: nil,
tallyGasUsed: defaultParams.GasCostBase + defaultParams.FilterGasCostMultiplierStdDev*6,
wantErr: types.ErrNoConsensus,
},
{
name: "Standard deviation filter - Max sigma 1.55",
tallyInputAsHex: "02000000000017A6B003000000000000000D242E726573756C742E74657874", // max_sigma = 1.55, number_type = uint64, json_path = $.result.text
outliers: []bool{true, false, false, false, false, true},
reveals: []types.RevealBody{
{Reveal: `{"result": {"text": 4, "number": 0}}`},
{Reveal: `{"result": {"text": 5, "number": 10}}`},
{Reveal: `{"result": {"text": 6, "number": 101}}`},
{Reveal: `{"result": {"text": 7, "number": 0}}`},
{Reveal: `{"result": {"text": 8, "number": 0}}`},
{Reveal: `{"result": {"text": 9, "number": 0}}`},
name: "Standard deviation filter - 32-bit integer (One overflow)",
tallyInputAsHex: "0200000000001E848000000000000000000D242E726573756C742E74657874", // sigma_multiplier = 2.0, number_type = 0x00, json_path = $.result.text
outliers: []bool{false, false, false, false, false, true},
reveals: []types.RevealBody{ // mean = 5.5 -> 5, stddev = 1.29
{Reveal: `{"result": {"text": 4}}`},
{Reveal: `{"result": {"text": 5}}`},
{Reveal: `{"result": {"text": 6}}`},
{Reveal: `{"result": {"text": 7}}`},
{Reveal: `{"result": {"text": 6}}`},
{Reveal: `{"result": {"text": 4294967296}}`}, // overflow
},
consensus: true,
consPubKeys: nil,
tallyGasUsed: defaultParams.GasCostBase + defaultParams.FilterGasCostMultiplierStdDev*6,
wantErr: nil,
},
{
name: "Standard deviation filter - Max sigma 1.45",
tallyInputAsHex: "02000000000016201003000000000000000D242E726573756C742E74657874", // max_sigma = 1.45, number_type = uint64, json_path = $.result.text
outliers: make([]bool, 6),
reveals: []types.RevealBody{
{Reveal: `{"result": {"text": 4, "number": 0}}`},
{Reveal: `{"result": {"text": 5, "number": 10}}`},
{Reveal: `{"result": {"text": 6, "number": 101}}`},
{Reveal: `{"result": {"text": 7, "number": 0}}`},
{Reveal: `{"result": {"text": 8, "number": 0}}`},
{Reveal: `{"result": {"text": 9, "number": 0}}`},
},
consensus: false,
consPubKeys: nil,
tallyGasUsed: defaultParams.GasCostBase + defaultParams.FilterGasCostMultiplierStdDev*6,
wantErr: types.ErrNoConsensus,
},
{
name: "Standard deviation filter int64 with negative reveals",
tallyInputAsHex: "02000000000016E36001000000000000000D242E726573756C742E74657874", // max_sigma = 1.5, number_type = int64, json_path = $.result.text
outliers: []bool{true, false, false, false, false, true},
reveals: []types.RevealBody{
name: "Standard deviation filter - 32-bit integer",
tallyInputAsHex: "0200000000001E848001000000000000000D242E726573756C742E74657874", // sigma_multiplier = 2.0, number_type = int64, json_path = $.result.text
outliers: []bool{true, false, false, false, false, false},
reveals: []types.RevealBody{ // mean = 5, stddev = 1
{Reveal: `{"result": {"text": -4, "number": 0}}`},
{Reveal: `{"result": {"text": -5, "number": 10}}`},
{Reveal: `{"result": {"text": -6, "number": 101}}`},
Expand All @@ -606,25 +572,29 @@ func TestFilter(t *testing.T) {
wantErr: nil,
},
{
name: "Standard deviation filter int64 median -0.5",
tallyInputAsHex: "02000000000007A12001000000000000000D242E726573756C742E74657874", // max_sigma = 0.5, number_type = int64, json_path = $.result.text
outliers: make([]bool, 4),
reveals: []types.RevealBody{
{Reveal: `{"result": {"text": 1, "number": 0}}`},
{Reveal: `{"result": {"text": 0, "number": 0}}`},
{Reveal: `{"result": {"text": -1, "number": 10}}`},
{Reveal: `{"result": {"text": -2, "number": 10}}`},
name: "Standard deviation filter - 128-bit (One corrupt and one overflow)",
tallyInputAsHex: "0200000000002DC6C002000000000000000D242E726573756C742E74657874", // sigma_multiplier = 3, number_type = 0x02, json_path = $.result.text
outliers: []bool{false, true, false, false, false, true, false, false},
reveals: []types.RevealBody{ // mean = 416667, stddev = 75277
{Reveal: `{"result": {"text": 200000, "number": 0}}`},
{Reveal: `{"result": {"number": 700000, "number": 0}}`}, // corrupt
{Reveal: `{"result": {"text": 400000, "number": 10}}`},
{Reveal: `{"result": {"text": 400000, "number": 101}}`},
{Reveal: `{"result": {"text": 400000, "number": 0}}`},
{Reveal: `{"result": {"text": 340282366920938463463374607431768211456, "number": 0}}`}, // overflow
{Reveal: `{"result": {"text": 500000, "number": 0}}`},
{Reveal: `{"result": {"text": 500000, "number": 0}}`},
},
consensus: false,
consensus: true,
consPubKeys: nil,
tallyGasUsed: defaultParams.GasCostBase + defaultParams.FilterGasCostMultiplierStdDev*4,
wantErr: types.ErrNoConsensus,
tallyGasUsed: defaultParams.GasCostBase + defaultParams.FilterGasCostMultiplierStdDev*8,
wantErr: nil,
},
{
name: "Standard deviation filter - Big Int",
tallyInputAsHex: "02000000000016E36004000000000000000D242E726573756C742E74657874", // max_sigma = 1.5, number_type = uint64, json_path = $.result.text
outliers: []bool{true, false, false, false, false, false, false, true},
reveals: []types.RevealBody{
name: "Standard deviation filter - 256-bit",
tallyInputAsHex: "02000000000016E36003000000000000000D242E726573756C742E74657874", // sigma_multiplier = 1.5, number_type = 0x03, json_path = $.result.text
outliers: []bool{false, false, false, false, false, false, false, true},
reveals: []types.RevealBody{ // mean = 5, stddev = 2
{Reveal: `{"result": {"text": 2, "number": 0}}`},
{Reveal: `{"result": {"text": 4, "number": 10}}`},
{Reveal: `{"result": {"text": 4, "number": 101}}`},
Expand All @@ -640,8 +610,8 @@ func TestFilter(t *testing.T) {
wantErr: nil,
},
{
name: "Standard deviation filter - Big Int (Negative numbers)",
tallyInputAsHex: "0200000000000F424004000000000000000D242E726573756C742E74657874", // max_sigma = 1.0, number_type = uint64, json_path = $.result.text
name: "Standard deviation filter - 256-bit (Negative numbers)",
tallyInputAsHex: "0200000000000F424003000000000000000D242E726573756C742E74657874", // sigma_multiplier = 1.0, number_type = 0x03, json_path = $.result.text
outliers: []bool{false, false, true, false, false, false},
reveals: []types.RevealBody{
{Reveal: `{"result": {"text": -28930, "number": 0}}`},
Expand All @@ -657,8 +627,8 @@ func TestFilter(t *testing.T) {
wantErr: nil,
},
{
name: "Standard deviation filter - Big Int (Negative numbers (2))",
tallyInputAsHex: "0200000000000F424004000000000000000D242E726573756C742E74657874", // max_sigma = 1.0, number_type = uint64, json_path = $.result.text
name: "Standard deviation filter - 256-bit (Negative numbers (2))",
tallyInputAsHex: "0200000000000F424003000000000000000D242E726573756C742E74657874", // sigma_multiplier = 1.0, number_type = 0x03, json_path = $.result.text
outliers: make([]bool, 6),
reveals: []types.RevealBody{
{Reveal: `{"result": {"text": -28930, "number": 0}}`},
Expand All @@ -674,8 +644,8 @@ func TestFilter(t *testing.T) {
wantErr: types.ErrNoConsensus,
},
{
name: "Standard deviation filter - Big Int (Negative numbers (3))",
tallyInputAsHex: "0200000000000F8C7804000000000000000D242E726573756C742E74657874", // max_sigma = 1.019, number_type = uint64, json_path = $.result.text
name: "Standard deviation filter - 256-bit (Negative numbers (3))",
tallyInputAsHex: "0200000000000F8C7803000000000000000D242E726573756C742E74657874", // sigma_multiplier = 1.019, number_type = 0x03, json_path = $.result.text
outliers: make([]bool, 6),
reveals: []types.RevealBody{
{Reveal: `{"result": {"text": -28930, "number": 0}}`},
Expand All @@ -691,8 +661,8 @@ func TestFilter(t *testing.T) {
wantErr: types.ErrNoConsensus,
},
{
name: "Standard deviation filter - Big Int (Negative numbers (4))",
tallyInputAsHex: "0200000000000F9C1804000000000000000D242E726573756C742E74657874", // max_sigma = 1.023, number_type = uint64, json_path = $.result.text
name: "Standard deviation filter - 256-bit (Negative numbers (4))",
tallyInputAsHex: "0200000000000F9C1803000000000000000D242E726573756C742E74657874", // sigma_multiplier = 1.023, number_type = 0x03, json_path = $.result.text
outliers: []bool{false, false, true, false, true, false},
reveals: []types.RevealBody{
{Reveal: `{"result": {"text": -28930, "number": 0}}`},
Expand All @@ -708,8 +678,8 @@ func TestFilter(t *testing.T) {
wantErr: nil,
},
{
name: "Standard deviation filter - Big Int (Very large numbers)",
tallyInputAsHex: "0200000000000F424004000000000000000D242E726573756C742E74657874", // max_sigma = 1.0, number_type = uint64, json_path = $.result.text
name: "Standard deviation filter - 256-bit (Very large numbers)",
tallyInputAsHex: "0200000000000F424003000000000000000D242E726573756C742E74657874", // sigma_multiplier = 1.0, number_type = 0x03, json_path = $.result.text
outliers: []bool{true, false, false, false, false, false, false, true},
reveals: []types.RevealBody{
{Reveal: `{"result": {"text": 2000000000000000000000000000000000000000, "number": 0}}`},
Expand All @@ -727,8 +697,8 @@ func TestFilter(t *testing.T) {
wantErr: nil,
},
{
name: "Standard deviation filter - Big Int (Some reveals too large)",
tallyInputAsHex: "0200000000000F424004000000000000000D242E726573756C742E74657874", // max_sigma = 1.0, number_type = uint64, json_path = $.result.text
name: "Standard deviation filter - 256-bit (Some reveals too large)",
tallyInputAsHex: "0200000000000F424003000000000000000D242E726573756C742E74657874", // sigma_multiplier = 1.0, number_type = 0x03, json_path = $.result.text
outliers: []bool{true, false, false, false, false, true, false, false, false},
reveals: []types.RevealBody{
{Reveal: `{"result": {"text": -58896044618658097711785492504343953926634992332820282019728792003956564819968, "number": 0}}`}, // too small
Expand Down
2 changes: 1 addition & 1 deletion x/tally/types/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestDecodeFilterInput(t *testing.T) {

gasMeter := NewGasMeter(1e13, 0, DefaultMaxTallyGasLimit, math.NewIntWithDecimal(1, 18), DefaultGasCostBase)

filter, err := NewFilterMode(b, 1, 1, int(DefaultParams().MaxRevealDataLength), gasMeter)
filter, err := NewFilterMode(b, 1, 1, gasMeter)
if tt.wantErr != nil {
require.ErrorIs(t, err, tt.wantErr)
return
Expand Down
20 changes: 10 additions & 10 deletions x/tally/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import "cosmossdk.io/errors"

var (
// Errors used in filter:
ErrInvalidFilterType = errors.Register("tally", 2, "invalid filter type")
ErrFilterInputTooShort = errors.Register("tally", 3, "filter input length too short")
ErrInvalidPathLen = errors.Register("tally", 4, "invalid JSON path length")
ErrInvalidNumberType = errors.Register("tally", 5, "invalid number type specified")
ErrInvalidFilterInput = errors.Register("tally", 6, "invalid filter input")
ErrInvalidSigma = errors.Register("tally", 7, "invalid sigma")
ErrOutofTallyGas = errors.Register("tally", 8, "out of tally gas")
ErrConsensusInError = errors.Register("tally", 9, "consensus in error")
ErrNoConsensus = errors.Register("tally", 10, "> 1/3 of reveals do not agree on reveal data")
ErrNoBasicConsensus = errors.Register("tally", 11, "> 1/3 of reveals do not agree on (exit_code_success, proxy_pub_keys)")
ErrInvalidFilterType = errors.Register("tally", 2, "invalid filter type")
ErrFilterInputTooShort = errors.Register("tally", 3, "filter input length too short")
ErrInvalidPathLen = errors.Register("tally", 4, "invalid JSON path length")
ErrInvalidNumberType = errors.Register("tally", 5, "invalid number type specified")
ErrInvalidFilterInput = errors.Register("tally", 6, "invalid filter input")
ErrInvalidSigmaMultiplier = errors.Register("tally", 7, "invalid sigma multiplier")
ErrOutofTallyGas = errors.Register("tally", 8, "out of tally gas")
ErrConsensusInError = errors.Register("tally", 9, "consensus in error")
ErrNoConsensus = errors.Register("tally", 10, "> 1/3 of reveals do not agree on reveal data")
ErrNoBasicConsensus = errors.Register("tally", 11, "> 1/3 of reveals do not agree on (exit_code_success, proxy_pub_keys)")
// Errors used in tally program execution:
ErrDecodingPaybackAddress = errors.Register("tally", 12, "failed to decode payback address")
ErrFindingTallyProgram = errors.Register("tally", 13, "failed to find tally program")
Expand Down
Loading

0 comments on commit ea5d1eb

Please sign in to comment.