Skip to content

Commit

Permalink
Cleanup fx interface compliance (ava-labs#1599)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph committed Jun 8, 2023
1 parent 0f6b09d commit 7b9912d
Show file tree
Hide file tree
Showing 19 changed files with 87 additions and 68 deletions.
2 changes: 0 additions & 2 deletions scripts/mocks.mockgen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,9 @@ github.com/ava-labs/avalanchego/vms/avm/metrics=Metrics=vms/avm/metrics/mock_met
github.com/ava-labs/avalanchego/vms/avm/states=Chain,State,Diff=vms/avm/states/mock_states.go
github.com/ava-labs/avalanchego/vms/avm/txs/mempool=Mempool=vms/avm/txs/mempool/mock_mempool.go
github.com/ava-labs/avalanchego/vms/components/avax=TransferableIn=vms/components/avax/mock_transferable_in.go
github.com/ava-labs/avalanchego/vms/components/avax=TransferableOut=vms/components/avax/mock_transferable_out.go
github.com/ava-labs/avalanchego/vms/components/verify=Verifiable=vms/components/verify/mock_verifiable.go
github.com/ava-labs/avalanchego/vms/platformvm/blocks/executor=Manager=vms/platformvm/blocks/executor/mock_manager.go
github.com/ava-labs/avalanchego/vms/platformvm/blocks=Block=vms/platformvm/blocks/mock_block.go
github.com/ava-labs/avalanchego/vms/platformvm/fx=Fx,Owner=vms/platformvm/fx/mock_fx.go
github.com/ava-labs/avalanchego/vms/platformvm/state=Chain=vms/platformvm/state/mock_chain.go
github.com/ava-labs/avalanchego/vms/platformvm/state=Diff=vms/platformvm/state/mock_diff.go
github.com/ava-labs/avalanchego/vms/platformvm/state=StakerIterator=vms/platformvm/state/mock_staker_iterator.go
Expand Down
8 changes: 4 additions & 4 deletions vms/avm/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestSetsAndGets(t *testing.T) {
Fx: &FxTest{
InitializeF: func(vmIntf interface{}) error {
vm := vmIntf.(secp256k1fx.VM)
return vm.CodecRegistry().RegisterType(&avax.TestVerifiable{})
return vm.CodecRegistry().RegisterType(&avax.TestState{})
},
},
}},
Expand All @@ -51,7 +51,7 @@ func TestSetsAndGets(t *testing.T) {
OutputIndex: 1,
},
Asset: avax.Asset{ID: ids.Empty},
Out: &avax.TestVerifiable{},
Out: &avax.TestState{},
}
utxoID := utxo.InputID()

Expand Down Expand Up @@ -116,7 +116,7 @@ func TestFundingNoAddresses(t *testing.T) {
Fx: &FxTest{
InitializeF: func(vmIntf interface{}) error {
vm := vmIntf.(secp256k1fx.VM)
return vm.CodecRegistry().RegisterType(&avax.TestVerifiable{})
return vm.CodecRegistry().RegisterType(&avax.TestState{})
},
},
}},
Expand All @@ -138,7 +138,7 @@ func TestFundingNoAddresses(t *testing.T) {
OutputIndex: 1,
},
Asset: avax.Asset{ID: ids.Empty},
Out: &avax.TestVerifiable{},
Out: &avax.TestState{},
}

