Skip to content

Commit

Permalink
feat: NameRevokeTx, NameTransferTx and their unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
randomshinichi committed May 27, 2019
1 parent 07aa8a7 commit 1c9eb4e
Show file tree
Hide file tree
Showing 2 changed files with 223 additions and 0 deletions.
116 changes: 116 additions & 0 deletions aeternity/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,122 @@ func NewNameUpdateTx(accountID, nameID string, pointers []string, nameTTL, clien
return NameUpdateTx{accountID, nameID, parsedPointers, nameTTL, clientTTL, fee, ttl, accountNonce}
}

// NameRevokeTx represents a transaction that revokes the name, i.e. has the same effect as waiting for the Name's TTL to expire.
type NameRevokeTx struct {
AccountID string
NameID string
Fee utils.BigInt
TTL uint64
AccountNonce uint64
}

// RLP returns a byte serialized representation
func (t *NameRevokeTx) RLP() (rlpRawMsg []byte, err error) {
// build id for the sender
aID, err := buildIDTag(IDTagAccount, t.AccountID)
if err != nil {
return
}
// build id for the name
nID, err := buildIDTag(IDTagName, t.NameID)
if err != nil {
return
}

rlpRawMsg, err = buildRLPMessage(
ObjectTagNameServiceRevokeTransaction,
rlpMessageVersion,
aID,
t.AccountNonce,
nID,
t.Fee.Int,
t.TTL)
return
}

