Skip to content

Commit

Permalink
feat: OracleRespondTx struct, RLP() and JSON() serialization and unit…
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
randomshinichi committed Apr 30, 2019
1 parent c656fb4 commit 680262a
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 3 deletions.
48 changes: 46 additions & 2 deletions aeternity/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,15 +466,15 @@ type OracleExtendTx struct {

// RLP returns a byte serialized representation
func (t *OracleExtendTx) RLP() (rlpRawMsg []byte, err error) {
aID, err := buildIDTag(IDTagOracle, t.OracleID)
oID, err := buildIDTag(IDTagOracle, t.OracleID)
if err != nil {
return
}

rlpRawMsg, err = buildRLPMessage(
ObjectTagOracleExtendTransaction,
rlpMessageVersion,
aID,
oID,
t.AccountNonce,
t.TTLType,
t.TTLValue,
Expand Down Expand Up @@ -595,6 +595,50 @@ type OracleRespondTx struct {
TxTTL uint64
}

func (t *OracleRespondTx) RLP() (rlpRawMsg []byte, err error) {
oID, err := buildIDTag(IDTagOracle, t.OracleID)
if err != nil {
return
}
queryIDBytes, err := Decode(t.QueryID)
if err != nil {
return
}

rlpRawMsg, err = buildRLPMessage(
ObjectTagOracleResponseTransaction,
rlpMessageVersion,
oID,
t.AccountNonce,
queryIDBytes,
t.Response,
t.ResponseTTLType,
t.ResponseTTLValue,
t.TxFee.Int,
t.TxTTL)
return
}

func (t *OracleRespondTx) JSON() (string, error) {
responseTTLTypeStr := ttlTypeIntToStr(t.ResponseTTLType)

swaggerT := models.OracleRespondTx{
Fee: t.TxFee,
Nonce: t.AccountNonce,
OracleID: models.EncodedHash(t.OracleID),
QueryID: models.EncodedHash(t.QueryID),
Response: &t.Response,
ResponseTTL: &models.RelativeTTL{
Type: &responseTTLTypeStr,
Value: &t.ResponseTTLValue,
},
TTL: t.TxTTL,
}
output, err := swaggerT.MarshalBinary()
return string(output), err

}

// NewOracleRespondTx is a constructor for a OracleRespondTx struct
func NewOracleRespondTx(OracleID string, AccountNonce uint64, QueryID string, Response string, TTLType uint64, TTLValue uint64, TxFee utils.BigInt, TxTTL uint64) OracleRespondTx {
return OracleRespondTx{OracleID, AccountNonce, QueryID, Response, TTLType, TTLValue, TxFee, TxTTL}
Expand Down
63 changes: 62 additions & 1 deletion aeternity/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ func TestOracleExtendTx_RLP(t *testing.T) {
}
}

func TestOracleQueryTxRLP(t *testing.T) {
func TestOracleQueryTx_RLP(t *testing.T) {
type fields struct {
SenderID string
AccountNonce uint64
Expand Down Expand Up @@ -548,3 +548,64 @@ func TestNamePointer_EncodeRLP(t *testing.T) {
})
}
}

func TestOracleRespondTx_RLP(t *testing.T) {
type fields struct {
OracleID string
AccountNonce uint64
QueryID string
Response string
ResponseTTLType uint64
ResponseTTLValue uint64
TxFee utils.BigInt
TxTTL uint64
}
tests := []struct {
name string
fields fields
wantTx string
wantErr bool
}{
{
name: "A normal oracle response",
fields: fields{
OracleID: "ok_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi",
AccountNonce: uint64(1),
QueryID: "oq_2NhMjBdKHJYnQjDbAxanmxoXiSiWDoG9bqDgk2MfK2X6AB9Bwx",
Response: "Hello back",
ResponseTTLType: 0,
ResponseTTLValue: 100,
TxFee: Config.Client.Fee,
TxTTL: Config.Client.TTL,
},
wantTx: "tx_+F0YAaEEzqet5HDJ+Z2dTkAIgKhvHUm7REti8Rqeu2S7z+tz/vMBoLT1h6fjQDFn1a7j+6wVQ886V47xiFwvkbL+x2yR3J9cikhlbGxvIGJhY2sAZIa15iD0gACCAfQC7+L+",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tx := NewOracleRespondTx(
tt.fields.OracleID,
tt.fields.AccountNonce,
tt.fields.QueryID,
tt.fields.Response,
tt.fields.ResponseTTLType,
tt.fields.ResponseTTLValue,
tt.fields.TxFee,
tt.fields.TxTTL,
)
gotTx, err := BaseEncodeTx(&tx)

txJSON, _ := tx.JSON()
fmt.Println(txJSON)
if (err != nil) != tt.wantErr {
t.Errorf("OracleRespondTx.RLP() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotTx, tt.wantTx) {
gotTxRawBytes, wantTxRawBytes := getRLPSerialized(gotTx, tt.wantTx)
t.Errorf("OracleRespondTx.RLP() = \n%v\n%v, want \n%v\n%v", gotTx, gotTxRawBytes, tt.wantTx, wantTxRawBytes)
}
})
}
}

0 comments on commit 680262a

Please sign in to comment.