Skip to content

Commit c24fe40

Browse files
committed
First draft of table loading.
1 parent 2718161 commit c24fe40

File tree

8 files changed

+281
-6
lines changed

8 files changed

+281
-6
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.envrc

actions.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ type GetActions struct {
1616
Receiver string `json:"receiver,omitempty"`
1717
Account string `json:"account"`
1818
ActionName string `json:"action_name,omitempty"`
19-
WithDBOperations bool `json:"with_db_ops"`
20-
WithRAMCosts bool `json:"with_ram_costs"`
21-
WithDeferredTransactions bool `json:"with_deferred"`
19+
WithDBOperations bool `json:"with_db_ops"` // dbops
20+
WithRAMCosts bool `json:"with_ram_costs"` // ramops
21+
WithDeferredTransactions bool `json:"with_deferred"` // dtrxops
2222
WithInlineTraces bool `json:"with_inline_traces"`
2323
} `json:"data"`
2424
}
@@ -33,7 +33,7 @@ type ActionTrace struct {
3333
ActionDepth int `json:"depth"`
3434
Trace json.RawMessage `json:"trace"`
3535
DBOperations json.RawMessage `json:"dbops,omitempty"`
36-
RAMConsumed json.RawMessage `json:"rams,omitempty"`
37-
DeferredTransactions json.RawMessage `json:"dtrxs,omitempty"`
36+
RAMConsumed json.RawMessage `json:"rams,omitempty"` // ramops
37+
DeferredTransactions json.RawMessage `json:"dtrxs,omitempty"` // dtrxops
3838
} `json:"data"`
3939
}

error.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package eosws
2+
3+
func init() {
4+
RegisterIncomingMessage("error", Error{})
5+
}
6+
7+
type Error struct {
8+
CommonIn
9+
10+
Data struct {
11+
Code string `json:"code"`
12+
Message string `json:"message"`
13+
Details map[string]interface{} `json:"details"`
14+
} `json:"data"`
15+
}

examples/actionload/main.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"flag"
6+
"fmt"
7+
"os"
8+
"time"
9+
10+
eosws "github.com/dfuse-io/eosws-go"
11+
)
12+
13+
func main() {
14+
flag.Parse()
15+
16+
COUNT := 1
17+
18+
for i := 0; i < COUNT; i++ {
19+
j := i
20+
go func() {
21+
// client, err := eosws.New("ws://localhost:8001/v1/stream", os.Getenv("EOSWS_API_KEY"), "https://origin.example.com")
22+
client, err := eosws.New("wss://mainnet.eos.dfuse.io/v1/stream", os.Getenv("EOSWS_API_KEY"), "https://origin.example.com")
23+
//client, err := eosws.New("wss://kylin.eos.dfuse.io/v1/stream", os.Getenv("EOSWS_API_KEY"), "https://origin.example.com")
24+
errorCheck("connecting to endpoint", err)
25+
26+
ga := &eosws.GetActions{}
27+
ga.ReqID = "1"
28+
ga.StartBlock = -10
29+
ga.Listen = true
30+
ga.WithProgress = 5
31+
ga.Data.Account = "eosio.token"
32+
ga.Data.ActionName = "transfer"
33+
ga.Data.WithInlineTraces = true
34+
35+
fmt.Println("Sending `get_actions` message")
36+
err = client.Send(ga)
37+
errorCheck("sending get_actions", err)
38+
39+
for {
40+
msg, err := client.Read()
41+
if err != nil {
42+
fmt.Println("DIED", j, err)
43+
return
44+
}
45+
46+
switch m := msg.(type) {
47+
case *eosws.ActionTrace:
48+
cnt, _ := json.Marshal(m)
49+
fmt.Println(string(cnt))
50+
case *eosws.Progress:
51+
fmt.Println("Progress", j, m.Data.BlockNum)
52+
default:
53+
fmt.Println("Unsupported message", m)
54+
}
55+
}
56+
}()
57+
}
58+
59+
time.Sleep(500 * time.Second)
60+
}
61+
62+
func errorCheck(prefix string, err error) {
63+
if err != nil {
64+
fmt.Printf("ERROR: %s: %s\n", prefix, err)
65+
os.Exit(1)
66+
}
67+
}

