addresses_test.gno
2.32 Kb · 65 lines
1package bolo
2
3import (
4 "chain"
5 "strings"
6 "testing"
7
8 "gno.land/p/nt/seqid/v0"
9)
10
11// TestAddresses: the addresses queries derive are the ones the realm's bankers
12// spend from, and each one is distinct (API §1, INV-18).
13func TestAddresses(cur realm, t *testing.T) {
14 if cur.PkgPath() != pkgPath {
15 t.Fatalf("pkgPath is %s, but the realm is %s", pkgPath, cur.PkgPath())
16 }
17 if realmAddr() != cur.Address() {
18 t.Errorf("realmAddr() = %s, the realm is %s", realmAddr(), cur.Address())
19 }
20 // Each fixed address is the one its role names: the keeper's deposit address
21 // for this realm, this realm's "slashed" sub-address, and the package one path
22 // up, where the registry lives.
23 if depositAddr() != chain.DeriveStorageDepositAddr(cur.PkgPath()) {
24 t.Errorf("depositAddr() isn't the realm's storage-deposit address")
25 }
26 if slashedAddr() != cur.Sub("slashed").Address() {
27 t.Errorf("slashedAddr() isn't the realm's slashed sub-address")
28 }
29 if registryAddr() != chain.PackageAddress(strings.TrimSuffix(cur.PkgPath(), "/v0")) {
30 t.Errorf("registryAddr() isn't the registry's, one path up")
31 }
32 if registryDepositAddr() != chain.DeriveStorageDepositAddr(strings.TrimSuffix(cur.PkgPath(), "/v0")) {
33 t.Errorf("registryDepositAddr() isn't the registry's storage-deposit address")
34 }
35 seen := map[address]string{
36 realmAddr(): "the realm",
37 slashedAddr(): "slashed",
38 depositAddr(): "the storage deposit",
39 registryAddr(): "the registry",
40 registryDepositAddr(): "the registry's storage deposit",
41 }
42 if len(seen) != 5 {
43 t.Fatalf("realm, slashed, deposit and registry addresses collide")
44 }
45 for _, id := range []seqid.ID{1, 2, 42, 1 << 63} {
46 c := &campaign{id: id}
47 if escrowSub(cur, c).Address() != escrowAddr(id) {
48 t.Errorf("campaign %d: escrowSub and escrowAddr differ", uint64(id))
49 }
50 if bondSub(cur, c).Address() != bondAddr(id) {
51 t.Errorf("campaign %d: bondSub and bondAddr differ", uint64(id))
52 }
53 for _, a := range []address{escrowAddr(id), bondAddr(id)} {
54 if other, dup := seen[a]; dup {
55 t.Errorf("campaign %d: %s is also %s", uint64(id), a, other)
56 }
57 seen[a] = "campaign " + idString(id)
58 }
59 }
60 // The address for "42" is campaign 42's; "042" names nothing (INV-18).
61 id, _ := parseID("42")
62 if escrowAddr(id) != escrowAddr(42) {
63 t.Errorf("parsed and stored IDs give different escrows")
64 }
65}