|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/big" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/ethereum/go-ethereum/common" |
| 9 | + "github.com/ethereum/go-ethereum/core/types" |
| 10 | + "github.com/urfoundation/sn/stabi" |
| 11 | +) |
| 12 | + |
| 13 | +func settlementVaultEventLog(t *testing.T, name string, indexed []common.Hash, values ...any) *types.Log { |
| 14 | + t.Helper() |
| 15 | + parsed, err := stabi.STSettlementVaultMetaData.ParseABI() |
| 16 | + if err != nil { |
| 17 | + t.Fatal(err) |
| 18 | + } |
| 19 | + event, ok := parsed.Events[name] |
| 20 | + if !ok { |
| 21 | + t.Fatalf("settlement-vault ABI has no %s event", name) |
| 22 | + } |
| 23 | + data, err := event.Inputs.NonIndexed().Pack(values...) |
| 24 | + if err != nil { |
| 25 | + t.Fatal(err) |
| 26 | + } |
| 27 | + return &types.Log{Topics: append([]common.Hash{event.ID}, indexed...), Data: data} |
| 28 | +} |
| 29 | + |
| 30 | +func decodeReleaseEvent(t *testing.T, log *types.Log) (string, map[string]any) { |
| 31 | + t.Helper() |
| 32 | + client := &CoreStClient{ |
| 33 | + coordinator: stabi.NewSTCoordinator(), |
| 34 | + vault: stabi.NewSTSettlementVault(), |
| 35 | + reserve: stabi.NewSTReserveSink(), |
| 36 | + } |
| 37 | + for _, decoder := range client.stEventDecoders() { |
| 38 | + if kind, data, ok := decoder(log); ok { |
| 39 | + return kind, data |
| 40 | + } |
| 41 | + } |
| 42 | + t.Fatal("release event was not decoded") |
| 43 | + return "", nil |
| 44 | +} |
| 45 | + |
| 46 | +func TestStEventDecoderMirrorsEmissionDustDeferral(t *testing.T) { |
| 47 | + pool := common.HexToHash("0x1234") |
| 48 | + log := settlementVaultEventLog( |
| 49 | + t, |
| 50 | + "EmissionDustDeferred", |
| 51 | + []common.Hash{common.BigToHash(big.NewInt(7)), common.BigToHash(big.NewInt(3)), pool}, |
| 52 | + big.NewInt(175_960_612), |
| 53 | + big.NewInt(99_999), |
| 54 | + uint64(100_000), |
| 55 | + ) |
| 56 | + kind, data := decodeReleaseEvent(t, log) |
| 57 | + want := map[string]any{ |
| 58 | + "e": "7", "no_id": "3", "pool_hotkey": pool.Hex(), |
| 59 | + "observed_alpha_rao": "175960612", "tao_equivalent_rao": "99999", "minimum_transfer_tao_rao": "100000", |
| 60 | + } |
| 61 | + if kind != "EmissionDustDeferred" || !reflect.DeepEqual(data, want) { |
| 62 | + t.Fatalf("decoded dust deferral = %s %#v, want %#v", kind, data, want) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func TestStEventDecoderMirrorsDeferredClaimCredit(t *testing.T) { |
| 67 | + coldkey := common.HexToHash("0xabcd") |
| 68 | + log := settlementVaultEventLog( |
| 69 | + t, |
| 70 | + "ClaimPaymentDeferred", |
| 71 | + []common.Hash{coldkey}, |
| 72 | + big.NewInt(175_960_612), |
| 73 | + big.NewInt(99_999), |
| 74 | + uint64(100_000), |
| 75 | + uint8(1), |
| 76 | + ) |
| 77 | + kind, data := decodeReleaseEvent(t, log) |
| 78 | + want := map[string]any{ |
| 79 | + "coldkey": coldkey.Hex(), "credit_alpha_rao": "175960612", "tao_equivalent_rao": "99999", |
| 80 | + "minimum_transfer_tao_rao": "100000", "reason": "1", |
| 81 | + } |
| 82 | + if kind != "ClaimPaymentDeferred" || !reflect.DeepEqual(data, want) { |
| 83 | + t.Fatalf("decoded claim deferral = %s %#v, want %#v", kind, data, want) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestStEventDecoderDistinguishesActualClaimPayment(t *testing.T) { |
| 88 | + coldkey := common.HexToHash("0xabcd") |
| 89 | + relayer := common.HexToAddress("0x0000000000000000000000000000000000000521") |
| 90 | + log := settlementVaultEventLog( |
| 91 | + t, |
| 92 | + "ClaimPaid", |
| 93 | + []common.Hash{coldkey, common.BytesToHash(relayer.Bytes())}, |
| 94 | + big.NewInt(351_921_226), |
| 95 | + ) |
| 96 | + kind, data := decodeReleaseEvent(t, log) |
| 97 | + want := map[string]any{"coldkey": coldkey.Hex(), "amount": "351921226", "caller": relayer.Hex()} |
| 98 | + if kind != "ClaimPaid" || !reflect.DeepEqual(data, want) { |
| 99 | + t.Fatalf("decoded claim payment = %s %#v, want %#v", kind, data, want) |
| 100 | + } |
| 101 | +} |
0 commit comments