Skip to content

Commit

Permalink
fix: implement genesis export for audit and cert stores
Browse files Browse the repository at this point in the history
Signed-off-by: Artur Troian <[email protected]>
  • Loading branch information
troian committed Jul 26, 2024
1 parent f417f49 commit 89368ad
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 3 deletions.
53 changes: 50 additions & 3 deletions x/audit/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,72 @@ import (

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
abci "github.com/tendermint/tendermint/abci/types"

"github.com/akash-network/node/x/audit/keeper"

types "github.com/akash-network/akash-api/go/node/audit/v1beta3"

"github.com/akash-network/node/x/audit/keeper"
)

// ValidateGenesis does validation check of the Genesis and returns error incase of failure
// ValidateGenesis does validation check of the Genesis and returns error in case of a failure
func ValidateGenesis(data *types.GenesisState) error {
for _, record := range data.Attributes {
if _, err := sdk.AccAddressFromBech32(record.Owner); err != nil {
return sdkerrors.ErrInvalidAddress.Wrap("audited attributes: invalid owner address")
}

if _, err := sdk.AccAddressFromBech32(record.Auditor); err != nil {
return sdkerrors.ErrInvalidAddress.Wrap("audited attributes: invalid auditor address")
}

if err := record.Attributes.Validate(); err != nil {
sdkerrors.Wrap(err, "audited attributes: invalid attributes")
}
}

return nil
}

// InitGenesis initiate genesis state and return updated validator details
func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, data *types.GenesisState) []abci.ValidatorUpdate {
for _, record := range data.Attributes {
owner, err := sdk.AccAddressFromBech32(record.Owner)

if err != nil {
panic(sdkerrors.ErrInvalidAddress.Wrap("audited attributes: invalid owner address").Error())
}

auditor, err := sdk.AccAddressFromBech32(record.Auditor)
if err != nil {
panic(sdkerrors.ErrInvalidAddress.Wrap("audited attributes: invalid auditor address"))
}

err = keeper.CreateOrUpdateProviderAttributes(ctx, types.ProviderID{
Owner: owner,
Auditor: auditor,
}, record.Attributes)
if err != nil {
panic(sdkerrors.Wrap(err, "unable to init genesis with provider"))
}
}

return []abci.ValidatorUpdate{}
}

// ExportGenesis returns genesis state as raw bytes for the provider module
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
var attr []types.AuditedAttributes

k.WithProviders(ctx, func(provider types.Provider) bool {
attr = append(attr, types.AuditedAttributes{
Owner: provider.Owner,
Auditor: provider.Auditor,
Attributes: provider.Attributes.Dup(),
})
return false
})

return &types.GenesisState{}
}

Expand Down
27 changes: 27 additions & 0 deletions x/cert/genesis.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cert

import (
"crypto/x509"
"encoding/json"
"encoding/pem"
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
Expand Down Expand Up @@ -42,6 +44,31 @@ func InitGenesis(ctx sdk.Context, keeper keeper.Keeper, data *types.GenesisState

// ExportGenesis returns genesis state as raw bytes for the provider module
func ExportGenesis(ctx sdk.Context, k keeper.Keeper) *types.GenesisState {
var res types.GenesisCertificates

k.WithCertificates1(ctx, func(id types.CertID, certificate types.CertificateResponse) bool {
block, rest := pem.Decode(certificate.Certificate.Cert)
if len(rest) > 0 {
panic(fmt.Sprintf("unable to decode certificate"))
}

cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
panic(err.Error())
}

if cert.SerialNumber.String() != id.Serial.String() {
panic(fmt.Sprintf("certificate id does not match"))
}

res = append(res, types.GenesisCertificate{
Owner: id.Owner.String(),
Certificate: certificate.Certificate,
})

return false
})

return &types.GenesisState{}
}

Expand Down
26 changes: 26 additions & 0 deletions x/cert/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package keeper

import (
"encoding/pem"

Check failure on line 4 in x/cert/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests

"encoding/pem" imported and not used

Check failure on line 4 in x/cert/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / coverage

"encoding/pem" imported and not used

Check failure on line 4 in x/cert/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / build-bins

"encoding/pem" imported and not used

Check failure on line 4 in x/cert/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / build-macos

"encoding/pem" imported and not used

Check failure on line 4 in x/cert/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / lint

"encoding/pem" imported and not used

Check failure on line 4 in x/cert/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / sims

"encoding/pem" imported and not used
"fmt"

Check failure on line 5 in x/cert/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests

"fmt" imported and not used

Check failure on line 5 in x/cert/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / coverage

"fmt" imported and not used

Check failure on line 5 in x/cert/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / build-bins

"fmt" imported and not used

Check failure on line 5 in x/cert/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / build-macos

"fmt" imported and not used

Check failure on line 5 in x/cert/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / lint

"fmt" imported and not used

Check failure on line 5 in x/cert/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / sims

"fmt" imported and not used

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand All @@ -16,6 +19,7 @@ type Keeper interface {
RevokeCertificate(sdk.Context, types.CertID) error
GetCertificateByID(ctx sdk.Context, id types.CertID) (types.CertificateResponse, bool)
WithCertificates(ctx sdk.Context, fn func(certificate types.CertificateResponse) bool)
WithCertificates1(ctx sdk.Context, fn func(id types.CertID, certificate types.CertificateResponse) bool)
WithCertificatesState(ctx sdk.Context, state types.Certificate_State, fn func(certificate types.CertificateResponse) bool)
WithOwner(ctx sdk.Context, id sdk.Address, fn func(types.CertificateResponse) bool)
WithOwnerState(ctx sdk.Context, id sdk.Address, state types.Certificate_State, fn func(types.CertificateResponse) bool)
Expand Down Expand Up @@ -134,6 +138,28 @@ func (k keeper) WithCertificates(ctx sdk.Context, fn func(certificate types.Cert
}
}

// WithCertificates1 iterates all certificates
func (k keeper) WithCertificates1(ctx sdk.Context, fn func(id types.CertID, certificate types.CertificateResponse) bool) {
store := ctx.KVStore(k.skey)
iter := store.Iterator(nil, nil)

defer func() {
_ = iter.Close()
}()

for ; iter.Valid(); iter.Next() {
id, err := parseCertID(iter.Key())
if err != nil {
panic(err.Error())
}

item := k.mustUnmarshal(iter.Key(), iter.Value())
if stop := fn(id, item); stop {
break
}
}
}

// WithCertificatesState iterates all certificates in certain state
func (k keeper) WithCertificatesState(ctx sdk.Context, state types.Certificate_State, fn func(certificate types.CertificateResponse) bool) {
store := ctx.KVStore(k.skey)
Expand Down
25 changes: 25 additions & 0 deletions x/cert/keeper/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,28 @@ func certificateSerialFromKey(key []byte) big.Int {

return *new(big.Int).SetBytes(key[keyAddrPrefixLen+addrLen:])
}

func parseCertID(from []byte) (types.CertID, error) {
res := types.CertID{
Serial: *big.NewInt(0),
}

// first byte is prefix id. skip it
from = from[1:]
addLen := from[0]

from = from[1:]

addr := from[:addLen-1]
serial := from[addLen:]

err := sdk.VerifyAddressFormat(addr)
if err != nil {
return res, err
}

res.Owner = sdk.AccAddress(addr)
res.Serial.SetBytes(serial)

return res, nil
}

0 comments on commit 89368ad

Please sign in to comment.