code.vegaprotocol.io/vega@v0.79.0/commands/chain_event_test.go (about) 1 // Copyright (C) 2023 Gobalsky Labs Limited 2 // 3 // This program is free software: you can redistribute it and/or modify 4 // it under the terms of the GNU Affero General Public License as 5 // published by the Free Software Foundation, either version 3 of the 6 // License, or (at your option) any later version. 7 // 8 // This program is distributed in the hope that it will be useful, 9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 // GNU Affero General Public License for more details. 12 // 13 // You should have received a copy of the GNU Affero General Public License 14 // along with this program. If not, see <http://www.gnu.org/licenses/>. 15 16 package commands_test 17 18 import ( 19 "errors" 20 "testing" 21 22 "code.vegaprotocol.io/vega/commands" 23 "code.vegaprotocol.io/vega/libs/test" 24 proto "code.vegaprotocol.io/vega/protos/vega" 25 commandspb "code.vegaprotocol.io/vega/protos/vega/commands/v1" 26 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestCheckChainEvent(t *testing.T) { 31 t.Run("Submitting a nil chain event fails", testNilChainEventFails) 32 t.Run("Submitting a chain event without event fails", testChainEventWithoutEventFails) 33 t.Run("Submitting an ERC20 chain event without tx ID fails", testErc20ChainEventWithoutTxIDFails) 34 t.Run("Submitting an ERC20 chain event without nonce succeeds", testErc20ChainEventWithoutNonceSucceeds) 35 t.Run("Submitting a built-in chain event without tx ID succeeds", testBuiltInChainEventWithoutTxIDSucceeds) 36 t.Run("Submitting a built-in chain event without nonce succeeds", testBuiltInChainEventWithoutNonceSucceeds) 37 } 38 39 func testNilChainEventFails(t *testing.T) { 40 err := checkChainEvent(nil) 41 42 assert.Contains(t, err.Get("chain_event"), commands.ErrIsRequired) 43 } 44 45 func testChainEventWithoutEventFails(t *testing.T) { 46 event := newErc20ChainEvent() 47 event.Event = nil 48 49 err := checkChainEvent(event) 50 51 assert.Contains(t, err.Get("chain_event.event"), commands.ErrIsRequired) 52 } 53 54 func testErc20ChainEventWithoutTxIDFails(t *testing.T) { 55 event := newErc20ChainEvent() 56 event.TxId = "" 57 58 err := checkChainEvent(event) 59 60 assert.Contains(t, err.Get("chain_event.tx_id"), commands.ErrIsRequired) 61 } 62 63 func testErc20ChainEventWithoutNonceSucceeds(t *testing.T) { 64 event := newErc20ChainEvent() 65 event.Nonce = 0 66 67 err := checkChainEvent(event) 68 69 assert.NotContains(t, err.Get("chain_event.nonce"), commands.ErrIsRequired) 70 } 71 72 func testBuiltInChainEventWithoutTxIDSucceeds(t *testing.T) { 73 event := newBuiltInChainEvent() 74 event.TxId = "" 75 76 err := checkChainEvent(event) 77 78 assert.NotContains(t, err.Get("chain_event.tx_id"), commands.ErrIsRequired) 79 } 80 81 func testBuiltInChainEventWithoutNonceSucceeds(t *testing.T) { 82 event := newBuiltInChainEvent() 83 event.Nonce = 0 84 85 err := checkChainEvent(event) 86 87 assert.NotContains(t, err.Get("chain_event.nonce"), commands.ErrIsRequired) 88 } 89 90 func checkChainEvent(cmd *commandspb.ChainEvent) commands.Errors { 91 err := commands.CheckChainEvent(cmd) 92 93 var e commands.Errors 94 if ok := errors.As(err, &e); !ok { 95 return commands.NewErrors() 96 } 97 98 return e 99 } 100 101 func newErc20ChainEvent() *commandspb.ChainEvent { 102 return &commandspb.ChainEvent{ 103 TxId: "my ID", 104 Nonce: test.RandomPositiveU64(), 105 Event: &commandspb.ChainEvent_Erc20{ 106 Erc20: &proto.ERC20Event{ 107 Index: 0, 108 Block: 0, 109 Action: nil, 110 }, 111 }, 112 } 113 } 114 115 func newBuiltInChainEvent() *commandspb.ChainEvent { 116 return &commandspb.ChainEvent{ 117 TxId: "my ID", 118 Nonce: test.RandomPositiveU64(), 119 Event: &commandspb.ChainEvent_Builtin{ 120 Builtin: &proto.BuiltinAssetEvent{}, 121 }, 122 } 123 }