state.AddUTXO(utxo)
Expand Down
4 changes: 2 additions & 2 deletions vms/avm/txs/initial_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func TestInitialStateVerifyNilOutput(t *testing.T) {

func TestInitialStateVerifyInvalidOutput(t *testing.T) {
c := linearcodec.NewDefault()
if err := c.RegisterType(&avax.TestVerifiable{}); err != nil {
if err := c.RegisterType(&avax.TestState{}); err != nil {
t.Fatal(err)
}
m := codec.NewDefaultManager()
Expand All @@ -146,7 +146,7 @@ func TestInitialStateVerifyInvalidOutput(t *testing.T) {

is := InitialState{
FxIndex: 0,
Outs: []verify.State{&avax.TestVerifiable{Err: errTest}},
Outs: []verify.State{&avax.TestState{Err: errTest}},
}
if err := is.Verify(m, numFxs); err == nil {
t.Fatalf("Should have erred due to an invalid output")
Expand Down
17 changes: 3 additions & 14 deletions vms/components/avax/mock_transferable_out.go

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

23 changes: 16 additions & 7 deletions vms/components/avax/test_verifiable.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,31 @@

package avax

import "github.com/ava-labs/avalanchego/snow"
import (
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/vms/components/verify"
)

type TestVerifiable struct{ Err error }
var (
_ verify.State = (*TestState)(nil)
_ TransferableOut = (*TestTransferable)(nil)
_ Addressable = (*TestAddressable)(nil)
)

func (*TestVerifiable) InitCtx(*snow.Context) {}
type TestState struct {
verify.IsState `json:"-"`

func (v *TestVerifiable) Verify() error {
return v.Err
Err error
}

func (v *TestVerifiable) VerifyState() error {
func (*TestState) InitCtx(*snow.Context) {}

func (v *TestState) Verify() error {
return v.Err
}

type TestTransferable struct {
TestVerifiable
TestState

Val uint64 `serialize:"true"`
}
Expand Down
12 changes: 9 additions & 3 deletions vms/components/verify/verification.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ package verify

import "github.com/ava-labs/avalanchego/snow"

// Verifiable can be verified
type Verifiable interface {
Verify() error
}

// State that can be verified
type State interface {
snow.ContextInitializable
Verifiable
VerifyState() error
IsState
}

type IsState interface {
isState()
}

type IsNotState interface {
isState() error
}

// All returns nil if all the verifiables were verified with no errors
Expand Down
5 changes: 5 additions & 0 deletions vms/nftfx/mint_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ package nftfx
import (
"encoding/json"

"github.com/ava-labs/avalanchego/vms/components/verify"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
)

var _ verify.State = (*MintOutput)(nil)

type MintOutput struct {
verify.IsState `json:"-"`

GroupID uint32 `serialize:"true" json:"groupID"`
secp256k1fx.OutputOwners `serialize:"true"`
}
Expand Down
6 changes: 2 additions & 4 deletions vms/nftfx/transfer_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ var (
)

type TransferOutput struct {
verify.IsState `json:"-"`

GroupID uint32 `serialize:"true" json:"groupID"`
Payload types.JSONByteSlice `serialize:"true" json:"payload"`
secp256k1fx.OutputOwners `serialize:"true"`
Expand Down Expand Up @@ -55,7 +57,3 @@ func (out *TransferOutput) Verify() error {
return out.OutputOwners.Verify()
}
}

func (out *TransferOutput) VerifyState() error {
return out.Verify()
}
2 changes: 1 addition & 1 deletion vms/platformvm/blocks/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func init() {
GenesisCodec = codec.NewManager(math.MaxInt32)

errs := wrappers.Errs{}
for _, c := range []codec.Registry{c, gc} {
for _, c := range []linearcodec.Codec{c, gc} {
errs.Add(
RegisterApricotBlockTypes(c),
txs.RegisterUnsignedTxsTypes(c),
Expand Down
8 changes: 7 additions & 1 deletion vms/platformvm/fx/fx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import (
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
)

var _ Fx = (*secp256k1fx.Fx)(nil)
var (
_ Fx = (*secp256k1fx.Fx)(nil)
_ Owner = (*secp256k1fx.OutputOwners)(nil)
_ Owned = (*secp256k1fx.TransferOutput)(nil)
)

// Fx is the interface a feature extension must implement to support the
// Platform Chain.
Expand Down Expand Up @@ -40,6 +44,8 @@ type Fx interface {
}

type Owner interface {
verify.IsNotState

verify.Verifiable
snow.ContextInitializable
}
Expand Down
3 changes: 3 additions & 0 deletions vms/platformvm/fx/mock_fx.go

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

21 changes: 12 additions & 9 deletions vms/platformvm/txs/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,22 @@ func init() {

// RegisterUnsignedTxsTypes allows registering relevant type of unsigned package
// in the right sequence. Following repackaging of platformvm package, a few
// subpackage-level codecs were introduced, each handling serialization of specific types.
// subpackage-level codecs were introduced, each handling serialization of
// specific types.
//
// RegisterUnsignedTxsTypes is made exportable so to guarantee that other codecs
// are coherent with components one.
func RegisterUnsignedTxsTypes(targetCodec codec.Registry) error {
func RegisterUnsignedTxsTypes(targetCodec linearcodec.Codec) error {
errs := wrappers.Errs{}

// The secp256k1fx is registered here because this is the same place it is
// registered in the AVM. This ensures that the typeIDs match up for utxos
// in shared memory.
errs.Add(targetCodec.RegisterType(&secp256k1fx.TransferInput{}))
targetCodec.SkipRegistrations(1)
errs.Add(targetCodec.RegisterType(&secp256k1fx.TransferOutput{}))
targetCodec.SkipRegistrations(1)
errs.Add(
// The Fx is registered here because this is the same place it is
// registered in the AVM. This ensures that the typeIDs match up for
// utxos in shared memory.
targetCodec.RegisterType(&secp256k1fx.TransferInput{}),
targetCodec.RegisterType(&secp256k1fx.MintOutput{}),
targetCodec.RegisterType(&secp256k1fx.TransferOutput{}),
targetCodec.RegisterType(&secp256k1fx.MintOperation{}),
targetCodec.RegisterType(&secp256k1fx.Credential{}),
targetCodec.RegisterType(&secp256k1fx.Input{}),
targetCodec.RegisterType(&secp256k1fx.OutputOwners{}),
Expand Down
9 changes: 8 additions & 1 deletion vms/propertyfx/mint_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@

package propertyfx

import "github.com/ava-labs/avalanchego/vms/secp256k1fx"
import (
"github.com/ava-labs/avalanchego/vms/components/verify"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
)

var _ verify.State = (*MintOutput)(nil)

type MintOutput struct {
verify.IsState `json:"-"`

secp256k1fx.OutputOwners `serialize:"true"`
}
9 changes: 8 additions & 1 deletion vms/propertyfx/owned_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@

package propertyfx

import "github.com/ava-labs/avalanchego/vms/secp256k1fx"
import (
"github.com/ava-labs/avalanchego/vms/components/verify"
"github.com/ava-labs/avalanchego/vms/secp256k1fx"
)

var _ verify.State = (*OwnedOutput)(nil)

type OwnedOutput struct {
verify.IsState `json:"-"`

secp256k1fx.OutputOwners `serialize:"true"`
}
6 changes: 2 additions & 4 deletions vms/secp256k1fx/mint_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import "github.com/ava-labs/avalanchego/vms/components/verify"
var _ verify.State = (*MintOutput)(nil)

type MintOutput struct {
verify.IsState `json:"-"`

OutputOwners `serialize:"true"`
}

Expand All @@ -19,7 +21,3 @@ func (out *MintOutput) Verify() error {
return out.OutputOwners.Verify()
}
}

func (out *MintOutput) VerifyState() error {
return out.Verify()
}
2 changes: 0 additions & 2 deletions vms/secp256k1fx/mint_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ func TestMintOutputVerify(t *testing.T) {
require := require.New(t)
err := tt.out.Verify()
require.ErrorIs(err, tt.expectedErr)
err = tt.out.VerifyState()
require.ErrorIs(err, tt.expectedErr)
})
}
}
8 changes: 2 additions & 6 deletions vms/secp256k1fx/output_owners.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ var (
ErrOutputUnoptimized = errors.New("output representation should be optimized")
ErrAddrsNotSortedUnique = errors.New("addresses not sorted and unique")
ErrMarshal = errors.New("cannot marshal without ctx")

_ verify.State = (*OutputOwners)(nil)
)

type OutputOwners struct {
verify.IsNotState `json:"-"`

Locktime uint64 `serialize:"true" json:"locktime"`
Threshold uint32 `serialize:"true" json:"threshold"`
Addrs []ids.ShortID `serialize:"true" json:"addresses"`
Expand Down Expand Up @@ -135,10 +135,6 @@ func (out *OutputOwners) Verify() error {
}
}

func (out *OutputOwners) VerifyState() error {
return out.Verify()
}

func (out *OutputOwners) Sort() {
utils.Sort(out.Addrs)
}
Expand Down
2 changes: 0 additions & 2 deletions vms/secp256k1fx/output_owners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ func TestOutputOwnersVerify(t *testing.T) {
require := require.New(t)
err := tt.out.Verify()
require.ErrorIs(err, tt.expectedErr)
err = tt.out.VerifyState()
require.ErrorIs(err, tt.expectedErr)
})
}
}
Expand Down
8 changes: 3 additions & 5 deletions vms/secp256k1fx/transfer_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import (
)

var (
_ verify.State = (*OutputOwners)(nil)
_ verify.State = (*TransferOutput)(nil)

ErrNoValueOutput = errors.New("output has no value")
)

type TransferOutput struct {
verify.IsState `json:"-"`

Amt uint64 `serialize:"true" json:"amount"`

OutputOwners `serialize:"true"`
Expand Down Expand Up @@ -51,10 +53,6 @@ func (out *TransferOutput) Verify() error {
}
}

func (out *TransferOutput) VerifyState() error {
return out.Verify()
}

func (out *TransferOutput) Owners() interface{} {
return &out.OutputOwners
}

0 comments on commit 7b9912d

Please sign in to comment.