Skip to content

Commit

Permalink
feat: OracleRespondTx prerequisites: APIGetOracleQueriesByPubkey(), b…
Browse files Browse the repository at this point in the history
…uildOracleQueryID(), leftPadByteSlice() and unittests
  • Loading branch information
randomshinichi committed Apr 30, 2019
1 parent 24f5f87 commit db05b75
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
17 changes: 17 additions & 0 deletions aeternity/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,20 @@ func getOracleByPubkey(node *apiclient.Node, pubkey string) (oracle *models.Regi
oracle = r.Payload
return
}

// APIGetOracleQueriesByPubkey get a list of queries made to a particular oracle
func (ae *Ae) APIGetOracleQueriesByPubkey(pubkey string) (oracleQueries *models.OracleQueries, err error) {
return getOracleQueriesByPubkey(ae.Node, pubkey)
}

// getOracleQueriesByPubkey get a list of queries made to a particular oracle
func getOracleQueriesByPubkey(node *apiclient.Node, pubkey string) (oracleQueries *models.OracleQueries, err error) {
p := external.NewGetOracleQueriesByPubkeyParams().WithPubkey(pubkey)
r, err := node.External.GetOracleQueriesByPubkey(p)
if err != nil {
err = fmt.Errorf("Error: %v", getErrorReason(err))
return
}
oracleQueries = r.Payload
return
}
33 changes: 33 additions & 0 deletions aeternity/hashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,39 @@ func hash(in []byte) (out []byte, err error) {
return
}

func leftPadByteSlice(length int, data []byte) []byte {
dataLen := len(data)
t := make([]byte, length-dataLen)
paddedSlice := append(t, data...)
return paddedSlice
}

func buildOracleQueryID(sender string, senderNonce uint64, recipient string) (id string, err error) {
queryIDBin := []byte{}
senderBin, err := Decode(sender)
if err != nil {
return
}
queryIDBin = append(queryIDBin, senderBin...)

senderNonceBytes := utils.NewBigIntFromUint64(senderNonce).Bytes()
senderNonceBytesPadded := leftPadByteSlice(32, senderNonceBytes)
queryIDBin = append(queryIDBin, senderNonceBytesPadded...)

recipientBin, err := Decode(recipient)
if err != nil {
return
}
queryIDBin = append(queryIDBin, recipientBin...)

hashedQueryID, err := hash(queryIDBin)
if err != nil {
return
}
id = Encode(PrefixOracleQueryID, hashedQueryID)
return
}

// Namehash calculate the Namehash of a string
// TODO: link to the
func Namehash(name string) []byte {
Expand Down
85 changes: 85 additions & 0 deletions aeternity/hashing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,88 @@ func Test_computeCommitmentID(t *testing.T) {
})
}
}

func Test_buildOracleQueryID(t *testing.T) {
type args struct {
sender string
senderNonce uint64
recipient string
}
tests := []struct {
name string
args args
wantId string
wantErr bool
}{
{
name: "a simple oracle query id",
args: args{
sender: "ak_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi",
senderNonce: uint64(3),
recipient: "ok_2a1j2Mk9YSmC1gioUq4PWRm3bsv887MbuRVwyv4KaUGoR1eiKi",
},
wantId: "oq_2NhMjBdKHJYnQjDbAxanmxoXiSiWDoG9bqDgk2MfK2X6AB9Bwx",
wantErr: false,
},
{
name: "this test case copied from aepp-middleware",
args: args{
sender: "ak_2ZjpYpJbzq8xbzjgPuEpdq9ahZE7iJRcAYC1weq3xdrNbzRiP4",
senderNonce: uint64(1),
recipient: "ok_2iqfJjbhGgJFRezjX6Q6DrvokkTM5niGEHBEJZ7uAG5fSGJAw1",
},
wantId: "oq_2YvZnoohcSvbQCsPKSMxc98i5HZ1sU5mR6xwJUZC3SvkuSynMj",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotId, err := buildOracleQueryID(tt.args.sender, tt.args.senderNonce, tt.args.recipient)
if (err != nil) != tt.wantErr {
t.Errorf("buildOracleQueryID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotId != tt.wantId {
gotIdBytes, _ := Decode(gotId)
wantIdBytes, _ := Decode(tt.wantId)
t.Errorf("buildOracleQueryID() = \n%v\n%v, want \n%v\n%v", gotId, gotIdBytes, tt.wantId, wantIdBytes)
}
})
}
}

func Test_leftPadByteSlice(t *testing.T) {
type args struct {
length int
data []byte
}
tests := []struct {
name string
args args
want []byte
}{
{
name: "Left pad a nonce of 3 to 32 bytes",
args: args{
length: 32,
data: []byte{3},
},
want: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3},
},
{
name: "Left pad a multi-byte value to 32 bytes",
args: args{
length: 32,
data: []byte{1, 2, 3, 4, 3},
},
want: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 3},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := leftPadByteSlice(tt.args.length, tt.args.data); !reflect.DeepEqual(got, tt.want) {
t.Errorf("leftPadByteSlice() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit db05b75

Please sign in to comment.