Skip to content

Commit

Permalink
refactor: NamePointer struct now embeds models.NamePointer. This give…
Browse files Browse the repository at this point in the history
…s automatic JSON serialization
  • Loading branch information
randomshinichi committed Apr 30, 2019
1 parent 9bd57e9 commit ccff0a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
21 changes: 12 additions & 9 deletions aeternity/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,11 @@ func NewNameClaimTx(accountID, name string, nameSalt utils.BigInt, fee utils.Big

// NamePointer extends the swagger gener ated models.NamePointer to provide RLP serialization
type NamePointer struct {
ID string
Key string
}

func (t *NamePointer) RLP() (rlpRawMsg []byte, err error) {
return []byte{}, nil
*models.NamePointer
}

func (t *NamePointer) EncodeRLP(w io.Writer) (err error) {
accountID, err := buildIDTag(IDTagAccount, t.ID)
accountID, err := buildIDTag(IDTagAccount, string(t.NamePointer.ID))
if err != nil {
return
}
Expand All @@ -286,7 +281,10 @@ func (t *NamePointer) EncodeRLP(w io.Writer) (err error) {
// NewNamePointer is a constructor for a swagger generated NamePointer struct.
// It returns a pointer because
func NewNamePointer(key string, id string) *NamePointer {
return &NamePointer{ID: id, Key: key}
np := models.NamePointer{ID: models.EncodedHash(id), Key: &key}
return &NamePointer{
NamePointer: &np,
}
}

// NameUpdateTx represents a transaction where one extends the lifetime of a reserved name on AENS
Expand Down Expand Up @@ -331,14 +329,19 @@ func (t *NameUpdateTx) RLP() (rlpRawMsg []byte, err error) {

// JSON representation of a Tx is useful for querying the node's debug endpoint
func (t *NameUpdateTx) JSON() (string, error) {
swaggerNamePointers := []*models.NamePointer{}
for _, np := range t.Pointers {
swaggerNamePointers = append(swaggerNamePointers, np.NamePointer)
}

swaggerT := models.NameUpdateTx{
AccountID: models.EncodedHash(t.AccountID),
ClientTTL: &t.ClientTTL,
Fee: t.Fee,
NameID: models.EncodedHash(t.NameID),
NameTTL: &t.NameTTL,
Nonce: t.Nonce,
Pointers: []*models.NamePointer{},
Pointers: swaggerNamePointers,
TTL: t.TTL,
}

Expand Down
15 changes: 7 additions & 8 deletions aeternity/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ func TestNamePointer_EncodeRLP(t *testing.T) {
tests := []struct {
name string
fields fields
wantW string
wantW []byte
wantErr bool
}{
{
Expand All @@ -526,24 +526,23 @@ func TestNamePointer_EncodeRLP(t *testing.T) {
Key: "account_pubkey",
ID: "ak_Egp9yVdpxmvAfQ7vsXGvpnyfNq71msbdUpkMNYGTeTe8kPL3v",
},
wantW: "I dunno",
// the reference value of wantW is taken from a correct serialization of NameUpdateTx.
// Unfortunately there is no way to get the node to serialize just the NamePointer.
wantW: []byte{241, 142, 97, 99, 99, 111, 117, 110, 116, 95, 112, 117, 98, 107, 101, 121, 161, 1, 31, 19, 163, 176, 139, 240, 1, 64, 6, 98, 166, 139, 105, 216, 117, 247, 128, 60, 236, 76, 8, 100, 127, 110, 213, 216, 76, 120, 151, 189, 80, 163},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &NamePointer{
ID: tt.fields.ID,
Key: tt.fields.Key,
}
p := NewNamePointer(tt.fields.Key, tt.fields.ID)
w := &bytes.Buffer{}
if err := p.EncodeRLP(w); (err != nil) != tt.wantErr {
t.Errorf("NamePointer.EncodeRLP() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotW := w.String(); gotW != tt.wantW {
if gotW := w.Bytes(); !bytes.Equal(gotW, tt.wantW) {
t.Errorf("NamePointer.EncodeRLP() = %v, want %v", gotW, tt.wantW)
fmt.Println(DecodeRLPMessage(w.Bytes()))
fmt.Println(DecodeRLPMessage(gotW))
}
})
}
Expand Down

0 comments on commit ccff0a6

Please sign in to comment.