Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Add ABCI methods for Fraud Proof Generation #43

Merged
merged 5 commits into from
Oct 11, 2022
Merged
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
5 changes: 5 additions & 0 deletions abci/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ type Client interface {
OfferSnapshotSync(types.RequestOfferSnapshot) (*types.ResponseOfferSnapshot, error)
LoadSnapshotChunkSync(types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error)
ApplySnapshotChunkSync(types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error)
GetAppHashSync(types.RequestGetAppHash) (*types.ResponseGetAppHash, error)
GenerateFraudProofSync(types.RequestGenerateFraudProof) (*types.ResponseGenerateFraudProof, error)
TriggerFraudProofGenerationModeSync(
types.RequestTriggerFraudProofGenerationMode,
) (*types.ResponseTriggerFraudProofGenerationMode, error)
}

//----------------------------------------
Expand Down
51 changes: 51 additions & 0 deletions abci/client/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,39 @@ func (cli *grpcClient) ApplySnapshotChunkAsync(params types.RequestApplySnapshot
return cli.finishAsyncCall(req, &types.Response{Value: &types.Response_ApplySnapshotChunk{ApplySnapshotChunk: res}})
}

func (cli *grpcClient) GetAppHashAsync(params types.RequestGetAppHash) *ReqRes {
req := types.ToRequestGetAppHash(params)
res, err := cli.client.GetAppHash(context.Background(), req.GetGetAppHash(), grpc.WaitForReady(true))
if err != nil {
cli.StopForError(err)
}
return cli.finishAsyncCall(req, &types.Response{Value: &types.Response_GetAppHash{GetAppHash: res}})
}

func (cli *grpcClient) GenerateFraudProofAsync(params types.RequestGenerateFraudProof) *ReqRes {
req := types.ToRequestGenerateFraudProof(params)
res, err := cli.client.GenerateFraudProof(context.Background(), req.GetGenerateFraudProof(), grpc.WaitForReady(true))
if err != nil {
cli.StopForError(err)
}
return cli.finishAsyncCall(req, &types.Response{Value: &types.Response_GenerateFraudProof{GenerateFraudProof: res}})
}

func (cli *grpcClient) TriggerFraudProofGenerationModeAsync(
params types.RequestTriggerFraudProofGenerationMode,
) *ReqRes {
req := types.ToRequestTriggerFraudProofGenerationMode(params)
res, err := cli.client.TriggerFraudProofGenerationMode(
context.Background(), req.GetTriggerFraudProofGenerationMode(), grpc.WaitForReady(true),
)
if err != nil {
cli.StopForError(err)
}
return cli.finishAsyncCall(
req, &types.Response{Value: &types.Response_TriggerFraudProofGenerationMode{TriggerFraudProofGenerationMode: res}},
)
}

// finishAsyncCall creates a ReqRes for an async call, and immediately populates it
// with the response. We don't complete it until it's been ordered via the channel.
func (cli *grpcClient) finishAsyncCall(req *types.Request, res *types.Response) *ReqRes {
Expand Down Expand Up @@ -417,3 +450,21 @@ func (cli *grpcClient) ApplySnapshotChunkSync(
reqres := cli.ApplySnapshotChunkAsync(params)
return cli.finishSyncCall(reqres).GetApplySnapshotChunk(), cli.Error()
}

func (cli *grpcClient) GetAppHashSync(
params types.RequestGetAppHash) (*types.ResponseGetAppHash, error) {
reqres := cli.GetAppHashAsync(params)
return cli.finishSyncCall(reqres).GetGetAppHash(), cli.Error()
}

func (cli *grpcClient) GenerateFraudProofSync(
params types.RequestGenerateFraudProof) (*types.ResponseGenerateFraudProof, error) {
reqres := cli.GenerateFraudProofAsync(params)
return cli.finishSyncCall(reqres).GetGenerateFraudProof(), cli.Error()
}

func (cli *grpcClient) TriggerFraudProofGenerationModeSync(
params types.RequestTriggerFraudProofGenerationMode) (*types.ResponseTriggerFraudProofGenerationMode, error) {
reqres := cli.TriggerFraudProofGenerationModeAsync(params)
return cli.finishSyncCall(reqres).GetTriggerFraudProofGenerationMode(), cli.Error()
}
60 changes: 60 additions & 0 deletions abci/client/local_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,39 @@ func (app *localClient) ApplySnapshotChunkAsync(req types.RequestApplySnapshotCh
)
}

func (app *localClient) GetAppHashAsync(req types.RequestGetAppHash) *ReqRes {
app.mtx.Lock()
defer app.mtx.Unlock()

res := app.Application.GetAppHash(req)
return app.callback(
types.ToRequestGetAppHash(req),
types.ToResponseGetAppHash(res),
)
}

