Skip to content

Commit

Permalink
fix: use updated mock data and fix filtering bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
hacheigriega committed Jun 18, 2024
1 parent f018f61 commit 52973d4
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 103 deletions.
12 changes: 0 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1009,20 +1009,8 @@ github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg
github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg=
github.com/sedaprotocol/rosetta-seda v0.0.0-20240427181737-e1d7563b2529 h1:VbJcd022MkoohRyAfktHnN99Brt/4eJr01mdLqPhGaE=
github.com/sedaprotocol/rosetta-seda v0.0.0-20240427181737-e1d7563b2529/go.mod h1:GdlDqGJN2g55PHiwYJs2bQMlL0rdlQQbauK4dcrOI6w=
<<<<<<< HEAD
<<<<<<< HEAD
github.com/sedaprotocol/seda-wasm-vm/tallyvm v0.0.0-20240615043354-53e73ba6d087 h1:XnU+MhjXJUDbPxfU1WR81MQmtxiA8NmwEnlHzVy5T9s=
github.com/sedaprotocol/seda-wasm-vm/tallyvm v0.0.0-20240615043354-53e73ba6d087/go.mod h1:AaX9uRy6qD2q+o1SapTRnGUe9HHZWTmBN2BVNAptq3U=
github.com/sedaprotocol/seda-wasm-vm/tallyvm v0.0.0-20240617203732-2649db4864d8 h1:9Vw+DoN9MW+qPDQvW1B+X7t0ulOUmYM3HTv7Khvqvj0=
github.com/sedaprotocol/seda-wasm-vm/tallyvm v0.0.0-20240617203732-2649db4864d8/go.mod h1:AaX9uRy6qD2q+o1SapTRnGUe9HHZWTmBN2BVNAptq3U=
=======
github.com/sedaprotocol/seda-wasm-vm/bind_go v0.0.0-20240516204717-b044adda94ee h1:XTqIflhEM54ISHEoKPhxmwaR6ICf8FAsQu9kVmgPXtU=
github.com/sedaprotocol/seda-wasm-vm/bind_go v0.0.0-20240516204717-b044adda94ee/go.mod h1:9f9r2Xii3J5jm0/psm5LKAygyfGMhJ6dVSbp8tMn9bg=
>>>>>>> 325b97e (feat: none filter for DR wasm)
=======
github.com/sedaprotocol/seda-wasm-vm/tallyvm v0.0.0-20240611223853-672f0b27ae4d h1:8LEKQQgg1KIwQzdSr3PMFmSXol08hpjaGM4dQiwAiTk=
github.com/sedaprotocol/seda-wasm-vm/tallyvm v0.0.0-20240611223853-672f0b27ae4d/go.mod h1:AaX9uRy6qD2q+o1SapTRnGUe9HHZWTmBN2BVNAptq3U=
>>>>>>> 33dcc8b (rebase base branch)
github.com/sedaprotocol/vrf-go v0.0.0-20231211075603-e5a17bb0b87c h1:PbSn7HpWeox6lqBu6Ba6YZS3On3euwn1BPz/egsnEgA=
github.com/sedaprotocol/vrf-go v0.0.0-20231211075603-e5a17bb0b87c/go.mod h1:DEIXHk41VUzOMVbZnIApssPXtZ+2zrETDP7kJjGc1RM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
Expand Down
85 changes: 42 additions & 43 deletions x/wasm-storage/keeper/abci.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
Expand All @@ -13,27 +14,25 @@ import (
)

type Request struct {
DrBinaryID string `json:"dr_binary_id"`
DrInputs string `json:"dr_inputs"`
GasLimit string `json:"gas_limit"`
GasPrice string `json:"gas_price"`
Height uint64 `json:"height"`
ID string `json:"id"`
Memo string `json:"memo"`
PaybackAddress string `json:"payback_address"`
ReplicationFactor int64 `json:"replication_factor"`
Reveals map[string]interface{} `json:"reveals"`
SedaPayload string `json:"seda_payload"`
TallyBinaryID string `json:"tally_binary_id"`
TallyInputs string `json:"tally_inputs"`
Version string `json:"version"`
DrBinaryID string `json:"dr_binary_id"`
DrInputs string `json:"dr_inputs"`
GasLimit string `json:"gas_limit"`
GasPrice string `json:"gas_price"`
Height uint64 `json:"height"`
ID string `json:"id"`
Memo string `json:"memo"`
PaybackAddress string `json:"payback_address"`
ReplicationFactor int64 `json:"replication_factor"`
Reveals map[string]RevealBody `json:"reveals"`
SedaPayload string `json:"seda_payload"`
TallyBinaryID string `json:"tally_binary_id"`
TallyInputs string `json:"tally_inputs"`
Version string `json:"version"`
}

