2020-12-16 14:54:18 -08:00
|
|
|
// Copyright 2020 dfuse Platform Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package serum
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-02-15 10:08:45 -08:00
|
|
|
"strings"
|
2020-12-16 14:54:18 -08:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-02-08 14:08:37 -08:00
|
|
|
bin "github.com/dfuse-io/binary"
|
2021-07-06 09:18:26 -07:00
|
|
|
"github.com/gagliardetto/solana-go"
|
|
|
|
"github.com/gagliardetto/solana-go/diff"
|
|
|
|
"github.com/gagliardetto/solana-go/rpc"
|
2020-12-16 14:54:18 -08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDecoder_EventQueue_Diff(t *testing.T) {
|
2021-02-15 10:08:45 -08:00
|
|
|
//t.Skip("diff event queue test")
|
|
|
|
|
2020-12-16 14:54:18 -08:00
|
|
|
oldDataFile := "testdata/serum-event-queue-old.bin.zst"
|
|
|
|
newDataFile := "testdata/serum-event-queue-new.bin.zst"
|
|
|
|
|
2021-02-15 10:08:45 -08:00
|
|
|
olDataJSONFile := strings.ReplaceAll(oldDataFile, ".bin.zst", ".json")
|
|
|
|
newDataJSONFile := strings.ReplaceAll(newDataFile, ".bin.zst", ".json")
|
2020-12-16 14:54:18 -08:00
|
|
|
|
|
|
|
if os.Getenv("TESTDATA_UPDATE") == "true" {
|
2021-07-20 12:29:06 -07:00
|
|
|
client := rpc.New("http://api.mainnet-beta.solana.com:80/rpc")
|
2020-12-16 14:54:18 -08:00
|
|
|
ctx := context.Background()
|
|
|
|
account := solana.MustPublicKeyFromBase58("13iGJcA4w5hcJZDjJbJQor1zUiDLE4jv2rMW9HkD5Eo1")
|
|
|
|
|
|
|
|
info, err := client.GetAccountInfo(ctx, account)
|
|
|
|
require.NoError(t, err)
|
2021-07-12 07:00:27 -07:00
|
|
|
writeCompressedFile(t, oldDataFile, info.Value.Data.GetBinary())
|
2020-12-16 14:54:18 -08:00
|
|
|
|
2021-02-15 10:08:45 -08:00
|
|
|
oldQueue := &EventQueue{}
|
2021-07-12 07:00:27 -07:00
|
|
|
require.NoError(t, oldQueue.Decode(info.Value.Data.GetBinary()))
|
2021-02-15 10:08:45 -08:00
|
|
|
writeJSONFile(t, olDataJSONFile, oldQueue)
|
2020-12-16 14:54:18 -08:00
|
|
|
|
|
|
|
time.Sleep(900 * time.Millisecond)
|
|
|
|
|
|
|
|
info, err = client.GetAccountInfo(ctx, account)
|
|
|
|
require.NoError(t, err)
|
2021-07-12 07:00:27 -07:00
|
|
|
writeCompressedFile(t, newDataFile, info.Value.Data.GetBinary())
|
2020-12-16 14:54:18 -08:00
|
|
|
|
2021-02-15 10:08:45 -08:00
|
|
|
newQueue := &EventQueue{}
|
2021-07-12 07:00:27 -07:00
|
|
|
require.NoError(t, newQueue.Decode(info.Value.Data.GetBinary()))
|
2021-02-15 10:08:45 -08:00
|
|
|
writeJSONFile(t, newDataJSONFile, newQueue)
|
2020-12-16 14:54:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
oldQueue := &EventQueue{}
|
|
|
|
require.NoError(t, oldQueue.Decode(readCompressedFile(t, oldDataFile)))
|
|
|
|
|
|
|
|
newQueue := &EventQueue{}
|
|
|
|
require.NoError(t, newQueue.Decode(readCompressedFile(t, newDataFile)))
|
|
|
|
|
|
|
|
fmt.Println("==>> All diff(s)")
|
|
|
|
diff.Diff(oldQueue, newQueue, diff.OnEvent(func(event diff.Event) { fmt.Println("Event " + event.String()) }))
|
|
|
|
}
|
|
|
|
|
2021-01-20 10:42:10 -08:00
|
|
|
func Test_fill(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
e *Event
|
|
|
|
expectIsFill bool
|
|
|
|
expectIsOut bool
|
|
|
|
expectIsBid bool
|
|
|
|
expectIsMaker bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Is Fill",
|
|
|
|
e: &Event{
|
|
|
|
Flag: 0b00000001,
|
|
|
|
},
|
|
|
|
expectIsFill: true,
|
|
|
|
expectIsOut: false,
|
|
|
|
expectIsBid: false,
|
|
|
|
expectIsMaker: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Is Out",
|
|
|
|
e: &Event{
|
|
|
|
Flag: 0b00000010,
|
|
|
|
},
|
|
|
|
expectIsFill: false,
|
|
|
|
expectIsOut: true,
|
|
|
|
expectIsBid: false,
|
|
|
|
expectIsMaker: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Is Fill & bid",
|
|
|
|
e: &Event{
|
|
|
|
Flag: 0b00000101,
|
|
|
|
},
|
|
|
|
expectIsFill: true,
|
|
|
|
expectIsOut: false,
|
|
|
|
expectIsBid: true,
|
|
|
|
expectIsMaker: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Is Fill & bid & maker",
|
|
|
|
e: &Event{
|
|
|
|
Flag: 0b00001101,
|
|
|
|
},
|
|
|
|
expectIsFill: true,
|
|
|
|
expectIsOut: false,
|
|
|
|
expectIsBid: true,
|
|
|
|
expectIsMaker: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.name, func(t *testing.T) {
|
|
|
|
assert.Equal(t, test.expectIsFill, test.e.Flag.IsFill())
|
|
|
|
assert.Equal(t, test.expectIsOut, test.e.Flag.IsOut())
|
|
|
|
assert.Equal(t, test.expectIsBid, test.e.Flag.IsBid())
|
|
|
|
assert.Equal(t, test.expectIsMaker, test.e.Flag.IsMaker())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-16 14:54:18 -08:00
|
|
|
func TestDecoder_EventQueue_DiffManual(t *testing.T) {
|
|
|
|
oldQueue := &EventQueue{
|
|
|
|
SerumPadding: [5]byte{},
|
|
|
|
Head: 120,
|
|
|
|
Count: 13,
|
|
|
|
SeqNum: 25,
|
|
|
|
Events: []*Event{
|
2021-02-15 10:08:45 -08:00
|
|
|
{OrderID: OrderID(bin.Uint128{Lo: 1})},
|
|
|
|
{OrderID: OrderID(bin.Uint128{Lo: 2})},
|
2020-12-16 14:54:18 -08:00
|
|
|
},
|
|
|
|
EndPadding: [7]byte{},
|
|
|
|
}
|
|
|
|
|
|
|
|
newQueue := &EventQueue{
|
|
|
|
Head: 120,
|
|
|
|
Count: 13,
|
|
|
|
SeqNum: 25,
|
|
|
|
Events: []*Event{
|
2021-02-15 10:08:45 -08:00
|
|
|
{OrderID: OrderID(bin.Uint128{Lo: 1})},
|
|
|
|
{OrderID: OrderID(bin.Uint128{Lo: 4})},
|
|
|
|
{OrderID: OrderID(bin.Uint128{Lo: 5})},
|
2020-12-16 14:54:18 -08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("All diff lines")
|
|
|
|
diff.Diff(oldQueue, newQueue, diff.OnEvent(func(event diff.Event) { fmt.Println("Event " + event.String()) }))
|
|
|
|
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println("Processed diff lines")
|
|
|
|
diff.Diff(oldQueue, newQueue, diff.OnEvent(func(event diff.Event) {
|
|
|
|
if match, _ := event.Match("Events[#]"); match {
|
|
|
|
fmt.Printf("Event %s => %v\n", event.Kind, event.Element())
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|