Skip to content

Commit

Permalink
feature: fleshing out NameUpdateTx, especially the JSON and Pointers …
Browse files Browse the repository at this point in the history
…overhaul. buildPointers is completely different now. Still does not work 100%
  • Loading branch information
randomshinichi committed Apr 3, 2019
1 parent c976216 commit 6d460af
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 20 deletions.
52 changes: 33 additions & 19 deletions aeternity/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,22 @@ func ttlTypeIntToStr(i uint64) string {
return oracleTTLTypeStr
}

func buildPointers(pointers []string) (ptrs [][]uint8, err error) {
func buildPointers(pointers []string) (ptrs []*models.NamePointer, err error) {
// TODO: handle errors
ptrs = make([][]uint8, len(pointers))
ptrs = make([]*models.NamePointer, len(pointers))
for i, p := range pointers {
switch GetHashPrefix(p) {
case PrefixAccountPubkey:
pID, err := buildIDTag(IDTagName, p)
ptrs[i] = pID
// pID, err := buildIDTag(IDTagAccount, p)
key := "account_pubkey"
ptrs[i] = &models.NamePointer{ID: models.EncodedHash(p), Key: &key}
if err != nil {
break
}
case PrefixOraclePubkey:
pID, err := buildIDTag(IDTagOracle, p)
ptrs[i] = pID
// pID, err := buildIDTag(IDTagOracle, p)
key := "oracle_pubkey"
ptrs[i] = &models.NamePointer{ID: models.EncodedHash(p), Key: &key}
if err != nil {
break
}
Expand Down Expand Up @@ -257,10 +259,11 @@ func NewNameClaimTx(accountID, name string, nameSalt uint64, fee utils.BigInt, t
}

// NameUpdateTx represents a transaction where one extends the lifetime of a reserved name on AENS

type NameUpdateTx struct {
AccountID string
NameID string
Pointers []string
Pointers []*models.NamePointer
NameTTL uint64
ClientTTL uint64
Fee utils.BigInt
Expand All @@ -275,40 +278,51 @@ func (t *NameUpdateTx) RLP() (rlpRawMsg []byte, err error) {
if err != nil {
return
}
// build id for the sender
// build id for the name
nID, err := buildIDTag(IDTagName, t.NameID)
if err != nil {
return
}
// build id for pointers
ptrs, err := buildPointers(t.Pointers)
if err != nil {
return
}

// create the transaction
rlpRawMsg, err = buildRLPMessage(
ObjectTagNameServiceClaimTransaction,
ObjectTagNameServiceUpdateTransaction,
rlpMessageVersion,
aID,
t.Nonce,
nID,
uint64(t.NameTTL),
ptrs,
uint64(t.ClientTTL),
t.NameTTL,
t.Pointers,
t.ClientTTL,
t.Fee.Int,
t.TTL)
return
}

// JSON representation of a Tx is useful for querying the node's debug endpoint
func (t *NameUpdateTx) JSON() (string, error) {
return "unimplemented", nil
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: t.Pointers,
TTL: t.TTL,
}

output, err := swaggerT.MarshalBinary()
return string(output), err
}

// NewNameUpdateTx is a constructor for a NameUpdateTx struct
func NewNameUpdateTx(accountID, nameID string, pointers []string, nameTTL, clientTTL uint64, fee utils.BigInt, ttl, nonce uint64) NameUpdateTx {
return NameUpdateTx{accountID, nameID, pointers, nameTTL, clientTTL, fee, ttl, nonce}
parsedPointers, err := buildPointers(pointers)
if err != nil {
panic(err)
}
return NameUpdateTx{accountID, nameID, parsedPointers, nameTTL, clientTTL, fee, ttl, nonce}
}

// OracleRegisterTx represents a transaction that registers an oracle on the blockchain's state
Expand Down
35 changes: 34 additions & 1 deletion aeternity/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,36 @@ func TestNameUpdateTx_RLP(t *testing.T) {
wantTx string
wantErr bool
}{
// TODO: Add test cases.
{
name: "update 1 pointer",
fields: fields{
AccountID: "ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi",
NameID: "nm_ie148R2qZYBfo1Ek3sZpfTLwBhkkqCRKi2Ce8JJ7yyWVRw2Sb", // fdsa.test
Pointers: []string{"ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi"},
NameTTL: uint64(0),
ClientTTL: uint64(6),
Fee: *utils.NewBigIntFromUint64(1),
TTL: 5,
Nonce: 5,
},
wantTx: "tx_+H4iAaEBzqet5HDJ+Z2dTkAIgKhvHUm7REti8Rqeu2S7z+tz/vMFoQJei0cGdDWb3EfrY0mtZADF0LoQ4yL6z10I/3ETJ0fpKADy8Y5hY2NvdW50X3B1YmtleaEBzqet5HDJ+Z2dTkAIgKhvHUm7REti8Rqeu2S7z+tz/vMGAQXLBNnv",
wantErr: false,
},
{
name: "update 3 pointers",
fields: fields{
AccountID: "ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi",
NameID: "nm_ie148R2qZYBfo1Ek3sZpfTLwBhkkqCRKi2Ce8JJ7yyWVRw2Sb", // fdsa.test
Pointers: []string{"ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi", "ak_Egp9yVdpxmvAfQ7vsXGvpnyfNq71msbdUpkMNYGTeTe8kPL3v", "ak_542o93BKHiANzqNaFj6UurrJuDuxU61zCGr9LJCwtTUg34kWt"},
NameTTL: uint64(0),
ClientTTL: uint64(6),
Fee: *utils.NewBigIntFromUint64(1),
TTL: 5,
Nonce: 5,
},
wantTx: "tx_+OMiAaEBzqet5HDJ+Z2dTkAIgKhvHUm7REti8Rqeu2S7z+tz/vMFoQJei0cGdDWb3EfrY0mtZADF0LoQ4yL6z10I/3ETJ0fpKAD4lvGOYWNjb3VudF9wdWJrZXmhAQkzfmKK/9rguLQf6vv/O43g1vpP+B727TdTmYbwitiB8Y5hY2NvdW50X3B1YmtleaEBHxOjsIvwAUAGYqaLadh194A87EwIZH9u1dhMeJe9UKPxjmFjY291bnRfcHVia2V5oQHOp63kcMn5nZ1OQAiAqG8dSbtES2LxGp67ZLvP63P+8wYBBYpSjmc=",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -226,6 +255,10 @@ func TestNameUpdateTx_RLP(t *testing.T) {
tt.fields.TTL,
tt.fields.Nonce,
)

txJSON, err := tx.JSON()
fmt.Println(txJSON)

gotTx, err := BaseEncodeTx(&tx)
if (err != nil) != tt.wantErr {
t.Errorf("NameUpdateTx.RLP() error = %v, wantErr %v", err, tt.wantErr)
Expand Down

0 comments on commit 6d460af

Please sign in to comment.