Skip to content

Commit

Permalink
test(tally): complete contract setup for tally testing
Browse files Browse the repository at this point in the history
  • Loading branch information
hacheigriega committed Dec 16, 2024
1 parent e9720fe commit 0feb5a6
Show file tree
Hide file tree
Showing 10 changed files with 671 additions and 512 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ require (
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/sedaprotocol/vrf-go v0.0.0-20231211075603-e5a17bb0b87c // indirect
github.com/shamaton/msgpack/v2 v2.2.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,8 @@ github.com/sedaprotocol/rosetta-seda v0.0.0-20240427181737-e1d7563b2529 h1:VbJcd
github.com/sedaprotocol/rosetta-seda v0.0.0-20240427181737-e1d7563b2529/go.mod h1:GdlDqGJN2g55PHiwYJs2bQMlL0rdlQQbauK4dcrOI6w=
github.com/sedaprotocol/seda-wasm-vm/tallyvm/v2 v2.1.1 h1:tYDyzF0PfF7wljUwvjlOLFIqOJje6axzpPbi7879qpM=
github.com/sedaprotocol/seda-wasm-vm/tallyvm/v2 v2.1.1/go.mod h1:GnPFBlYHV+AcpLcUKcxkNkfHf2CtmTSmPbHat8flDBY=
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/shamaton/msgpack/v2 v2.2.0 h1:IP1m01pHwCrMa6ZccP9B3bqxEMKMSmMVAVKk54g3L/Y=
github.com/shamaton/msgpack/v2 v2.2.0/go.mod h1:6khjYnkx73f7VQU7wjcFS9DFjs+59naVWJv1TB7qdOI=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
Expand Down
475 changes: 0 additions & 475 deletions x/tally/keeper/abci_test.go

This file was deleted.

File renamed without changes.
265 changes: 265 additions & 0 deletions x/tally/keeper/endblock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
package keeper_test

import (
"encoding/hex"
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/require"

"github.com/sedaprotocol/seda-wasm-vm/tallyvm/v2"

"github.com/sedaprotocol/seda-chain/x/tally/keeper/testdata"
"github.com/sedaprotocol/seda-chain/x/tally/types"
)

// TestTallyVM tests tally VM using a sample tally wasm that performs
// preliminary checks on the given reveal data.
func TestTallyVM(t *testing.T) {
cases := []struct {
name string
requestJSON []byte
args []string
expErr string
}{
{
name: "Three reveals",
requestJSON: []byte(`{
"commits":{},
"exec_program_id":"9471d36add157cd7eaa32a42b5ddd091d5d5d396bf9ad67938a4fc40209df6cf",
"exec_inputs":"",
"exec_gas_limit":5000000000,
"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_program_id":"8ade60039246740faa80bf424fc29e79fe13b32087043e213e7bc36620111f6b",
"tally_inputs":"AAEBAQE=",
"tally_gas_limit":50000000000000,
"version":"1.0.0"
}`),
args: []string{"6d792d74616c6c792d696e70757473", "[{\"reveal\":[54],\"salt\":[211,159,121,219,109,120,111,102,218,223,158,61,107,199,122,219,183,57,237,221,157,209,215,117,111,70,182,113,238,185,115,142,158,221,189,159,219,151,54,239,126,58,225,183,188,109,174,95],\"exit_code\":0,\"gas_used\":\"10\"},{\"reveal\":[45],\"salt\":[211,159,121,219,109,120,111,102,218,223,158,61,107,199,122,219,183,57,237,221,157,209,215,117,111,70,182,113,238,185,115,142,158,221,189,159,219,151,54,239,126,58,225,183,188,109,174,95],\"exit_code\":0,\"gas_used\":\"10\"},{\"reveal\":[13],\"salt\":[211,159,121,219,109,120,111,102,218,223,158,61,107,199,122,219,183,57,237,221,157,209,215,117,111,70,182,113,238,185,115,142,158,221,189,159,219,151,54,239,126,58,225,183,188,109,174,95],\"exit_code\":0,\"gas_used\":\"10\"}]", "[0,0,0]"},
expErr: "",
},
{
name: "One less outlier provided",
requestJSON: []byte(`{
"commits":{},
"exec_program_id":"9471d36add157cd7eaa32a42b5ddd091d5d5d396bf9ad67938a4fc40209df6cf",
"exec_inputs":"",
"exec_gas_limit":5000000000,
"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_program_id":"8ade60039246740faa80bf424fc29e79fe13b32087043e213e7bc36620111f6b",
"tally_inputs":"AAEBAQE=",
"tally_gas_limit":50000000000000,
"version":"1.0.0"
}`),
args: []string{"6d792d74616c6c792d696e70757473", "[{\"reveal\":[54],\"salt\":[211,159,121,219,109,120,111,102,218,223,158,61,107,199,122,219,183,57,237,221,157,209,215,117,111,70,182,113,238,185,115,142,158,221,189,159,219,151,54,239,126,58,225,183,188,109,174,95],\"exit_code\":0,\"gas_used\":\"10\"},{\"reveal\":[45],\"salt\":[211,159,121,219,109,120,111,102,218,223,158,61,107,199,122,219,183,57,237,221,157,209,215,117,111,70,182,113,238,185,115,142,158,221,189,159,219,151,54,239,126,58,225,183,188,109,174,95],\"exit_code\":0,\"gas_used\":\"10\"},{\"reveal\":[13],\"salt\":[211,159,121,219,109,120,111,102,218,223,158,61,107,199,122,219,183,57,237,221,157,209,215,117,111,70,182,113,238,185,115,142,158,221,189,159,219,151,54,239,126,58,225,183,188,109,174,95],\"exit_code\":0,\"gas_used\":\"10\"}]", "[0,0]"},
expErr: "abort: Number of reveals (3) does not equal number of consensus reports (2)",
},
{
name: "One reveal",
requestJSON: []byte(`{
"commits":{},
"exec_program_id":"9471d36add157cd7eaa32a42b5ddd091d5d5d396bf9ad67938a4fc40209df6cf",
"exec_inputs":"",
"exec_gas_limit":5000000000,
"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_program_id":"8ade60039246740faa80bf424fc29e79fe13b32087043e213e7bc36620111f6b",
"tally_inputs":"AAEBAQE=",
"tally_gas_limit":50000000000000,
"version":"1.0.0"
}`),
args: []string{"6d792d74616c6c792d696e70757473", "[{\"reveal\":[99],\"salt\":[127,205,251,227,158,90,247,125,26,235,174,58,225,253,92,231,78,124,233,215,59,227,150,186,111,94,30,107,205,59,239,110,220,235,78,189,105,198,156,219,135,61,231,159,27,233,214,223],\"exit_code\":0,\"gas_used\":\"10\"}]", "[0]"},
expErr: "",
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var req types.Request
err := json.Unmarshal(tc.requestJSON, &req)
require.NoError(t, err)

result := tallyvm.ExecuteTallyVm(testdata.SampleTallyWasm(), tc.args, map[string]string{
"VM_MODE": "tally",
"CONSENSUS": fmt.Sprintf("%v", true),
"BLOCK_HEIGHT": fmt.Sprintf("%d", 1),
"DR_ID": req.ID,
"EXEC_PROGRAM_ID": req.ExecProgramID,
"EXEC_INPUTS": req.ExecInputs,
"EXEC_GAS_LIMIT": fmt.Sprintf("%v", req.ExecGasLimit),
"TALLY_INPUTS": req.TallyInputs,
"TALLY_PROGRAM_ID": req.TallyProgramID,
"DR_REPLICATION_FACTOR": fmt.Sprintf("%v", req.ReplicationFactor),
"DR_GAS_PRICE": req.GasPrice,
"DR_TALLY_GAS_LIMIT": fmt.Sprintf("%v", req.TallyGasLimit),
"DR_MEMO": req.Memo,
"DR_PAYBACK_ADDRESS": req.PaybackAddress,
})

if tc.expErr != "" {
require.Contains(t, result.Stderr[0], tc.expErr)
} else {
require.Equal(t, 0, len(result.Stderr))

bz, err := hex.DecodeString(tc.args[0])
require.NoError(t, err)
require.Contains(t, string(result.Result), string(bz))
}
})
}
}

