Skip to content

Commit

Permalink
feat!: x/POA Module rewrite (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups authored Oct 20, 2023
1 parent 9ee0d51 commit b8e962d
Show file tree
Hide file tree
Showing 80 changed files with 10,280 additions and 9,447 deletions.
73 changes: 73 additions & 0 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: e2e
on:
push:
branches:
- main
pull_request:

env:
LINT_VERSION: v1.52
GO_VERSION: 1.21.0

TAR_PATH: /tmp/poa.tar
IMAGE_NAME: poa
DOCKER_TAG: poa:local

jobs:
build-docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Go ${{ env.GO_VERSION }}
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and export
uses: docker/build-push-action@v5
with:
tags: ${{ env.DOCKER_TAG }}
outputs: type=docker,dest=${{ env.TAR_PATH }}

- name: Upload host artifact
uses: actions/upload-artifact@v3
with:
name: ${{ env.IMAGE_NAME }}
path: ${{ env.TAR_PATH }}

test:
needs: build-docker
runs-on: ubuntu-latest
strategy:
matrix:
test:
- "ictest-poa"
fail-fast: false

steps:
- name: Set up Go ${{ env.GO_VERSION }}
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}

- name: checkout chain
uses: actions/checkout@v4

- name: Download Host Artifact
uses: actions/download-artifact@v3
with:
name: ${{ env.IMAGE_NAME }}
path: /tmp

- name: Load Docker Image
run: |
docker image load -i ${{ env.TAR_PATH }}
docker image ls -a
- name: Run Test
run: make ${{ matrix.test }}
21 changes: 0 additions & 21 deletions .github/workflows/go-integration.yml

This file was deleted.

4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ go.work
go.work.sum

# Depinject debug file
debug_container.dot
debug_container.dot

build/
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# make local-image
# docker run --rm -it poa:local q

FROM golang:1.21-alpine3.18 as builder

RUN set -eux; apk add --no-cache git libusb-dev linux-headers gcc musl-dev make go;

ENV GOPATH=""

COPY go.mod .
COPY go.sum .
RUN go mod download

COPY . .

RUN cd simapp && make build

FROM alpine:3.18

COPY --from=builder /go/simapp/build/* /bin/poad

ENTRYPOINT ["/bin/poad"]
18 changes: 14 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

DOCKER := $(shell which docker)

##################
### Build ####
##################
export GO111MODULE = on

####################
### Testing ####
####################

test:
@echo "--> Running tests"
Expand All @@ -14,7 +16,15 @@ test-integration:
@echo "--> Running integration tests"
cd integration; go test -v ./...

.PHONY: test test-integration
ictest-poa:
cd e2e && go clean -testcache && go test -race -v -run TestPOA .

###############################################################################
### Docker ###
###############################################################################

local-image:
docker build . -t poa:local

###################
### Protobuf ####
Expand Down
50 changes: 1 addition & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,7 @@ Describe specialized concepts and definitions used throughout the spec.

## State



Each Begin Block we iterate over the unconfirmed store and check if 2/3+ of validators have seen the same hash. If so, we finalize the attestation and add it to the finalized store.

```go
func (ave AttestationVoteExtension) ExtendVote(ctx sdk.Context, req *abci.RequestExtendVote) (*abci.ResponseExtendVote, error) {
// TODO: do we limit size of votes each block?
messagemax := ave.MaxVoteSize(ctx)
// []*CoveredHeader
observations := ave.Cache.CoveredHeaderObservations(ctx)
marshalledObservations := [][]byte{}
for _, observation := range observations {
bz, err := json.Marshal(observation)
if err != nil {
return nil, fmt.Errorf("failed to encode vote extension: %w", err)
}
if messagemax - len(bz) > 0 {
marshalledObservations = append(marshalledObservations, bz)
messagemax -= len(bz)
} else {
break
}
}
// turn the [][]byte into single []byte parsable json (i.e. add commas and [])
return &abci.ResponseExtendVote{VoteExtension: bz}, nil
}

// TODO: can we track covered chains and latest heights on the ve struct?
func (ave AttestationVoteExtension) VerifyVoteExtension(ctx sdk.Context, req *abci.RequestVerifyVoteExtension) (*abci.ResponseVerifyVoteExtension, error) {
var vote CoveredHeader
if err := json.Unmarshal(req.VoteExtension, &vote); err != nil {
return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_REJECT}, nil
}
// map[chain_id]height
chains := ave.CoveredChains(ctx)
switch {
case chains[req.ChainId] == nil:
return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_REJECT}, nil
case chains[req.ChainId] >= req.Height:
return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_REJECT}, nil
case len(ve.Data) != 1024:
return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_REJECT}, nil
}
if err := ave.Keeper.WriteHeaderAttestation(ctx, vote.HeaderAttestation(req.Validator)); err != nil {
return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_REJECT}, nil
}
return &abci.ResponseVerifyVoteExtension{Status: abci.ResponseVerifyVoteExtension_ACCEPT}, nil
}
```
Specify state stored with the module and the flow

## Messages

Expand Down
59 changes: 59 additions & 0 deletions ante/disable_staking.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package poaante

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/strangelove-ventures/poa"

stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
)

type MsgStakingFilterDecorator struct {
}

func NewPOAStakingFilterDecorator() MsgStakingFilterDecorator {
return MsgStakingFilterDecorator{}
}

// AnteHandle performs an AnteHandler check that returns an error if the tx contains a message that is blocked.
func (msfd MsgStakingFilterDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
currHeight := ctx.BlockHeight()
if currHeight <= 1 {
// allow GenTx to pass
return next(ctx, tx, simulate)
}

invalid, err := msfd.hasInvalidStakingMsg(tx.GetMsgs())
if err != nil {
return ctx, err
}

if invalid {
return ctx, poa.ErrStakingActionNotAllowed
}

return next(ctx, tx, simulate)
}

func (msfd MsgStakingFilterDecorator) hasInvalidStakingMsg(msgs []sdk.Msg) (bool, error) {
for _, msg := range msgs {
switch msg.(type) {
case *stakingtypes.MsgBeginRedelegate:
return true, nil
case *stakingtypes.MsgCancelUnbondingDelegation:
return true, nil
case *stakingtypes.MsgCreateValidator: // TODO:? wrap it to our own Message to allow & mint the needed token?
return true, nil
case *stakingtypes.MsgDelegate:
return true, nil
case *stakingtypes.MsgEditValidator: // TODO:?
return true, nil
case *stakingtypes.MsgUndelegate:
return true, nil
case *stakingtypes.MsgUpdateParams: // TODO: ? allow or wrap with isAdmin?
return true, nil
default:
return false, nil
}
}
return false, nil
}
Loading

0 comments on commit b8e962d

Please sign in to comment.