type TallyingList map[string]Request

type tallyArg struct {
Reveals map[string]any
Outliers []bool
type RevealBody struct {
ExitCode uint8 `json:"exit_code"`
Reveal string `json:"reveal"` // base64-encoded string
}

func (k Keeper) EndBlock(ctx sdk.Context) error {
Expand Down Expand Up @@ -82,47 +81,47 @@ func (k Keeper) ExecuteTally(ctx sdk.Context) error {
if err != nil {
return err
}
var tallyingList TallyingList
err = json.Unmarshal(queryRes, &tallyingList)
var tallyList []Request
err = json.Unmarshal(queryRes, &tallyList)
if err != nil {
return err
}

// 3. Loop through the list to apply filter and execute tally.
// TODO: is it ok to use a map?
<<<<<<< HEAD
for id := range tallyingList {
// TODO: filtering

tallyID, err := hex.DecodeString(tallyingList[id].TallyBinaryID)
for id, req := range tallyList {
tallyInputs, err := base64.StdEncoding.DecodeString(req.TallyInputs)
if err != nil {
return fmt.Errorf("failed to decode tally ID to hex: %w", err)
return fmt.Errorf("failed to decode tally input: %w", err)
}
tallyWasm, err := k.DataRequestWasm.Get(ctx, tallyID)
=======
for id, req := range tallyingList {
keys := make([]string, 0, len(req.Reveals))

// Sort reveals.
keys := make([]string, len(req.Reveals))
i := 0
for k := range req.Reveals {
keys = append(keys, k)
keys[i] = k
i++
}
sort.Strings(keys)

// Sort reveals.
reveals := make([]RevealBody, 0, len(req.Reveals))
for _, k := range keys {
reveals = append(reveals, req.Reveals[k])
reveals := make([]RevealBody, len(req.Reveals))
for i, k := range keys {
reveals[i] = req.Reveals[k]
}
outliers, consensus, err := Outliers(req.TallyInputs, reveals)

outliers, consensus, err := ApplyFilter(tallyInputs, reveals)
if err != nil {
return err
}
tallyWasm, err := k.DataRequestWasm.Get(ctx, req.TallyBinaryID)
>>>>>>> 325b97e (feat: none filter for DR wasm)

tallyID, err := hex.DecodeString(req.TallyBinaryID)
if err != nil {
return fmt.Errorf("failed to decode tally ID to hex: %w", err)
}
tallyWasm, err := k.DataRequestWasm.Get(ctx, tallyID)
if err != nil {
return fmt.Errorf("failed to get tally wasm for DR ID %s: %w", id, err)
return fmt.Errorf("failed to get tally wasm for DR ID %d: %w", id, err)
}

args, err := tallyVMArg(req.TallyInputs, req.Reveals, outliers)
args, err := tallyVMArg(tallyInputs, req.Reveals, outliers)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions x/wasm-storage/keeper/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import (
// Random tally wasms.
// var mockFetchResponse = []byte(`{"145438bc73d32082ff459e18d1a3db72c50cdaaf908bd90ac9616e265daf4a17":{"commits":{},"dr_binary_id":"fcc85f81d2604dca02fdd1330cc39dc1a6446b8abb16b4f068d8a1d1e2a48877","dr_inputs":"","gas_limit":"20","gas_price":"10","height":5389299128623366229,"id":"145438bc73d32082ff459e18d1a3db72c50cdaaf908bd90ac9616e265daf4a17","memo":"","payback_address":"","replication_factor":2,"reveals":{},"seda_payload":"","tally_binary_id":"3256cbd8d4e68865ebdf3636df7a433fec8999b30a6202a1eb4a9f92363d5550","tally_inputs":"AwMDAwM=","version":"1.0.0"},"573d63b1ea24330e31e18768953699c7ca031e44483cc228a9638010baa8bad0":{"commits":{},"dr_binary_id":"a88516c04de6305f973bf43d7d0112c831eb511a2366df682a335d2aa0dec20b","dr_inputs":"","gas_limit":"20","gas_price":"10","height":7096383960515817382,"id":"573d63b1ea24330e31e18768953699c7ca031e44483cc228a9638010baa8bad0","memo":"","payback_address":"","replication_factor":3,"reveals":{},"seda_payload":"","tally_binary_id":"5d0cef6880aade3af9d59e285bb59ab128327d3ae49f943e31e554ab99b4d21e","tally_inputs":"BgYGBgY=","version":"1.0.0"},"830a37e6d676d4e6ef4458e7d93fa4126caa38efa3df7cfc28c7b0d7997fbe8d":{"commits":{},"dr_binary_id":"a88516c04de6305f973bf43d7d0112c831eb511a2366df682a335d2aa0dec20b","dr_inputs":"","gas_limit":"20","gas_price":"10","height":5515264981634441762,"id":"830a37e6d676d4e6ef4458e7d93fa4126caa38efa3df7cfc28c7b0d7997fbe8d","memo":"","payback_address":"","replication_factor":3,"reveals":{},"seda_payload":"","tally_binary_id":"5d0cef6880aade3af9d59e285bb59ab128327d3ae49f943e31e554ab99b4d21e","tally_inputs":"AgICAgI=","version":"1.0.0"},"b413cf7eb89f35cc44292ed3c6bd7a02bb3fd118c105117ad8e1037e77dd8db8":{"commits":{},"dr_binary_id":"a88516c04de6305f973bf43d7d0112c831eb511a2366df682a335d2aa0dec20b","dr_inputs":"","gas_limit":"20","gas_price":"10","height":14844550059642515049,"id":"b413cf7eb89f35cc44292ed3c6bd7a02bb3fd118c105117ad8e1037e77dd8db8","memo":"","payback_address":"","replication_factor":3,"reveals":{},"seda_payload":"","tally_binary_id":"5d0cef6880aade3af9d59e285bb59ab128327d3ae49f943e31e554ab99b4d21e","tally_inputs":"AwMDAwM=","version":"1.0.0"},"facc77c8dfb3ff645dbc5fb8778ab276841a3e2b4a80c1df5dc28fa516d1816c":{"commits":{},"dr_binary_id":"fcc85f81d2604dca02fdd1330cc39dc1a6446b8abb16b4f068d8a1d1e2a48877","dr_inputs":"","gas_limit":"20","gas_price":"10","height":8215532109948458411,"id":"facc77c8dfb3ff645dbc5fb8778ab276841a3e2b4a80c1df5dc28fa516d1816c","memo":"","payback_address":"","replication_factor":2,"reveals":{},"seda_payload":"","tally_binary_id":"3256cbd8d4e68865ebdf3636df7a433fec8999b30a6202a1eb4a9f92363d5550","tally_inputs":"AwMDAwM=","version":"1.0.0"}}`)

// All requests use sample tally wasm.
var mockFetchResponse2 = []byte(`{"145438bc73d32082ff459e18d1a3db72c50cdaaf908bd90ac9616e265daf4a17":{"commits":{},"dr_binary_id":"fcc85f81d2604dca02fdd1330cc39dc1a6446b8abb16b4f068d8a1d1e2a48877","dr_inputs":"","gas_limit":"20","gas_price":"10","height":5389299128623366229,"id":"145438bc73d32082ff459e18d1a3db72c50cdaaf908bd90ac9616e265daf4a17","memo":"","payback_address":"","replication_factor":2,"reveals":{},"seda_payload":"","tally_binary_id":"2f12d9175337bf340095ee955f8dff5c7baf4cadb0958e63ac4676a6a56fa71e","tally_inputs":"AwMDAwM=","version":"1.0.0"},"573d63b1ea24330e31e18768953699c7ca031e44483cc228a9638010baa8bad0":{"commits":{},"dr_binary_id":"a88516c04de6305f973bf43d7d0112c831eb511a2366df682a335d2aa0dec20b","dr_inputs":"","gas_limit":"20","gas_price":"10","height":7096383960515817382,"id":"573d63b1ea24330e31e18768953699c7ca031e44483cc228a9638010baa8bad0","memo":"","payback_address":"","replication_factor":3,"reveals":{},"seda_payload":"","tally_binary_id":"2f12d9175337bf340095ee955f8dff5c7baf4cadb0958e63ac4676a6a56fa71e","tally_inputs":"BgYGBgY=","version":"1.0.0"},"830a37e6d676d4e6ef4458e7d93fa4126caa38efa3df7cfc28c7b0d7997fbe8d":{"commits":{},"dr_binary_id":"a88516c04de6305f973bf43d7d0112c831eb511a2366df682a335d2aa0dec20b","dr_inputs":"","gas_limit":"20","gas_price":"10","height":5515264981634441762,"id":"830a37e6d676d4e6ef4458e7d93fa4126caa38efa3df7cfc28c7b0d7997fbe8d","memo":"","payback_address":"","replication_factor":3,"reveals":{},"seda_payload":"","tally_binary_id":"2f12d9175337bf340095ee955f8dff5c7baf4cadb0958e63ac4676a6a56fa71e","tally_inputs":"AgICAgI=","version":"1.0.0"},"b413cf7eb89f35cc44292ed3c6bd7a02bb3fd118c105117ad8e1037e77dd8db8":{"commits":{},"dr_binary_id":"a88516c04de6305f973bf43d7d0112c831eb511a2366df682a335d2aa0dec20b","dr_inputs":"","gas_limit":"20","gas_price":"10","height":14844550059642515049,"id":"b413cf7eb89f35cc44292ed3c6bd7a02bb3fd118c105117ad8e1037e77dd8db8","memo":"","payback_address":"","replication_factor":3,"reveals":{},"seda_payload":"","tally_binary_id":"2f12d9175337bf340095ee955f8dff5c7baf4cadb0958e63ac4676a6a56fa71e","tally_inputs":"AwMDAwM=","version":"1.0.0"},"facc77c8dfb3ff645dbc5fb8778ab276841a3e2b4a80c1df5dc28fa516d1816c":{"commits":{},"dr_binary_id":"fcc85f81d2604dca02fdd1330cc39dc1a6446b8abb16b4f068d8a1d1e2a48877","dr_inputs":"","gas_limit":"20","gas_price":"10","height":8215532109948458411,"id":"facc77c8dfb3ff645dbc5fb8778ab276841a3e2b4a80c1df5dc28fa516d1816c","memo":"","payback_address":"","replication_factor":2,"reveals":{},"seda_payload":"","tally_binary_id":"2f12d9175337bf340095ee955f8dff5c7baf4cadb0958e63ac4676a6a56fa71e","tally_inputs":"AwMDAwM=","version":"1.0.0"}}`)
// All requests use sample tally wasm and filter type none.
var mockFetchResponse2 = []byte(`[{"commits":{},"dr_binary_id":"9471d36add157cd7eaa32a42b5ddd091d5d5d396bf9ad67938a4fc40209df6cf","dr_inputs":"","gas_limit":"20","gas_price":"10","height":1661661742461173125,"id":"fba5314c57e52da7d1a2245d18c670fde1cb8c237062d2a1be83f449ace0932e","memo":"","payback_address":"","replication_factor":3,"reveals":{"1b85dfb9420e6757630a0db2280fa1787ec8c1e419a6aca76dbbfe8ef6e17521":{"exit_code":0,"gas_used":"10","reveal":"Ng==","salt":"05952214b2ba3549a8d627c57d2d0dd1b0a2ce65c46e3b2f25c273464be8ba5f"},"1dae290cd880b79d21079d89aee3460cf8a7d445fb35cade70cf8aa96924441c":{"exit_code":0,"gas_used":"10","reveal":"LQ==","salt":"05952214b2ba3549a8d627c57d2d0dd1b0a2ce65c46e3b2f25c273464be8ba5f"},"421e735518ef77fc1209a9d3585cdf096669b52ea68549e2ce048d4919b4c8c0":{"exit_code":0,"gas_used":"10","reveal":"DQ==","salt":"05952214b2ba3549a8d627c57d2d0dd1b0a2ce65c46e3b2f25c273464be8ba5f"}},"seda_payload":"","tally_binary_id":"2f12d9175337bf340095ee955f8dff5c7baf4cadb0958e63ac4676a6a56fa71e","tally_inputs":"AAEBAQE=","version":"1.0.0"},{"commits":{},"dr_binary_id":"9471d36add157cd7eaa32a42b5ddd091d5d5d396bf9ad67938a4fc40209df6cf","dr_inputs":"","gas_limit":"20","gas_price":"10","height":9859593541233596221,"id":"d4e40f45fbf529134926acf529baeb6d4f37b5c380d7ab6b934833e7c00d725f","memo":"","payback_address":"","replication_factor":1,"reveals":{"c9a4c8f1e70a0059a88b4768a920e41c95c587b8387ea3286d8fa4ee3b68b038":{"exit_code":0,"gas_used":"10","reveal":"Yw==","salt":"f837455a930a66464f1c50586dc745a6b14ea807727c6069acac24c9558b6dbf"}},"seda_payload":"","tally_binary_id":"2f12d9175337bf340095ee955f8dff5c7baf4cadb0958e63ac4676a6a56fa71e","tally_inputs":"AAEBAQE=","version":"1.0.0"}]`)

var drWasmByteArray = []byte("82a9dda829eb7f8ffe9fbe49e45d47d2dad9664fbb7adf72492e3c81ebd3e29134d9bc12212bf83c6840f10e8246b9db54a4859b7ccd0123d86e5872c1e5082")

Expand Down
89 changes: 44 additions & 45 deletions x/wasm-storage/keeper/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package keeper
import (
"errors"

"github.com/ethereum/go-ethereum/rlp"
"github.com/tidwall/gjson"
)

const (
None = iota
Mode
StdDeviation
filterNone byte = 0x00
filterMode byte = 0x01
filterStdDev byte = 0x02
)

type (
Expand Down Expand Up @@ -73,49 +72,49 @@ func calculate[T comparable](reveals []T) ([]bool, bool) {
return outliers, true
}

// Outliers calculates which reveals are in acceptance criteria.
// It returns a list of True and False. Where True means data at index i is an
// outlier.
//
// Note: <param: tallyInput> is a rlp encoded and <param:reveals> is JSON serialized.
func Outliers(filterInput []byte, reveals []RevealBody) ([]bool, bool, error) {
var rlpAsList []any
if err := rlp.DecodeBytes(filterInput, &rlpAsList); err != nil {
return nil, false, err
// ApplyFilter processes filter of the type specified in the first byte of
// tally inputs. It returns an outlier list, which is a boolean list where
// true at index i means that the reveal at index i is an outlier, consensus
// boolean, and error.
func ApplyFilter(tallyInputs []byte, reveals []RevealBody) ([]bool, bool, error) {
if len(tallyInputs) < 1 {
return nil, false, errors.New("tally inputs should be at least 1 byte")
}
filteringAlgo, ok := rlpAsList[0].([]uint)
if !ok || len(filteringAlgo) != 1 {
return nil, false, errors.New("can not RLP decode algo type from filter input")
}

outliers := make([]bool, 0, len(reveals))
var consensus bool

switch filteringAlgo[0] {
case None:
for range reveals {
outliers = append(outliers, false)
}
case Mode:
var filter modeFilter
if err := rlp.DecodeBytes(filterInput, &filter); err != nil {
return nil, false, err
}

if filter.JSONPath == "" {
return nil, false, errors.New("empty JSON path")
}

exitCodes := make([]uint8, 0, len(reveals))
revealData := make([][]byte, 0, len(reveals))
for _, r := range reveals {
exitCodes = append(exitCodes, r.ExitCode)
revealData = append(revealData, r.Reveal)
switch tallyInputs[0] {
case filterNone:
outliers := make([]bool, len(reveals))
for i := range outliers {
outliers[i] = false
}
outliers, consensus = FilterMode(filter.JSONPath, exitCodes, revealData)
return outliers, consensus, nil
case StdDeviation:
return nil, false, errors.New("filter type Standard deviation not implemented")
return outliers, true, nil

case filterMode:
// TODO: Reactivate mode filter
/*
var filter modeFilter
if err := rlp.DecodeBytes(tallyInputs, &filter); err != nil {
return nil, false, err
}
if filter.JSONPath == "" {
return nil, false, errors.New("empty JSON path")
}
exitCodes := make([]uint8, len(reveals))
revealData := make([][]byte, len(reveals))
for i, r := range reveals {
exitCodes[i] = r.ExitCode
revealData[i] = r.Reveal
}
outliers, consensus := FilterMode(filter.JSONPath, exitCodes, revealData)
return outliers, consensus, nil
*/
return nil, false, errors.New("filter type mode is not implemented")

case filterStdDev:
return nil, false, errors.New("filter type standard deviation is not implemented")

default:
return nil, false, errors.New("filter type is invalid")
}
return outliers, true, nil
}
2 changes: 1 addition & 1 deletion x/wasm-storage/keeper/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestOutliers_None(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _, err := keeper.Outliers(tt.tallyInput, tt.reveals)
got, _, err := keeper.ApplyFilter(tt.tallyInput, tt.reveals)
if tt.wantErr != nil {
require.ErrorIs(t, err, tt.wantErr)
return
Expand Down

0 comments on commit 52973d4

Please sign in to comment.