Skip to content

Commit acc9d0d

Browse files
committed
wrap submitBlockSignatures ecall
1 parent 4df8169 commit acc9d0d

File tree

6 files changed

+77
-175
lines changed

6 files changed

+77
-175
lines changed

go-cosmwasm/api/ecall_record.go

Lines changed: 0 additions & 155 deletions
This file was deleted.

go-cosmwasm/api/lib.go

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
v1types "github.com/scrtlabs/SecretNetwork/go-cosmwasm/types/v1"
1818

1919
"github.com/scrtlabs/SecretNetwork/go-cosmwasm/types"
20+
tmapi "github.com/scrtlabs/tm-secret-enclave/api"
2021
)
2122

2223
// nice aliases to the rust names
@@ -48,6 +49,32 @@ func HealthCheck() ([]byte, error) {
4849
}
4950

5051
func SubmitBlockSignatures(header []byte, commit []byte, txs []byte, encRandom []byte /* valSet []byte, nextValSet []byte */) ([]byte, []byte, error) {
52+
recorder := tmapi.GetRecorder()
53+
54+
// Create combined input for recording/replay (hash all inputs together)
55+
input := make([]byte, 0, len(header)+len(commit)+len(txs)+len(encRandom))
56+
input = append(input, header...)
57+
input = append(input, commit...)
58+
input = append(input, txs...)
59+
input = append(input, encRandom...)
60+
61+
// In replay mode, try to get from recorded data
62+
if recorder.IsReplayMode() {
63+
if output, err, found := recorder.Replay("SubmitBlockSignatures", input); found {
64+
fmt.Printf("[SubmitBlockSignatures] Replay mode: returning recorded result\n")
65+
if err != nil {
66+
return nil, nil, err
67+
}
68+
// Output is 64 bytes: [32 bytes decrypted][32 bytes next_validator_set_evidence]
69+
if len(output) == 64 {
70+
return output[:32], output[32:], nil
71+
}
72+
return nil, nil, fmt.Errorf("SubmitBlockSignatures: invalid recorded data (expected 64 bytes, got %d)", len(output))
73+
}
74+
return nil, nil, fmt.Errorf("SubmitBlockSignatures: no recorded data found for input (replay mode)")
75+
}
76+
77+
// SGX mode: call the actual enclave
5178
errmsg := C.Buffer{}
5279
spidSlice := sendSlice(header)
5380
defer freeAfterSend(spidSlice)
@@ -59,10 +86,33 @@ func SubmitBlockSignatures(header []byte, commit []byte, txs []byte, encRandom [
5986
defer freeAfterSend(txsSlice)
6087

6188
res, err := C.submit_block_signatures(spidSlice, apiKeySlice, txsSlice, encRandomSlice /* valSetSlice, nextValSetSlice,*/, &errmsg)
89+
90+
var buf1, buf2 []byte
91+
var callErr error
6292
if err != nil {
63-
return nil, nil, errorWithMessage(err, errmsg)
93+
callErr = errorWithMessage(err, errmsg)
94+
} else {
95+
buf1 = receiveVector(res.buf1)
96+
buf2 = receiveVector(res.buf2)
97+
}
98+
99+
// Record the result - 64 bytes: [32 bytes buf1][32 bytes buf2]
100+
if callErr == nil {
101+
output := make([]byte, 64)
102+
copy(output[:32], buf1)
103+
copy(output[32:], buf2)
104+
if recordErr := recorder.Record("SubmitBlockSignatures", input, output, nil); recordErr != nil {
105+
fmt.Printf("[SubmitBlockSignatures] Warning: failed to record ecall: %v\n", recordErr)
106+
} else {
107+
fmt.Printf("[SubmitBlockSignatures] SGX mode: recorded ecall result\n")
108+
}
109+
} else {
110+
if recordErr := recorder.Record("SubmitBlockSignatures", input, nil, callErr); recordErr != nil {
111+
fmt.Printf("[SubmitBlockSignatures] Warning: failed to record ecall error: %v\n", recordErr)
112+
}
64113
}
65-
return receiveVector(res.buf1), receiveVector(res.buf2), nil
114+
115+
return buf1, buf2, callErr
66116
}
67117

68118
func SubmitValidatorSetEvidence(evidence []byte) error {
@@ -175,6 +225,12 @@ func ReleaseCache(cache Cache) {
175225
}
176226

177227
func InitEnclaveRuntime(moduleCacheSize uint16) error {
228+
// Skip in non-SGX replay mode - there's no enclave
229+
if tmapi.GetRecorder().IsReplayMode() {
230+
fmt.Println("[InitEnclaveRuntime] Non-SGX replay mode: skipping (no enclave)")
231+
return nil
232+
}
233+
178234
errmsg := C.Buffer{}
179235

180236
config := C.EnclaveRuntimeConfig{
@@ -536,11 +592,11 @@ func CreateAttestationReport(no_epid bool, no_dcap bool, is_migration_report boo
536592
}
537593

538594
func GetEncryptedSeed(cert []byte) ([]byte, error) {
539-
recorder := GetRecorder()
595+
recorder := tmapi.GetRecorder()
540596

541597
// In replay mode, try to get from recorded data
542598
if recorder.IsReplayMode() {
543-
if output, err, found := ReplayGetEncryptedSeed(cert); found {
599+
if output, err, found := recorder.Replay("GetEncryptedSeed", cert); found {
544600
fmt.Printf("[GetEncryptedSeed] Replay mode: returning recorded result\n")
545601
return output, err
546602
}
@@ -562,7 +618,7 @@ func GetEncryptedSeed(cert []byte) ([]byte, error) {
562618
}
563619

564620
// Record the result for non-SGX nodes
565-
if recordErr := RecordGetEncryptedSeed(cert, output, callErr); recordErr != nil {
621+
if recordErr := recorder.Record("GetEncryptedSeed", cert, output, callErr); recordErr != nil {
566622
fmt.Printf("[GetEncryptedSeed] Warning: failed to record ecall: %v\n", recordErr)
567623
} else {
568624
fmt.Printf("[GetEncryptedSeed] SGX mode: recorded ecall result\n")

go.mod

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@ replace (
88
cosmossdk.io/api => github.com/scrtlabs/cosmos-sdk-api v0.7.6-secret.0
99
cosmossdk.io/store => github.com/scrtlabs/cosmos-sdk-store v1.1.1-secret.1
1010
cosmossdk.io/x/tx => github.com/scrtlabs/cosmos-sdk-x-tx v0.13.7-secret.0
11-
github.com/cometbft/cometbft => github.com/scrtlabs/tendermint v0.38.19-secret.1
11+
github.com/cometbft/cometbft => github.com/scrtlabs/tendermint v0.38.19-secret.1-non-sgx.0
1212
github.com/cosmos/cosmos-sdk => github.com/scrtlabs/cosmos-sdk v0.50.14-secret.7
1313
github.com/cosmos/iavl => github.com/scrtlabs/iavl v1.2.2-secret.0
14+
15+
// Use local tm-secret-enclave with recorder support
16+
github.com/scrtlabs/tm-secret-enclave => ../tm-secret-enclave
1417
github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
1518

1619
github.com/zondax/ledger-go => github.com/zondax/ledger-go v1.0.0
@@ -76,7 +79,7 @@ require (
7679
github.com/gogo/protobuf v1.3.2
7780
github.com/golang/mock v1.6.0
7881
github.com/hashicorp/go-metrics v0.5.3
79-
github.com/scrtlabs/tm-secret-enclave v1.13.1
82+
github.com/scrtlabs/tm-secret-enclave v1.13.1-non-sgx.0
8083
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028
8184
gopkg.in/yaml.v2 v2.4.0
8285
)

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,10 +1619,8 @@ github.com/scrtlabs/cosmos-sdk-x-tx v0.13.7-secret.0 h1:i3k5706sDHKhaCvzokB+n33/
16191619
github.com/scrtlabs/cosmos-sdk-x-tx v0.13.7-secret.0/go.mod h1:V6DImnwJMTq5qFjeGWpXNiT/fjgE4HtmclRmTqRVM3w=
16201620
github.com/scrtlabs/iavl v1.2.2-secret.0 h1:P96PL1Lf8OBSW9pMrlaRxhceZ4z9Hc7jk12g9ShWeHw=
16211621
github.com/scrtlabs/iavl v1.2.2-secret.0/go.mod h1:GiM43q0pB+uG53mLxLDzimxM9l/5N9UuSY3/D0huuVw=
1622-
github.com/scrtlabs/tendermint v0.38.19-secret.1 h1:NxZ21CS6INBjL2QCL087/BJLb1NAIeoY07mHasTuqKs=
1623-
github.com/scrtlabs/tendermint v0.38.19-secret.1/go.mod h1:CZUJG1djTJUVbpjGS9JmQx9CFfF4goKi3LzYUQtxWO8=
1624-
github.com/scrtlabs/tm-secret-enclave v1.13.1 h1:0mXcBdoWyqEGhQEdbXMjSuTi9LKKMld2BqEj0eNpoxU=
1625-
github.com/scrtlabs/tm-secret-enclave v1.13.1/go.mod h1:nxZQtzzAqBNBLOEXSv4cKlUnVA4vRmHOn6ujr3kxVME=
1622+
github.com/scrtlabs/tendermint v0.38.19-secret.1-non-sgx.0 h1:Xcr8LzUDqFEZBs+9bezrOfRS+4eHShGYT9wQ8JdGMo0=
1623+
github.com/scrtlabs/tendermint v0.38.19-secret.1-non-sgx.0/go.mod h1:o8pxZlFnd7pwJ/awZi1wi1+9guKu7NpE97/iijSFNvk=
16261624
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
16271625
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
16281626
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=

x/compute/module.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/scrtlabs/SecretNetwork/x/compute/internal/keeper"
2121
"github.com/scrtlabs/SecretNetwork/x/compute/internal/types"
2222
tmenclave "github.com/scrtlabs/tm-secret-enclave"
23+
tmapi "github.com/scrtlabs/tm-secret-enclave/api"
2324

2425
crontypes "github.com/scrtlabs/SecretNetwork/x/cron/types"
2526
)
@@ -207,8 +208,9 @@ func (am AppModule) BeginBlock(c context.Context) error {
207208
ctx.Logger().Error("Failed to execute cron message", "error", err)
208209
}
209210
}
210-
211-
am.keeper.SetRandomSeed(ctx, random, validator_set_evidence)
211+
if tmapi.GetRecorder().IsSGXMode() {
212+
am.keeper.SetRandomSeed(ctx, random, validator_set_evidence)
213+
}
212214
} else {
213215
ctx.Logger().Debug("Non-encrypted block", "Block_hash", block_header.LastBlockId.Hash, "Height", ctx.BlockHeight(), "Txs", len(x2_data))
214216
}
@@ -217,6 +219,9 @@ func (am AppModule) BeginBlock(c context.Context) error {
217219

218220
// EndBlock returns the end blocker for the compute module.
219221
func (am AppModule) EndBlock(c context.Context) error {
222+
if tmapi.GetRecorder().IsReplayMode() {
223+
return nil
224+
}
220225
ctx := c.(sdk.Context)
221226

222227
_, bytesCronMsgs, err := am.keeper.GetScheduledMsgs(ctx, crontypes.ExecutionStage_EXECUTION_STAGE_END_BLOCKER)

x/registration/internal/keeper/keeper.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/hex"
66
"encoding/json"
77
"fmt"
8-
"os"
98
"path/filepath"
109

1110
"cosmossdk.io/core/store"
@@ -15,6 +14,7 @@ import (
1514
sdk "github.com/cosmos/cosmos-sdk/types"
1615
"github.com/scrtlabs/SecretNetwork/x/registration/internal/types"
1716
ra "github.com/scrtlabs/SecretNetwork/x/registration/remote_attestation"
17+
tmapi "github.com/scrtlabs/tm-secret-enclave/api"
1818
)
1919

2020
// Keeper will have a reference to Wasmer with it's own data directory.
@@ -25,16 +25,11 @@ type Keeper struct {
2525
router baseapp.MessageRouter
2626
}
2727

28-
// isNonSGXReplayMode returns true if running in non-SGX replay mode
29-
func isNonSGXReplayMode() bool {
30-
return os.Getenv("SECRET_NODE_MODE") == "replay"
31-
}
32-
3328
// NewKeeper creates a new contract Keeper instance
3429
func NewKeeper(cdc codec.Codec, storeService store.KVStoreService, router baseapp.MessageRouter, enclave EnclaveInterface, homeDir string, bootstrap bool) Keeper {
3530
if !bootstrap {
3631
// Skip seed initialization in non-SGX replay mode - there's no enclave to load seeds into
37-
if isNonSGXReplayMode() {
32+
if tmapi.GetRecorder().IsReplayMode() {
3833
fmt.Println("[Registration] Non-SGX replay mode: skipping seed initialization (no enclave)")
3934
} else {
4035
InitializeNode(homeDir, enclave)

0 commit comments

Comments
 (0)