// JSON representation of a Tx is useful for querying the node's debug endpoint
func (t *NameRevokeTx) JSON() (string, error) {
swaggerT := models.NameRevokeTx{
AccountID: models.EncodedHash(t.AccountID),
Fee: t.Fee,
NameID: models.EncodedHash(t.NameID),
Nonce: t.AccountNonce,
TTL: t.TTL,
}

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

// NewNameRevokeTx is a constructor for a NameRevokeTx struct
func NewNameRevokeTx(accountID, name string, fee utils.BigInt, ttl, accountNonce uint64) NameRevokeTx {
return NameRevokeTx{accountID, name, fee, ttl, accountNonce}
}

// NameTransferTx represents a transaction that transfers ownership of one name to another account.
type NameTransferTx struct {
AccountID string
NameID string
RecipientID string
Fee utils.BigInt
TTL uint64
AccountNonce uint64
}

// RLP returns a byte serialized representation
func (t *NameTransferTx) RLP() (rlpRawMsg []byte, err error) {
// build id for the sender
aID, err := buildIDTag(IDTagAccount, t.AccountID)
if err != nil {
return
}

// build id for the recipient
rID, err := buildIDTag(IDTagAccount, t.RecipientID)
if err != nil {
return
}

// build id for the name
nID, err := buildIDTag(IDTagName, t.NameID)
if err != nil {
return
}

// create the transaction
rlpRawMsg, err = buildRLPMessage(
ObjectTagNameServiceTransferTransaction,
rlpMessageVersion,
aID,
t.AccountNonce,
nID,
rID,
t.Fee.Int,
t.TTL,
)
return
}

// JSON representation of a Tx is useful for querying the node's debug endpoint
func (t *NameTransferTx) JSON() (string, error) {
swaggerT := models.NameTransferTx{
AccountID: models.EncodedHash(t.AccountID),
Fee: t.Fee,
NameID: models.EncodedHash(t.NameID),
Nonce: t.AccountNonce,
RecipientID: models.EncodedHash(t.RecipientID),
TTL: t.TTL,
}

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

// NewNameTransferTx is a constructor for a NameTransferTx struct
func NewNameTransferTx(AccountID, NameID, RecipientID string, Fee utils.BigInt, TTL, AccountNonce uint64) NameTransferTx {
return NameTransferTx{AccountID, NameID, RecipientID, Fee, TTL, AccountNonce}
}

// OracleRegisterTx represents a transaction that registers an oracle on the blockchain's state
type OracleRegisterTx struct {
AccountID string
Expand Down
107 changes: 107 additions & 0 deletions aeternity/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,113 @@ func TestNameUpdateTx_RLP(t *testing.T) {
}
}

func TestNameRevokeTx_RLP(t *testing.T) {
type fields struct {
AccountID string
NameID string
Fee utils.BigInt
TTL uint64
AccountNonce uint64
}
testCases := []struct {
name string
fields fields
wantTx string
wantErr bool
}{
{
name: "normal revoke one name",
fields: fields{
AccountID: "ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi",
NameID: "nm_ie148R2qZYBfo1Ek3sZpfTLwBhkkqCRKi2Ce8JJ7yyWVRw2Sb", // fdsa.test
Fee: *utils.NewBigIntFromUint64(1),
TTL: 5,
AccountNonce: 5,
},
wantTx: "tx_+EkjAaEBzqet5HDJ+Z2dTkAIgKhvHUm7REti8Rqeu2S7z+tz/vMFoQJei0cGdDWb3EfrY0mtZADF0LoQ4yL6z10I/3ETJ0fpKAEFCjOeGw==",
wantErr: false,
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
tx := NewNameRevokeTx(
tt.fields.AccountID,
tt.fields.NameID,
tt.fields.Fee,
tt.fields.TTL,
tt.fields.AccountNonce,
)
txJSON, _ := tx.JSON()
fmt.Println(txJSON)

gotTx, err := BaseEncodeTx(&tx)
if (err != nil) != tt.wantErr {
t.Errorf("NameRevokeTx.RLP() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotTx, tt.wantTx) {
gotTxRawBytes, wantTxRawBytes := getRLPSerialized(gotTx, tt.wantTx)
t.Errorf("NameRevokeTx.RLP() = \n%v\n%v, want \n%v\n%v", gotTx, gotTxRawBytes, tt.wantTx, wantTxRawBytes)
}
})
}
}

func TestNameTransferTx_RLP(t *testing.T) {
type fields struct {
AccountID string
NameID string
RecipientID string
Fee utils.BigInt
TTL uint64
AccountNonce uint64
}
testCases := []struct {
name string
fields fields
wantTx string
wantErr bool
}{
{
name: "normal name transfer transaction",
fields: fields{
AccountID: "ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi",
NameID: "nm_ie148R2qZYBfo1Ek3sZpfTLwBhkkqCRKi2Ce8JJ7yyWVRw2Sb", // fdsa.test
RecipientID: "ak_Egp9yVdpxmvAfQ7vsXGvpnyfNq71msbdUpkMNYGTeTe8kPL3v",
Fee: *utils.NewBigIntFromUint64(1),
TTL: 5,
AccountNonce: 5,
},
wantTx: "tx_+GskAaEBzqet5HDJ+Z2dTkAIgKhvHUm7REti8Rqeu2S7z+tz/vMFoQJei0cGdDWb3EfrY0mtZADF0LoQ4yL6z10I/3ETJ0fpKKEBHxOjsIvwAUAGYqaLadh194A87EwIZH9u1dhMeJe9UKMBBeUht+4=",
wantErr: false,
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
tx := NewNameTransferTx(
tt.fields.AccountID,
tt.fields.NameID,
tt.fields.RecipientID,
tt.fields.Fee,
tt.fields.TTL,
tt.fields.AccountNonce,
)
txJSON, _ := tx.JSON()
fmt.Println(txJSON)

gotTx, err := BaseEncodeTx(&tx)
if (err != nil) != tt.wantErr {
t.Errorf("NameTransferTx.RLP() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotTx, tt.wantTx) {
gotTxRawBytes, wantTxRawBytes := getRLPSerialized(gotTx, tt.wantTx)
t.Errorf("NameTransferTx.RLP() = \n%v\n%v, want \n%v\n%v", gotTx, gotTxRawBytes, tt.wantTx, wantTxRawBytes)
}
})
}
}

func TestOracleRegisterTx_RLP(t *testing.T) {
type fields struct {
accountID string
Expand Down

0 comments on commit 1c9eb4e

Please sign in to comment.