examples/tableload/main.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"flag"
6+
"fmt"
7+
"os"
8+
"time"
9+
10+
eosws "github.com/dfuse-io/eosws-go"
11+
)
12+
13+
func main() {
14+
flag.Parse()
15+
16+
COUNT := 1
17+
18+
for i := 0; i < COUNT; i++ {
19+
j := i
20+
go func() {
21+
client, err := eosws.New("ws://localhost:8001/v1/stream", os.Getenv("EOSWS_API_KEY"), "https://origin.example.com")
22+
// client, err := eosws.New("wss://mainnet.eos.dfuse.io/v1/stream", os.Getenv("EOSWS_API_KEY"), "https://origin.example.com")
23+
//client, err := eosws.New("wss://kylin.eos.dfuse.io/v1/stream", os.Getenv("EOSWS_API_KEY"), "https://origin.example.com")
24+
errorCheck("connecting to endpoint", err)
25+
26+
ga := &eosws.GetTableRows{}
27+
ga.ReqID = "1"
28+
ga.StartBlock = 3360000
29+
ga.Listen = true
30+
ga.Fetch = true
31+
ga.WithProgress = 5
32+
ga.Data.JSON = true
33+
ga.Data.Code = "eosio.token"
34+
ga.Data.Scope = "eoscanadacom"
35+
ga.Data.TableName = "accounts"
36+
37+
fmt.Println("Sending `get_table_rows` message")
38+
err = client.Send(ga)
39+
errorCheck("sending get_table_rows", err)
40+
41+
for {
42+
msg, err := client.Read()
43+
if err != nil {
44+
fmt.Println("DIED", j, err)
45+
return
46+
}
47+
48+
switch m := msg.(type) {
49+
case *eosws.ActionTrace:
50+
cnt, _ := json.Marshal(m)
51+
fmt.Println(string(cnt))
52+
case *eosws.Progress:
53+
fmt.Println("Progress", j, m.Data.BlockNum)
54+
case *eosws.TableDelta:
55+
cnt, _ := json.Marshal(m)
56+
fmt.Println(string(cnt))
57+
case *eosws.TableSnapshot:
58+
cnt, _ := json.Marshal(m)
59+
fmt.Println(string(cnt))
60+
default:
61+
fmt.Println("Unsupported message", m)
62+
cnt, _ := json.Marshal(m)
63+
fmt.Println(string(cnt))
64+
}
65+
}
66+
}()
67+
}
68+
69+
time.Sleep(500 * time.Second)
70+
}
71+
72+
func errorCheck(prefix string, err error) {
73+
if err != nil {
74+
fmt.Printf("ERROR: %s: %s\n", prefix, err)
75+
os.Exit(1)
76+
}
77+
}

message.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type CommonOut struct {
2222
Fetch bool `json:"fetch,omitempty"`
2323
Listen bool `json:"listen,omitempty"`
2424
StartBlock int64 `json:"start_block,omitempty"`
25-
WithProgress int64 `json:"with_progress,omitempty"`
25+
WithProgress int64 `json:"with_progress,omitempty"`
2626
}
2727

2828
func (c *CommonOut) SetType(v string) { c.Type = v }

name.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package eosws
2+
3+
import "strings"
4+
5+
func StringToName(s string) (val uint64, err error) {
6+
// ported from the eosio codebase, libraries/chain/include/eosio/chain/name.hpp
7+
var i uint32
8+
sLen := uint32(len(s))
9+
for ; i <= 12; i++ {
10+
var c uint64
11+
if i < sLen {
12+
c = uint64(charToSymbol(s[i]))
13+
}
14+
15+
if i < 12 {
16+
c &= 0x1f
17+
c <<= 64 - 5*(i+1)
18+
} else {
19+
c &= 0x0f
20+
}
21+
22+
val |= c
23+
}
24+
25+
return
26+
}
27+
28+
func charToSymbol(c byte) byte {
29+
if c >= 'a' && c <= 'z' {
30+
return c - 'a' + 6
31+
}
32+
if c >= '1' && c <= '5' {
33+
return c - '1' + 1
34+
}
35+
return 0
36+
}
37+
38+
var base32Alphabet = []byte(".12345abcdefghijklmnopqrstuvwxyz")
39+
40+
func NameToString(in uint64) string {
41+
// ported from libraries/chain/name.cpp in eosio
42+
a := []byte{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'}
43+
44+
tmp := in
45+
i := uint32(0)
46+
for ; i <= 12; i++ {
47+
bit := 0x1f
48+
if i == 0 {
49+
bit = 0x0f
50+
}
51+
c := base32Alphabet[tmp&uint64(bit)]
52+
a[12-i] = c
53+
54+
shift := uint(5)
55+
if i == 0 {
56+
shift = 4
57+
}
58+
59+
tmp >>= shift
60+
}
61+
62+
return strings.TrimRight(string(a), ".")
63+
}

tables.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package eosws
2+
3+
import "encoding/json"
4+
5+
func init() {
6+
RegisterOutgoingMessage("get_table_rows", GetTableRows{})
7+
RegisterIncomingMessage("table_delta", TableDelta{})
8+
RegisterIncomingMessage("table_snapshot", TableSnapshot{})
9+
}
10+
11+
type GetTableRows struct {
12+
CommonOut
13+
14+
Data struct {
15+
JSON bool `json:"json,omitempty"`
16+
Verbose bool `json:"verbose,omitempty"`
17+
18+
Code string `json:"code"`
19+
Scope string `json:"scope"`
20+
TableName string `json:"table_name"`
21+
} `json:"data"`
22+
}
23+
24+
type TableDelta struct {
25+
CommonIn
26+
27+
Data struct {
28+
BlockNum uint32 `json:"block_num"`
29+
Op string `json:"op,omitempty"`
30+
Code string `json:"code,omitempty"`
31+
Scope string `json:"scope,omitempty"`
32+
TableName string `json:"name,omitempty"`
33+
Key string `json:"key,omitempty"`
34+
Payer string `json:"payer,omitempty"`
35+
JSON json.RawMessage `json:"json"`
36+
Hex string `json:"hex"`
37+
} `json:"data"`
38+
}
39+
40+
type TableSnapshot struct {
41+
CommonIn
42+
43+
Data struct {
44+
BlockNum uint32 `json:"block_num"`
45+
Rows []json.RawMessage `json:"rows"`
46+
} `json:"data"`
47+
}
48+
49+
type TableSnapshotRow struct {
50+
Key string `json:"key"`
51+
Data json.RawMessage `json:"data"`
52+
}

0 commit comments

Comments
 (0)