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 29, 2024
1 parent f417f49 commit a70e8b8
Show file tree
Hide file tree
Showing 4 changed files with 125 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")

Check failure on line 28 in x/audit/genesis.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `sdkerrors.Wrap` is not checked (errcheck)
}
}

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"))

Check failure on line 52 in x/cert/genesis.go

View workflow job for this annotation

GitHub Actions / lint

S1039: unnecessary use of fmt.Sprintf (gosimple)
}

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"))

Check failure on line 61 in x/cert/genesis.go

View workflow job for this annotation

GitHub Actions / lint

S1039: unnecessary use of fmt.Sprintf (gosimple)
}

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

return false
})

return &types.GenesisState{}
}

Expand Down
23 changes: 23 additions & 0 deletions x/cert/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,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 +135,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 a70e8b8

Please sign in to comment.