Skip to content

draft: cron module, added implicit hash to header #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: secret-0.38.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
622 changes: 419 additions & 203 deletions abci/types/types.pb.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package consensus
import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -2368,6 +2369,7 @@ func (cs *State) signVote(
) (*types.Vote, error) {
// Flush the WAL. Otherwise, we may not recompute the same vote to sign,
// and the privValidator will refuse to sign anything.
fmt.Printf("signVote: hash: %v\n", hex.EncodeToString(hash))
if err := cs.wal.FlushAndSync(); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ require (
github.com/google/uuid v1.6.0
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/oasisprotocol/curve25519-voi v0.0.0-20220708102147-0a8a51822cae
github.com/scrtlabs/tm-secret-enclave v1.11.8
github.com/scrtlabs/tm-secret-enclave v1.12.3-implicit-hash
golang.org/x/sync v0.10.0
gonum.org/v1/gonum v0.15.1
google.golang.org/protobuf v1.36.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,8 @@ github.com/sasha-s/go-deadlock v0.3.5 h1:tNCOEEDG6tBqrNDOX35j/7hL5FcFViG6awUGROb
github.com/sasha-s/go-deadlock v0.3.5/go.mod h1:bugP6EGbdGYObIlx7pUZtWqlvo8k9H6vCBBsiChJQ5U=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/scrtlabs/tm-secret-enclave v1.11.8 h1:fctIfJDHGl8D+fcXlZLX6S4yDeePIsuyzdG5HngFNPQ=
github.com/scrtlabs/tm-secret-enclave v1.11.8/go.mod h1:nxZQtzzAqBNBLOEXSv4cKlUnVA4vRmHOn6ujr3kxVME=
github.com/scrtlabs/tm-secret-enclave v1.12.3-implicit-hash h1:vssSAjnieWcH4XsGjpGv5MRk1fJTL4boCWrwbgkCFDQ=
github.com/scrtlabs/tm-secret-enclave v1.12.3-implicit-hash/go.mod h1:nxZQtzzAqBNBLOEXSv4cKlUnVA4vRmHOn6ujr3kxVME=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
Expand Down
7 changes: 7 additions & 0 deletions proto/tendermint/abci/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ message RequestPrepareProposal {
bytes next_validators_hash = 7;
// address of the public key of the validator proposing the block.
bytes proposer_address = 8;
// scheduled_msgs_hash
bytes implicit_hash = 9;
}

message RequestProcessProposal {
Expand All @@ -153,6 +155,8 @@ message RequestProcessProposal {
bytes next_validators_hash = 7;
// address of the public key of the original proposer of the block.
bytes proposer_address = 8;
// scheduled_msgs_hash
bytes implicit_hash = 9;
}

// Extends a vote with application-injected data
Expand All @@ -169,6 +173,8 @@ message RequestExtendVote {
bytes next_validators_hash = 7;
// address of the public key of the original proposer of the block.
bytes proposer_address = 8;
// scheduled_msgs_hash
bytes implicit_hash = 9;
}

// Verify the vote extension
Expand All @@ -194,6 +200,7 @@ message RequestFinalizeBlock {
bytes proposer_address = 8;
tendermint.types.EncryptedRandom encrypted_random = 9; // a random number encrypted by the proposer to be used in contracts
tendermint.types.Commit commit = 10;
bytes implicit_hash = 11;
}

//----------------------------------------
Expand Down
231 changes: 144 additions & 87 deletions proto/tendermint/types/types.pb.go

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions proto/tendermint/types/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ message Header {

// encrypted random
EncryptedRandom encrypted_random = 15; // a random number encrypted by the proposer to be used in contracts

// scheduled_msgs_hash
bytes implicit_hash = 16;
}

// Data contains the set of transactions included in the block
Expand Down
10 changes: 9 additions & 1 deletion state/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"context"
"encoding/hex"
"fmt"
"time"

abci "github.com/cometbft/cometbft/abci/types"
cryptoenc "github.com/cometbft/cometbft/crypto/encoding"
"github.com/cometbft/cometbft/libs/fail"
Expand All @@ -14,7 +16,6 @@ import (
"github.com/cometbft/cometbft/proxy"
"github.com/cometbft/cometbft/types"
tmenclave "github.com/scrtlabs/tm-secret-enclave"
"time"
)

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -138,6 +139,7 @@ func (blockExec *BlockExecutor) CreateProposalBlock(
Time: block.Time,
NextValidatorsHash: block.NextValidatorsHash,
ProposerAddress: block.ProposerAddress,
ImplicitHash: block.ImplicitHash,
},
)
if err != nil {
Expand Down Expand Up @@ -173,6 +175,7 @@ func (blockExec *BlockExecutor) ProcessProposal(
Misbehavior: block.Evidence.Evidence.ToABCI(),
ProposerAddress: block.ProposerAddress,
NextValidatorsHash: block.NextValidatorsHash,
ImplicitHash: block.ImplicitHash,
})
if err != nil {
return false, err
Expand Down Expand Up @@ -239,6 +242,7 @@ func (blockExec *BlockExecutor) applyBlock(state State, blockID types.BlockID, b
// todo: change to log level debug later
blockExec.logger.Info(fmt.Sprintf("Submitted validator set to enclave for height %d, val set hash: %s", block.Height, hex.EncodeToString(block.ValidatorsHash)))

fmt.Printf("tendermint implicithash: %+v\n", block.ImplicitHash)
// ScrtLabs <- changes end

startTime := time.Now().UnixNano()
Expand All @@ -253,7 +257,9 @@ func (blockExec *BlockExecutor) applyBlock(state State, blockID types.BlockID, b
Txs: block.Txs.ToSliceOfBytes(),
EncryptedRandom: block.EncryptedRandom.ToProto(),
Commit: blockExec.blockStore.LoadSeenCommit(block.Height).ToProto(),
ImplicitHash: block.ImplicitHash,
})
fmt.Printf("tendermint implicithash2: %+v\n", block.ImplicitHash)
endTime := time.Now().UnixNano()
blockExec.metrics.BlockProcessingTime.Observe(float64(endTime-startTime) / 1000000)
if err != nil {
Expand Down Expand Up @@ -367,6 +373,7 @@ func (blockExec *BlockExecutor) ExtendVote(
Misbehavior: block.Evidence.Evidence.ToABCI(),
NextValidatorsHash: block.NextValidatorsHash,
ProposerAddress: block.ProposerAddress,
ImplicitHash: block.ImplicitHash,
}

resp, err := blockExec.proxyApp.ExtendVote(ctx, &req)
Expand Down Expand Up @@ -769,6 +776,7 @@ func ExecCommitBlock(
DecidedLastCommit: commitInfo,
Misbehavior: block.Evidence.Evidence.ToABCI(),
Txs: block.Txs.ToSliceOfBytes(),
ImplicitHash: block.ImplicitHash,
})
if err != nil {
logger.Error("error in proxyAppConn.FinalizeBlock", "err", err)
Expand Down
16 changes: 13 additions & 3 deletions state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ package state

import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"os"
"time"
"encoding/hex"

"github.com/cosmos/gogoproto/proto"

tmenclave "github.com/scrtlabs/tm-secret-enclave"
cmtstate "github.com/cometbft/cometbft/proto/tendermint/state"
cmtversion "github.com/cometbft/cometbft/proto/tendermint/version"
"github.com/cometbft/cometbft/types"
cmttime "github.com/cometbft/cometbft/types/time"
"github.com/cometbft/cometbft/version"
tmenclave "github.com/scrtlabs/tm-secret-enclave"
)

// database keys
Expand Down Expand Up @@ -278,6 +279,12 @@ func (state State) MakeBlock(
// println("Invalid random generated")
panic("Failed to validate generated random")
}

implicitHash, err := tmenclave.GetImplicitHash()
fmt.Printf("Implicit hash: %s\n", hex.EncodeToString(implicitHash))
if err != nil {
panic("Failed to get implicit hash")
}
// ScrtLabs changes out <-

// Fill rest of header with state data.
Expand All @@ -286,9 +293,12 @@ func (state State) MakeBlock(
timestamp, state.LastBlockID,
state.Validators.Hash(), state.NextValidators.Hash(),
state.ConsensusParams.Hash(), state.AppHash, state.LastResultsHash,
proposerAddress, &encryptedRandom,
proposerAddress, &encryptedRandom, implicitHash,
)

fmt.Printf("------------ HEADER ---------------\n %+v", block.Header)
fmt.Printf("------------ HEADER HASH : %+v\n", block.Header.Hash())

return block
}

Expand Down
13 changes: 11 additions & 2 deletions state/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package state

import (
"bytes"
"encoding/hex"
"errors"
"fmt"
"encoding/hex"
tmenclave "github.com/scrtlabs/tm-secret-enclave"

"github.com/cometbft/cometbft/crypto"
"github.com/cometbft/cometbft/types"
tmenclave "github.com/scrtlabs/tm-secret-enclave"
)

//-----------------------------------------------------
Expand Down Expand Up @@ -119,6 +120,14 @@ func validateBlock(state State, block *types.Block) error {
}
}

expectedImplicitHash, err := tmenclave.GetImplicitHash() // hash from last block's scheduling
if err != nil {
return fmt.Errorf("failed to get implicit hash: %w", err)
}
if !bytes.Equal(block.Header.ImplicitHash, expectedImplicitHash) {
return fmt.Errorf("implicit_hash mismatch: expected %X, got %X", hex.EncodeToString(expectedImplicitHash), hex.EncodeToString(block.Header.ImplicitHash))
}

// Validate block Time
switch {
case block.Height > state.InitialHeight:
Expand Down
42 changes: 38 additions & 4 deletions types/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"runtime/debug"
"strings"
"time"

Expand Down Expand Up @@ -356,7 +357,8 @@ type Header struct {

// ScrtLabs -> change in
// encrypted Random Source
EncryptedRandom *EnclaveRandom `json:"encrypted_random"`
EncryptedRandom *EnclaveRandom `json:"encrypted_random"`
ImplicitHash cmtbytes.HexBytes `json:"implicit_hash"`
// ScrtLabs <- change out
}

Expand All @@ -367,7 +369,7 @@ func (h *Header) Populate(
timestamp time.Time, lastBlockID BlockID,
valHash, nextValHash []byte,
consensusHash, appHash, lastResultsHash []byte,
proposerAddress Address, encryptedRandom *EnclaveRandom,
proposerAddress Address, encryptedRandom *EnclaveRandom, implicitHash []byte,
) {
h.Version = version
h.ChainID = chainID
Expand All @@ -381,6 +383,7 @@ func (h *Header) Populate(
h.ProposerAddress = proposerAddress
// ScrtLabs -> change in
h.EncryptedRandom = encryptedRandom
h.ImplicitHash = implicitHash
// ScrtLabs <- change out
}

Expand Down Expand Up @@ -433,6 +436,9 @@ func (h Header) ValidateBasic() error {
if err := ValidateHash(h.NextValidatorsHash); err != nil {
return fmt.Errorf("wrong NextValidatorsHash: %v", err)
}
if err := ValidateHash(h.ImplicitHash); err != nil {
return fmt.Errorf("wrong ImplicitHash: %v", err)
}
if err := ValidateHash(h.ConsensusHash); err != nil {
return fmt.Errorf("wrong ConsensusHash: %v", err)
}
Expand Down Expand Up @@ -469,7 +475,27 @@ func (h *Header) Hash() cmtbytes.HexBytes {
if err != nil {
return nil
}
return merkle.HashFromByteSlices([][]byte{
fmt.Printf("h.ImplicitHash: %X\n", h.ImplicitHash)
debug.PrintStack()

merkleHash1 := merkle.HashFromByteSlices([][]byte{
hbz,
cdcEncode(h.ChainID),
cdcEncode(h.Height),
pbt,
bzbi,
cdcEncode(h.LastCommitHash),
cdcEncode(h.DataHash),
cdcEncode(h.ValidatorsHash),
cdcEncode(h.NextValidatorsHash),
cdcEncode(h.ConsensusHash),
cdcEncode(h.AppHash),
cdcEncode(h.LastResultsHash),
cdcEncode(h.EvidenceHash),
cdcEncode(h.ProposerAddress),
cdcEncode(h.ImplicitHash),
})
merkleHash2 := merkle.HashFromByteSlices([][]byte{
hbz,
cdcEncode(h.ChainID),
cdcEncode(h.Height),
Expand All @@ -485,6 +511,9 @@ func (h *Header) Hash() cmtbytes.HexBytes {
cdcEncode(h.EvidenceHash),
cdcEncode(h.ProposerAddress),
})
fmt.Printf("merkleHash1: %X\n", merkleHash1)
fmt.Printf("merkleHash2: %X\n", merkleHash2)
return merkleHash1
}

// StringIndented returns an indented string representation of the header.
Expand All @@ -507,6 +536,7 @@ func (h *Header) StringIndented(indent string) string {
%s Results: %v
%s Evidence: %v
%s Proposer: %v
%s ImplicitHash: %v
%s}#%v`,
indent, h.Version,
indent, h.ChainID,
Expand All @@ -522,6 +552,7 @@ func (h *Header) StringIndented(indent string) string {
indent, h.LastResultsHash,
indent, h.EvidenceHash,
indent, h.ProposerAddress,
indent, h.ImplicitHash,
indent, h.Hash(),
)
}
Expand All @@ -548,7 +579,8 @@ func (h *Header) ToProto() *cmtproto.Header {
LastCommitHash: h.LastCommitHash,
ProposerAddress: h.ProposerAddress,
// ScrtLabs -> change in
EncryptedRandom: h.EncryptedRandom.ToProto(),
EncryptedRandom: h.EncryptedRandom.ToProto(),
ImplicitHash: h.ImplicitHash,
// ScrtLabs <- change out
}
}
Expand Down Expand Up @@ -591,6 +623,8 @@ func HeaderFromProto(ph *cmtproto.Header) (Header, error) {
h.ProposerAddress = ph.ProposerAddress
// ScrtLabs -> change in
h.EncryptedRandom = encRandom
h.ImplicitHash = ph.ImplicitHash
fmt.Printf("h.ImplicitHash: %X\n", h.ImplicitHash)
// ScrtLabs <- change out
return *h, h.ValidateBasic()
}
Expand Down
5 changes: 5 additions & 0 deletions types/vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"runtime/debug"
"time"

"github.com/cometbft/cometbft/crypto"
Expand Down Expand Up @@ -146,6 +147,10 @@ func (vote *Vote) ExtendedCommitSig() ExtendedCommitSig {
//
// See CanonicalizeVote
func VoteSignBytes(chainID string, vote *cmtproto.Vote) []byte {
fmt.Printf("----------------debugStack for VoteSignBytes ---------------\n")
debug.PrintStack()
fmt.Printf("VoteSignBytes: %v\n", vote)
fmt.Printf("vote.BlockID: %v\n", vote.BlockID)
pb := CanonicalizeVote(chainID, vote)
bz, err := protoio.MarshalDelimited(&pb)
if err != nil {
Expand Down