浏览代码

Add antireplay cache

tags/v2.0.0-rc1
9seconds 5 年前
父节点
当前提交
ab1b3b4863
共有 7 个文件被更改,包括 97 次插入0 次删除
  1. 11
    0
      antireplay/noop.go
  2. 26
    0
      antireplay/noop_test.go
  3. 24
    0
      antireplay/stable_bloom_filter.go
  4. 26
    0
      antireplay/stable_bloom_filter_test.go
  5. 2
    0
      go.mod
  6. 4
    0
      go.sum
  7. 4
    0
      mtglib/init.go

+ 11
- 0
antireplay/noop.go 查看文件

@@ -0,0 +1,11 @@
1
+package antireplay
2
+
3
+import "github.com/9seconds/mtg/v2/mtglib"
4
+
5
+type noop struct{}
6
+
7
+func (n noop) SeenBefore(_ []byte) bool { return false }
8
+
9
+func NewNoop() mtglib.AntiReplayCache {
10
+	return noop{}
11
+}

+ 26
- 0
antireplay/noop_test.go 查看文件

@@ -0,0 +1,26 @@
1
+package antireplay_test
2
+
3
+import (
4
+	"testing"
5
+
6
+	"github.com/9seconds/mtg/v2/antireplay"
7
+	"github.com/stretchr/testify/suite"
8
+)
9
+
10
+type NoopTestSuite struct {
11
+	suite.Suite
12
+}
13
+
14
+func (suite *NoopTestSuite) TestOp() {
15
+	filter := antireplay.NewNoop()
16
+
17
+	suite.False(filter.SeenBefore([]byte{1, 2, 3}))
18
+	suite.False(filter.SeenBefore([]byte{4, 5, 6}))
19
+	suite.False(filter.SeenBefore([]byte{1, 2, 3}))
20
+	suite.False(filter.SeenBefore([]byte{4, 5, 6}))
21
+}
22
+
23
+func TestNoop(t *testing.T) {
24
+	t.Parallel()
25
+	suite.Run(t, &NoopTestSuite{})
26
+}

+ 24
- 0
antireplay/stable_bloom_filter.go 查看文件

@@ -0,0 +1,24 @@
1
+package antireplay
2
+
3
+import (
4
+	"github.com/9seconds/mtg/v2/mtglib"
5
+	"github.com/OneOfOne/xxhash"
6
+	boom "github.com/tylertreat/BoomFilters"
7
+)
8
+
9
+type stableBloomFilter struct {
10
+	filter boom.StableBloomFilter
11
+}
12
+
13
+func (s *stableBloomFilter) SeenBefore(digest []byte) bool {
14
+	return s.filter.TestAndAdd(digest)
15
+}
16
+
17
+func NewStableBloomFilter(byteSize uint, errorRate float64) mtglib.AntiReplayCache {
18
+	sf := boom.NewDefaultStableBloomFilter(byteSize*8, errorRate) // nolint: gomnd
19
+    sf.SetHash(xxhash.New64())
20
+
21
+	return &stableBloomFilter{
22
+		filter: *sf,
23
+	}
24
+}

+ 26
- 0
antireplay/stable_bloom_filter_test.go 查看文件

@@ -0,0 +1,26 @@
1
+package antireplay_test
2
+
3
+import (
4
+	"testing"
5
+
6
+	"github.com/9seconds/mtg/v2/antireplay"
7
+	"github.com/stretchr/testify/suite"
8
+)
9
+
10
+type StableBloomFilterTestSuite struct {
11
+	suite.Suite
12
+}
13
+
14
+func (suite *StableBloomFilterTestSuite) TestOp() {
15
+	filter := antireplay.NewStableBloomFilter(500, 0.001)
16
+
17
+	suite.False(filter.SeenBefore([]byte{1, 2, 3}))
18
+	suite.False(filter.SeenBefore([]byte{4, 5, 6}))
19
+	suite.True(filter.SeenBefore([]byte{1, 2, 3}))
20
+	suite.True(filter.SeenBefore([]byte{4, 5, 6}))
21
+}
22
+
23
+func TestStableBloomFilter(t *testing.T) {
24
+	t.Parallel()
25
+	suite.Run(t, &StableBloomFilterTestSuite{})
26
+}

+ 2
- 0
go.mod 查看文件

@@ -3,6 +3,7 @@ module github.com/9seconds/mtg/v2
3 3
 go 1.16
4 4
 
5 5
 require (
6
+	github.com/OneOfOne/xxhash v1.2.8 // indirect
6 7
 	github.com/alecthomas/kong v0.2.16
7 8
 	github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15
8 9
 	github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5
@@ -15,6 +16,7 @@ require (
15 16
 	github.com/rs/zerolog v1.20.0 // indirect
16 17
 	github.com/stretchr/objx v0.3.0 // indirect
17 18
 	github.com/stretchr/testify v1.7.0
19
+	github.com/tylertreat/BoomFilters v0.0.0-20200520150052-42a7b4300c0c // indirect
18 20
 	github.com/xeipuuv/gojsonschema v1.2.0
19 21
 	golang.org/x/net v0.0.0-20210226172049-e18ecbb05110
20 22
 	golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 // indirect

+ 4
- 0
go.sum 查看文件

@@ -1,3 +1,5 @@
1
+github.com/OneOfOne/xxhash v1.2.8 h1:31czK/TI9sNkxIKfaUfGlU47BAxQ0ztGgd9vPyqimf8=
2
+github.com/OneOfOne/xxhash v1.2.8/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q=
1 3
 github.com/alecthomas/kong v0.2.16 h1:F232CiYSn54Tnl1sJGTeHmx4vJDNLVP2b9yCVMOQwHQ=
2 4
 github.com/alecthomas/kong v0.2.16/go.mod h1:kQOmtJgV+Lb4aj+I2LEn40cbtawdWJ9Y8QLq+lElKxE=
3 5
 github.com/alecthomas/units v0.0.0-20210208195552-ff826a37aa15 h1:AUNCr9CiJuwrRYS3XieqF+Z9B9gNxo/eANAJCF2eiN4=
@@ -40,6 +42,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
40 42
 github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
41 43
 github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
42 44
 github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
45
+github.com/tylertreat/BoomFilters v0.0.0-20200520150052-42a7b4300c0c h1:pGEq55pv/5i+G/Dy+kDsUugzvg6R02jzegxtrwOhE7A=
46
+github.com/tylertreat/BoomFilters v0.0.0-20200520150052-42a7b4300c0c/go.mod h1:OYRfF6eb5wY9VRFkXJH8FFBi3plw2v+giaIu7P054pM=
43 47
 github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
44 48
 github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
45 49
 github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=

+ 4
- 0
mtglib/init.go 查看文件

@@ -17,6 +17,10 @@ type Network interface {
17 17
 	IdleTimeout() time.Duration
18 18
 }
19 19
 
20
+type AntiReplayCache interface {
21
+	SeenBefore(data []byte) bool
22
+}
23
+
20 24
 type Logger interface {
21 25
 	Named(name string) Logger
22 26
 

正在加载...
取消
保存