From 7b9912dfc2cbecad5b47b61927045df8b3f40e19 Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Thu, 8 Jun 2023 14:47:23 -0400 Subject: [PATCH] Cleanup fx interface compliance (#1599) --- scripts/mocks.mockgen.txt | 2 -- vms/avm/state_test.go | 8 +++---- vms/avm/txs/initial_state_test.go | 4 ++-- vms/components/avax/mock_transferable_out.go | 17 +++------------ vms/components/avax/test_verifiable.go | 23 ++++++++++++++------ vms/components/verify/verification.go | 12 +++++++--- vms/nftfx/mint_output.go | 5 +++++ vms/nftfx/transfer_output.go | 6 ++--- vms/platformvm/blocks/codec.go | 2 +- vms/platformvm/fx/fx.go | 8 ++++++- vms/platformvm/fx/mock_fx.go | 3 +++ vms/platformvm/txs/codec.go | 21 ++++++++++-------- vms/propertyfx/mint_output.go | 9 +++++++- vms/propertyfx/owned_output.go | 9 +++++++- vms/secp256k1fx/mint_output.go | 6 ++--- vms/secp256k1fx/mint_output_test.go | 2 -- vms/secp256k1fx/output_owners.go | 8 ++----- vms/secp256k1fx/output_owners_test.go | 2 -- vms/secp256k1fx/transfer_output.go | 8 +++---- 19 files changed, 87 insertions(+), 68 deletions(-) diff --git a/scripts/mocks.mockgen.txt b/scripts/mocks.mockgen.txt index 047e54259b13..c09b6b97d722 100644 --- a/scripts/mocks.mockgen.txt +++ b/scripts/mocks.mockgen.txt @@ -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 diff --git a/vms/avm/state_test.go b/vms/avm/state_test.go index 32a3874f0043..fe207b38a379 100644 --- a/vms/avm/state_test.go +++ b/vms/avm/state_test.go @@ -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{}) }, }, }}, @@ -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() @@ -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{}) }, }, }}, @@ -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) diff --git a/vms/avm/txs/initial_state_test.go b/vms/avm/txs/initial_state_test.go index 1d294870d3a1..c3ead8ef8a79 100644 --- a/vms/avm/txs/initial_state_test.go +++ b/vms/avm/txs/initial_state_test.go @@ -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() @@ -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") diff --git a/vms/components/avax/mock_transferable_out.go b/vms/components/avax/mock_transferable_out.go index bb3ad3aef0bb..c2a067e6e4ea 100644 --- a/vms/components/avax/mock_transferable_out.go +++ b/vms/components/avax/mock_transferable_out.go @@ -11,11 +11,14 @@ import ( reflect "reflect" snow "github.com/ava-labs/avalanchego/snow" + verify "github.com/ava-labs/avalanchego/vms/components/verify" gomock "github.com/golang/mock/gomock" ) // MockTransferableOut is a mock of TransferableOut interface. type MockTransferableOut struct { + verify.IsState + ctrl *gomock.Controller recorder *MockTransferableOutMockRecorder } @@ -76,17 +79,3 @@ func (mr *MockTransferableOutMockRecorder) Verify() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Verify", reflect.TypeOf((*MockTransferableOut)(nil).Verify)) } - -// VerifyState mocks base method. -func (m *MockTransferableOut) VerifyState() error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "VerifyState") - ret0, _ := ret[0].(error) - return ret0 -} - -// VerifyState indicates an expected call of VerifyState. -func (mr *MockTransferableOutMockRecorder) VerifyState() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "VerifyState", reflect.TypeOf((*MockTransferableOut)(nil).VerifyState)) -} diff --git a/vms/components/avax/test_verifiable.go b/vms/components/avax/test_verifiable.go index 0e9eb877190e..36ea7d57f8ff 100644 --- a/vms/components/avax/test_verifiable.go +++ b/vms/components/avax/test_verifiable.go @@ -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"` } diff --git a/vms/components/verify/verification.go b/vms/components/verify/verification.go index b615d70b3a6c..566facbdf865 100644 --- a/vms/components/verify/verification.go +++ b/vms/components/verify/verification.go @@ -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 diff --git a/vms/nftfx/mint_output.go b/vms/nftfx/mint_output.go index fe91a1b41d22..4c2f53698307 100644 --- a/vms/nftfx/mint_output.go +++ b/vms/nftfx/mint_output.go @@ -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"` } diff --git a/vms/nftfx/transfer_output.go b/vms/nftfx/transfer_output.go index 2d4182ac3f30..c87d47984e39 100644 --- a/vms/nftfx/transfer_output.go +++ b/vms/nftfx/transfer_output.go @@ -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"` @@ -55,7 +57,3 @@ func (out *TransferOutput) Verify() error { return out.OutputOwners.Verify() } } - -func (out *TransferOutput) VerifyState() error { - return out.Verify() -} diff --git a/vms/platformvm/blocks/codec.go b/vms/platformvm/blocks/codec.go index ac0f42da83f2..6472c8610c5e 100644 --- a/vms/platformvm/blocks/codec.go +++ b/vms/platformvm/blocks/codec.go @@ -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), diff --git a/vms/platformvm/fx/fx.go b/vms/platformvm/fx/fx.go index 8bb95a2e0a8f..706d86debfcd 100644 --- a/vms/platformvm/fx/fx.go +++ b/vms/platformvm/fx/fx.go @@ -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. @@ -40,6 +44,8 @@ type Fx interface { } type Owner interface { + verify.IsNotState + verify.Verifiable snow.ContextInitializable } diff --git a/vms/platformvm/fx/mock_fx.go b/vms/platformvm/fx/mock_fx.go index 78a2d4e64fd2..d450e305aecd 100644 --- a/vms/platformvm/fx/mock_fx.go +++ b/vms/platformvm/fx/mock_fx.go @@ -11,6 +11,7 @@ import ( reflect "reflect" snow "github.com/ava-labs/avalanchego/snow" + verify "github.com/ava-labs/avalanchego/vms/components/verify" gomock "github.com/golang/mock/gomock" ) @@ -124,6 +125,8 @@ func (mr *MockFxMockRecorder) VerifyTransfer(arg0, arg1, arg2, arg3 interface{}) // MockOwner is a mock of Owner interface. type MockOwner struct { + verify.IsNotState + ctrl *gomock.Controller recorder *MockOwnerMockRecorder } diff --git a/vms/platformvm/txs/codec.go b/vms/platformvm/txs/codec.go index d3667b9406db..cf67ce40ee9e 100644 --- a/vms/platformvm/txs/codec.go +++ b/vms/platformvm/txs/codec.go @@ -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{}), diff --git a/vms/propertyfx/mint_output.go b/vms/propertyfx/mint_output.go index b3c65a8bdd12..3aebd115a404 100644 --- a/vms/propertyfx/mint_output.go +++ b/vms/propertyfx/mint_output.go @@ -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"` } diff --git a/vms/propertyfx/owned_output.go b/vms/propertyfx/owned_output.go index 979a97c7ce5a..30e32ca3ddf2 100644 --- a/vms/propertyfx/owned_output.go +++ b/vms/propertyfx/owned_output.go @@ -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"` } diff --git a/vms/secp256k1fx/mint_output.go b/vms/secp256k1fx/mint_output.go index ed4d35c0dc3e..996f05171bbe 100644 --- a/vms/secp256k1fx/mint_output.go +++ b/vms/secp256k1fx/mint_output.go @@ -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"` } @@ -19,7 +21,3 @@ func (out *MintOutput) Verify() error { return out.OutputOwners.Verify() } } - -func (out *MintOutput) VerifyState() error { - return out.Verify() -} diff --git a/vms/secp256k1fx/mint_output_test.go b/vms/secp256k1fx/mint_output_test.go index dd484df971c4..7d092a6772ea 100644 --- a/vms/secp256k1fx/mint_output_test.go +++ b/vms/secp256k1fx/mint_output_test.go @@ -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) }) } } diff --git a/vms/secp256k1fx/output_owners.go b/vms/secp256k1fx/output_owners.go index 39c076d65635..2475a5ea5d4e 100644 --- a/vms/secp256k1fx/output_owners.go +++ b/vms/secp256k1fx/output_owners.go @@ -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"` @@ -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) } diff --git a/vms/secp256k1fx/output_owners_test.go b/vms/secp256k1fx/output_owners_test.go index a8a5cfba55a2..b09e28bea923 100644 --- a/vms/secp256k1fx/output_owners_test.go +++ b/vms/secp256k1fx/output_owners_test.go @@ -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) }) } } diff --git a/vms/secp256k1fx/transfer_output.go b/vms/secp256k1fx/transfer_output.go index dc57d2305b6c..234cc1f68dab 100644 --- a/vms/secp256k1fx/transfer_output.go +++ b/vms/secp256k1fx/transfer_output.go @@ -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"` @@ -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 }