|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sync" |
| 6 | + "testing" |
| 7 | + |
| 8 | + api "github.com/attestantio/go-eth2-client/api/v1" |
| 9 | + "github.com/attestantio/go-eth2-client/spec" |
| 10 | + "github.com/icon-project/btp2/chain/icon/client" |
| 11 | + "github.com/icon-project/btp2/common/log" |
| 12 | + "github.com/icon-project/btp2/common/types" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + iconEndpoint = "https://berlin.net.solidwallet.io/api/v3/icon_dex" |
| 18 | + ethNodeAddr = "http://1.1.1.1" |
| 19 | + ethExecutionEndpoint = ethNodeAddr + ":8545" |
| 20 | + ethConsensusEndpoint = ethNodeAddr + ":9596" |
| 21 | + |
| 22 | + iconBMC = "cxb61b42e51c20054b4f5fd31b7d64af8c59579829" |
| 23 | + ethBMC = "0xD99d2A85E9B0Fa3E7fbA4756699f4307BFBb80c3" |
| 24 | + btpAddressICON = "btp://0x3.icon/" + iconBMC |
| 25 | + btpAddressETH = "btp://0xaa36a7.eth2/" + ethBMC |
| 26 | +) |
| 27 | + |
| 28 | +func newTestConsensusLayer() (*ConsensusLayer, error) { |
| 29 | + return NewConsensusLayer(ethConsensusEndpoint, log.WithFields(log.Fields{log.FieldKeyPrefix: "test"})) |
| 30 | +} |
| 31 | + |
| 32 | +func iconGetStatus() (*types.BMCLinkStatus, error) { |
| 33 | + c := client.NewClient(iconEndpoint, log.WithFields(log.Fields{log.FieldKeyPrefix: "test"})) |
| 34 | + p := &client.CallParam{ |
| 35 | + ToAddress: client.Address(iconBMC), |
| 36 | + DataType: "call", |
| 37 | + Data: client.CallData{ |
| 38 | + Method: client.BMCGetStatusMethod, |
| 39 | + Params: client.BMCStatusParams{ |
| 40 | + Target: btpAddressETH, |
| 41 | + }, |
| 42 | + }, |
| 43 | + } |
| 44 | + bs := &client.BMCStatus{} |
| 45 | + err := client.MapError(c.Call(p, bs)) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + ls := &types.BMCLinkStatus{} |
| 50 | + if ls.TxSeq, err = bs.TxSeq.Value(); err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + if ls.RxSeq, err = bs.RxSeq.Value(); err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + if ls.Verifier.Height, err = bs.Verifier.Height.Value(); err != nil { |
| 57 | + return nil, err |
| 58 | + } |
| 59 | + if ls.Verifier.Extra, err = bs.Verifier.Extra.Value(); err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + return ls, nil |
| 63 | +} |
| 64 | + |
| 65 | +//func TestReceiver_RunMonitoring(t *testing.T) { |
| 66 | +// eth := newTestConsensusLayer() |
| 67 | +// defer eth.Stop() |
| 68 | +// |
| 69 | +// bls, err := iconGetStatus() |
| 70 | +// assert.NoError(t, err) |
| 71 | +// |
| 72 | +// wg := sync.WaitGroup{} |
| 73 | +// wg.Add(1) |
| 74 | +// go func() { |
| 75 | +// eth.Monitoring(bls) |
| 76 | +// }() |
| 77 | +// wg.Wait() |
| 78 | +//} |
| 79 | + |
| 80 | +func TestConsensusLayer_genesis(t *testing.T) { |
| 81 | + c, err := newTestConsensusLayer() |
| 82 | + defer c.Term() |
| 83 | + assert.NoError(t, err) |
| 84 | + |
| 85 | + resp, err := c.Genesis() |
| 86 | + assert.NoError(t, err) |
| 87 | + fmt.Printf("%+v\n", resp) |
| 88 | +} |
| 89 | + |
| 90 | +func TestConsensusLayer_LightClientUpdates(t *testing.T) { |
| 91 | + c, err := newTestConsensusLayer() |
| 92 | + defer c.Term() |
| 93 | + assert.NoError(t, err) |
| 94 | + |
| 95 | + resp, err := c.LightClientUpdates(523, 2) |
| 96 | + assert.NoError(t, err) |
| 97 | + fmt.Printf("%+v\n", resp) |
| 98 | +} |
| 99 | + |
| 100 | +func TestConsensusLayer_Events(t *testing.T) { |
| 101 | + c, err := newTestConsensusLayer() |
| 102 | + defer c.Term() |
| 103 | + assert.NoError(t, err) |
| 104 | + eth2Topics := []string{TopicLCOptimisticUpdate, TopicLCFinalityUpdate} |
| 105 | + wg := sync.WaitGroup{} |
| 106 | + wg.Add(1) |
| 107 | + go func() { |
| 108 | + err = c.Events(eth2Topics, func(event *api.Event) { |
| 109 | + fmt.Printf("%s\n", event.Topic) |
| 110 | + if event.Topic == TopicLCFinalityUpdate { |
| 111 | + u, err := ToLightClientFinalityUpdate(event.Data.(*spec.VersionedLCFinalityUpdate)) |
| 112 | + assert.NoError(t, err) |
| 113 | + fmt.Printf("%+v\n", u) |
| 114 | + } else if event.Topic == TopicLCOptimisticUpdate{ |
| 115 | + u, err := ToLightClientOptimisticUpdate(event.Data.(*spec.VersionedLCOptimisticUpdate)) |
| 116 | + assert.NoError(t, err) |
| 117 | + fmt.Printf("%+v\n", u) |
| 118 | + } |
| 119 | + }) |
| 120 | + assert.NoError(t, err) |
| 121 | + }() |
| 122 | + wg.Wait() |
| 123 | +} |
0 commit comments