1
1
package base_test
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"fmt"
6
7
"os"
7
8
"strings"
8
9
"testing"
9
10
11
+ "github.com/pkg/errors"
10
12
"github.com/rs/zerolog"
11
13
"github.com/rs/zerolog/log"
12
14
"github.com/stretchr/testify/require"
@@ -21,6 +23,8 @@ import (
21
23
zctx "github.com/zeta-chain/node/zetaclient/context"
22
24
"github.com/zeta-chain/node/zetaclient/db"
23
25
"github.com/zeta-chain/node/zetaclient/testutils/mocks"
26
+ "google.golang.org/grpc/codes"
27
+ "google.golang.org/grpc/status"
24
28
)
25
29
26
30
const (
@@ -518,9 +522,10 @@ func TestPostVoteInbound(t *testing.T) {
518
522
ob := newTestSuite (t , chains .Ethereum )
519
523
520
524
ob .zetacore .WithPostVoteInbound ("" , "sampleBallotIndex" )
521
-
522
525
// post vote inbound
523
526
msg := sample .InboundVote (coin .CoinType_Gas , chains .Ethereum .ChainId , chains .ZetaChainMainnet .ChainId )
527
+ ob .zetacore .MockGetCctxByHash (errors .New ("not found" ))
528
+
524
529
ballot , err := ob .PostVoteInbound (context .TODO (), & msg , 100000 )
525
530
require .NoError (t , err )
526
531
require .Equal (t , "sampleBallotIndex" , ballot )
@@ -533,12 +538,64 @@ func TestPostVoteInbound(t *testing.T) {
533
538
// create sample message with long Message
534
539
msg := sample .InboundVote (coin .CoinType_Gas , chains .Ethereum .ChainId , chains .ZetaChainMainnet .ChainId )
535
540
msg .Message = strings .Repeat ("1" , crosschaintypes .MaxMessageLength + 1 )
541
+ ob .zetacore .MockGetCctxByHash (errors .New ("not found" ))
536
542
537
543
// post vote inbound
538
544
ballot , err := ob .PostVoteInbound (context .TODO (), & msg , 100000 )
539
545
require .NoError (t , err )
540
546
require .Empty (t , ballot )
541
547
})
548
+
549
+ t .Run ("should not post vote cctx already exists and ballot is not found" , func (t * testing.T ) {
550
+ //Arrange
551
+ // create observer
552
+ ob := newTestSuite (t , chains .Ethereum )
553
+
554
+ ob .zetacore .WithPostVoteInbound ("" , "sampleBallotIndex" )
555
+ msg := sample .InboundVote (coin .CoinType_Gas , chains .Ethereum .ChainId , chains .ZetaChainMainnet .ChainId )
556
+
557
+ ob .zetacore .MockGetCctxByHash (nil )
558
+ ob .zetacore .MockGetBallotByID (msg .Digest (), status .Error (codes .NotFound , "not found ballot" ))
559
+
560
+ var logBuffer bytes.Buffer
561
+ consoleWriter := zerolog.ConsoleWriter {Out : & logBuffer }
562
+ logger := zerolog .New (consoleWriter )
563
+ ob .Observer .Logger ().Inbound = logger
564
+
565
+ // Act
566
+ ballot , err := ob .PostVoteInbound (context .TODO (), & msg , 100000 )
567
+ // Assert
568
+ require .NoError (t , err )
569
+ require .Equal (t , ballot , msg .Digest ())
570
+
571
+ logOutput := logBuffer .String ()
572
+ require .Contains (t , logOutput , "inbound detected: cctx exists but the ballot does not" )
573
+ })
574
+
575
+ t .Run ("should post vote cctx already exists but ballot is found" , func (t * testing.T ) {
576
+ //Arrange
577
+ // create observer
578
+ ob := newTestSuite (t , chains .Ethereum )
579
+
580
+ msg := sample .InboundVote (coin .CoinType_Gas , chains .Ethereum .ChainId , chains .ZetaChainMainnet .ChainId )
581
+ ob .zetacore .WithPostVoteInbound (sample .ZetaIndex (t ), msg .Digest ())
582
+ ob .zetacore .MockGetCctxByHash (nil )
583
+ ob .zetacore .MockGetBallotByID (msg .Digest (), nil )
584
+
585
+ var logBuffer bytes.Buffer
586
+ consoleWriter := zerolog.ConsoleWriter {Out : & logBuffer }
587
+ logger := zerolog .New (consoleWriter )
588
+ ob .Observer .Logger ().Inbound = logger
589
+
590
+ // Act
591
+ ballot , err := ob .PostVoteInbound (context .TODO (), & msg , 100000 )
592
+ // Assert
593
+ require .NoError (t , err )
594
+ require .Equal (t , ballot , msg .Digest ())
595
+
596
+ logOutput := logBuffer .String ()
597
+ require .Contains (t , logOutput , "inbound detected: vote posted" )
598
+ })
542
599
}
543
600
544
601
func createDatabase (t * testing.T ) * db.DB {
0 commit comments