Skip to content

Commit

Permalink
feat: OracleQueryTx struct revision, unittest structure (but no worki…
Browse files Browse the repository at this point in the history
…ng test)
  • Loading branch information
randomshinichi committed Mar 27, 2019
1 parent 857338f commit 41f9821
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 30 deletions.
16 changes: 12 additions & 4 deletions aeternity/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ type ContractConfig struct {

// OracleConfig configurations for contracts
type OracleConfig struct {
QueryFee utils.BigInt `json:"query_fee" yaml:"query_fee" mapstructure:"query_fee"`
VMVersion uint64 `json:"vm_version" yaml:"vm_version" mapstructure:"vm_version"`
QueryFee utils.BigInt `json:"query_fee" yaml:"query_fee" mapstructure:"query_fee"`
QueryTTLType uint64 `json:"query_ttl_type" yaml:"query_ttl_type" mapstructure:"query_ttl_type"`
QueryTTLValue uint64 `json:"query_ttl_value" yaml:"query_ttl_value" mapstructure:"query_ttl_value"`
ResponseTTLType uint64 `json:"response_ttl_type" yaml:"response_ttl_type" mapstructure:"response_ttl_type"`
ResponseTTLValue uint64 `json:"response_ttl_value" yaml:"response_ttl_value" mapstructure:"response_ttl_value"`
VMVersion uint64 `json:"vm_version" yaml:"vm_version" mapstructure:"vm_version"`
}

// StateChannelConfig configurations for contracts TODO: not complete
Expand Down Expand Up @@ -100,8 +104,12 @@ var Config = ProfileConfig{
VMVersion: 0,
},
Oracles: OracleConfig{
QueryFee: *utils.NewBigIntFromUint64(0),
VMVersion: 0,
QueryFee: *utils.NewBigIntFromUint64(0),
QueryTTLType: 0,
QueryTTLValue: 300,
ResponseTTLType: 0,
ResponseTTLValue: 300,
VMVersion: 0,
},
StateChannels: StateChannelConfig{ // UNUSED
LockPeriod: 0,
Expand Down
100 changes: 75 additions & 25 deletions aeternity/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ func createSignedTransaction(txRaw []byte, signatures [][]byte) (rlpRawMsg []byt
return
}

func ttlTypeIntToStr(i uint64) string {
var oracleTTLTypeStr string
if i == 0 {
oracleTTLTypeStr = "delta"
} else {
oracleTTLTypeStr = "block"
}
return oracleTTLTypeStr
}

func buildPointers(pointers []string) (ptrs [][]uint8, err error) {
// TODO: handle errors
ptrs = make([][]uint8, len(pointers))
Expand Down Expand Up @@ -360,20 +370,15 @@ func (t *OracleRegisterTx) JSON() (string, error) {
abiVersionCasted := int64(t.AbiVersion)
vmVersionCasted := int64(t.VMVersion)

var oracleTTLTypeStr string
if t.OracleTTLType == 0 {
oracleTTLTypeStr = "delta"
} else {
oracleTTLTypeStr = "block"
}
ttlTypeStr := ttlTypeIntToStr(t.OracleTTLType)

swaggerT := models.OracleRegisterTx{
AbiVersion: &abiVersionCasted,
AccountID: models.EncodedHash(t.AccountID),
Fee: t.TxFee,
Nonce: t.AccountNonce,
OracleTTL: &models.TTL{
Type: &oracleTTLTypeStr,
Type: &ttlTypeStr,
Value: &t.OracleTTLValue,
},
QueryFee: t.QueryFee,
Expand Down Expand Up @@ -422,12 +427,7 @@ func (t *OracleExtendTx) RLP() (rlpRawMsg []byte, err error) {

// JSON representation of a Tx is useful for querying the node's debug endpoint
func (t *OracleExtendTx) JSON() (string, error) {
var oracleTTLTypeStr string
if t.TTLType == 0 {
oracleTTLTypeStr = "delta"
} else {
oracleTTLTypeStr = "block"
}
oracleTTLTypeStr := ttlTypeIntToStr(t.TTLType)

swaggerT := models.OracleExtendTx{
Fee: t.Fee,
Expand All @@ -451,21 +451,71 @@ func NewOracleExtendTx(oracleID string, accountNonce, ttlType, ttlValue uint64,

// OracleQueryTx represents a transaction that a program sends to query an oracle
type OracleQueryTx struct {
SenderID string
AccountNonce uint64
OracleID string
Query string
QueryFee utils.BigInt
QueryTTL uint64
TTLType uint64
TTLValue uint64
TxFee utils.BigInt
TxTTL uint64
SenderID string
AccountNonce uint64
OracleID string
Query string
QueryFee utils.BigInt
QueryTTLType uint64
QueryTTLValue uint64
ResponseTTLType uint64
ResponseTTLValue uint64
TxFee utils.BigInt
TxTTL uint64
}

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

rlpRawMsg, err = buildRLPMessage(
ObjectTagOracleQueryTransaction,
rlpMessageVersion,
aID,
[]byte(t.Query),
t.QueryFee.Int,
t.QueryTTLType,
t.QueryTTLValue,
t.ResponseTTLType,
t.ResponseTTLValue,
t.TxFee.Int,
t.TxTTL,
t.AccountNonce)
return
}

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

swaggerT := models.OracleQueryTx{
Fee: t.TxFee,
Nonce: t.AccountNonce,
OracleID: models.EncodedHash(t.OracleID),
Query: &t.Query,
QueryFee: t.QueryFee,
QueryTTL: &models.TTL{
Type: &queryTTLTypeStr,
Value: &t.QueryTTLValue,
},
ResponseTTL: &models.RelativeTTL{
Type: &responseTTLTypeStr,
Value: &t.ResponseTTLValue,
},
SenderID: models.EncodedHash(t.SenderID),
TTL: t.TxTTL,
}

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

// NewOracleQueryTx is a constructor for a OracleQueryTx struct
func NewOracleQueryTx(SenderID string, AccountNonce uint64, OracleID, Query string, QueryFee utils.BigInt, QueryTTL, TTLType, TTLValue uint64, TxFee utils.BigInt, TxTTL uint64) OracleQueryTx {
return OracleQueryTx{SenderID, AccountNonce, OracleID, Query, QueryFee, QueryTTL, TTLType, TTLValue, TxFee, TxTTL}
func NewOracleQueryTx(SenderID string, AccountNonce uint64, OracleID, Query string, QueryFee utils.BigInt, QueryTTLType, QueryTTLValue, ResponseTTLType, ResponseTTLValue uint64, TxFee utils.BigInt, TxTTL uint64) OracleQueryTx {
return OracleQueryTx{SenderID, AccountNonce, OracleID, Query, QueryFee, QueryTTLType, QueryTTLValue, ResponseTTLType, ResponseTTLValue, TxFee, TxTTL}
}

// OracleRespondTx represents a transaction that an oracle sends to respond to an incoming query
Expand Down
74 changes: 73 additions & 1 deletion aeternity/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ func TestOracleExtendTx_RLP(t *testing.T) {
TTLType: 0,
TTLValue: 300,
Fee: *utils.NewBigIntFromUint64(10),
TTL: 0,
},
wantTx: "tx_6xkBoQTOp63kcMn5nZ1OQAiAqG8dSbtES2LxGp67ZLvP63P+8wEAggEsCgDoA8Ab",
wantErr: false,
Expand All @@ -376,7 +377,78 @@ func TestOracleExtendTx_RLP(t *testing.T) {
return
}
if !reflect.DeepEqual(gotTx, tt.wantTx) {
t.Errorf("OracleExtendTx.RLP() = %v, want %v", gotTx, tt.wantTx)
gotTxRawBytes, wantTxRawBytes := getRLPSerialized(gotTx, tt.wantTx)
t.Errorf("OracleExtendTx.RLP() = \n%v\n%v, want \n%v\n%v", gotTx, gotTxRawBytes, tt.wantTx, wantTxRawBytes)
}
})
}
}

func TestOracleQueryTx_RLP(t *testing.T) {
type fields struct {
SenderID string
AccountNonce uint64
OracleID string
Query string
QueryFee utils.BigInt
QueryTTLType uint64
QueryTTLValue uint64
ResponseTTLType uint64
ResponseTTLValue uint64
TxFee utils.BigInt
TxTTL uint64
}
tests := []struct {
name string
fields fields
wantTx string
wantErr bool
}{
{
name: "Normal query to an Oracle",
fields: fields{
SenderID: "ak_Egp9yVdpxmvAfQ7vsXGvpnyfNq71msbdUpkMNYGTeTe8kPL3v",
AccountNonce: uint64(1),
OracleID: "ok_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi",
Query: "Are you okay?",
QueryFee: aeternity.Config.Client.Oracles.QueryFee,
QueryTTLType: aeternity.Config.Client.Oracles.QueryTTLType,
QueryTTLValue: aeternity.Config.Client.Oracles.QueryTTLValue,
ResponseTTLType: aeternity.Config.Client.Oracles.ResponseTTLType,
ResponseTTLValue: aeternity.Config.Client.Oracles.ResponseTTLValue,
TxFee: aeternity.Config.Client.Fee,
TxTTL: aeternity.Config.Client.TTL,
},
wantTx: "I don't know you tell me",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tx := aeternity.OracleQueryTx{
SenderID: tt.fields.SenderID,
AccountNonce: tt.fields.AccountNonce,
OracleID: tt.fields.OracleID,
Query: tt.fields.Query,
QueryFee: tt.fields.QueryFee,
QueryTTLType: tt.fields.QueryTTLType,
QueryTTLValue: tt.fields.QueryTTLValue,
ResponseTTLType: tt.fields.ResponseTTLType,
ResponseTTLValue: tt.fields.ResponseTTLValue,
TxFee: tt.fields.TxFee,
TxTTL: tt.fields.TxTTL,
}
txJson, _ := tx.JSON()
fmt.Println(txJson)

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

0 comments on commit 41f9821

Please sign in to comment.