func (app *localClient) GenerateFraudProofAsync(req types.RequestGenerateFraudProof) *ReqRes {
app.mtx.Lock()
defer app.mtx.Unlock()

res := app.Application.GenerateFraudProof(req)
return app.callback(
types.ToRequestGenerateFraudProof(req),
types.ToResponseGenerateFraudProof(res),
)
}

func (app *localClient) TriggerFraudProofGenerationModeAsync(req types.RequestTriggerFraudProofGenerationMode) *ReqRes {
app.mtx.Lock()
defer app.mtx.Unlock()

res := app.Application.TriggerFraudProofGenerationMode(req)
return app.callback(
types.ToRequestTriggerFraudProofGenerationMode(req),
types.ToResponseTriggerFraudProofGenerationMode(res),
)
}

//-------------------------------------------------------

func (app *localClient) FlushSync() error {
Expand Down Expand Up @@ -323,6 +356,33 @@ func (app *localClient) ApplySnapshotChunkSync(
return &res, nil
}

func (app *localClient) GetAppHashSync(
req types.RequestGetAppHash) (*types.ResponseGetAppHash, error) {
app.mtx.Lock()
defer app.mtx.Unlock()

res := app.Application.GetAppHash(req)
return &res, nil
}

func (app *localClient) GenerateFraudProofSync(
req types.RequestGenerateFraudProof) (*types.ResponseGenerateFraudProof, error) {
app.mtx.Lock()
defer app.mtx.Unlock()

res := app.Application.GenerateFraudProof(req)
return &res, nil
}

func (app *localClient) TriggerFraudProofGenerationModeSync(
req types.RequestTriggerFraudProofGenerationMode) (*types.ResponseTriggerFraudProofGenerationMode, error) {
app.mtx.Lock()
defer app.mtx.Unlock()

res := app.Application.TriggerFraudProofGenerationMode(req)
return &res, nil
}

//-------------------------------------------------------

func (app *localClient) callback(req *types.Request, res *types.Response) *ReqRes {
Expand Down
70 changes: 70 additions & 0 deletions abci/client/mocks/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions abci/client/socket_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,22 @@ func (cli *socketClient) ApplySnapshotChunkAsync(req types.RequestApplySnapshotC
return cli.queueRequest(types.ToRequestApplySnapshotChunk(req))
}

func (cli *socketClient) GetAppHashAsync(req types.RequestGetAppHash) *ReqRes {
return cli.queueRequest(types.ToRequestGetAppHash(req))
}

func (cli *socketClient) GenerateFraudProofAsync(
req types.RequestGenerateFraudProof,
) *ReqRes {
return cli.queueRequest(types.ToRequestGenerateFraudProof(req))
}

func (cli *socketClient) TriggerFraudProofGenerationModeAsync(
req types.RequestTriggerFraudProofGenerationMode,
) *ReqRes {
return cli.queueRequest(types.ToRequestTriggerFraudProofGenerationMode(req))
}

//----------------------------------------

func (cli *socketClient) FlushSync() error {
Expand Down Expand Up @@ -417,6 +433,33 @@ func (cli *socketClient) ApplySnapshotChunkSync(
return reqres.Response.GetApplySnapshotChunk(), cli.Error()
}

func (cli *socketClient) GetAppHashSync(
req types.RequestGetAppHash) (*types.ResponseGetAppHash, error) {
reqres := cli.queueRequest(types.ToRequestGetAppHash(req))
if err := cli.FlushSync(); err != nil {
return nil, err
}
return reqres.Response.GetGetAppHash(), cli.Error()
}

func (cli *socketClient) GenerateFraudProofSync(
req types.RequestGenerateFraudProof) (*types.ResponseGenerateFraudProof, error) {
reqres := cli.queueRequest(types.ToRequestGenerateFraudProof(req))
if err := cli.FlushSync(); err != nil {
return nil, err
}
return reqres.Response.GetGenerateFraudProof(), cli.Error()
}

func (cli *socketClient) TriggerFraudProofGenerationModeSync(
req types.RequestTriggerFraudProofGenerationMode) (*types.ResponseTriggerFraudProofGenerationMode, error) {
reqres := cli.queueRequest(types.ToRequestTriggerFraudProofGenerationMode(req))
if err := cli.FlushSync(); err != nil {
return nil, err
}
return reqres.Response.GetTriggerFraudProofGenerationMode(), cli.Error()
}

//----------------------------------------

func (cli *socketClient) queueRequest(req *types.Request) *ReqRes {
Expand Down
15 changes: 15 additions & 0 deletions abci/example/kvstore/persistent_kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,21 @@ func (app *PersistentKVStoreApplication) ApplySnapshotChunk(
return types.ResponseApplySnapshotChunk{Result: types.ResponseApplySnapshotChunk_ABORT}
}

func (app *PersistentKVStoreApplication) GetAppHash(
req types.RequestGetAppHash) types.ResponseGetAppHash {
return types.ResponseGetAppHash{}
}

func (app *PersistentKVStoreApplication) GenerateFraudProof(
req types.RequestGenerateFraudProof) types.ResponseGenerateFraudProof {
return types.ResponseGenerateFraudProof{}
}

func (app *PersistentKVStoreApplication) TriggerFraudProofGenerationMode(
req types.RequestTriggerFraudProofGenerationMode) types.ResponseTriggerFraudProofGenerationMode {
return types.ResponseTriggerFraudProofGenerationMode{}
}

//---------------------------------------------
// update validators

Expand Down
9 changes: 9 additions & 0 deletions abci/server/socket_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,15 @@ func (s *SocketServer) handleRequest(req *types.Request, responses chan<- *types
case *types.Request_ApplySnapshotChunk:
res := s.app.ApplySnapshotChunk(*r.ApplySnapshotChunk)
responses <- types.ToResponseApplySnapshotChunk(res)
case *types.Request_GetAppHash:
res := s.app.GetAppHash(*r.GetAppHash)
responses <- types.ToResponseGetAppHash(res)
case *types.Request_GenerateFraudProof:
res := s.app.GenerateFraudProof(*r.GenerateFraudProof)
responses <- types.ToResponseGenerateFraudProof(res)
case *types.Request_TriggerFraudProofGenerationMode:
res := s.app.TriggerFraudProofGenerationMode(*r.TriggerFraudProofGenerationMode)
responses <- types.ToResponseTriggerFraudProofGenerationMode(res)
default:
responses <- types.ToResponseException("Unknown request")
}
Expand Down
53 changes: 48 additions & 5 deletions abci/types/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,23 @@ type Application interface {
CheckTx(RequestCheckTx) ResponseCheckTx // Validate a tx for the mempool

// Consensus Connection
InitChain(RequestInitChain) ResponseInitChain // Initialize blockchain w validators/other info from TendermintCore
BeginBlock(RequestBeginBlock) ResponseBeginBlock // Signals the beginning of a block
DeliverTx(RequestDeliverTx) ResponseDeliverTx // Deliver a tx for full processing
EndBlock(RequestEndBlock) ResponseEndBlock // Signals the end of a block, returns changes to the validator set
Commit() ResponseCommit // Commit the state and return the application Merkle root hash

// Initialize blockchain w validators/other info from TendermintCore
InitChain(RequestInitChain) ResponseInitChain
// Signals the beginning of a block
BeginBlock(RequestBeginBlock) ResponseBeginBlock
// Deliver a tx for full processing
DeliverTx(RequestDeliverTx) ResponseDeliverTx
// Signals the end of a block, returns changes to the validator set
EndBlock(RequestEndBlock) ResponseEndBlock
// Commit the state and return the application Merkle root hash
Commit() ResponseCommit
// Get appHash
GetAppHash(RequestGetAppHash) ResponseGetAppHash
// Generate FraudProof
GenerateFraudProof(RequestGenerateFraudProof) ResponseGenerateFraudProof
// Trigger Fraud Proof Generation Mode
TriggerFraudProofGenerationMode(RequestTriggerFraudProofGenerationMode) ResponseTriggerFraudProofGenerationMode

// State Sync Connection
ListSnapshots(RequestListSnapshots) ResponseListSnapshots // List available snapshots
Expand Down Expand Up @@ -95,6 +107,19 @@ func (BaseApplication) ApplySnapshotChunk(req RequestApplySnapshotChunk) Respons
return ResponseApplySnapshotChunk{}
}

func (BaseApplication) GetAppHash(req RequestGetAppHash) ResponseGetAppHash {
return ResponseGetAppHash{}
}

func (BaseApplication) GenerateFraudProof(req RequestGenerateFraudProof) ResponseGenerateFraudProof {
return ResponseGenerateFraudProof{}
}

func (BaseApplication) TriggerFraudProofGenerationMode(
req RequestTriggerFraudProofGenerationMode) ResponseTriggerFraudProofGenerationMode {
return ResponseTriggerFraudProofGenerationMode{}
}

//-------------------------------------------------------

// GRPCApplication is a GRPC wrapper for Application
Expand Down Expand Up @@ -182,3 +207,21 @@ func (app *GRPCApplication) ApplySnapshotChunk(
res := app.app.ApplySnapshotChunk(*req)
return &res, nil
}

func (app *GRPCApplication) GetAppHash(
ctx context.Context, req *RequestGetAppHash) (*ResponseGetAppHash, error) {
res := app.app.GetAppHash(*req)
return &res, nil
}

func (app *GRPCApplication) GenerateFraudProof(
ctx context.Context, req *RequestGenerateFraudProof) (*ResponseGenerateFraudProof, error) {
res := app.app.GenerateFraudProof(*req)
return &res, nil
}

func (app *GRPCApplication) TriggerFraudProofGenerationMode(
ctx context.Context, req *RequestTriggerFraudProofGenerationMode) (*ResponseTriggerFraudProofGenerationMode, error) {
res := app.app.TriggerFraudProofGenerationMode(*req)
return &res, nil
}
Loading