// TestTallyVM_EnvVars tests passing environment variables to tally VM.
func TestTallyVM_EnvVars(t *testing.T) {
cases := []struct {
name string
requestJSON []byte
args []string
expResult string
expErr string
}{
{
name: "Test passing all environment variables",
requestJSON: []byte(`{
"commits":{},
"exec_program_id":"9471d36add157cd7eaa32a42b5ddd091d5d5d396bf9ad67938a4fc40209df6cf",
"exec_inputs":"",
"exec_gas_limit":5000000000,
"gas_price":"10",
"height":1661661742461173200,
"id":"fba5314c57e52da7d1a2245d18c670fde1cb8c237062d2a1be83f449ace0932e",
"memo":"mock_data_request_num_one",
"payback_address":"YrzimoSJXwpA7ju71AkhkirkDCU=",
"consensus_filter":"AQAAAAAAAAALcmVzdWx0LnRleHQ=",
"replication_factor":3,
"reveals":{},
"seda_payload":"",
"tally_program_id":"5f3b31bff28c64a143119ee6389d62e38767672daace9c36db54fa2d18e9f391",
"tally_inputs":"AAEBAQE=",
"tally_gas_limit":50000000000000,
"version":"1.0.0"
}`),
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var req types.Request
err := json.Unmarshal(tc.requestJSON, &req)
require.NoError(t, err)

envs := map[string]string{
"VM_MODE": "tally",
"CONSENSUS": fmt.Sprintf("%v", true),
"BLOCK_HEIGHT": fmt.Sprintf("%d", 1),
"DR_ID": req.ID,
"EXEC_PROGRAM_ID": req.ExecProgramID,
"EXEC_INPUTS": req.ExecInputs,
"EXEC_GAS_LIMIT": fmt.Sprintf("%v", req.ExecGasLimit),
"TALLY_INPUTS": req.TallyInputs,
"TALLY_PROGRAM_ID": req.TallyProgramID,
"DR_REPLICATION_FACTOR": fmt.Sprintf("%v", req.ReplicationFactor),
"DR_GAS_PRICE": req.GasPrice,
"DR_TALLY_GAS_LIMIT": fmt.Sprintf("%v", req.TallyGasLimit),
"DR_MEMO": req.Memo,
"DR_PAYBACK_ADDRESS": req.PaybackAddress,
}

result := tallyvm.ExecuteTallyVm(testdata.SampleTallyWasm2(), tc.args, envs)

require.Equal(t, 0, len(result.Stderr))
for key := range envs {
require.Contains(t, string(result.Result), fmt.Sprintf("%s=%s", key, envs[key]))
}
})
}
}

func TestProcessTallies(t *testing.T) {
f := initFixture(t)

drID := f.commitRevealDataRequest(t, "c2VkYXByb3RvY29s")

err := f.tallyKeeper.ProcessTallies(f.Context(), f.coreContractAddr)
require.NoError(t, err)

// TODO check tally result

// TODO check events
// fmt.Println(f.logBuf.String())
// require.Contains(t, f.logBuf.String(), exp)

dataResult, err := f.batchingKeeper.GetLatestDataResult(f.Context(), drID)
require.NoError(t, err)
fmt.Println(dataResult)

dataResults, err := f.batchingKeeper.GetDataResults(f.Context(), false)
require.NoError(t, err)
require.Equal(t, *dataResult, dataResults[0])
}
Loading

0 comments on commit 0feb5a6

Please sign in to comment.